Latest web development tutorials

Servlet life cycle

Servlet life cycle can be defined as the whole process from creation through destruction. The following is the procedure to follow Servlet:

  • Servlet is initialized by calling theinit () method.
  • Servlet calls theservice () method to handle the client's request.
  • Servlet by callingdestroy () method terminates (END).
  • Finally, Servlet by JVM's garbage collector garbage collection.

Let us now discuss in detail the life cycle approach.

init () method

init method is designed to be called only once. It is invoked when you first create a Servlet, at each subsequent user requests no longer call. Therefore, it is used for one-time initialization, like Applet init method.

When users create a Servlet corresponding to the first call to the Servlet URL, but you can also specify the Servlet is loaded when the server is first started.

When the user invokes a Servlet, it will create a Servlet instance, each user request will generate a new thread, the appropriate time transfer to the doGet or doPost method. init () method simply create or load some data that will be used throughout the life cycle Servlet.

Init method is defined as follows:

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

service () method

service () method is the main method performs the actual task. Servlet container (ie, Web server) calls the service () method to handle requests from the client (browser) and the response format to write back to the client.

Each time the server receives a Servlet request, the server will generate a new thread and calls the service. service () method checks the HTTP request type (GET, POST, PUT, DELETE, etc.), and calls doGet, doPost, doPut, doDelete methods at the appropriate time.

The following is the method characterized by:

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

service () method is called by the container, service method calls doGet, doPost, doPut, doDelete methods at the appropriate time. So, you do not have to service () method to do anything, you only need to type a request from a client to override doGet () or doPost () can be.

doGet () and doPost () method of each service request is the most commonly used method. Here are the characteristics of these two methods.

doGet () method

GET requests from normal requests a URL, or from a METHOD unspecified HTML form, it is processed by the doGet () method.

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

doPost () method

POST request comes from a specially designated METHOD is POST HTML form, it is processed by the doPost () method.

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

destroy () method

destroy () method is called only once, to be called at the end of Servlet life cycle. destroy () method allows you to turn off the Servlet database connection, stop the background thread, list, or click on the Cookie counter written to disk, and perform other similar clean-up activities.

After the call to destroy () method, servlet object is marked for garbage collection. destroy method is defined as follows:

  public void destroy () {
    // Termination of the code ...
  }

Chart

The following figure shows a typical Servlet lifecycle approach.

  • HTTP server is the first to reach the request is delegated to the Servlet container.
  • Servlet container before calling the service () method to load the Servlet.
  • Then Servlet container handles multiple requests generated by multiple threads, each thread of execution in a single instance of the Servlet service () method.
Servlet life cycle