当前位置:网站首页>浅谈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实例。
边栏推荐
- Mysql5.7 二进制 部署
- [Go through 7] Notes from the first section of the fully connected neural network video
- OFDM Lecture 16 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
- Do you use tomatoes to supervise your peers?Add my study room, come on together
- 【过一下14】自习室的一天
- RDD和DataFrame和Dataset
- 【过一下8】全连接神经网络 视频 笔记
- Basic properties of binary tree + oj problem analysis
- Dashboard Display | DataEase Look at China: Data Presents China's Capital Market
- 第四讲 反向传播随笔
猜你喜欢
NodeJs接收上传文件并自定义保存路径
Using QR codes to solve fixed asset management challenges
DOM and its applications
coppercam入门手册[6]
Lecture 5 Using pytorch to implement linear regression
Mesos学习
jvm three heap and stack
The software design experiment four bridge model experiment
【过一下9】卷积
Pycharm中使用pip安装第三方库安装失败:“Non-zero exit code (2)“的解决方法
随机推荐
第5讲 使用pytorch实现线性回归
【过一下6】机器视觉视频 【过一下2被挤掉了】
phone call function
第四讲 反向传播随笔
实现跨域的几种方式
redis复制机制
【技能】长期更新
1.3 mysql batch insert data
2022 Hangzhou Electric Multi-School 1st Session 01
【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
学习总结week2_3
类的底层机制
uva1325
server disk array
【过一下11】随机森林和特征工程
ES6 生成器
【过一下 17】pytorch 改写 keras
uva1325
物理层的接口有哪几个方面的特性?各包含些什么内容?
学习总结week3_4类与对象