一、获取请求参数通用方式

1. String getParameter(String name):根据参数名称获取参数值 例:username=flypig&password=2343

2. String[] getParameterValues(String name):根据参数名称获取参数值的数组  例:hooby=xx&hobby=name

3. Enumeration<String> getParameterNames():获取所有请求的参数名称

4. Map<String, String[]> getParameterMap():获取所有参数的map集合

1 @WebServlet("/RequestDemo6")
 2 public class RequestDemo6 extends HttpServlet {
 3     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 4         //post 获取请求参数
 5 
 6         //根据参数名称获取参数值
 7         String username = request.getParameter("username");
 8 //        System.out.println("post");
 9 //        System.out.println(username);
10 
11         //根据参数名称获取参数值的数组
12         String[] hobbies = request.getParameterValues("hobby");
13 //        for (String hobby : hobbies) {
14 //            System.out.println(hobby);
15 //        }
16 
17         //获取所有请求的参数名称
18 
19         Enumeration<String> parameterNames = request.getParameterNames();
20 //        while(parameterNames.hasMoreElements()){
21 //            String name = parameterNames.nextElement();
22 //            System.out.print(name+":");
23 //            String value = request.getParameter(name);
24 //            System.out.println(value);
25 //        }
26 
27         //获取所有参数的map集合
28         Map<String, String[]> parameterMap = request.getParameterMap();
29         //遍历
30         Set<String> keySet = parameterMap.keySet();
31         for (String name : keySet){
32 
33             //根据键获取值
34             String[] values = parameterMap.get(name);
35             System.out.println(name);
36             for (String value : values) {
37                 System.out.println(value);
38             }
39             System.out.println("--------------");
40         }
41 
42 
43     }
44 
45     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
46         //get获取请求参数
47 
48         //根据参数名称获取参数值
49 //        String username = request.getParameter("username");
50 //        System.out.println("get");
51 //        System.out.println(username);
52 
53         this.doPost(request,response);
54     }
55 }

 

二、解决乱码问题

解决请求参数中文乱码问题:

  * get方式:tomcat 8已经将get方式乱码问题解决

  * post方式:会乱码

    *解决:在获取参数前,设置request的编码request.setCharacterEncoding("utf-8")

1 @WebServlet("/RequestDemo7")
 2 public class RequestDemo7 extends HttpServlet {
 3     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 4 
 5         //设置流的编码
 6         request.setCharacterEncoding("utf-8");
 7 
 8         //post 获取请求参数username
 9         String username = request.getParameter("username");
10         System.out.println(username);
11 
12     }
13 
14     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
15         //get获取请求参数
16 
17         //根据参数名称获取参数值
18 //        String username = request.getParameter("username");
19 //        System.out.println("get");
20 //        System.out.println(username);
21 
22         this.doPost(request,response);
23     }
24 }