Servlet Lifecycle in Java | Complete Beginner’s Guide

Introduction

Every Java program has a beginning and an end.
Similarly, a Servlet follows a well-defined lifecycle that is managed by the Servlet container (such as Apache Tomcat).

Understanding the Servlet Lifecycle is extremely important because:

  • It explains how servlets are created, used, and destroyed

  • It helps you write efficient and bug-free web applications

  • It is a frequently asked interview topic

This guide explains the lifecycle step by step, using simple language, diagrams, and examples.

What Is the Servlet Lifecycle?

Simple Definition:

The Servlet Lifecycle is the sequence of steps that a servlet goes through from the moment it is loaded until it is destroyed.

 

These steps are fully controlled by the Servlet Container, not by the developer.

Who Manages the Servlet Lifecycle?

The Servlet Container (like Tomcat):

  • Loads the servlet class

  • Creates the servlet object

  • Calls lifecycle methods

  • Handles multiple requests

  • Destroys the servlet when needed

Developers do not manually create or destroy servlets.

The Servlet lifecycle consists of three main phases:

  1. Initializationinit()

  2. Request Processingservice()

  3. Destructiondestroy()

Servlet Lifecycle Diagram (Text-Based)

Servlet Loaded
      |
      ↓
   init()
      |
      ↓
 service()
 (doGet / doPost)
      |
      ↓
   destroy()
      |
      ↓
Servlet Unloaded

Phase 1: Initialization (init())

What Happens in init()?

  • Called only once

  • Executed when servlet is first loaded

  • Used to perform startup tasks

Example Uses:

  • Database connection setup

  • Loading configuration data

  • Initializing resources

Method Signature:

public void init() throws ServletException {
    // Initialization code
}

init() is called only once during the servlet’s lifetime.

Phase 2: Request Handling (service())

What Happens in service()?

  • Called for every request

  • Handles client requests

  • Delegates to:

    • doGet()

    • doPost()

    • doPut()

    • doDelete()

Flow:

Client Request → service() → doGet() / doPost()

Example:

protected void doGet(HttpServletRequest request,
                     HttpServletResponse response)
                     throws IOException {

    response.getWriter().println("Hello from Servlet!");
}

This is the most frequently executed phase.

Phase 3: Destruction (destroy())

What Happens in destroy()?

  • Called only once

  • Executed before servlet is removed

  • Used to release resources

Example Uses:

  • Closing database connections

  • Releasing memory

  • Logging shutdown messages

Method Signature:

public void destroy() {
    // Cleanup code
}

After destroy(), the servlet is no longer available.

Complete Lifecycle Example

public class LifeCycleServlet extends HttpServlet {

    public void init() {
        System.out.println("Servlet Initialized");
    }

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
                         throws IOException {

        response.getWriter().println("Servlet is processing request");
    }

    public void destroy() {
        System.out.println("Servlet Destroyed");
    }
}

Execution Order:

  1. init() → once

  2. doGet() → multiple times

  3. destroy() → once

Real-Life Analogy

Think of a Shop 

  • Opening the shopinit()

  • Serving customersservice()

  • Closing the shopdestroy()

The shop opens once, serves many customers, and closes once.