SERVLET lifecycle

xiaoxiao2021-03-06  117

The following is taken from << Mass Servlet >>

Servlet foundation

The servlet container (previously known as the ServelT engine) is actually the software that executes servlet. All servers that support servlets include a ServelT container (integrated or through plug-in.) Terminology supports Java server often refers to an enhanced servlethttp server (ie, it includes a servlet container for running servlet)

Servlet lifecycle: The following is a process of calling servlets.

1. When the server is started, the servlet is loaded by the server when the servlet is requested or configured by the client.

The loading of the servlet can be implemented from a local or remote address by using a custom Java class load tool (allowed to be automatically servlet overloaded). This step is equivalent to

Class C = Class.Forname ("com.sourcestream.myservlet);

The term loading refers to the simultaneous load and initialization of the servlet simultaneously.

2. The server creates a servlet class instance to serve all requests. With multi-thread, you can serve in parallel requests by a single servlet instance. Only five exceptions are because of the servlet for the SingLethreadModel interface, this, the server creates an instance pool and selects one to serve each new request. This step is equivalent to the following Java code:

Servlet S = (servlet) c.newinstance ();

3. The server calls the subscript of the servlet. It is used to ensure completing the previous execution process for the first time requesting servlet. If the servlet creates multiple servlet instances, the init () method is called for each instance.

4. The server constructs a servletRequest or httpservletRequest object from the data included in the customer request, and also constructs a servletResponse or httpservletResponse object to return response to provide methods. Its parameter type depends on whether the servlet is expanded from GenericServlet or httpservlet. (Note: This step will not happen if the server does not receive the request for this servlet.)

5. The server calls the Service () method (for httpservlets, service () will call more specific methods: DOGET () or dopost ()), in which the structured object is transmitted as a parameter. When the parallel request arrives, multiple service () methods can run in separate threads simultaneously. (Unless servlet achieves a SingLethReadModel interface)

6 Analyze the Servletreuest or HttpServletRequest object. The service () method handles the customer's request. And call the ServletResponse or HttpServletResponse object to respond.

7. If the server receives another request to the servlet, this processing is repeated from 5 steps.

8. Once the servlet container detects a servlet to be uninstalled, this may be because it is necessary to recycle resources or because it is being closed, the server will call servlet's desk after all servlet's service () threads (or after the server specifies time) DESTROY ( )method. The servlet can then collect cleanup in a useless storage unit. The servlet container does not need to specify a time for retain a servlet. The above 8 steps are the life cycle of the servlet.

转载请注明原文地址:https://www.9cbs.com/read-99325.html

New Post(0)