Servlet lifecycle implementation

  • Instance and initialization timing

  • Ready/call/service phase

  • Destruction of the timing

A case in field

The life cycle of a Servlet

The Servlet does not have a main() method and cannot run independently. Its execution is completely controlled and scheduled by the Servlet engine. The lifecycle refers to the entire process of when a servlet container creates a servlet instance, calls its methods for processing requests, and destroys instances. (The default life cycle is discussed here)

  • Instance and initialization timing

When the request arrives, the container looks for the servlet object to see if it exists, and if it doesn’t, it creates an instance and initializes it.

  • Ready/call/service phase

When a request arrives, the container calls the servlet object’s service() method, which can be called multiple times throughout its life cycle.

The HttpServlet service() method calls the doGet() or doPost() methods depending on the request. However, both do methods, by default, throw exceptions that require subclasses to override.

  • Destruction of the timing

When the container is closed (when the application is stopped), the Servlet instance in the application is destroyed.

The above lifecycle can be observed using the lifecycle method in the Servlet. The lifecycle of a Servlet can be observed by observing three lifecycle methods that are not called manually by the user but automatically called by the container at specific times.

Init method, executed after the Servlet instance has been created (to prove that the Servlet instance has been created)

Public void init(ServletConfig config) throws ServletException {system.out.println (" an instance is created "); }Copy the code

The service method, executed each time a request reaches a Servlet method, to process the request (proving that the Servlet is serviced)

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {system.out.println (" service called "); }Copy the code

Method destroy, executed when the Servlet instance is destroyed (to prove that the instance of the Servlet was destroyed)

Public void destroy() {system.out.println (" instance destroyed "); }Copy the code

The Servlet life cycle can be summarized in four simple steps: Servlet class loading → instantiation → service → destruction. To describe how Tomcat works with servlets, take a look at the sequence diagram below:

1. The Web Client sends an Http request to the Servlet container (Tomcat)

2. The Servlet container receives requests from the Web Client

3. The Servlet container creates an HttpRequest object and encapsulates the Web Client request information into this object

The Servlet container creates an HttpResponse object

5. The Servlet container calls the Service method of the HttpServlet object, passing the HttpRequest object and the HttpResponse object as parameters to the HttpServlet object

6, HttpServlet calls the relevant methods of the HttpRequest object to obtain Http request information

7. The HttpServlet calls the relevant methods of the HttpResponse object to generate the response data

The Servlet container passes the HttpServlet response to the Web Client

extension

The Servlet configuration

In addition to configuring basic access information, a Servlet can also configure initialization parameters, self-start, etc., and a Servlet can configure multiple access paths, and can also use the wildcard “*”.

The basic configuration

<servlet> <servlet-name>helloweb</servlet-name><! <servlet-class>com.xxx.web.HelloWeb</servlet-class><! </servlet> <servlet-mapping> <servlet-name> HelloWeb </servlet-name><! <url pattern>/ helloWeb </url pattern><! <url-pattern>/*</url-pattern> </servlet-mapping>Copy the code

Initialization parameter

<servlet> <servlet-name>helloweb</servlet-name><! <servlet-class>com.xxx.web.HelloWeb</servlet-class><! -- Resource path for servlet --> <! -- Initializing parameters --> <init-param> <param-name>param1</param-name> <param-value>1</param-value> </init-param> <init-param> <param-name>param2</param-name> <param-value>2</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>helloweb</servlet-name><! <url pattern>/ helloWeb </url pattern><! <url-pattern>/*</url-pattern> </servlet-mapping>Copy the code

Since the launch

<servlet> <servlet-name>helloweb</servlet-name><! <servlet-class>com.xxx.web.HelloWeb</servlet-class><! -- Resource path for servlet --> <! -- Initializing parameters --> <init-param> <param-name>param1</param-name> <param-value>1</param-value> </init-param> <init-param> <param-name>param2</param-name> <param-value>2</param-value> </init-param> <! </load-on-startup> 2</load-on-startup> </servlet> <servlet-mapping> <servlet-name> helloWeb </servlet-name><! <url pattern>/ helloWeb </url pattern><! <url-pattern>/*</url-pattern> </servlet-mapping>Copy the code

Servlet-name: specifies the name of the servlet object

Servlet-class: The class to call to create the servlet object

Param-name: indicates the parameter name

Param-value: indicates the parameter value

Load-on-startup: The order in which Servlet objects are loaded when the Servlet container is started

Servlet-mapping /servlet-name: specifies the servlet-name configured in the servlet

Url-pattern: indicates the relative URL path of servlets accessed by customers

Description: Multiple URl-patterns can be configured (a servlet can be accessed by multiple URl-patterns). When multiple servlets are configured with the same URl-pattern, An error in Java. Lang. Reflect. InvocationTargetExceptionion; The wildcard * can only be placed first or last, not in the middle, and cannot stand alone (separated by /, not part of a word). The more precise the better. The general configuration is ok.