Servlet Lifecycle

Servlet Tejas Sawant

Servlets provide methods that are called when specific events occur in a larger context. Programming in this environment involves writing predefined methods, which are called as required by a managing program.
Servlets operate in the context of a request and response model managed by a servlet engine. The engine does the following: 

  • Loads a servlet when it’s first requested
  • Calls the servlet’s init() method
  • Handles any number of requests by calling the servlet’s service() method
  • When shutting down, calls each servlet destroy() method.

As with applets, there are standard base classes javax.servlet.GenericServlet and javax.servlet.http.HttpServlet that implements the servlet callback methods. Servlet programming, then, consists of subclassing one of these classes and overriding the necessary method to accomplish the specific task at hand.
The following sections examine each of these lifecycle methods.


When a request for a servlet is received by the servlet engine, it checks to see if the servlet is already loaded. If not, the servlet engine uses a class loader to get the particular servlet class required and then invokes its constructor to get an instance of the servlet. After the servlet is loaded, but before it services any requests, the servlet engine calls an initialization method with the following signature: 

public void init(ServletConfig config) throws ServletException

This method is called only once, just before the servlet is placed into service. The ServletConfig object provides access to the servlet context and to any initialization parameters coded for the servlet. To maintain a reference to the servlet context, the config object must be stored as an instance variable, a task that’s done by the init(ServletConfig) method in GenericServlet. For this reason, it’s important to call super.init(config) within the init() method of any subclass. Inside the init() method, the servlet can perform any necessary startup tasks, such as establishing database connections. If any errors occur that make the servlet unable to handle requests, it should throw an UnavailableException. This prevents requests from being directed to the servlet.


After the init() method completes successfully, the servlet is able to accept requests. By default, only a single instance of the servlet is created, and the servlet engine dispatches each request to the instance in a separate thread. The servlet method that’s called has the following signature: 

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

The ServletRequest object is constructed by the servlet engine and acts as a wrapper for information about the client and the request. This includes the identity of the remote system, the request parameters, and any input stream associated with the request. Similarly, the ServletResponse object provides the means for a servlet to communicate its results back to the original requester. It includes methods for opening an output stream and for specifying the content type and length. As important as the service() method is, it’s rarely used. The reason for this is most servlets are designed to operate in the HTTP environment, for which there’s a specialized javax.servlet.http package. Rather than extending javax.servlet.GenericServlet directly, most servlets extend its subclass javax.servlet.http.HttpServlet. This subclass provides specialized methods corresponding to each HTTP request method:

  • GET requests are handled by doGet()
  • POST requests by doPost() 

The signatures for these methods use HTTP-specific versions of the request and response objects: 

public void doGet( 
    HttpServletRequest request, 
    HttpServletResponse response
 ) throws ServletException, IOException;

The service(Request, Response) method in HttpServlet casts the request and response objects into their HTTP-specific counterparts, and then calls service(HttpServletRequest, HttpServletResponse), which examines the request and calls the appropriate doGet(), doPost(), or other methods. A typical HTTP servlet, then, includes an override to one or more of these subsidiary methods, rather than an override to service(). 


The servlet specification allows a servlet engine to unload a servlet at any time. This may be done to conserve system resources or in preparation for the servlet engine shutdown. The servlet engine notifies each loaded servlet this is about to happen by calling its destroy() method. By overriding destroy(), you can release any resources allocated during init().

Source :- JSP: The Complete Reference by Phil Hanna


Related posts