5.1 Request 获取请求数据的几种方法

 1 //获取请求头和请求数据
 2 //请求数据(1.通过超链接 2.通过表单)
 3 //获取请求数据的时候一般来说  都要先检查  再使用
 4 public class RequestDemo2 extends HttpServlet {
 5 
 6     //获取数据的相关方法
 7     public void doGet(HttpServletRequest request, HttpServletResponse response)
 8             throws ServletException, IOException {
 9         
10         System.out.println("-----------获取数据方式1-------------");
11         
12         String value = request.getParameter("username");
13         if(value != null && !value.trim().equals("")) {
14             System.out.println(value);
15         }
16         
17         System.out.println("-----------获取数据方式2----------- -");
18         
19         Enumeration e = request.getParameterNames();
20         while(e.hasMoreElements()) {
21             String name = (String) e.nextElement();
22             value = request.getParameter(name);
23             System.out.println(name + "=" + value);
24         }
25         
26         System.out.println("-----------获取数据方式3-------------");
27         
28         String[] values = request.getParameterValues("username");
29         
30         for(int i = 0;i<values.length && values[i] != null;i++) {
31             System.out.println(values[i]);
32         }
33         
34         System.out.println("-----------获取数据方式4-------------");
35         /*使用Map  (常用)*/
36         /*Map<String,String[]> map = request.getParameterMap();
37         User user = new User();
38         try {
39             BeanUtils.populate(user, map);   //用map集合的数据填充bean
40             BeanUtils.copyProperties(user, formbean);
41         } catch(Exception e1) {
42             e1.printStackTrace();
43         }*/
44         
45         System.out.println("-----------获取数据方式5-------------");     //用于文件上传
46         
47         InputStream in = request.getInputStream();
48         
49         int len = 0;
50         byte[] buffer = new byte[1024];
51         while((len = in.read()) > 0) {
52             System.out.print(new String(buffer,0,len));
53         }
54     }    
55     
56     //获取头的相关方法
57     private void test1(HttpServletRequest request) {
58         String HeadValue = request.getHeader("Accept-Encoding");
59         System.out.println(HeadValue);
60         
61         System.out.println("-------------------------");    
62         
63         Enumeration e = request.getHeaders("Accept-Encoding");
64         while(e.hasMoreElements()) {
65             String value = (String) e.nextElement();
66             System.out.println(value);
67         }
68         
69         System.out.println("-------------------------");    
70         
71         e = request.getHeaderNames();
72         
73         while(e.hasMoreElements()) {
74             String name = (String) e.nextElement();
75             String value = request.getHeader(name);
76             System.out.println(name + "=" + value);
77         }
78     }
79 
80     public void doPost(HttpServletRequest request, HttpServletResponse response)
81             throws ServletException, IOException {
82         doGet(request, response);
83     }
84 
85 }
 1 //获取请求头和请求数据
 2 //请求数据(1.通过超链接 2.通过表单)
 3 //获取请求数据的时候一般来说  都要先检查  再使用
 4 public class RequestDemo2 extends HttpServlet {
 5 
 6     //获取数据的相关方法
 7     public void doGet(HttpServletRequest request, HttpServletResponse response)
 8             throws ServletException, IOException {
 9         
10         System.out.println("-----------获取数据方式1-------------");
11         
12         String value = request.getParameter("username");
13         if(value != null && !value.trim().equals("")) {
14             System.out.println(value);
15         }
16         
17         System.out.println("-----------获取数据方式2----------- -");
18         
19         Enumeration e = request.getParameterNames();
20         while(e.hasMoreElements()) {
21             String name = (String) e.nextElement();
22             value = request.getParameter(name);
23             System.out.println(name + "=" + value);
24         }
25         
26         System.out.println("-----------获取数据方式3-------------");
27         
28         String[] values = request.getParameterValues("username");
29         
30         for(int i = 0;i<values.length && values[i] != null;i++) {
31             System.out.println(values[i]);
32         }
33         
34         System.out.println("-----------获取数据方式4-------------");
35         /*使用Map  (常用)*/
36         /*Map<String,String[]> map = request.getParameterMap();
37         User user = new User();
38         try {
39             BeanUtils.populate(user, map);   //用map集合的数据填充bean
40             BeanUtils.copyProperties(user, formbean);
41         } catch(Exception e1) {
42             e1.printStackTrace();
43         }*/
44         
45         System.out.println("-----------获取数据方式5-------------");     //用于文件上传
46         
47         InputStream in = request.getInputStream();
48         
49         int len = 0;
50         byte[] buffer = new byte[1024];
51         while((len = in.read()) > 0) {
52             System.out.print(new String(buffer,0,len));
53         }
54     }    
55     
56     //获取头的相关方法
57     private void test1(HttpServletRequest request) {
58         String HeadValue = request.getHeader("Accept-Encoding");
59         System.out.println(HeadValue);
60         
61         System.out.println("-------------------------");    
62         
63         Enumeration e = request.getHeaders("Accept-Encoding");
64         while(e.hasMoreElements()) {
65             String value = (String) e.nextElement();
66             System.out.println(value);
67         }
68         
69         System.out.println("-------------------------");    
70         
71         e = request.getHeaderNames();
72         
73         while(e.hasMoreElements()) {
74             String name = (String) e.nextElement();
75             String value = request.getHeader(name);
76             System.out.println(name + "=" + value);
77         }
78     }
79 
80     public void doPost(HttpServletRequest request, HttpServletResponse response)
81             throws ServletException, IOException {
82         doGet(request, response);
83     }
84 
85 }

 

posted @ 2017-08-14 11:41  白日梦想家12138  阅读(526)  评论(0编辑  收藏  举报