A deep understanding of the servlet life cycle

Source: Internet
Author: User
Tags garbage collection html form http request
What is a servlet

A Java Servlet is a program that runs on a Web server or application server, as a middle tier between a request from a Web browser or other HTTP client and a database or application on an HTTP server. implements the same functionality as CGI, but compared to CGI, the advantage is that performance is significantly better. The Servlet executes within the address space of the WEB server. This makes it unnecessary to create a separate process to process each client request . Servlets are platform-independent because they are written in Java. the Java security Manager on the server performs a series of restrictions to protect resources on the server computer. Therefore, the Servlet is trustworthy. The full functionality of the Java class library is available to the Servlet. It can interact with applets, databases, or other software through the sockets and RMI mechanisms. Cause Analysis: (CGI creation process, servlet creation thread )

(1) CGI Analysis: when the user's browser makes a http/cgi request, or calls a CGI program, the server will be new to enable a process , the more the number of CGI calls, the more processing time consumes the system .
(2) servlet Analysis: The servlet is in a server process, it runs its service method in a multithreaded manner , one instance can serve multiple requests, and its instances are generally not destroyed. Serlvet Architecture

The following illustration shows the location of the servlet in a Web application:
the life cycle of Serlvet

the life cycle of a servlet can be divided into four stages, the Servlet initializes by calling the Init () method. The Servlet invokes the service () method to handle the client's request. the Servlet terminates (ends) by calling the Destroy () method. Finally, the Servlet is garbage collected by the JVM's garbage collector.
the life cycle approach of Serlvet init () method

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

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

Service content: the service () method is the primary way to perform the actual task. 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. Service method and Doget, Dopost method of contact :

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 calls doget, DoPost, Doput,dodelete when appropriate and other methods. doget () method

A GET request comes from a normal request for a URL , or from an HTML form that does not specify a method, which is handled by the Doget () method.

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 () method.

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

The Destroy () method is called only once , and is called at the end of the Servlet life cycle. The **destroy () method allows your Servlet to shut down a database connection, stop a background thread, write a Cookie list or hit counter to disk, and perform other similar cleanup activities.
After the Destroy () method is called, the Servlet object is marked as garbage collection. The Destroy method definition is as follows:

public void Destroy () {
    //Terminator code ...
  }
Frame Composition:

The following illustration shows a typical Servlet life cycle scenario. The first HTTP request to reach the server is assigned to the Servlet container. the servlet container loads the servlet before invoking the service () method. The servlet container then processes multiple requests produced by multiple threads , each executing a single Servlet instance's service () method.

Reference: Rookie Tutorial http://www.runoob.com/servlet/servlet-life-cycle.html

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.