Web 基础10 Resquest对象的简介

1.1 Resquest的概述

  Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象和代表响应的response对象。

  request和response对象既然代表请求和响应,那我们要获取客户机提交过来的数据,只需要找request对象就行了。要向客户机输出数据,只需要找response对象即可。

  Request代表请求对象,其中封装了对请求中具有请求行、请求头、实体内容的操作的方法

1.1.1 Resquest的一些方法

方法 描述
String getRequestURI() Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
StringBuffer getRequestURL() Reconstructs the URL the client used to make the request.
String getMethod() Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.
String getQueryString() Returns the query string that is contained in the request URL after the path.
  • 代码实例
public class RequestDemo extends HttpServlet {
    
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       /*
        *      获取请求行
                   StringBuffer getRequestURL()  
                   String getRequestURI()  
                   String getMethod()  
                   String getContextPath() 
                   String getQueryString() 
        */
       //http://localhost:8080/myRequest/RequestDemo
       String url = request.getRequestURL().toString();//完整的URL
       
       // /myRequest/RequestDemo
       String uri = request.getRequestURI();//请求行中的资源路径
       
       //GET
       String method = request.getMethod();//请求方式
       
       // /myRequest
       String contextPath = request.getContextPath();//当前项目的路径
       
       //username=zhangsan&password=123
       String queryString = request.getQueryString();//请求行中资源路径后面的参数
       
       System.out.println(url);
       System.out.println(uri);
       System.out.println(method);
       System.out.println(contextPath);
       System.out.println(queryString);
   }

    
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       // TODO Auto-generated method stub
       doGet(request, response);
   }

}

1.2 Resquest应用于表单的提交

定义一个HTML


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
    <form action="/myRequest/RequestDemo6" method="get">
        用户名:<input type="text" name="username" />
        <br />
        密码:<input type="password" name="password"/>
        <br />
        兴趣爱好:
            <input type="checkbox" name="hobbies" value="java"/>java
            <input type="checkbox" name="hobbies" value="php"/>php
            <input type="checkbox" name="hobbies" value="python"/>python
            <input type="checkbox" name="hobbies" value="android"/>android
        <br />
        <input type="submit" name="submit" value="注册"/>
    </form>
</body>
</html>


1.2.1 获取表单提交的数据

  String getParameter(String name) :根据表单项的name来获取表单项的value

String username = request.getParameter("username");
System.out.println(username);

String password = request.getParameter("password");
System.out.println(password);

String gender = request.getParameter("gender");
System.out.println(gender);

  • 注意:
    • 有表单项提交过来,但是没有值, 打印的是空字符串
    • 表单项根本没有提交过来,打印的就是null,就不能调用字符串的方法,否则会报空指针异常

1.2.2 获取表单提交的数据2

   String[] getParameterValues(String name)

String hobbies = request.getParameter("hobbies");System.out.println(hobbies);

String[] hobbies = request.getParameterValues("hobbies");
for (String h : hobbies) {
    System.out.println(h);
}


String[] username = request.getParameterValues("username");
for (String s : username) {
    System.out.println(s);
}

1.2.3 获取表单提交的数据3

//Map getParameterMap() 
Map<String,String[]> map = request.getParameterMap();
//遍历Map
//获取所有的key
Set<String> keys = map.keySet();
//获取每一个key
for (String key : keys) {
    System.out.println(key);
    //通过key来获取Map的value
    String[] arr = map.get(key);
    //遍历数组,获取每一个表单项的value
    for (String value : arr) {
        System.out.println("   " + value);
    }
}
//Enumeration getParameterNames() 
Enumeration<String> e = request.getParameterNames();
while(e.hasMoreElements()) {
    String name = e.nextElement();
    System.out.println(name);
}

1.3获取表单提交的数据乱码的处理

  • 原因:

    • 编码前后不一致
    • 表单提交数据用的是UTF-8
    • 请求对象接收数据转换成字符串 用的是默认的ISO-8859-1
  • 对于post请求

    • request.setCharacterEncoding("UTF-8");//只能处理请求体,这里设置请求对象创建字符串所使用的编码是UTF-8

默认也是ISO-8859-1
request.setCharacterEncoding("UTF-8");//只能处理请求体

String username = request.getParameter("username");
System.out.println(username);
        
  • 对于get请求
    • 浏览器
      • 按照表单页面的编码(UTF-8)进行转码,转成字节数组,拼接在请求资源的后面
      • 接下来把字节数组做一个转义(url编码)在发送
    • 服务器
      • 服务器首先拿到这个数据做一个url解码,(正确的)
      • 然后把解码之后的数据按照ISO-8859-1拼成字符串(错误)
//把字符串打回原形
byte[] bys = request.getParameter("username").getBytes("ISO-8859-1");
//把字节数组按照正确的编码转换字符串
String username = new String(bys,"UTF-8");
System.out.println(username);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,908评论 18 139
  • 编码问题一直困扰着开发人员,尤其在 Java 中更加明显,因为 Java 是跨平台语言,不同平台之间编码之间的切换...
    x360阅读 2,506评论 1 20
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,764评论 18 399
  • 人物有特点,说话要矫情,男主不必帅掉渣,关键得痴情。或是富家子,或是穷白丁,身边有些好姐妹,可以诉衷情。女主白富美...
    柳梦梅阅读 28,066评论 0 0
  • 中秋节快到了, 上周四我们302班搞了一次班队活动--------做冰皮月饼,来迎接中秋节的到来! 当...
    陈小孜2008阅读 319评论 0 1