Servlet life cycle, how it works

Source: Internet
Author: User

    1. Servlet life cycle: servlet load---> Instantiation---> Services---> Destroy.
    2. Init (): The init () method is executed only once in the servlet's life cycle. It is executed when the server loads the servlet and is responsible for initializing the Servlet object. You can configure the server to get the servlet into the servlet at the start of the server or client for the first time. Init () is not repeated regardless of how many clients access the servlet.
    3. Service (): It is the core of the servlet and is responsible for responding to customer requests. Whenever a client requests a HttpServlet object, the Service () method of the object is called and passed to the method a "request" (ServletRequest) object and a "response" (Servletresponse) object as a parameter. The service () method already exists in HttpServlet. The default service function is to invoke the DO function corresponding to the method of the HTTP request.
    4. Destroy (): Executes only once, and executes the method when the server-side stops and the servlet is unloaded. When a Servlet object exits its lifecycle, it is responsible for releasing the resource that is occupied. A servlet may produce additional threads when running the service () method, so it is necessary to confirm that the threads have been terminated or completed when the Destroy () method is called.

How Tomcat and Servlet work:

Steps:

    1. The Web Client makes an HTTP request to the servlet container (Tomcat)
    2. The servlet container receives a request from the Web client
    3. The servlet container creates a HttpRequest object that encapsulates the information requested by the Web client into this object.
    4. Servlet container creates a HttpResponse object
    5. The servlet container invokes the service method of the HttpServlet object, passing the HttpRequest object and the HttpResponse object as arguments to the HttpServlet object.
    6. HttpServlet invokes the method of the HttpRequest object to obtain the HTTP request information.
    7. HttpServlet calls the methods of the HttpResponse object to generate the response data.
    8. The servlet container passes the results of the HttpServlet response to the Web Client.

How Servlets work:

1, first of all, a simple explanation of the servlet receiving and responding to the customer request process, first the customer sends a request, the servlet is called the Service () method to respond to the request, through the source code is visible, the service () method to match the way of the request, Choose to call Doget,dopost and other methods, and then enter the corresponding method to invoke the logical layer method, to achieve the response to the customer. There are no doget (), DoPost (), and so on in the Servlet interface and Genericservlet, these methods are defined in HttpServlet, but all return error information, so each time we define a servlet, Must implement these methods such as Doget or Dopost.

2, each custom servlet must implement the Servlet interface, the Servlet interface defines five methods, of which the more important three methods involve the life cycle of the servlet, respectively, the above mentioned Init (), service (), Destroy ( Method Genericservlet is a generic, non-protocol-specific servlet that implements the Servlet interface. And HttpServlet inherits from Genericservlet, so HttpServlet also implements the Servlet interface. So we only need to inherit httpservlet when we define the servlet.

3. The servlet interface and Genericservlet are not specific to any protocol, and HttpServlet is a class specific to the HTTP protocol, so the service () method is implemented in HttpServlet and the request is ServletRequest, Servletresponse Strong to HttpRequest and HttpResponse.

Time to create a Servlet object:

  1. When the servlet container starts: reads the information in the Web. XML configuration file, constructs the specified Servlet object, creates a ServletConfig object, and invokes the ServletConfig object as a parameter to invoke the Init method of the Servlet object.
  2. After the servlet container is started: The client makes a request to the servlet for the first time, the servlet container determines if there is a specified Servlet object in memory, creates it if not, and then creates a HttpRequest, HttpResponse object based on the client's request. This invokes the service method of the Servlet object.
  3. The servlet servlet container automatically creates the servlet at startup, which is determined by the <load-on-startup> property set for the servlet in the Web. xml file. From there, we can see that the same type of servlet object exists as a singleton 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>        

Servlet life cycle, how it works

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.