当前位置:网站首页>Servlet life cycle
Servlet life cycle
2022-08-09 16:12:00 【Ran959】
Servlet life cycle
The life cycle of a servlet, as its name suggests, is the whole process from the appearance of a servlet to its demise (destruction).
Mainly divided into the following stages:
Loading class—>Instantiation (Allocating space for objects)—>Initialization(assignment to the properties of the object)—>request response (service phase)—>destroy
Three methods of the servlet life cycle:
init() initialization phase
service() handles client request phase
destroy() termination phase
The container (tomcat, etc.) loads the servlet
Instantiation Phase
1.1 When the client sends the first request for the first time, the servlet container parses the request and finds whether there is the corresponding servlet according to the request.
1.2 Determine whether there is an object of the Servlet implementation class?If it exists, use it directly. If it does not exist, first create an object of the servlet implementation class.
Initialization Phase
Servlet initialization is the first phase of its life cycle and the basis for other phases.Only after initialization is complete, the servlet can process requests from clients.
Servlet initialization phase is divided into 2 steps:
- Load and instantiate the servlet;
- Call the init() method to initialize
1. Loading and instantiation
The servlet container is responsible for loading and instantiating servlets.
When the container starts or requests a servlet for the first time, the container will read the configuration information in web.xml (configure load-on-startup=1, the default is 0) or @WebServlet, and load the specified servlet.After the loading is successful, the container will instantiate the servlet through reflection.
2.Call the init() method to initialize
After loading and instantiation is completed, the servlet container will create a servlet object and call the servlet's init method (the init method can only be called once in the servlet life cycle) to initialize the servlet instance.
Request Response Phase
After the initialization is completed, the service() method is called, and the service() determines the client's request method.
3.1 If it is a get request, execute the doGet() method.
3.2 If it is a post request, execute doPost().
3.3 After the processing method is completed, the corresponding result will be returned to the client, and the processing of a single request is completed.
When the user sends the second and subsequent requests, it will determine whether the object exists, but no longer execute init(), and directly execute the service method to call the doGet() / doPost() method.
Service Termination Phase
When the server shuts down, restarts or removes the servlet instance, the servlet calls the destroy() method to destroy it, announcing the end of the life cycle.
public class EmpServlet extends HttpServlet{
//初始化servlet,调用init方法
public void init() throws ServletException {
System.out.println("初始化时调用");
}
//开启服务
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)throws ServletException, IOException {
System.out.println("Called when the service is started");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
// call destroy when destroyed
public void destroy() {
System.out.println("call when destroyed");
} }
}
边栏推荐
猜你喜欢
随机推荐
浅析Servlet三大容器的常用方法及其作用域
docker安装seata(指定配置文件、数据库、容器数据卷等)
08-Lock版的生产者消费者问题
如何将List<Map>进行分组数值计算合并排序
第五讲 测试技术与用例设计
MySql中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
[MySql] implement multi-table query - one-to-one, one-to-many
【微信小程序】利用MPFlutter开发微信小程序
【最新】【获取ip】获取本地ip 获取本机ip地址
DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基MW:1000
如何灵活运用量化交易接口的优势取长补短?
C语言程序设计笔记(浙大翁恺版) 第三周:判断
Mongodb增加权限管理
shell------常用小工具,sort,uniq,tr,cut
navicat for Oraclel链接oracle 报错oracle library is not loaded的解决办法
注解与反射
正则表达式实战:最新豆瓣top250爬虫超详细教程
DBCO-PEG-DSPE,磷脂-聚乙二醇-二苯并环辛炔,在无铜离子的催化下反应
百度地图——鹰眼轨迹服务
UDP多线程实现聊天









