zoukankan      html  css  js  c++  java
  • JSP之request表单的两种提交及乱码处理

    表单post提交方式与得到

    程序演示:

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP '4.jsp' starting page</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22 
    23   </head>
    24   
    25   <body>
    26     <form method="post" action="5.jsp">
    27       用户名:<br>
    28       <input type="text" name="name"/><br>
    29       性别:<br>
    30       <input type="radio" name="sex" value="男">男</input>
    31        <input type="radio" name="sex" value="女">女</input><br>
    32      兴趣:<br>
    33      <input type="checkbox" name="interesting" value="篮球">篮球</input>
    34      <input type="checkbox" name="interesting" value="旅游">旅游</input>
    35     <input type="checkbox" name="interesting" value="历史">历史</input>
    36     <input type="checkbox" name="interesting" value="读书">读书</input><br>
    37     <input type="submit" ></input>
    38     <input type="reset"></input>
    39     </form>
    40     
    41   </body>
    42 </html>
     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP '5.jsp' starting page</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22 
    23   </head>
    24   
    25   <body>
    26     <%
    27     request.setCharacterEncoding("UTF-8");
    28     out.print("用户名:"+request.getParameter("name")+"<br>");
    29      out.print("性别:"+request.getParameter("sex")+"<br>");
    30      String[] interest=request.getParameterValues("interesting");
    31      out.print("兴趣:"+"<br>");
    32      for(String i:interest){
    33           out.print(i+"<br>");
    34      }
    35      %>
    36   </body>
    37 </html>

    表单get提交方式与得到

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP '2.jsp' starting page</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22 
    23   </head>
    24   
    25   <body>
    26     <form method="get" action="3.jsp">
    27       用户名:<br>
    28       <input type="text" name="name"/><br>
    29       性别:<br>
    30       <input type="radio" name="sex" value="男">男</input>
    31        <input type="radio" name="sex" value="女">女</input><br>
    32      兴趣:<br>
    33      <input type="checkbox" name="interesting" value="篮球">篮球</input>
    34      <input type="checkbox" name="interesting" value="旅游">旅游</input>
    35     <input type="checkbox" name="interesting" value="历史">历史</input>
    36     <input type="checkbox" name="interesting" value="读书">读书</input><br>
    37     <input type="submit" ></input>
    38     <input type="reset"></input>
    39     </form>
    40     
    41     
    42     
    43     
    44     
    45   </body>
    46 </html>
     1 <%@page import="java.net.URLDecoder"%>
     2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 
     8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9 <html>
    10   <head>
    11     <base href="<%=basePath%>">
    12     
    13     <title>My JSP '3.jsp' starting page</title>
    14     
    15     <meta http-equiv="pragma" content="no-cache">
    16     <meta http-equiv="cache-control" content="no-cache">
    17     <meta http-equiv="expires" content="0">    
    18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    19     <meta http-equiv="description" content="This is my page">
    20     <!--
    21     <link rel="stylesheet" type="text/css" href="styles.css">
    22     -->
    23 
    24   </head>
    25   
    26   <body>
    27   <% //获取请求里包含的查询字符串
    28       String subStr=request.getQueryString();
    29   
    30   //使用urldecoder解码
    31   String codeStr=URLDecoder.decode(subStr, "UTF-8");
    32   //以&符号分解
    33   String[] spls=codeStr.split("&");
    34   
    35   for(String spl:spls){
    36      out.print(spl+"<br>");
    37   
    38   }
    39   
    40   
    41   %>
    42   
    43   
    44   
    45   </body>
    46 </html>
  • 相关阅读:
    【POJ】1067 取石子游戏(博弈论)
    【POJ】2348 Euclid's Game(扩欧)
    【POJ】1061 青蛙的约会 / 【BZOJ】1477(扩欧)
    【POJ】3090 Visible Lattice Points(欧拉函数)
    【BZOJ】2190 [SDOI2008]仪仗队(欧拉函数)
    【POJ】2115 C Looooops(扩欧)
    【BZOJ】1015 [JSOI2008]星球大战starwar(并查集+离线处理)
    [BZOJ4822][Cqoi2017]老C的任务
    [BZOJ1001][BeiJing2006]狼抓兔子
    [BZOJ1188][HNOI2007]分裂游戏
  • 原文地址:https://www.cnblogs.com/ztyy04126/p/4943094.html
Copyright © 2011-2022 走看看