How the servlet performs the process and lifecycle phases

Source: Internet
Author: User
Tags garbage collection html form

First, the implementation process

1. Client request servlet and submit to server

2. The server will look for the corresponding URL address (servlet address) in the Web.xml, that is, the contents of the <url-pattern></url-pattern> tag, and the corresponding servlet name, that is < The contents of the Servlet-name></servlet-name> label

3. According to the servlet name found in step two, look for the contents of the processing class that corresponds to the servlet, that is, the <servlet-class></servlet-class> tag, which is the specific class of the servlet to which you want to access

4. Executing the doget () and Dopost () methods in the Servlet class


Second, life cycle

1. Initialization phase, calling the constructor method to generate the servlet instance

2. Invoke initialization method: the init () method in the servlet

3. In response to the client request phase, invoke the service () method. The service () method chooses to execute the doget () or Dopost () method according to the submission mode

4. Terminate phase, call Destroy () method


init () method

The Init method is designed to be called only once. It is invoked the first time the Servlet is created, and is no longer invoked on each subsequent user request. Therefore, it is used for one-time initialization, just like an Applet's Init method.

The servlet is created when the user first invokes the URL that corresponds to the servlet, but you can also specify that the servlet be loaded the first time the server starts.

When a user invokes a servlet, a servlet instance is created, and each user request generates a new thread, which is transferred to the Doget or DoPost method when appropriate. The init () method simply creates or loads some data that will be used for the entire lifecycle of the Servlet.

The Init method is defined as follows:

public void Init () throws Servletexception {
  //initialization code ...
}
Service () method

The service () method is the primary way to perform actual tasks. The Servlet container (that is, the WEB server) invokes the service () method to process requests from the client (browser) and writes the formatted response back to the client.

Each time the server receives a Servlet request, the server generates a new thread and invokes the service. The service () method checks the HTTP request type (GET, POST, put, DELETE, and so on) and invokes Doget, DoPost, Doput,dodelete, and so on when appropriate.

The following are the characteristics of the method:

public void Service (ServletRequest request, 
                    servletresponse response) 
      throws Servletexception, ioexception{
}

The service () method is invoked by the container, and the service method invokes Doget, DoPost, DoPut, Dodelete, and so on when appropriate. So, you don't have to do anything with the service () method, you simply overload doget () or DoPost () based on the type of request from the client.

The Doget () and DoPost () methods are the most commonly used methods in each service request. The following are the characteristics of the two methods. doget () method

Get requests come from a normal request for a URL, or from an HTML form that does not specify method, which is handled by the Doget () methods.

public void doget (HttpServletRequest request,
                  httpservletresponse response)
    throws Servletexception, IOException {
    //Servlet code
}
doPost () method

The POST request comes from an HTML form that specifically specifies the method as POST, which is handled by the DoPost () methods.

public void DoPost (HttpServletRequest request,
                   httpservletresponse response)
    throws Servletexception, IOException {
    //Servlet code
}
Destroy () method

The Destroy () method is invoked only once and is invoked at the end of the Servlet lifecycle. The Destroy () method lets your Servlet close the database connection, stop the background thread, write the Cookie list or hit counter to disk, and perform other similar cleanup activities.

After the Destroy () method is invoked, the Servlet object is marked for garbage collection. The Destroy method definition looks like this:

  public void Destroy () {
    //terminating code ...
  }

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.