## Request:
1. request对象和response对象的原理
1. request和response对象是由服务器创建的。我们来使用它们
2. request对象是来获取请求消息,response对象是来设置响应消息
(94条消息) Java必要准确区别之数组Array和集合list、map、set_小吴先森的博客-CSDN博客
http://localhpst/day/demo01 其实就是请求带着请求消息进来找demo1的资源 因为服务器端有一个servlet配置的urlpattern是/demo01,就找到了这个类,找到这个类之后,tomcat会自动执行里面的servlet的方法
注意图片里的红字
2. request对象继承体系结构:
ServletRequest -- 接口 extends的接口
| 继承
HttpServletRequest -- 接口 这个是servlet种的service参数里面的接口名
| 实现https://mp.csdn.net/mp_blog/creation/editor/123563457
org.apache.catalina.connector.RequestFacade ---这是一个类(tomcat创建的) 这个是打印了req输出得到的
所以是tomcat实现了 HttpServletRequest 接口 从而通过tomcat创建request对象req 再传递给service方法
3. request功能: 重要
1. 获取请求消息数据 : 有四部分 请求行 请求头 请求空行 请求体
1. 获取请求行数据
格式: * GET /day14/demo1?name=zhangsan HTTP/1.1
* 方法:
1. 获取请求方式 :GET
* String getMethod()
2. (*重点)获取虚拟目录:/day14 注意:这个是虚拟目录
* String getContextPath()
3. 获取Servlet路径: /demo1 注意:这个是servlet路径
* String getServletPath()
4. 获取get方式请求参数:name=zhangsan
* String getQueryString()
5. (*重点)获取请求URI:/day14/demo1 同时获得虚拟目录和servlet路径
* String getRequestURI(): /day14/demo1 ------URI比较短但是获取的范围大
* StringBuffer getRequestURL() :http://localhost/day14/demo1 ----URL比较长
* URL:统一资源定位符 : http://localhost/day14/demo1 中华人民共和国
* URI:统一资源标识符 : /day14/demo1 共和国
URI比较短但是获取的范围大 就好比共和国的范围更大 我觉着可能是概括范围简略
6. 获取协议及版本:HTTP/1.1
* String getProtocol()
7. 获取客户机的IP地址:
* String getRemoteAddr()
可以直接创建servlet 直接new servlet
演示request的方法
package cn.itcast.web.request; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet( "/RequestDemo01") public class RequestDemo01 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //为什么写再doget因为浏览器请求是get /* 1. 获取请求行数据 格式: * GET /day14/demo1?name=zhangsan HTTP/1.1 * 方法: 1. 获取请求方式 :GET * String getMethod() 2. (*重点)获取虚拟目录:/day14 注意:这个是虚拟目录 * String getContextPath() 3. 获取Servlet路径: /demo1 注意:这个是servlet路径 * String getServletPath() 4. 获取get方式请求参数:name=zhangsan * String getQueryString() 5. (*重点)获取请求URI:/day14/demo1 同时获得虚拟目录和servlet路径 * String getRequestURI(): /day14/demo1 ------URI比较短 * StringBuffer getRequestURL() :http://localhost/day14/demo1 ----URL比较长 * URL:统一资源定位符 : http://localhost/day14/demo1 中华人民共和国 * URI:统一资源标识符 : /day14/demo1 共和国 6. 获取协议及版本:HTTP/1.1 * String getProtocol() 7. 获取客户机的IP地址: * String getRemoteAddr() */ // 1. 获取请求方式 :GET String method = request.getMethod(); System.out.println(method); // 2. (*重点)获取虚拟目录: String contextPath = request.getContextPath(); System.out.println(contextPath); //3. 获取Servlet路径: String servletPath = request.getServletPath(); System.out.println(servletPath); // 4. 获取get方式请求参数: String queryString = request.getQ