JavaWeb之servlet管理机制
2021-06-10 12:02
标签:实现 方式 load etl 就会 nts service des http 简单的说,浏览器发出请求到tocat服务器,服务器就会初始化一个servlet实例(servlet采取生命托管的方式实现单例,不存在时才会创建实例),servlet示例会启动一个线程来处理该请求,并进行响应该请求,动态生成web内容 servlet类源码解析 JavaWeb之servlet管理机制 标签:实现 方式 load etl 就会 nts service des http 原文地址:https://www.cnblogs.com/tar8087/p/14456647.html一、什么是Servlet
二、什么是Tomcat服务器
三、web.xml配置文件解析
1 servlet>
2
3 servlet-name>loginservlet-name>
4
5 servlet-class>com.TestServlet.servlet.LoginServletservlet-class>
6
13 init-param>
14 param-name>name1param-name>
15 param-value>value1param-value>
16 param-name>name2param-name>
17 param-value>value2param-value>
18 init-param>
19
20 load-on-startup>1load-on-startup>
21 servlet>
22
23 servlet-mapping>
24
25 servlet-name>loginservlet-name>
26
27 url-pattern>/indexurl-pattern>
28 servlet-mapping>
29
30
四、servlet类的继承关系
1 public class LoginServlet extends HttpServlet {
2
3 @Override
4 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
5 super.service(req, resp);
6 }
7
8 @Override
9 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
10 super.doGet(req, resp);
11 }
12
13 @Override
14 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
15 super.doPost(req, resp);
16 }
17 }
1 // HtppServlet继承类GenericServlet类
2 public abstract class HttpServlet extends GenericServlet {}
3 /* GenericServlet类,实现了三个接口,分别为
4 Servlet: servlet接口
5 ServletConfig: 封装servlet配置信息
6 Serializable: 序列化接口,在反序列化时进行版本比对
7 */
8 public abstract class GenericServlet implements Servlet, ServletConfig, Serializable {}
1 public interface Servlet {
2 /**
3 * servlet声明周期
4 * 1、init() 初始化,仅第一次servlet时执行,同时会携带一个ServletConfig参数
5 * 2、service() 用户处理请求,每次访问servlet时都会执行一次
6 * 3、destory() 重新配置或者重新部署项目时执行,用于销毁servlet对象,销毁后的对象由gc进行回收
7 * */
8 void init(ServletConfig var1) throws ServletException;
9
10 ServletConfig getServletConfig();
11
12 void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
13
14 String getServletInfo();
15
16 void destroy();
17 }
1 public interface ServletConfig {
2 // 获取当前servlet类名
3 String getServletName();
4 // 获取servlet全局上下文
5 ServletContext getServletContext();
6 // 获取web.xml文件中
1 // 序列化
2 private static final long serialVersionUID = 1L;
3 // ServletConfig 属性
4 private transient ServletConfig config;
5 // 无参构造方法
6 public GenericServlet() {
7 }
8 // 实现了servlet中的destroy() 方法
9 public void destroy() {
10 }
11 // 重写了并实现了ServletConfig中的getInitParameter(),便于获取web.xml配置文件中的
1 // doGet方法,未进行任何处理,
2 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
3 String protocol = req.getProtocol();
4 String msg = lStrings.getString("http.method_get_not_supported");
5 if (protocol.endsWith("1.1")) {
6 resp.sendError(405, msg);
7 } else {
8 resp.sendError(400, msg);
9 }
10
11 }
12
13 // doPost方法,未进行任何处理,
14 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
15 String protocol = req.getProtocol();
16 String msg = lStrings.getString("http.method_post_not_supported");
17 if (protocol.endsWith("1.1")) {
18 resp.sendError(405, msg);
19 } else {
20 resp.sendError(400, msg);
21 }
22
23 }
24
25 // 提供了自己的受保护的service方法,便于子类访问,类中未进行具体操作,而是通过获取请求类型来定位调用方法来处理
26 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
27 String method = req.getMethod();
28 long lastModified;
29 if (method.equals("GET")) {
30 lastModified = this.getLastModified(req);
31 if (lastModified == -1L) {
32 this.doGet(req, resp);
33 } else {
34 long ifModifiedSince;
35 try {
36 ifModifiedSince = req.getDateHeader("If-Modified-Since");
37 } catch (IllegalArgumentException var9) {
38 ifModifiedSince = -1L;
39 }
40
41 if (ifModifiedSince ) {
42 this.maybeSetLastModified(resp, lastModified);
43 this.doGet(req, resp);
44 } else {
45 resp.setStatus(304);
46 }
47 }
48 } else if (method.equals("HEAD")) {
49 lastModified = this.getLastModified(req);
50 this.maybeSetLastModified(resp, lastModified);
51 this.doHead(req, resp);
52 } else if (method.equals("POST")) {
53 this.doPost(req, resp);
54 } else if (method.equals("PUT")) {
55 this.doPut(req, resp);
56 } else if (method.equals("DELETE")) {
57 this.doDelete(req, resp);
58 } else if (method.equals("OPTIONS")) {
59 this.doOptions(req, resp);
60 } else if (method.equals("TRACE")) {
61 this.doTrace(req, resp);
62 } else {
63 String errMsg = lStrings.getString("http.method_not_implemented");
64 Object[] errArgs = new Object[]{method};
65 errMsg = MessageFormat.format(errMsg, errArgs);
66 resp.sendError(501, errMsg);
67 }
68
69 }
70
71 // 实现了接口servlet类中的service()方法,但没有将具体处理,仅仅将ServletRequest和ServletResponse进行
72 // 此处体现了面向对象思想,也体现了适配器模式的应用。
73 public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
74 HttpServletRequest request;
75 HttpServletResponse response;
76 try {
77 request = (HttpServletRequest)req;
78 response = (HttpServletResponse)res;
79 } catch (ClassCastException var6) {
80 throw new ServletException("non-HTTP request or response");
81 }
82
83 // 调用该类的受保护类
84 this.service(request, response);
85 }