2 HTTP(请求消息)
2.1 概念
2.2 请求消息数据格式
1、请求行
请求方式 请求url 请求协议/版本
POST /myfirstweb_war_exploded/demo2 HTTP/1.1
2、请求头:浏览器告诉服务器一些信息
请求头名称:请求头值
Host: localhost:8088
…
- 常见的请求头:
1、User-Agent:浏览器告诉服务器,访问的浏览器的版本信息
2、referer:告诉服务器,我(当前请求)从哪里来
3、请求空行:分割post请求的请求头和请求体
空行
4、请求体:封装post请求消息的请求参数
username=zhangsan
**注:**get方式没有请求体
- 字符串格式
POST /myfirstweb_war_exploded/demo2 HTTP/1.1
Host: localhost:8088
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Cookie: JSESSIONID=FEBD6BB7572B1617880FCAA31945D11B; Idea-1d85ba42=ef467e86-8eec-484f-a3d4-344ba31f2115
username=zhangsan
3 Requst
3.1 request对象和response对象的原理
3.2 request对象继承体系结构
ServletRequest ——>接口
| 继承
HttpServletRequest ——>接口
| 实现
org.apache.catalina.connector.RequestFacade ——>类
3.2 request对象的功能
3.2.1获取请求消息数据
3.2.1.1 获取请求行数据
GET /myfirstweb_war_exploded/demo2?name=zhangsan HTTP/1.1
- 获取请求方式:GET ——>
String getMethod()
- 获取虚拟目录:/myfirstweb_war_exploded ——>
String getContextPath()
(掌握) - 获取Servlet路径:/demo2 ——>
String getServletPath()
- 获取get方式的请求参数:name=zhangsan ——>
String getQueryString()
- 获取请求的URI:/myfirstweb_war_exploded/demo2 ——>
String getStringURI()
(掌握)
http://localhost/myfirstweb_war_exploded/demo2 ——> StringBuffer getStringURL()
- 获取协议及版本:HTTP/1.1 ——>
String getProtocol()
- 获取客户机的IP地址 ——>
String getRemoteAddr()
@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 获取请求方式 :GET
String method = request.getMethod();
System.out.println(method);
//2.(*)获取虚拟目录:/day14
String contextPath = request.getContextPath();
System.out.println(contextPath);
//3. 获取Servlet路径: /demo1
String servletPath = request.getServletPath();
System.out.println(servletPath);
//4. 获取get方式请求参数:name=zhangsan
String queryString = request.getQueryString();
System.out.println(queryString);
//5.(*)获取请求URI:/day14/demo1
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURI);
System.out.println(requestURL);
//6. 获取协议及版本:HTTP/1.1
String protocol = request.getProtocol();
System.out.println(protocol);
//7. 获取客户机的IP地址:
String remoteAddr = request.getRemoteAddr();
System.out.println(remoteAddr);
}
}
3.2.1.2 获取请求头数据
(掌握)
- 常见的请求头:
1、User-Agent:浏览器告诉服务器,访问的浏览器的版本信息
2、referer:告诉服务器,我(当前请求)从哪里来
方法:
String getHeader(String name)
: 通过请求头名称获取请求头的值Enumeration<String> getHeaderNames()
: 获取所有的请求头名称
@WebServlet("/requestDemo2")
public class RequestDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取所有请求头名称
Enumeration<String> headerNames = request.getHeaderNames();
//2.遍历
while(headerNames.hasMoreElements()){
String name = headerNames.nextElement();
//根据名称获取请求头的值
String value = request.getHeader(name);
System.out.println(name+"---"+value);
}
}
}
User-Agent:浏览器告诉服务器,访问的浏览器的版本信息
@WebServlet("/requestDemo3")
public class RequestDemo3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//演示获取请求头数据:user-agent
String agent = request.getHeader("user-agent");
//判断agent的浏览器版本
if(agent.contains("Chrome")){
//谷歌
System.out.println("谷歌来了...");
}else if(agent.contains("Firefox")){
//火狐
System.out.println("火狐来了...");
}
}
}
referer:告诉服务器,我(当前请求)从哪里来
得从其它界面跳转,而不是直接输入URL跳转
@WebServlet("/requestDemo4")
public class RequestDemo4 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
<