Servlet Life Cycle

A servlet is basically a small Java program that runs within a Web server. It can receive requests from clients and return responses. The whole life cycle of a servlet breaks up into 3 phases:
Initialization: A servlet is first loaded and initialized, usually when it is requested by the corresponding clients. Some websites allow the users to load and initialize servlets when the server is started up so that the first request will get responded more quickly.
Service: After initialization, the servlets serve clients on request, implementing the application logic of the web application they belong to.
Destruction: When all pending requests are processed and the servlets have been idle for a specific amount of time, they may be destroyed by the server and release all the resources they occupy. More specifically, the behavior of a servlet is described in javax.servlet.Servlet interface.

Initialize


When the servlet is first created, it is in the initialization stage. The web server invokes the init() method of the servlet in this stage. It should be noted here that init() is only called once and is not called for each request. Since there is no constructor available in Servlet so this urges its use for one time initialization just as the init() method of applet.

Initialize stage has the following characteristics and usage 

  • Executed once, when the servlet gets loaded for the first time
  • Not called for each client request.
  • The above two points make it an ideal place to perform the startup tasks which are done in constructor in a normal class.

Signature of the init method is:

public void init(ServletConfig config) throws ServletException

Service


 

The service() method is the engine of the servlet, which actually processes the client’s request. On every request from the client, the server spawns a new thread and calls the service() method as shown in the figure below. This makes it more efficient as compared to the technologies that single thread to respond to requests.

web server

The figure below show both versions of the implementation of service cycle. In the upper part of the diagram, we assume that the servlet is made by sub-classing from GenericServlet (Remember, GenericServlet is used for constructing protocol independent servlets.). To provide the desired functionality, service() method is overridden. The client sends a request to the web server, a new thread is created to serve this request followed by calling the service() method. Finally a response is prepared and sent back to the user according to the request.

servlet

The second part of the figure illustrates a situation in which servlet is made using HttpServlet class. Now, this servlet can only serve the HTTP type requests. In these servlets doGet() and doPost() are overridden to provide desired behaviors. When a request is sent to the web server, the web server after creating a thread, passes on this request to service() method. The Service() method checks the HTTP request types (GET, POST) and calls the doGet() or doPost method depending on how the request is originally sent. After forming the response by doGet() or doPost() method, the response is sent back to the service() method that is finally sent to the user by the web server.

Signature of the service method is:

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

Destroy


The web server may decide to remove a previously loaded servlet instance, perhaps because it is explicitly asked to do so by the server administrator, or perhaps servlet container shuts down or the servlet has been idle for a long time, or may be server is overloaded. Before it does, however, it calls the servlets destroy() method. This makes it a perfect spot for releasing the acquired resources.

The following figure can help to summarize the life cycle of the Servlet.

life cycle

The web server creates a servlet instance. After successful creation, the servlet enters into initialization phase. Here, init() method is invoked for once. In case of web server fails in previous two stages, the servlet instance is unloaded from the server. After initialization stage, the Servlet becomes available to serve the clients request and to generate response accordingly. Finally, the servlet is destroyed and unloaded from the web server.

Signature of the destroy method is:

public void destroy() {
}

Leave a comment