Talk about the life cycle of Servlet in detail

1. What is the life cycle

  • The life cycle represents all the processes experienced by a java object from its initial creation to its final destruction

2. Who manages the life cycle of the Servlet object

  • The life cycle of the Servlet object, JavaWeb programmers cannot interfere. Including the invocation of related methods of the Servlet object, Javaweb programmers cannot interfere.
  • The entire process of Servlet object creation, method invocation and object destruction is managed by the web container
  • Web container manages the life cycle of Servlet objects

3. Will the Servlet object be instantiated during the startup phase of the web container?

  • "By default", the Servlet object will not be instantiated during the startup phase of the web container.
  • If you want to instantiate the Servlet object during the startup phase of the web server, you need to make special settings, which are related configurations in the web.xml file, for example:
<servlet>
    <servlet-name>testLifeCycle</servlet-name>
    <servlet-class>com.javaweb.servlet.math.TestLifeCycle</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>testLifeCycle</servlet-name>
    <url-pattern>/testLifeCycle</url-pattern>
</servlet-mapping>
  • url-pattern is the request path entered by the corresponding user in the browser
  • servlet-class is the full class name of the corresponding java applet
  • servlet-name is the specific class name of the corresponding java applet
  • The smaller the natural number in load-on-startup, the higher the priority

4. Servlet life cycle

  • 4.1: The user enters the URL in the browser address bar: http://localhost:8080/hcz3/testLifeCycle
    Note: /hcz3 here is the name of the project I arranged in the Tomcat server
  • 4.2: Web container interception request path: /hcz3/testLifeCycle
  • 4.3: The web container finds the Servlet object corresponding to the request path /hcz3/testLifeCycle in the container context
  • 4.4: If the corresponding Servlet object is not found
    • (1) Obtain the full class name of the Servlet corresponding to the request path/testLifeCycle through the relevant configuration information in the web.xml file
    • (2) Through the reflection mechanism, call the parameterless construction method of the Servlet class to complete the instantiation of the Servlet object
public TestLifeCycle(){
    
    
  System.out.println("无参构造方法");
 }
  • (3) The web container calls the init method of the Servlet object to complete the initialization operation
@Override
 public void init(ServletConfig arg0) throws ServletException {
    
    
  System.out.println("Servlet中的init方法");
 }
  • (4) The web container calls the service method of the Servlet object to provide services
@Override
 public void service(ServletRequest request, ServletResponse response)
   throws ServletException, IOException {
    
    
  System.out.println("Servlet中的service方法");
 }
  • 4.5: If the corresponding Servlet object is found
    • The web container directly calls the service method of the Servlet object to provide services
  • 4.6: When will the web container destroy the Servlet object
    • When the web container is closed
    • When webapp is redeployed
    • When the Servlet object has not been accessed by users for a long time
    • The web container will call the destroy method of the Servlet object to complete the destruction
@Override
 public void destroy() {
    
    
  System.out.println("Servlet中的destroy方法");
 }

5. Is the Servlet object singleton mode

  • The Servlet object is a singleton , but it does not conform to the singleton pattern , and can only be called a pseudo singleton, because the construction method of a true singleton is private.
  • The Tomcat server supports multi-threading, so the Servlet object runs in a single-instance multi-threaded environment.
  • Then if there are instance variables in the Servlet object, and the instance variables involve modification operations, then this Servlet object must have thread safety issues. It is not recommended to use instance variables in the Servlet object, and try to use local variables.

6. After the Servlet object is instantiated, where is the Servlet object stored? (When a Servlet object is found)

  • Most web containers store Servlet objects and corresponding url-patterns in the Map collection
  • There is such a Map collection in the web container, Map<String,Servlet>
key Value
/login LoginServlet object reference
/delete DeleteServlet object reference
/save SaveServlet object reference

7. When the server starts, it will parse the web.xml file in each webapp, and then what does it do? (When the Servlet object is not found)

  • Store the url-pattern and the corresponding Servlet complete class in the web.xml file into the Map collection
  • There is such a Map collection
    Map<String,String> in the web container
key value
/login com.javaweb.servlet.math.LoginServlet
/delete com.javaweb.servlet.math.DeleteServlet
/save com.javaweb.servlet.math.SaveServlet

8. What code can be written in these methods in the Servlet interface? When to use these methods?

  1. No parameter construction method

  2. init method

  • The execution time of the above two methods is almost the same. The object is being created when the constructor is executed, and the object is already created when the init method is executed.
    • If the system requires a special program to be executed at the time of object creation, try to write this program into the init method
    • Why is it not recommended to write the code into the constructor?
      • Because when writing a construction method, it may lead to no parameter construction method does not exist
      • A class does not write any construction method, there is a parameterless construction method by default, but once a parameter construction method is written, the system no longer provides a parameterless construction method
  • The init method in Servlet is a code initialization time provided by SUN for JavaWeb programmers. If you want to execute a special program at the initialization time, this program can be written to the init method and it will be automatically called in the future
  1. service method
  • This method must be rewritten, because this method needs to complete business logic processing , request processing , and complete response processing
  1. destroy method
  • If you want to execute a special code at the time of destruction, you need to write this code into the destroy method and it will be called automatically

9. Summary

  1. The construction method of the Servlet class is executed only once
  2. The init method of the Servlet object is executed only once
  3. The service method of the Servlet object is executed once as long as the user calls it once
  4. The destroy method of the Servlet object is executed only once

10. Attention

  • When the init method is executed, the Servlet object has been created
  • When the destroy method is executed, the Servlet object has not been destroyed and will be destroyed soon

Guess you like

Origin blog.csdn.net/hcz666/article/details/108475517
Recommended