Various stages of the servlet life cycle
1, creating objects
Creating an object is created only when the user accesses the Web server for the first time, and the Servlet object already exists for the second or more times, so it is no longer created.
Object creation Principle:
When a user accesses the Web server for the first time, the server passes the URL address entered by the user. Parsing the Web. xml file, obtaining the full class name of the Servlet object, and creating the Servlet object through the reflection mechanism, when the server is accessed again, because the Servlet object already exists, it is no longer created, and the Servlet object already exists in the container directly.
2, Initialize
At this point, the Servlet object has been created. Invokes the Init () method of the Servlet object for initialization. Initialization is done after the object is created and executed only once.
3, processing requests
Is the core, calling the servlet's service () method to process the request. Executable multiple times.
4, Object destruction
When the Servlet object is not accessed for a long time, or the container is closed, the Destroy () method is called before destroying to prepare for destruction.
Summary: Servlets are single-instance, initialized once, and have thread-safety issues.
The life cycle of the servlet is managed by the Web container, the programmer cannot change its control, and it is emphasized that our job is to complete the implementation of the methods in the servlet.