Servlets - SPLessons

Servlets Life Cycle

Chapter 5

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Servlets Life Cycle

Servlets Life Cycle

shape Description

Servlets Life Cycle, When the Servlet is deployed in the server the Servlet container creates life cycle of the Servlet. The central abstraction in the servlet API is the Servlet interface, all the Servlets have to implement this interface either directly or by extending a class such as GenericServlet , HttpServlet. The following steps show the different methods used in Servlet Life Cycle.

shape Conceptual

The figure below demonstrate the Servlet Life Cycle structure.

shape Methods

Loading The Servlet Class: When the web container gets the request from the browser for servlet then servlet class will be loaded.

Creating Servlet Instance: The web container creates an object for the servlet when servlet class is loaded, here the important point is an object and will be created in life cycle only once.

Init Method: This method will be called by the web container after an object was created to the servlet. The functionality of the init() method is to initialize the servlet, following is the regular syntax to declare the init() method.

public void init(ServletConfig config) throws ServletException

Service Method: The service() method will be called by web container when servlet will have the request, in case servlet is not initialized then above methods will be utilized. In the life cycle of servlet this method will be utilized only once. Following is a syntax to declare the service() method.

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

Destroy Method: Web container is the responsibility to call this method utilized while vanishing an object of the Servlet from the service, it provides the chance for servlet to clean up the memory or junk data. Following is the Syntax for the destroy method.

public void destroy()

Following is the code structure of life cycle methods.

Can user call servlet destory() from service()?
The destory() is part of servlet life cycle methods, it is used to kill the servlet instance. Servlet Engine is used to call destory(). In case, if user call destory method from service(), it just execute the code written in the destory(), but it wont kill the servlet instance. The destroy() will be called before killing the servlet instance by servlet engine.

shape Example

Following is an example to understand the life cycle methods of Servlet.
//Following is the code to undestand Servlets Life Cycle methods.

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletLifeCycle extends HttpServlet
	public ServletLifeCycle()
		System.out.println("Default constructor");
	public void init(ServletConfig config)
		System.out.println("Init method...!");
	public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
		PrintWriter pw = res.getWriter();
		pw.println("Hello, Welcome to splessons...!");
	public void destroy()
		System.out.println("Destroy method");



Sample rule of WEB.XML file as follows.

Flow of the Code

Now compile the code and analyse the output. Following is the result will be displayed in the browser.

Following is the result will be displayed in the console.


shape Key Points

  • Servlets Life Cycle – The init() will be called by web container only once.
  • Servlets Life Cycle – The destroy() cleans the junk files to free up the memory.
  • The destroy() will be called only if the servlet is unloaded.