Pr.Pg Next Pg

Servlets tutorials - Life Cycle

  • The life cycle of a Java servlet is a very simple object-oriented design. A servlet is constructed and initialized. It then services zero or more requests until the service that it extends shuts down. At this point the servlet is destroyed and garbage is collected.
  • Servlet life cycle path are listed below:
    1. First when browser send a request for a servlet, it first initialized by calling the init() method.

    2. Then next service() method is called by the servlet for processing the client request.

    3. Than at last servlet is terminated by calling the destroy() method.

    4. Finally, servlet is garbage collected by the garbage collector of the JVM.

  • Now let us discuss the life cycle methods in details.

 

The init() method

  • The init() method is where the servletís life begins.

  • During initialization stage of the servlet life cycle, the web container initializes the servlet instance by calling the init() method, passing an object implementing the javax.servlet.ServletConfig interface. This configuration object allows the servlet to access name-value initialization parameters from the web application.

  • In the init() method the servlet creates and initializes the resources that it will be using while handling requests.

  • The init method definition looks like this:

public void init() throws ServletException

{

//Initialization code;

}

 

The service() method

  • The service() method handles all requests sent by a client. It cannot start servicing requests until the init() method has been executed.

  • Each request is serviced in its own separate thread. The web container calls the service() method of the servlet for every request type such as (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.

  • Here is the signature of this method:

public void service(ServletRequest request, ServletResponse response)

throws ServletException, IOException

{

//Service code

}

  • Servlet developer have to override either doGet or doPost method depending on what type of request are coming from client, because service method is called by the web container to doGet, doPost, doPut, doDelete etc methods as appropriate.

  • The doGet() and doPost() are most frequently used methods within each service request. Here are the signature of these two methods.

 

The doGet() Method

  • doGet is a default method, if not specified.

  • A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method.

  • doGet is used when there is are requirement of sending data appended to a query string in the URL. 

  • doGet signature look like below code:

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

//Servlet code;

}

 

The doPost() Method

  • doPost is more secure than doGet method.

  • doPost can send more amount of data compare to doGet.

  • A POST request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method.

  • doPost signature look like below code.

public void dopost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

// Servlet code;

}

 

The destroy() method

  • This method signifies the end of a servletís life. When a service is being shut down it calls the servletís destroy() method. This is where any resources that were created in the init() method should be cleaned up.

  • After the destroy() method is called, the servlet object is marked for garbage collection. The destroy method definition looks like this:

public void destroy()

{

//Finalization code;

}

  • Destroy method in Servlets are similar to destructor in C++.

 

Architecture Digram of Life Cycle of Servlets

  • The following figure depicts a typical servlet life-cycle scenario.

    1. First the HTTP requests coming from a client to the server invoke the servlet container.

    2. Than the servlet container loads the servlet before invoking the service() method.

    3. Than the servlet container handles each request in new thread execution and each thread execute service method of a single instance of the servlet.

 

servlets life cycle

Pr.Pg border                                              Next Pg