当前位置:网站首页>Servlet-02 lifecycle

Servlet-02 lifecycle

2022-06-23 09:28:00 Java backend development

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 :

  1. Loading phase
  2. Instantiation phase
  3. Initialization phase ( call init() Method )
  4. Service stage ( call service() Method )
  5. 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

  1. 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 >
    
  2. 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 ");
        }
    }
    
  3. 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 !");
        }
    }
    
  4. test

    function Tomcat The server , You can find myServlet01 During the server startup phase , The initialization is complete .
     Insert picture description here
    visit myServlet01 Registered URL:http://localhost:8080/servlet08/s1, Get the picture below :
     Insert picture description here
    visit myServlet02 Registered URL:http://localhost:8080/servlet08/s2,Servlet The container is initialized on the first access myServlet02, And call service() Method .
     Insert picture description here
    close Tomcat The server ,myServlet01 and myServlet02 Were destroyed .
     Insert picture description here

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 .
原网站

版权声明
本文为[Java backend development]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230850053424.html