目录
4.2.1 延时跳转(访问到Servlet类3秒后跳转到百度 )
一、Http请求
1.1、 HTTP协议(掌握)
全称: 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。
简而言之:浏览器和服务器数据交换固定的格式。
分类:
- 请求报文协议(浏览器发送给服务器的数据):1)请求行 2)请求头 3)请求体
- 响应报文协议(服务器发送给浏览器的数据):1)响应行 2)响应头 3)响应体
特点:
- 先有请求
- 再有响应
- 一个请求对应一个响应
1.2、浏览器抓包观察请求报文协议
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>GET请求</h2>
<form action="/seeHttp" method="get">
用户名:<input type="text" name="username" value="zhangsan" /><br>
密码:<input type="text" name="pwd" value="123" /><br>
<input type="submit" value="提交"/>
</form>
<h2>POST请求</h2>
<form action="/seeHttp" method="post">
用户名:<input type="text" name="username" value="zhangsan"/><br>
密码:<input type="text" name="pwd" value="123"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
@WebServlet("/seeHttp")
public class 观察请求报文协议 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("post请求 = " + request);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("get请求 = " + request);
}
}
启动服务,打开浏览器f12,访问页面。
抓包结果
2.1.1 请求报文协议的get请求和post请求区别:
1. GET请求:
1)请求行直接传递请求参数.将请求参数追加在URL后面,不安全。例如:form.html?username=jack&username=1234
补充:但是get请求可以作为商品的分享。
2)URL长度限制(不同浏览器限制大小不一致),GET请求方式的数据大小,不可以传输数据量较大或者非文本数据。例如图片或者视频。
3)请求体里面没有内容。2. POST请求:
1)请求参数以请求体形式发送给服务器,数据传输安全。
2)请求数据可以为非文本数据,可以传输数据量较大的数据。
3)只有表单设置为method=”post”才是post请求.
其他的都是get请求。常见GET请求:地址栏直接访问、<a href=””>、<img src=””> location.href=""等
二、Request
2.1、request介绍
用户通过浏览器访问服务器时,Tomcat将HTTP请求中所有的信息都封装在Request对象中。开发人员可以通过request对象方法,来获取浏览器发送给服务器的所有信息。
request体系结构:
ServletRequest (兼容大部分协议)
|
HttpServletRequest (HTTP协议)
|
org.apache.catalina.connector.RequestFacade(由tomcat厂商提供实现类)
在javaEE中是面向接口编程 (面向对象的一种思想)
2.2、Request获取Http请求行信息
例:
GET /day09_request/requestDemo1 HTTP/1.1
请求行数据: 请求方式 请求地址 协议/版本
javaee文档:
方法 | 说明 |
---|---|
String getMethod() | 获取请求方式的类型 字母是大写的 GET POST 掌握 |
StringBuffer getRequestURL() | 获取客户端发出请求完整URL http://localhost:8080/day09_request/requestDemo1 |
String getRemoteAddr() | 获取客户端IP地址 |
String getProtocol() | 获取当前协议的名称和版本 HTTP/1.1 |
String getContextPath() | 获取项目虚拟路径(项目名),空的,因为访问路径设置为 /【掌握】 |
String getRequestURI() | 获取URI /requestRow01Servlet 统一资源标识符: 范围广,只要能唯一标识资源的: 本地路径,远程地址 url 是 uri 的子集 |
测例:
@WebServlet("/reqGetLine")
public class Request获取Http请求行信息 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.String getMethod()获取请求方式的类型 字母是大写的 GET POST 掌握
System.out.println(request.getMethod());//GET
//2.StringBuffer getRequestURL()获取客户端发出请求完整URL
//http://localhost:8080/reqGetLine
StringBuffer requestURL = request.getRequestURL();
/* StringBuffer 和StringBuilder一样都表示字符串缓冲区,内容都可变,用法都一样。
区别:
1.StringBuffer多线程安全,效率低
2.StringBuilder多线程不安全,效率高 建议使用*/
System.out.println("请求路径 = " + requestURL);
//3.String getRemoteAddr()获取客户端IP地址
/* 这里获取的是ipv6的表示的ip地址,因为这里我们客户端和服务器都是这哎同一个电脑上,并且我的ip地址使用的是localhost
如果不使用localhost那么就是ipv4表示方法 127.0.0.1*/
System.out.println("户端IP地址 = " + request.getRemoteAddr());// 0:0:0:0:0:0:0:1
//4.String getProtocol()获取当前协议的名称和版本 HTTP/1.1
System.out.println("当前协议的名称和版本 = " + request.getProtocol());// HTTP/1.1
//5.String getContextPath()获取项目虚拟路径(项目名),空的,因为访问路径设置为 /【掌握】
System.out.println("取项目虚拟路径(项目名) = " + request.getContextPath());
//6.String getRequestURI()获取URI /requestRow01Servlet<br/>统一资源标识符:
// <br/> 范围广,只要能唯一标识资源的: 本地路径,远程地址<br/> url 是 uri 的子集<br/>
System.out.println("获取URI = " + request.getRequestURI());// /httpRequest01Servlet
}
}
控制台打印:
GET
请求路径 = http://localhost:8080/reqGetLine
户端IP地址 = 0:0:0:0:0:0:0:1
当前协议的名称和版本 = HTTP/1.1
取项目虚拟路径(项目名) =
获取URI = /reqGetLine
2.3、 Request获取Http请求头信息
例如:
Host: localhost:8080(主机: 指的就是tomcat访问地址)
name(请求头属性名,固定不变的) : value(对应的属性值,动态改变)
常见请求头:
user-agent (用户的系统版本和浏览器)
referer(上一次地址)
如果在浏览器抓包中看不到某个头,就表示这个头所对应的值是null
方法 | 作用 |
---|---|
String getHeader(String name) | 根据请求头的key关键字获取请求头信息 |
Enumeration getHeaderNames() | 返回此请求包含的所有头信息的key,属于对枚举类型进行遍历 |
测例:
@WebServlet("/reqGetHead")
public class req获取Http请求头 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("get请求");
//1.Enumeration getHeaderNames()返回此请求包含的所有头信息的key,属于对枚举类型进行遍历
Enumeration<String> en = request.getHeaderNames();
/*
此接口的功能与 Iterator 接口的功能是重复的。此外,Iterator 接口添加了一个可选的移除操作,
并使用较短的方法名。新的实现应该优先考虑使用 Iterator 接口而不是 Enumeration 接口。
1.hasMoreElements() 表示判断迭代器中是否有数据
2.nextElement()获取数据
*/
while(en.hasMoreElements()){
//说明有数据
System.out.println(en.nextElement());
}
//2.String getHeader(String name)根据请求头的key关键字获取请求头信息
//2.1需求:获取key是user-agent的值
String header = request.getHeader("user-agent");
//Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36
System.out.println("header = " + header);
//2.2获取key是referer的值
//获取的上一次浏览的地址 如果没有上一次那么结果是null
System.out.println(request.getHeader("referer"));
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("post请求");
}
}