Skip to content
Home Β» Servlet Life Cycle

Servlet Life Cycle


Servlet Life Cycle

Introduction

A Servlet is a server-side Java component used to handle client requests and generate dynamic web content. The Servlet Life Cycle defines the sequence of steps through which a servlet passes from its creation to destruction.
This life cycle is completely controlled by the Servlet Container (Web Container) such as Apache Tomcat, Jetty, or WebLogic.


Role of Servlet Container

The Servlet Container is responsible for:

  • Loading servlet classes
  • Creating servlet instances
  • Managing threads
  • Calling life cycle methods
  • Destroying servlet objects

πŸ“Œ Developers do NOT control the servlet life cycle directly.


Phases of Servlet Life Cycle

The servlet life cycle consists of four major phases:

  1. Loading and Instantiation
  2. Initialization (init())
  3. Request Processing (service())
  4. Destruction (destroy())

1. Loading and Instantiation Phase

Class Loading

  • When:
    • A client sends a request for the servlet OR
    • Server starts (if load-on-startup is configured)
  • The container loads the servlet class using Class Loader

Instantiation

  • The container creates only ONE object of the servlet class
  • This single object serves multiple client requests

πŸ“Œ Servlet follows Singleton behavior by default.


2. Initialization Phase – init() Method

public void init(ServletConfig config) throws ServletException

Purpose

  • Called only once after servlet object creation
  • Used to initialize resources required throughout servlet life

Key Points

  • Receives ServletConfig object
  • Reads initialization parameters
  • Allocates expensive resources

Common Tasks in init()

  • Database connection creation
  • Loading driver classes
  • Reading configuration files
  • Initializing shared objects

πŸ“Œ If init() throws ServletException, the servlet is not put into service.


3. Request Processing Phase – service() Method

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

Purpose

  • Handles every client request
  • Executes business logic
  • Generates response

Multithreading Concept

  • For every client request:
    • A new thread is created
    • Same servlet object is used
  • This improves performance but introduces thread-safety issues

πŸ“Œ Instance variables are shared among threads β†’ risk of data inconsistency.


HTTP-Specific Request Handling

When using HttpServlet, the service() method internally calls:

HTTP MethodServlet Method
GETdoGet()
POSTdoPost()
PUTdoPut()
DELETEdoDelete()
protected void doPost(HttpServletRequest req,
                      HttpServletResponse res)
        throws ServletException, IOException

πŸ“Œ Developers generally override doGet() or doPost().


4. Destruction Phase – destroy() Method

public void destroy()

Purpose

  • Called only once
  • Invoked before servlet removal

When Called

  • Server shutdown
  • Application redeployment
  • Servlet unloaded by container

Common Cleanup Tasks

  • Closing database connections
  • Releasing memory
  • Stopping background threads
  • Writing logs

πŸ“Œ After destroy(), servlet object becomes eligible for Garbage Collection.


Servlet Life Cycle Flow Diagram (Textual)

Servlet Class Loaded
        ↓
Servlet Object Created
        ↓
init()  β†’ (once)
        ↓
service() β†’ (multiple times, multithreaded)
        ↓
destroy() β†’ (once)

Important MCA Exam Points

βœ” Servlet container manages life cycle
βœ” Only one servlet instance per servlet
βœ” init() and destroy() are called once
βœ” service() executes for every request
βœ” Servlet is multithreaded
βœ” Thread safety is developer’s responsibility


Servlet Life Cycle Conclusion

The servlet life cycle defines the complete operational process of a servlet from creation to destruction. The servlet container loads the servlet class, creates a single instance, and calls the init() method to initialize resources. For each client request, the container invokes the service() method, which handles requests using multithreading and delegates them to appropriate HTTP methods. Finally, before unloading the servlet, the container calls the destroy() method to release resources. Understanding the servlet life cycle is essential for developing efficient, scalable, and thread-safe web applications.