表单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>