Servlet
-
概念
-
步骤
-
执行原理
-
生命周期
-
Servlet3.0 注解配置
-
Servlet的体系结构
Servlet – 接口
|
GenericServlet – 抽象类
|
HttpServlet – 抽象类-
GenericServlet:将Servlet接口中其他的方法做了默认空实现,只将service()方法作为抽象
-
将来定义Servlet类时,可以继承GenericServlet,实现service()方法即可
@WebServlet("/demo02") public class ServletDemo02 extends GenericServlet { @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { System.out.println("123465"); } }
-
HttpServlet:对http协议的一种封装,简化操作
① 定义类继承HttpServlet
② 复写doGet/doPost方法
@WebServlet("/demo03") public class ServletDemo03 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doGet..."); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("doPost..."); } }
-
HTTP
概念
Hyper Text Transfer Protocol 超文本传输协议
特点
- 基于TCP/IP的高级协议
- 默认端口号:80
- 基于请求/响应模型的:一次请求对应一次响应
- 无状态的:每次请求之间相互独立,不能交互数据
历史版本
1.0:每一次请求响应都会建立新的连接
1.1:复用连接
请求消息数据格式
-
请求行
- 请求方式 :请求url 请求协议/版本 GET /login.html HTTP/1.1
- GET:
- 请求参数在请求行中,在url后。
- 请求的url长度有限制的
- 不太安全
- POST:
- 请求参数在请求体中
- 请求的url长度有限制的
- 相对安全
-
请求头:客户端浏览器告诉服务器一些信息
请求头名称: 请求头值
-
常见的请求头:
-
User-Agent:浏览器告诉服务器,我访问你使用的浏览器版本信息
可以在服务器端获取该头的信息,解决浏览器的兼容性问题
-
Referer:http://localhost/login.html
告诉服务器,我(当前请求)从哪里来?
作用:防盗链;统计工作
-
-
-
请求空行
空行,就是用于分割POST请求的请求头,和请求体的
-
请求体(正文):
封装POST请求消息的请求参数的
响应头
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;
charset=UTF-8 Content-Length: 169
Date: Sun, 12 Sep 2021 03:14:55 GMT
请求标头
GET /servlet_war_exploded/ HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: "Microsoft Edge";v="93", " Not;A Brand";v="99", "Chromium";v="93"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent:浏览器告诉服务器,我访问你使用的浏览器版本信息
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.38
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,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
Cookie: JSESSIONID=1B9F915601CFFD31D45AC665F32F32A2; Idea-f49894ec=3f78fd14-ba70-4f7e-84df-168b38d0658f