首页 > 其他 > 详细

request获取请求头和请求数据

时间:2014-07-08 23:00:42      阅读:600      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 1 package cn.itcast.request;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.io.PrintWriter;
 6 import java.lang.reflect.InvocationTargetException;
 7 import java.util.Enumeration;
 8 import java.util.Map;
 9 
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 
15 import org.apache.commons.beanutils.BeanUtils;
16 
17 import cn.itcast.User;
18 
19 //获取请求头和请求数据
20 //获取请求数据(获取请求数据时一般来说都要先检查再使用)
21 public class RequestDemo extends HttpServlet {
22 
23     public void doGet(HttpServletRequest request, HttpServletResponse response)
24             throws ServletException, IOException {
25 
26         System.out.println("--------------获取方法1----------------");
27         String value = request.getParameter("username");
28         if (value != null && !value.trim().equals("")) {
29             System.out.println(value);
30         }
31 
32         System.out.println("--------------获取方法2----------------");
33         Enumeration e = request.getParameterNames();
34         while (e.hasMoreElements()) {
35             String name = (String) e.nextElement();
36             value = request.getParameter(name);
37             System.out.println(name + "=" + value);
38         }
39 
40         System.out.println("--------------获取方法3----------------");
41         String[] values = request.getParameterValues("username");
42         for (int i = 0; values != null && i < values.length; i++) {
43             System.out.println(values);
44         }
45 
46         System.out.println("--------------获取方法4----------------");
47         Map map = request.getParameterMap();
48         User user = new User();
49 
50         try {
51             BeanUtils.populate(user, map);
52         } catch (Exception e1) {
53             e1.printStackTrace();
54         }
55         System.out.println(map);
56 
57     }
58 
59     // 获取头相关方法
60     private void test1(HttpServletRequest request) throws IOException {
61         String headValue = request.getHeader("Accept-Encoding");
62         System.out.println(headValue);
63         System.out.println("-------------------------------");
64 
65         Enumeration e = request.getHeaders("Accept-Encoding");
66         while (e.hasMoreElements()) {
67             String value = (String) e.nextElement();
68             System.out.println(value);
69         }
70 
71         System.out.println("-------------------------------");
72         e = request.getHeaderNames();
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         System.out.println("--------------获取方法5----------------");
81 
82         InputStream in = request.getInputStream();
83         int len = 0;
84         byte buffer[] = new byte[1024];
85         while ((len = in.read(buffer)) > 0) {
86             System.out.println(len);
87             System.out.println(new String(buffer, 0, len));
88         }
89     }
90 
91     public void doPost(HttpServletRequest request, HttpServletResponse response)
92             throws ServletException, IOException {
93 
94     }
95 
96 }
View Code
bubuko.com,布布扣
 1 package cn.itcast;
 2 
 3 public class User {
 4     private String username[];
 5     private String password;
 6 
 7     public String[] getUsername() {
 8         return username;
 9     }
10 
11     public void setUsername(String[] username) {
12         this.username = username;
13     }
14 
15     public String getPassword() {
16         return password;
17     }
18 
19     public void setPassword(String password) {
20         this.password = password;
21     }
22 
23 }
View Code
bubuko.com,布布扣
 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 2 <html>
 3     <head>
 4         <title>test.html</title>
 5 
 6         <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 7         <meta http-equiv="description" content="this is my page">
 8         <meta http-equiv="content-type" content="text/html; charset=UTF-8">
 9 
10         <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
11 
12     </head>
13 
14     <body>
15         <a href="/ServletDemo/servlet/RequestDemo?username=xxxx">点击看看</a>
16 
17         <form action="/ServletDemo/servlet/RequestDemo" method="post">
18             用户名1:
19             <input type="text" name="username" />
20             用户名2:
21             <input type="text" name="username" />
22             密码:<input type="password" name="password"/>
23             <input type="submit" value="提交" />
24 
25         </form>
26     </body>
27 </html>
View Code

 

request获取请求头和请求数据,布布扣,bubuko.com

request获取请求头和请求数据

原文:http://www.cnblogs.com/aineko/p/3830380.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!