ServletRequest的一些知识点

浏览器向服务器的请求(浏览器将数据发送给服务器时,数据存放的地方)
 请求方式:GET和POST
* GET:发送的数据,追加在请求的URL之上
* POST:发送的数据在HTTP请求体中
 浏览器发送数据
* 表单form,
* method属性:指定的请求方式
* action属性:接收数据的程序路径
 服务器获得浏览器发送的数据
* 获得单个数据
* request.getParameter("username");
* 获得一组数据
* request.getParameterValues("love");
* 处理中文乱码
* request.setCharacterEncoding("UTF-8");
* 注意:
* 此方法只对POST请求有效,GET需要单独处理
* 需要放置在获得数据之前,建议放在第一个行

 获得一个数组内的数据

public class GetDataRequestServlet2 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        //获得表单提交的所有的数据
        //获得用户名信息
        String username = request.getParameter("username");
        System.out.println("name: " + username);
        //获得用户密码
        String password = request.getParameter("userpassword");
        System.out.println("pwd: " + password);
        //获得性别
        String sex = request.getParameter("sex");
        System.out.println("sex: " + sex);
        
        //获得爱好,存在多个选择
//        String love = request.getParameter("love");
//        System.out.println("love : " + love);
        String[] loves = request.getParameterValues("love");
        //System.out.println(loves);  //[Ljava.lang.String;@9260ee
        String loveStr = Arrays.toString(loves);
        System.out.println(loveStr);
        
        
        String error = request.getParameter("error");
        System.out.println("error : " + error);
        

    }

}r

request 处理中文

 1 public class GetCNDataRequestServlet extends HttpServlet {
 2 
 3     public void doGet(HttpServletRequest request, HttpServletResponse response)
 4             throws ServletException, IOException {
 5         //确定请求编码
 6         //request.setCharacterEncoding("UTF-8");
 7         System.out.println("get");
 8         
 9         //get请求处理提交数据中的 中文
10         String username = request.getParameter("username");
11         System.out.println(username);
12         //解码操作
13         String u = URLDecoder.decode(username, "GBK");
14         System.out.println(u);
15         
16         String str = new String(username.getBytes("ISO-8859-1"),"UTF-8");
17         System.out.println("**** " + str);
18         
19         //获得用户get请求提交的数据
20         String query = request.getQueryString();
21         System.out.println(query);
22         //解码
23         String q = URLDecoder.decode(query, "UTF-8");
24         System.out.println(q);
25         
26         //提交后url中的内容:getCNDataRequestServlet?username=%E4%B8%AD%E6%96%87
27     }
28 
29     public void doPost(HttpServletRequest request, HttpServletResponse response)
30             throws ServletException, IOException {
31         //需要通知tomcat,用户提交的数据的字符集UTF-8
32         request.setCharacterEncoding("UTF-8");
33         
34         System.out.println("post");
35         String username = request.getParameter("username");
36         System.out.println(username);
37 
38     }
39 
40 }

 request处理中文就不要用get方式处理,因为很麻烦!!

posted @ 2017-03-28 11:26  xuyatao  阅读(297)  评论(0编辑  收藏  举报