一、repuset对象和response对象的原理
1. request和response对象是有服务器创建的,我们可以使用
2. request对象是来获取请求消息,response对象是来设置响应消息
二、request对象继承体系结构:
ServletRequest -- 接口
继承
HttpServletRequest -- 接口
实现
org.apache.catalina.connector.RequestFacade 类(tomcat)
三、request功能:
1. 获取请求消息数据:
1. 获取请求行数据:
1. 获取请求方法:String getMethod()
2. 获取虚拟目录:String getContextPath()
3. 获取Servlet路径:String getServletPath()
4. 获取get方式请求参数:String getQueryString()
5. 获取请求的URI: String getRequestURI() StringBuffer getRequestURL():http://localhost/day14/demo1
6. 获取协议及版本号:String getProtocol()
7. 获取客户机的IP地址: String getRemoteAddr()
代码:
1 package cn.aikang.Servlet;
2
3 import javax.servlet.ServletException;
4 import javax.servlet.annotation.WebServlet;
5 import javax.servlet.http.HttpServlet;
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8 import java.io.IOException;
9
10 /**
11 * @Author: 艾康
12 * @Date: 2019/10/15 10:16
13 */
14 @WebServlet("/servlettest5")
15 public class ServletTest5 extends HttpServlet {
16 @Override
17 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
18 //获取请求行数据并输出
19 //获取请求方式
20 System.out.println(req.getMethod());
21 //获取虚拟目录
22 System.out.println(req.getContextPath());
23 //获取Servlet路径
24 System.out.println(req.getServletPath());
25 //获取get方式请求参数
26 System.out.println(req.getQueryString());
27 //获取请求的URL
28 System.out.println(req.getRequestURL());
29 //获取请求的URI
30 System.out.println(req.getRequestURI());
31 //获取协议版本号
32 System.out.println(req.getProtocol());
33 //获取客户机的IP
34 System.out.println(req.getRemoteAddr());
35 }
36
37 @Override
38 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
39 this.doGet(req,resp);
40 }
41 }