life cycle methods of a servlet
Servlets in Java are managed by a servlet container (like Apache Tomcat). The servlet life cycle is controlled by the container, which includes the following phases:
1. Loading and Instantiation: The servlet class is loaded into memory and an instance of the servlet is created.
2. Initialization (init method): The servlet is initialized by calling its init method. This method is called only once in the servlet’s life cycle.
3. Request Handling (service method): For each client request, the service method is called. This method dispatches requests to the doGet, doPost, doPut, doDelete, etc., methods based on the HTTP request type.
4. Destruction (destroy method): When the servlet is no longer needed, the container calls the destroy method to allow the servlet to release resources.
Detailed Explanation methods of a servlet
1. Loading and Instantiation: When a request is made to a servlet for the first time, the servlet container loads the servlet class, creating a new instance of the servlet.
2. Initialization (init method): This method is called once when the servlet is first loaded into memory. It is used to perform any required initialization, like opening a database connection, loading configuration settings, etc.

Table of Contents
java
public void init() throws ServletException {
// Initialization code here
}
3. Request Handling (service method): The service method is called for each request and it determines the type of request (GET, POST, etc.) and dispatches it to the corresponding method (doGet, doPost, etc.).
java
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Request handling code here
}
4. Destruction (destroy method): The destroy method is called only once at the end of the servlet's life cycle. It is used to clean up resources like closing database connections, releasing file handles, etc.
java
public void destroy() {
// Cleanup code here
}
Java Example
Here's a complete example demonstrating the life cycle methods of a servlet:
java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet {
@Override
public void init() throws ServletException {
// Initialization code
System.out.println("Servlet is being initialized");
}
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Request handling code
String method = request.getMethod();
if (method.equals("GET")) {
doGet(request, response);
} else if (method.equals("POST")) {
doPost(request, response);
}
// Handle other methods if needed
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("Hello, this is GET response");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("Hello, this is POST response");
}
@Override
public void destroy() {
// Cleanup code
System.out.println("Servlet is being destroyed");
}
}