Servlet life cycle, working principle

Servlet life cycle:

Servlet Load -> Instantiate -> Service -> Destroy.

init():
In the life cycle of a servlet, the init() method is executed only once. It is executed when the server loads the servlet and is responsible for initializing the servlet object. The server can be configured to load the servlet when the server is started or when the client first accesses the servlet. No matter how many clients access the servlet, init() is never executed repeatedly.
service():

It is the core of the servlet and is responsible for responding to the client's request. Whenever a client requests an HttpServlet object, the object's Service() method is called, and the method is passed a "request" (ServletRequest) object and a "response" (ServletResponse) object as parameters. 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.

destroy():
This method is executed only once, when the server is stopped and the servlet is unloaded. When the servlet object exits the life cycle, it is responsible for releasing the occupied resources. A servlet may spawn other threads while running the service() method, so make sure that these threads have terminated or completed when the destroy() method is called.

How Tomcat and Servlet work:

write picture description here

step:
  1. The Web Client makes an Http request to the Servlet container (Tomcat)
  2. The servlet container receives the request of the Web Client
  3. The servlet container creates an HttpRequest object and encapsulates the information requested by the Web Client into this object.
  4. 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 the Http request information.
  7. HttpServlet calls the relevant methods of the HttpResponse object to generate response data.
  8. The servlet container transmits the response result of the HttpServlet to the Web Client.

Servlet works:

  • First, briefly explain the process of servlet receiving and responding to client requests. First, the client sends a request, and the servlet calls the service() method to respond to the request. As can be seen from the source code, the service() method matches the request method. Choose to call doGet, doPost and other methods, and then enter the corresponding method to call the method of the logic layer to realize the response to the customer. There are no doGet(), doPost() and other methods in the Servlet interface and GenericServlet. These methods are defined in HttpServlet, but they all return error information. Therefore, every time we define a servlet, we must implement doGet or these methods like doPost.

  • Each custom servlet must implement the servlet interface. There are five methods defined in the servlet interface. The three most important methods involve the life cycle of the servlet, namely init() and service() mentioned above. , destroy () method. GenericServlet is a generic, not specific to any protocol Servlet, it implements the Servlet interface. HttpServlet inherits from GenericServlet, so HttpServlet also implements the Servlet interface. So we only need to inherit HttpServlet when we define Servlet.

  • The Servlet interface and GenericServlet are not specific to any protocol, while HttpServlet is a class specific to the HTTP protocol, so the service() method is implemented in HttpServlet, and the request ServletRequest and ServletResponse 
    are forced into HttpRequest and HttpResponse.

When to create a servlet object:

  1. When the servlet container starts: read the information in the web.xml configuration file, construct the specified servlet object, create the servletconfig object, and call the init method of the servlet object with the servletconfig object as a parameter.
  2. After the servlet container is started: the customer sends a request to the servlet for the first time, the servlet container will judge whether the specified servlet object exists in the memory, if not, it will be created, and then the HttpRequest and HttpResponse objects will be created according to the customer's request, so as to call the service method of the servlet object. .
  3. Servlet 
    The servlet container automatically creates a servlet at startup, which is determined by the attributes set for the servlet in the web.xml file. From this we can also see that Servlet objects of the same type exist as singletons in the Servlet container.
<servlet>
        <servlet-name>Init</servlet-name>
        <servlet-class>org.xl.servlet.InitServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
</servlet>

Guess you like

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