Servlet (ii) Lifecycle

Source: Internet
Author: User

The life cycle of a servlet can be defined as the whole process from its creation to its destruction. The following is the general line of servlet life:

  • The container calls the init () method to initialize the servlet.
  • Servlet calls the Service () method to process client requests
  • The container calls the destroy () method to destroy the servlet.
  • Finally, the servlet is treated as garbage by the jvm gc thread.

Init () method:

The init () method is called only once when the servlet is created, regardless of the number of user requests. Generally, the servlet is created when the container receives the user request for the first time, but you can also specify the servlet to load it when the container starts using load-on-startup.

When a user calls a servlet, each servlet in the container provides external services in singleton mode, but allocates a thread to each user. The init () method usually loads some data that will be used in the servlet lifecycle.

The init () method is defined as follows:

public void init() throws ServletException {  // Initialization code...}

 

Service () method:

The Service () method is the main method that servlet provides external services. The servlet container will call this method to process requests from the client and return formatted results to the client.

Whenever the servlet container receives a request from the client, it allocates a new thread and calls the Service () method. Then, the Service () method checks the client's request type, in the service () method, the doget, dopost, dodelete, and doput methods are called based on the request type.

The Service () method is declared as follows:

public void service(ServletRequest request,                     ServletResponse response)       throws ServletException, IOException{}

From the above we can see that for the Service () method, developers do not need to make any adjustments, but they need to rewrite the called doget or dopost method based on the request type. Doget and dopost are the two most frequently used methods. Their definitions are as follows:

public void doGet(HttpServletRequest request,                  HttpServletResponse response)    throws ServletException, IOException {    // Servlet code}
public void doPost(HttpServletRequest request,                   HttpServletResponse response)    throws ServletException, IOException {    // Servlet code}

Requests in the get mode are generated through common URL requests and HTML forms without the specified method. Therefore, such requests are processed by the doget method; the HTML form with the method specified will generate the post mode request, which will be processed by the dopost method.

 

Destroy () method:

The destroy () method is called only once when the Servlet's life ends. This method will give the servlet some final opportunity to scan the end, such as closing the database connection and stopping the background thread, write cookie list. After the destroy method is called, The servlet object will be marked as "spam" and be disposed of by GC. The destroy () method is defined as follows:

  public void destroy() {    // Finalization code...  }

 

Architecture diagram:

Describes the servlet lifecycle diagram:

  • First, HTTP requests for Web servers are delegated to servlet containers.
  • The servlet container loads the servlet before calling the Service () method.
  • Finally, the servlet container will process multi-user requests in multiple threads, and each thread will execute the single-sample servlet Service () method.

 

/******************************* Alien Jordan shot time ***** *************************/

The servlet life begins with the init () method at the time of creation, and ends with the destroy () method after the completion of the mission,

Life starts once, that is, INIT () only once, and life ends once, that is, destroy () only once,

In the wave of life, this great man will provide multiple services, that is, Service () A lot of times

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.