Servlet Lifecycle Detailed

Source: Internet
Author: User

I. Basic CONCEPTS
The servlet lifecycle is divided into three phases
1, initialization phase invoke init () method
2. Call service () method in response to customer request phase
3, Terminate phase call Destroy () method


Second, detailed
1. Initialization phase
The servlet container mounts the servlet at the following time:
The ①servlet container automatically mounts certain servlet when it is started, and it only needs to be on the web. Add code between the XML file:<load-on-startup>1</load-on-startup>
② The first time a customer sends a request to the servlet after the servlet container starts
After the ③servlet class file is updated, remount the servlet
The servlet creates a servlet instance after the container is loaded and invokes the Init () method of the servlet for initialization. During the entire lifecycle of the servlet, theinit () method is invoked only once .

    
2. Responding to customer request stage
① Working principle
First the customer sends a request, the servlet invokes the service () method to respond to the request, through the source code visible, the service () method in the way of the request to match, select the call Doget,dopost and other methods, then enter the corresponding method to call the logical layer of the method to achieve customer response. There are no doget,dopost and so on in the Servlet interface and Genericservlet, and these methods are defined in HttpServlet, but they all return error information, so each time a servlet is defined, Must implement these methods such as Doget or Dopost.

Each custom servlet must implement the Servlet interface, and five methods are defined in the Servlet interface, where the more important three methods relate to the servlet lifecycle, which is the aforementioned init (), service (), Destroy () Method. Genericservlet is a general-purpose servlet that is not specific to any protocol, and it implements the Servlet interface. And HttpServlet inherits from Genericservlet, so HttpServlet also implements the Servlet interface. So we just need to inherit httpservlet when we define the servlet.

servlet interfaces and Genericservlet are not specific to any protocol, and HttpServlet is a class that is specific to the HTTP protocol, so the service () method is implemented in HttpServlet and the request

Servletrequest,servletresponse strong into HttpRequest and HttpResponse.

public void Service (ServletRequest req,servletresponse res) throws Servletexception,ioexception
{
       HttpRequest request;
      HttpResponse response;
 
     try
     {
          req = (httprequest) request;
         res = (httpresponse) response;
     }
      catch (classcastexception e)
      {
          throw new Servletexception ("Non-http request Response");
     }
      Service (request,response); The last call to the
}
code httpservlet its own service (Request,response) method and then invokes the corresponding Doxxx method according to the request, because the Doxxx method in HttpServlet is to return an error message.

protected void doget (HttpServletRequest res,httpservletresponse resp) throws Servletexception,ioexception
{
   String protocol = Req.getprotocol ();
   String msg = istrings.getstring ("http.method_get_not_supported");
   if (protocol.equals ("1.1"))
   {
      resp.senderror ( HTTPSERVLETRESPONSE.SC.METHOD.NOT.ALLOWED,MSG);
  }
   esle
   {
      resp.senderror (httpservletresponse.sc_ BAD_REQUEST,MSG);
  }
}
So you want to override these methods in a custom servlet. In front of the source, no secrets.

② Response Request Phase
For a user's request to the servlet, the servlet container creates the ServletRequest object and Servletresponse object that is specific to the request, and then invokes the Servlet's service method. The service method obtains the customer request information from the ServletRequest object, processes the request, and returns the response information to the customer through the Servletresponse object. For Tomcat, it puts the passed arguments in a Hashtable, which is defined as the Hashtable private Hashtable Paramhashstringarray = new Hashtable (). This is a string-->string[] key-value mapping. HashMap thread is unsafe, hashtable thread safe.

3. servlet termination phase
When a Web application is terminated, or the servlet container terminates, or the servlet container remount A new instance of the servlet, the servlet container invokes the servlet's Destroy () method, in Destroy () method to release the resources that the servlet occupies.

Original post address: http://www.cnblogs.com/cuiliang/archive/2011/10/21/2220671.html

Related Article

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.