Servlet life cycle and operation principle summary

Servlet life cycle:

Servlet class loading -> create a servlet class instance -> Servlet initialization (really become a servlet) -> a request arrives, call the service method (main work) -> finally until the servlet is destroyed.

1. Servlet class loading:
1. After starting the servlet container (web container), the servlet container (web container) will first look for the deployment description file (web.xml) of the web application, and read from the deployment description file (web.xml) Get the context initialization parameters, create a ServletContext object at this time, and all parts of the web application share this context;
2. The servlet container (web container) creates a String name-value pair for the context-param (the parameter name and parameter value are both String types);
3. The servlet container (web container) passes the name-value pair to the ServletContext object;
4. If there is a Listener tag in the deployment description file (web.xml), a Listener instance is created;
5. The servlet container (web container) calls the Listener The contextInitialized method, pass in the ServletContextEvent object, this object contains a ServletContext reference, the event processing code can get the context initialization parameters.

2. Create a Servlet class instance:
1. The servlet container (web container) reads the Servlet tag in the deployment description file (web.xml), including initialization parameters (init-param);
2. The servlet container (web container) creates an instance of ServletConfig ;
3. The servlet container (web container) creates name-value pairs for servlet initialization parameters;
4. The servlet container (web container) fills the ServletConfig instance with name-value pairs;
5. The servlet container (web container) creates a new instance of the servlet class (generally Created when the first request comes, or created when the web container starts by setting the load-on-start parameter).

3. Servlet initialization init method:
1. Because the Servlet init method will only be executed once in a servlet life cycle, the init method is executed when the web container loads the servlet, which can be configured when starting the web server. Or the servlet is loaded when the client first accesses the servlet. No matter how many clients access the servlet, the init method will not be executed repeatedly. Therefore, before calling the service method, the Servlet initialization must be completed;
2. Because there are two init methods in GenericServlet, the init method with parameters calls the init method without parameters, so if you need to rewrite the init method, you only need to rewrite A parameterless init method.

Comprehensive 1, 2 and 3: When starting the servlet container (web container), the container first looks for a deployment description file (web.xml), which records the servlets that can provide services, each servlet is assigned a servlet name, That is, the complete class file name of the Java that this servlet actually corresponds to. The servlet container creates an instance for each servlet with the autoload option. Therefore, every servlet class must have a public parameterless constructor. When the servlet is instantiated, the servlet container (web container) will call the init method of each servlet to instantiate each instance. After the init method is executed, the servlet is in the "initialized" state. So, once the servlet is instantiated, the init method must be called. The servlet is not initialized immediately after the servlet container (web container) starts, but after the request is received. Pre-initialize the servlet with <load-on-statup> ...... </load-on-statup> in the web.xml file. After the initialization fails, the init() method throws a ServletException exception, and the servlet object will be recycled by the garbage collector. When the client accesses the server for the first time, the servlet implementation class is loaded, the object is created, and the initialization method is executed.

Fourth, the service method of Servlet:

The service method is the core of Servlet. Whenever a client requests an HttpServlet object, the Service() method of the object is called, and a "request" (ServletRequest) object and a "response" (ServletResponse) object are passed to this method as parameter. The Service() method already exists in HttpServlet. The default service function is to call the do function corresponding to the method of the HTTP request. Every time a request comes, the service method is called. In HttpServlet, the service method is the method used to judge the request (without overriding), and to override the doGet method or the doPost method.

5. The destroy method
of servlet: in the life cycle of servlet, the destroy() method is executed only once, and the method is executed when the web server stops and unloads the servlet. Because a servlet may spawn other threads while running the service() method, make sure that these threads have terminated or completed when the destroy() method is called. Called when the servlet instance is destroyed, and the end of the call means that the life cycle of the servlet has ended.

To sum up: From a container perspective, the life cycle of a servlet is controlled by the servlet container (the web container), which begins when it is loaded into the memory of the web server and ends when the servlet is terminated or reloaded. This operation is generally performed dynamically. However, web servers usually provide an administrative option to force the loading and initialization of a specific servlet when the web server starts; from a code perspective, the servlet life cycle is defined by the interface javax.servlet.Servlet. All Java servlets must directly or indirectly implement the javax.servlet.Servlet interface in order to run on the Servlet Engine. The javax.servlet.Servlet interface defines some methods that are called in a certain order at a specific time during the servlet's life cycle.

Servlet operating principle:


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326207690&siteId=291194637