1. Loading and instance
A servlet cannot run independently, it must be deployed into a servlet container, instantiated by a container, and invoking the servlet's method. When the servlet container is started or when a client sends a request, the servlet container looks in memory for the servlet instance and, if it does not exist, creates a servlet instance. If an instance of the servlet exists, the instance request is fetched directly from memory.
Note: The servlet container loads the Servlet class based on the location of the Servlet class, and after the load succeeds, the servlet instance is created by the container
2. Initialization
After the instantiation is complete, the servlet container will invoke the servlet's init () method for initialization, which is intended to allow the Servlet object to do some work before processing the client request. For example: Set the connection parameters and establish a JDBC connection.
Note: For each servlet instance, the Init () method can only be called once.
3. Service
Once the Servlet is initialized, it is in a ready state to respond to the request. When the servlet container receives a client request, the service () method of the servlet is called to process the request client request. The servlet instance obtains the client's request through the ServletRequest object and sets the response information by invoking the method of the Servletresponse object.
4. Destruction
When a servlet container determines whether a servlet should be released (shutting down a container or needing to reclaim resources), the container invokes the servlet's Destroy () method, which indicates that those resources can be reclaimed by the system rather than directly.
Note: The Destroy () method is executed when the server is stopped, or when the system recycles resources.
The life cycle of the "Javaweb notes" servlet