当前位置:网站首页>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");
} }
}
边栏推荐
猜你喜欢
DBCO-PEG-DSPE,磷脂-聚乙二醇-二苯并环辛炔,在无铜离子的催化下反应
代码随想录笔记_动态规划_377组合总和IV
【Qt5 + OpenGL】glPointSize(10); error: undefined reference to `__imp_glPointSize‘
网站授权QQ登录
Startup error: Caused by: org.apache.ibatis.binding.BindingException summary solution
【Qt】 No such slot...,No such signal
如何设计一个高并发系统?
启动报错:Caused by: org.apache.ibatis.binding.BindingException汇总解决
DSPE-PEG-Hydrazide, DSPE-PEG-HZ, Phospholipid-Polyethylene Glycol-Hydrazide MW: 1000
[MySql]实现多表查询-一对一,一对多
随机推荐
百度地图——地图找房功能
Bean的生命周期
[MySql] implement multi-table query - one-to-one, one-to-many
Anaconda3安装后无法启动,启动闪退 2020-9
Two-dimensional array to realize the eight queens problem
redis6在centos7的安装
优化软件测试成本的 7 个步骤
注解与反射
量化程序化交易如何去使用以及执行?
docker安装单机版redis、集群版redis
Swift中的Error处理
约束性统计星号‘*’
6大论坛,30+技术干货议题,2022首届阿里巴巴开源开放周来了!
Mongodb增加权限管理
如何灵活运用量化交易接口的优势取长补短?
docker安装seata(指定配置文件、数据库、容器数据卷等)
JS——循环结构经典例题解析与分享
回归测试:意义、挑战、最佳实践和工具
如何让你的量化交易系统具有概率优势,具有正向收益预期呢?
benchmark性能测试