本文最后更新于:2024年3月18日 凌晨
                
              
            
            
              Servlet 解析请求 
很多情况下,需要传递一些信息,从浏览器到 Web 服务器,最终到后台程序,浏览器使用两种方法可将这些信息传递到 Web 服务器,分别为 GET 方法和 POST 方法。 
 
GET 方法 
GET 方法向页面请求发送已编码的用户信息,页面和已编码的信息中间用 ? 字符分隔,如下所示: 
 
1 http ://www.test.com/hello?key1 =value1 &key2 =value2 
GET 方法是默认的从浏览器向 Web 服务器传递信息的方法,它会产生一个很长的字符串,出现在浏览器的地址栏中,如果您要向服务器传递的是密码或其他的敏感信息,请不要使用 GET 方法,GET 方法有大小限制:请求字符串中最多只能有 1024 个字符。 
这些信息使用 QUERY_STRING 头传递,并可以通过 QUERY_STRING 环境变量访问,Servlet 使用doGet()方法处理这种类型的请求。 
 
POST 方法 
另一个向后台程序传递信息的比较可靠的方法是 POST 方法,POST 方法打包信息的方式与 GET 方法基本相同,但是 POST 方法不是把信息作为 URL 中 ? 字符后的文本字符串进行发送,而是把这些信息作为一个单独的消息,消息以标准输出的形式传到后台程序,您可以解析和使用这些标准输出,Servlet 使用doPost()方法处理这种类型的请求。 
 
使用 Servlet 读取表单数据 
Servlet 处理表单数据,这些数据会根据不同的情况使用不同的方法自动解析:
getParameter() :您可以调用 request.getParameter() 方法来获取表单参数的值。getParameterValues() :如果参数出现一次以上,则调用该方法,并返回多个值,例如复选框。getParameterNames() :如果您想要得到当前请求中的所有参数的完整列表,则调用该方法。 
 
 
使用GET方法实例 
下面是一个简单的 URL,将使用 GET 方法向 HelloForm 程序传递两个值。 
 
http://localhost:8080/TomcatTest/HelloForm?name=test&url=www.test.com 
使用getParameter()方法,可以很容易地访问传递的信息: 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 @WebServlet("/HelloForm") public  class  HelloForm  extends  HttpServlet  private  static  final  long  serialVersionUID = 1L ;public  HelloForm ()  super ();protected  void  doGet (HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException "text/html;charset=UTF-8" );"使用 GET 方法读取表单数据" ;new  String(request.getParameter("name" ).getBytes("ISO-8859-1" ),"UTF-8" );"<!DOCTYPE html> \n" ;"<html>\n"  +"<head><title>"  + title + "</title></head>\n"  +"<body bgcolor=\"#f0f0f0\">\n"  +"<h1 align=\"center\">"  + title + "</h1>\n"  +"<ul>\n"  +"  <li><b>站点名</b>:" "\n"  +"  <li><b>网址</b>:" "url" ) + "\n"  +"</ul>\n"  +"</body></html>" );public  void  doPost (HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException 
使用 POST 方法实例 
让我们对上面的 Servlet 做小小的修改,以便它可以处理 GET 和 POST 方法,Servlet 程序使用 GET 和 POST 方法处理由 Web 浏览器给出的输入。 
 
注意 :如果表单提交的数据中有中文数据则需要转码:
1 String name =new  String(request.getParameter("name" ).getBytes("ISO8859-1" ),"UTF-8" );
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 @WebServlet("/HelloForm") public  class  HelloForm  extends  HttpServlet  private  static  final  long  serialVersionUID = 1L ;public  HelloForm ()  super ();protected  void  doGet (HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException "text/html;charset=UTF-8" );"使用 POST 方法读取表单数据" ;new  String(request.getParameter("name" ).getBytes("ISO8859-1" ),"UTF-8" );"<!DOCTYPE html> \n" ;"<html>\n"  +"<head><title>"  + title + "</title></head>\n"  +"<body bgcolor=\"#f0f0f0\">\n"  +"<h1 align=\"center\">"  + title + "</h1>\n"  +"<ul>\n"  +"  <li><b>站点名</b>:" "\n"  +"  <li><b>网址</b>:" "url" ) + "\n"  +"</ul>\n"  +"</body></html>" );public  void  doPost (HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException 
读取所有的表单参数 
以下是通用的实例,使用 HttpServletRequest 的 getParameterNames() 方法读取所有可用的表单参数,该方法返回一个枚举,其中包含未指定顺序的参数名。 
一旦我们有一个枚举,我们可以以标准方式循环枚举,使用 hasMoreElements() 方法来确定何时停止,使用 nextElement() 方法来获取每个参数的名称。 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 @WebServlet("/ReadParams") public  class  ReadParams  extends  HttpServlet  private  static  final  long  serialVersionUID = 1L ;public  ReadParams ()  super ();protected  void  doGet (HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException "text/html;charset=UTF-8" );"读取所有的表单数据" ;"<!doctype html public \"-//w3c//dtd html 4.0 "  +"transitional//en\">\n" ;"<html>\n"  +"<head><meta charset=\"utf-8\"><title>"  + title + "</title></head>\n"  +"<body bgcolor=\"#f0f0f0\">\n"  +"<h1 align=\"center\">"  + title + "</h1>\n"  +"<table width=\"100%\" border=\"1\" align=\"center\">\n"  +"<tr bgcolor=\"#949494\">\n"  +"<th>参数名称</th><th>参数值</th>\n" +"</tr>\n" );while (paramNames.hasMoreElements()) {"<tr><td>"  + paramName + "</td>\n" );if  (paramValues.length == 1 ) {0 ];if  (paramValue.length() == 0 )"<td><i>没有值</i></td>" );else "<td>"  + paramValue + "</td>" );else  {"<td><ul>" );for (int  i=0 ; i < paramValues.length; i++) {"<li>"  + paramValues[i]);"</ul></td>" );"</tr>" );"\n</table>\n</body></html>" );protected  void  doPost (HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException