当前位置:网站首页>浅谈Servlet生命周期
浅谈Servlet生命周期
2022-08-05 05:13:00 【来一杯奶喵】
Servlet生命周期
Servlet的生命周期可以分为四个阶段,分别为:实例化、初始化、服务以及销毁。
- 实例化:servlet容器创建servlet的实例
- 初始化:servlet容器调用init(ServletConfig)方法初始化
- 服务:请求servlet,容器调用service()方法
- doGet()方法
- doPost()方法
- 销毁:销毁实例之前调用destroy()方法
public interface Servlet {
//初始化
void init(ServletConfig var1) throws ServletException;
ServletConfig getServletConfig();
//服务
void service(ServletRequest var1, ServletResponse var2) throws ServletException, IOException;
String getServletInfo();
//销毁
void destroy();
}Servlet实例化:
Servlet 创建于用户第一次调用对应于该 Servlet 的 URL 时。每当用户调用一个Servlet,容器就会创建一个Servlet实例。
Servlet初始化(init()方法):
Init()方法只能调用一次,只在第一次创建servlet时被调用。
服务(service()方法):
service() 方法是执行实际任务的主要方法。Servlet 容器调用 service() 方法来处理来自客户端(浏览器)的请求,并将响应返回给客户端。
Service方法会识别请求类型(GET、POST、PUT等)并调用doGet/doPost等方法,因此我们只需要重写其方法,就可以处理不同类型的请求。
protected void service(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
String method = req.getMethod();
long lastModified;
//Service方法识别请求类型
if (method.equals("GET")) {
lastModified = this.getLastModified(req);
if (lastModified == -1L) {
this.doGet(req, resp);
} else {
long ifModifiedSince;
try {
ifModifiedSince = req.getDateHeader("If-Modified-Since");
} catch (IllegalArgumentException var9) {
ifModifiedSince = -1L;
}
if (ifModifiedSince < lastModified / 1000L * 1000L) {
this.maybeSetLastModified(resp, lastModified);
this.doGet(req, resp);
} else {
resp.setStatus(304);
}
}
} else if (method.equals("HEAD")) {
lastModified = this.getLastModified(req);
this.maybeSetLastModified(resp, lastModified);
this.doHead(req, resp);
} else if (method.equals("POST")) {
this.doPost(req, resp);
} else if (method.equals("PUT")) {
this.doPut(req, resp);
} else if (method.equals("DELETE")) {
this.doDelete(req, resp);
} else if (method.equals("OPTIONS")) {
this.doOptions(req, resp);
} else if (method.equals("TRACE")) {
this.doTrace(req, resp);
} else {
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[]{method};
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}doGet()方法及doPost()方法:
Get请求是一个url的普通请求或是未指定method的表单提交的请求,这种请求会调用doGet()方法处理。
Post请求是指定了 method 为 POST表单提交的请求,调用doPost()方法处理。
销毁destroy():
destroy()方法和init()方法一样,只被调用一次,destroy()方法在servlet生命周期结束时被调用。调用这个方法后servlet对象会被标记为垃圾由JVM垃圾回收器回收掉。
我们来总结一下servlet生命周期的整个流程:
①用户第一次调用一个Servlet的url时,servlet容器创建servlet实例。
②调用init()方法。
③再调用service()方法处理请求,响应用户。
④调用destroy()方法销毁servlet实例。
边栏推荐
猜你喜欢

【过一下8】全连接神经网络 视频 笔记

开发一套高容错分布式系统

The difference between span tag and p

【过一下3】卷积&图像噪音&边缘&纹理

DOM及其应用

OFDM 十六讲 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems

【过一下12】整整一星期没记录

Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)

A blog clears the Redis technology stack
![[Go through 3] Convolution & Image Noise & Edge & Texture](/img/7b/2214020cadf06d9211fd40fb5f1b63.png)
[Go through 3] Convolution & Image Noise & Edge & Texture
随机推荐
学习总结week3_1函数
pycharm中调用Matlab配置:No module named ‘matlab.engine‘; ‘matlab‘ is not a package
【过一下 17】pytorch 改写 keras
Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
Opencv中,imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) 报错:error:!_src.empty() in function ‘cv::cvtColor‘
重新审视分布式系统:永远不会有完美的一致性方案……
human weakness
[WeChat applet] WXML template syntax - conditional rendering
JSX基础
redis事务
DOM and its applications
day8字典作业
OFDM Lecture 16 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
学习总结day5
电话溥功能
Using pip to install third-party libraries in Pycharm fails to install: "Non-zero exit code (2)" solution
Pycharm中使用pip安装第三方库安装失败:“Non-zero exit code (2)“的解决方法
RL reinforcement learning summary (1)
redis 缓存清除策略
uva1325