HttpServletRequest详解(包括如何解决Requset中文乱码问题)

HttpServletRequest

> 这个对象封装了客户端提交过来的一切数据。 

1. 可以获取客户端请求头信息

        //得到一个枚举集合  
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = (String) headerNames.nextElement();
            String value = request.getHeader(name);
            System.out.println(name+"="+value);
            
        }

2. 获取客户端提交过来的数据


        String name = request.getParameter("name");
        String address = request.getParameter("address");
        System.out.println("name="+name);
        System.out.println("address="+address);

        -------------------------------------------------

        //name=zhangsan&name=lisi&name=wangwu 一个key可以对应多个值。

        Map map = request.getParameterMap();
        
        Set keySet = map.keySet();
        Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            String key = (String) iterator.next();
            System.out.println("key="+key + "--的值总数有:"+map.get(key).length);
            String value = map.get(key)[0];
            String value1 = map.get(key)[1];
            String value2 = map.get(key)[2];
            
            System.out.println(key+" ======= "+ value + "=" + value1 + "="+ value2);

例子:1. 可以获取客户端请求头信息   2. 获取客户端提交过来的数据

        

public class RequsetServlst extends HttpServlet {

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  //1.获取请求头里面的信息
     Enumeration hEnumeration=request.getHeaderNames();
     while (hEnumeration.hasMoreElements()) {
   String name = (String) hEnumeration.nextElement();
   String value=request.getHeader(name);
   System.out.println(name+"="+value);
   System.out.println("---------------------------");

     //2.获取客户端提交上来的信息
   String username=request.getParameter("username");
   String address=request.getParameter("address");
   System.out.println("---------------------------");
  //3.获取所有参数,得到一个枚举

  //第一种枚举,就和上面 .获取请求头里面的信息格式一样 
  Enumeration  hEn= request .getParameterNames();
  while ( hEn.hasMoreElements()) {
   String key = (String) hEn.nextElement();
   String value= request.getInitParameter(key);
   System.out.println("key="+key+"--"+"value"+value);
  }

 //第二种枚举
  java.util.Map map=request.getParameterMap();  
     Set keySet=map.keySet();
     Iterator iterator=keySet.iterator();   
     while (iterator.hasNext()) {      
   String key = (String) iterator.next();
   System.out.println("key="+key+"--的值总数:"+map.get(key).length);
   String valu=map.get(key)[0];
   String valu2=map.get(key)[1];
   String valu3=map.get(key)[2];
   System.out.println(key+"="+valu+";"+valu2+";"+valu3);
  }
  }
 }
 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
  doGet(request, response);
 }
}


        

3. 获取中文数据

> 客户端提交数据给服务器端,如果数据中带有中文的话,有可能会出现乱码情况,那么可以参照以下方法解决。

如果是GET方式
    
    1. 代码转码
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            
            System.out.println("userName="+username+"==password="+password);
            
            //get请求过来的数据,在url地址栏上就已经经过编码了,所以我们取到的就是乱码,
            //tomcat收到了这批数据,getParameter 默认使用ISO-8859-1去解码
            
            //先让文字回到ISO-8859-1对应的字节数组 , 然后再按utf-8组拼字符串
            username = new String(username.getBytes("ISO-8859-1") , "UTF-8");
            System.out.println("userName="+username+"==password="+password);
        
            直接在tomcat里面做配置,以后get请求过来的数据永远都是用UTF-8编码。 
    

    2. 可以在tomcat里面做设置处理 conf/server.xml 加上URIEncoding="utf-8"
 
          


如果是POST方式

        这个说的是设置请求体里面的文字编码。  get方式,用这行,有用吗? ---> 没用
        request.setCharacterEncoding("UTF-8"); 
        这行设置一定要写在getParameter之前。

例子:3. 获取中文数据

public class loginServlet extends HttpServlet {
 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  //1.获取接收到的数据,解决中文乱码
  //get方式
   test01(request);
   //post方式
   test02(request);
 }
private void test01(HttpServletRequest request) throws UnsupportedEncodingException {
  //1.获取数据
  String username=request.getParameter("username");
  String password=request.getParameter("password");
     //假如是中文,将会乱码
  System.out.println("------------------");
  System.out.println("username="+username);
  System.out.println("username="+username);
  //下面代码是解决获取的数据中文乱码
  username=new String(username.getBytes("ISO-8859-1"), "utf-8");
  password=new String(password.getBytes("ISO-8859-1"), "utf-8");
  System.out.println("------------------");
  System.out.println("username="+username);
  System.out.println("username="+username);
 }

 private void test02(HttpServletRequest request) throws UnsupportedEncodingException {
  
  request.setCharacterEncoding("utf-8");
  String username=request.getParameter("username");
   String password=request.getParameter("password");  
  System.out.println("------------------");
  System.out.println("username="+username);
  System.out.println("username="+username);
 }

 
 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.print("来了一个post请求");
  doGet(request, response);
 }
}

你可能感兴趣的:(HttpServletRequest详解(包括如何解决Requset中文乱码问题))