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:
- Loading and Instantiation
- Initialization (
init()) - Request Processing (
service()) - Destruction (
destroy())
1. Loading and Instantiation Phase
Class Loading
- When:
- A client sends a request for the servlet OR
- Server starts (if
load-on-startupis 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
ServletConfigobject - 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 Method | Servlet Method |
|---|---|
| GET | doGet() |
| POST | doPost() |
| PUT | doPut() |
| DELETE | doDelete() |
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.
