当前位置:网站首页>Servlet-02 lifecycle
Servlet-02 lifecycle
2022-06-23 09:28:00 【Java backend development】
List of articles
Servlet The life cycle of Servlet From creation to destruction .Servlet The life cycle of Servlet Container management , Mainly divided into the following 5 Stages :
- Loading phase
- Instantiation phase
- Initialization phase ( call init() Method )
- Service stage ( call service() Method )
- The destruction ( call destroy( Method ))
When the server starts ,Servlet The container first reads web.xml Configuration information in , And load 、 initialization load-on-startup The value is less than 0 Or no specified Servlet class ( Other Servlet Load and initialize when first requested ). Initialize it Servlet Object will handle all client requests , Until the last time the server shuts down , Will destroy Servlet object , perform destroy() Method .
1、 load 、 Instantiation and initialization Servlet
When the server starts , Load and read web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" metadata-complete="true">
<servlet>
<servlet-name>myServlet-01</servlet-name>
<servlet-class>com.chen.servlet.myServlet01</servlet-class>
<!--Web Server Startup time ,Servlet It also loads 、 Instantiation -->
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myServlet-01</servlet-name>
<url-pattern>/s1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>myServlet-02</servlet-name>
<servlet-class>com.chen.servlet.myServlet02</servlet-class>
<!-- Only when the first request occurs ,Servlet Before loading 、 Instantiation -->
<load-on-startup>-1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myServlet-02</servlet-name>
<url-pattern>/s2</url-pattern>
</servlet-mapping>
</web-app >
- because load-on-startup The value of the element ≥ 0, therefore myServlet01 It is loaded when the server starts 、 Instantiation .
- because load-on-startup The value of the element < 0, therefore myServlet02 It will not be loaded until it is first requested 、 Instantiation .
Servlet Once the class is loaded 、 Instantiation , The container will call init(ServletConfig conf) Method is used to initialize the Servlet object .
@Override
public void init() throws ServletException {
System.out.println("myServlet01 initialization !");
}
2、 Service stage
Servlet When the container receives a request from a client , A separate... Will be created for the request ServletRequest object req and ServletResponse object resp, Pass them in as parameters service() In the way , And call this method to process the request .
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(" Called myServlet02 Of service() Method ");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
- When Servlet When the container returns the response information to the client ,ServletRequest Objects and ServletResponse The object will be destroyed ;
- about Servlet Every request of ,Servlet The container will call once service() Method , namely service() Method in Servlet Will be called many times in the life cycle of
3、 Destruction phase
When the server is down ,Servlet The container will call destory() Method , The destruction Serlvet example , Release the resources used by the station .
@Override
public void destroy() {
System.out.println("myServlet01 Be destroyed ");
}
4、 Test case
web.xml The configuration file
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" metadata-complete="true"> <servlet> <servlet-name>myServlet-01</servlet-name> <servlet-class>com.chen.servlet.myServlet01</servlet-class> <!--Web Server Startup time ,Servlet It also loads 、 Instantiation --> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myServlet-01</servlet-name> <url-pattern>/s1</url-pattern> </servlet-mapping> <servlet> <servlet-name>myServlet-02</servlet-name> <servlet-class>com.chen.servlet.myServlet02</servlet-class> <!-- Only when the first request occurs ,Servlet Before loading 、 Instantiation --> <load-on-startup>-1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myServlet-02</servlet-name> <url-pattern>/s2</url-pattern> </servlet-mapping> </web-app >myServlet01 class
public class myServlet01 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(" Called myServlet02 Of service() Method "); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override public void init() throws ServletException { System.out.println("myServlet01 initialization !"); } @Override public void destroy() { System.out.println("myServlet01 Be destroyed "); } }myServlet02 class
public class myServlet02 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println(" Called myServlet02 Of service() Method "); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } @Override public void init() throws ServletException { System.out.println("myServlet02 initialization !"); } @Override public void destroy() { System.out.println("myServlet02 Be destroyed !"); } }test
function Tomcat The server , You can find myServlet01 During the server startup phase , The initialization is complete .

visit myServlet01 Registered URL:http://localhost:8080/servlet08/s1, Get the picture below :
visit myServlet02 Registered URL:http://localhost:8080/servlet08/s2,Servlet The container is initialized on the first access myServlet02, And call service() Method .
close Tomcat The server ,myServlet01 and myServlet02 Were destroyed .
5、 summary
Servlet The life cycle of is divided into : Loading phase 、 Instantiation phase 、 Initialization phase and destruction phase . The first three phases are bound together and occur continuously .
- if Servlet Of load-on-startup Element value ≥ 0, The first three phases occur when the server starts ;
- if Servlet Of load-on-startup Element value < 0, The first three phases occur on the first request .
边栏推荐
- Kotlin Series 1: getting started with basics
- [网鼎杯 2020 青龙组]AreUSerialz
- Redis学习笔记—数据类型:字符串(string)
- [nanopi2 trial experience] the first step of bare metal
- 什么是闭包函数
- [GXYCTF2019]BabySQli
- RGB与CMYK颜色模式
- 【CTF】bjdctf_2020_babyrop
- Redis学习笔记—遍历键
- A method of realizing video call and interactive live broadcast in small programs
猜你喜欢
随机推荐
Click Add drop-down box
Redis学习笔记—遍历键
Redis学习笔记—redis-cli详解
微信小程序:点击按钮频繁切换,重叠自定义markers,但是值不改变
36 krypton launched | cloud native database company "tuoshupai" completed a new round of strategic financing, and the valuation has reached the level of quasi Unicorn
Pizza ordering design - simple factory model
Three methods to find the limit of univariate function -- lobida's rule and Taylor's formula
C#之Lambda不得不说的用法
Redis学习笔记—单个键管理
UEFI源码学习3.7 - NorFlashDxe
Redis learning notes - publish and subscribe
使用base64,展示图片
什么是闭包函数
ARM中常见的英文解释
Cesium loading orthophoto scheme
cooding代码库的使用笔记
UEFI source code learning 4.1 - pcihostbridgedxe
Redis learning notes - traverse key
线性表(SequenceList)的顺序表示与实现----线性结构
12个球,有一个与其他不一样,提供一个天平,三次找出来






