A Servlet is a server-side Java program that handles client requests and generates dynamic responses. Its life cycle is managed by the Servlet container using the jakarta.servlet API.
- Managed by the Servlet container through defined life cycle methods
- Helps build efficient and scalable web applications
Note: In Jakarta EE 9 and above, use the jakarta.servlet.* package instead of the older javax.servlet.* namespace.
States of the Servlet Life Cycle
The Servlet life cycle consists of four main stages:
- Loading a Servlet
- Initializing a Servlet
- Handling Client Requests
- Destroying a Servlet

1. Loading a Servlet
The first stage of the Servlet lifecycle involves loading and initializing the Servlet. The Servlet container performs the following operations:
- Loading: The Servlet container loads the Servlet class into memory.
- Instantiation: The container creates an instance of the Servlet using the no-argument constructor.
When is a Servlet loaded?
- During application startup (if configured in deployment descriptor).
- On first client request (lazy loading).
2. Initializing a Servlet
After instantiation, the container initializes the Servlet using the init() method.
- init(ServletConfig config) method is called once in the Servlet’s lifetime.
- It prepares the Servlet to handle requests (e.g., database connection setup).
- Called only once, unlike service() which is called per request.
Example:
@Override
public void init() throws ServletException {
// Initialization code (e.g., database connection)
}
3. Handling request
Once initialized, the Servlet is ready to handle client requests.
3.1 Request and Response Objects
- ServletRequest and ServletResponse objects are created for each request.
- For HTTP, HttpServletRequest and HttpServletResponse objects are used.
3.2 The service() Method
- The container calls service(ServletRequest req, ServletResponse res) for each request.
- Determines the HTTP request type (GET, POST, PUT, DELETE).
- Delegates the request to appropriate methods: doGet(), doPost(), etc.
Example:
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
// Handle request and generate response
}
4. Destroying a Servlet
When the Servlet container decides to remove the Servlet, it follows these steps which are listed below
- Allow Active Threads to Complete: The container ensures that all threads executing the service() method complete their tasks.
- Invoke the destroy() Method: The container calls the destroy() method to allow the Servlet to release resources (e.g., closing database connections, freeing memory).
- Release Servlet Instance: After the destroy() method is executed, the Servlet container releases all references to the Servlet instance, making it eligible for garbage collection
Servlet Life Cycle Methods
There are three life cycle methods of a Servlet:
- init()
- service()
- destroy()

1. init() Method
- Invoked once when the Servlet is instantiated.
- Used to initialize resources (e.g., database connections).
- Prefer the non-parameterized init() method for fewer redundant calls.
Example:
@Override
public void init() throws ServletException {
// Initialization code
}
- Exception handling is possible in init().
- Constructor is not recommended for initialization because it cannot throw ServletException.
2. service() Method
- Handles client requests and responses.
- Determines HTTP request type (GET, POST, PUT, DELETE) and delegates to
doGet(),doPost(), etc.
Example:
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
// Request handling code
}
- Connects client and server.
- Accepts ServletRequest and ServletResponse objects.
3. destroy() Method
- Called once at the end of the Servlet’s life cycle.
- Cleans up resources (e.g., closes DB connections, releases memory).
Example:
@Override
public void destroy() {
// Cleanup code
}
Java Servlet Example
Below is a sample program to illustrate Servlet in Java.
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
public class AdvanceJavaConcepts extends HttpServlet {
private final String output; // Immutable (thread-safe)
@Override
public void init() throws ServletException {
output = "Advance Java Concepts";
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
try (PrintWriter out = resp.getWriter()) {
out.println(output); // Thread-safe
}
}
@Override
public void destroy() {
System.out.println("Servlet destroyed");
}
}
Explanation:
- init() Method: Initializes the output variable when the Servlet is first loaded.
- doGet() Method: Responds to client GET requests and prints the output as HTML.
- Thread Safety: output is immutable, ensuring safe access by multiple threads.
- destroy() Method: Invoked before Servlet removal to release resources or perform cleanup.