Talk about the life cycle of Servlet objects

What is Servlet object life cycle?

  1. When is the Servlet object created?
  2. When is the Servlet object destroyed?
  3. How many Servlet objects are created?
  4. The life cycle representation of a Servlet object, that is, what is the whole process of a Servlet object from creation to final destruction?

        

Through the above questions, let's talk about the life cycle of Servlet objects

        The entire life cycle of the Servlet is managed by the Servlet container, which uses the jakarta.servlet.Servlet interface to understand and manage the Servlet object. The life cycle of the Servlet can be divided into four stages: loading stage, initialization stage, Request processing and destruction phase

                

Who maintains the Servlet object?

        The creation of the Servlet object, the invocation of the method on the object, and the final destruction of the object, JavaWeb programmers have no right to intervene, and the life cycle of the Servlet object is solely responsible for by the Web Server. The Tomcat server is usually called the Web container (Web Container), and manages the creation and destruction of Servlet objects through the Web container.

        

Are the Servlet objects we create ourselves managed by the Web container?

        The Servlet objects created by ourselves are not managed by the Web container. The Servlet objects created by the Web container will be placed in a collection (HashMap). Only the Servlets placed in this collection can be managed by the Web container. Servlet objects are not in this collection, so they will not be managed by the web container.
        
    The underlying implementation of the Web container is a collection of HashMaps, in which the relationship between the Servlet object and the request path is stored.

        
When the server starts, is the Servlet object created (by default)?

        Provide a parameterless construction method in the Servlet, output a piece of prompt information in the construction method, and observe whether the construction method is executed when starting the server.

        AServlet class

public class AServlet implements Servlet {
    public AServlet() {
        System.out.println("AServlet无参构造方法被执行了");
    }
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("AServlet's init method execute!");
    }
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("AServlet's service method execute!");
    }
    @Override
    public void destroy() {
        System.out.println("AServlet's destroy method execute!");
    }
    
    @Override
    public String getServletInfo() {
        return null;
    }
    @Override
    public ServletConfig getServletConfig() {
        return null;
    }
}

        web.xml configuration

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
         version="5.0">
    
    <servlet>
        <servlet-name>AServlet</servlet-name>
        <servlet-class>servlet.lxy.AServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>AServlet</servlet-name>
        <url-pattern>/servlet/test</url-pattern>
    </servlet-mapping>
</web-app>

        By default, after the Tomcat server is started, the console does not output the relevant construction information of AServlet, indicating that the AServlet object has not been instantiated at this time.

        

When the user sends the first request, the console outputs the following:

        The AServlet parameterless construction method is executed
        AServlet's init method execute!
        AServlet's service method execute!

According to the above output content, it is concluded that
        the Servlet object is instantiated when the user sends the first request, that is, the construction method of AServlet is executed, and the construction method without parameters is executed.
        After the AServlet object is created, the Tomcat server immediately calls the init method of the AServlet object (the Aservlet object already exists during the execution of the init method). After the init method is executed, the Tomcat server immediately calls the service method of the AServlet object.

        

The user continues to send a second, multiple requests, and the console outputs the following:

        AServlet's service method execute!

        AServlet's service method execute!

        AServlet's service method execute!

According to the above output results, it can be concluded that:

        When the user sends the second or multiple requests, the Servlet object is not newly created, and the previously created Servlet object is still used, and the service method of the object is directly called without going through the init method of the Servlet object. These phenomena can explain:

        First, the Servlet object is a singleton (single instance, although the Servlet object is a single instance, but the Servlet does not conform to the singleton pattern, we call it a false singleton. The reason why the Servlet object is a singleton is because the Servlet object The creation of Java web programmers cannot perceive the creation of this object, which can only be managed by Tomcat (or other web application servers). Tomcat only creates a Servlet object, which leads to a singleton, but it is a false singleton, and a true singleton Example mode, its construction method is privatized.
        Second, the no-argument construction method, the init method is only executed when the user sends a request for the first time, that is to say, the no-argument construction method is only executed once, and the init method is only executed by Tomcat The server calls once
        Third, as long as the user sends a request, the service method will definitely be called once by the Tomcat server, and if it is sent multiple times, the service method will also be called multiple times.

        

When shutting down the server, the console output the following:

        AService's destory method execute!

 According to the above output results, it can be concluded that:

        The destroy method of the Servlet is called only once by the Tomcat server. When the Web server is closed, Tomcat needs to destroy the memory of the Servlet object. Before destroying the Servlet object, the Tomcat server will call the destroy method once. When the destroy method is called, the Servlet object still survives. After the destroy method is executed, the Servlet object is destroyed. .

        

       The above is an introduction to the life cycle process of Servlet objects. Only by being familiar with the life cycle of Servlets can we properly handle Servlet objects at the corresponding time. If you have any questions, welcome to exchange and discuss in the comment area!

Guess you like

Origin blog.csdn.net/qq_43500084/article/details/127794289