Understanding the Servlet Life Cycle and Types of Servlets in Java Web Development
Servlets are Java-based web components that generate dynamic content in response to client requests. They have a well-defined life cycle, which starts when the servlet is loaded into the memory and ends when it is removed.
In this blog, we will discuss the servlet life cycle and the different types of servlets.
To get pre-requisites about Servlets read my previous blog post.
Servlet Life Cycle:
The servlet life cycle is a sequence of steps that a servlet goes through from its creation to its destruction. The life cycle consists of four stages:
Loading:
The servlet container loads the servlet class into the memory when the web application starts or when the servlet is first accessed.
Initialization:
The servlet container creates an instance of the servlet and initializes it by calling its init() method. This method is called only once during the life cycle of the servlet and is used for performing initialization tasks such as opening a database connection or reading configuration parameters.
Request Processing:
The servlet container calls the service() method of the servlet to process client requests. This method is called each time the servlet receives a request from the client.
Destruction:
The servlet container calls the destroy() method of the servlet when the web application is stopped or when the servlet is removed from the container. This method is used for performing cleanup tasks such as closing the database connection or releasing system resources.
Here is an example of a servlet that demonstrates the servlet life cycle:
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("Servlet initialized.");
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Servlet service called.");
}
public void destroy() {
System.out.println("Servlet destroyed.");
}
}
In this example, we have defined a servlet called “MyServlet” that extends the HttpServlet class. The servlet overrides the init()
, service()
, and destroy()
methods of the HttpServlet class.
When the servlet is loaded into the container, the init()
method is called. In our example, we have overridden the init()
method to print a message to the console indicating that the servlet has been initialized.
When a client makes a request to the servlet, the service()
method is called. In our example, we have overridden the service()
method to print a message to the console indicating that the service method has been called.
When the web application is stopped or the servlet is removed from the container, the destroy()
method is called. In our example, we have overridden the destroy()
method to print a message to the console indicating that the servlet has been destroyed.
By running this servlet and accessing it in a web browser, you can see the servlet life cycle in action. The console output will show messages indicating when the servlet is initialized, when the service method is called, and when the servlet is destroyed.
Types of Servlets:
There are two types of servlets:
- GenericServlet
- HttpServlet
GenericServlet:
GenericServlet is an abstract class that provides a base implementation for servlets. It implements the Servlet and ServletConfig interfaces and provides default implementations for all the methods defined in these interfaces except for the service() method. The service() method is left unimplemented so that subclasses can provide their own implementation.
HttpServlet:
HttpServlet is a subclass of GenericServlet that provides additional methods for handling HTTP requests. It implements the HttpServletRequest and HttpServletResponse interfaces, which provide methods for accessing HTTP request and response headers, parameters, and other information.
HttpServlet provides default implementations for the doGet(), doPost(), doPut(), doDelete(), and other HTTP request methods.
Here I’ve listed out some Methods of Http Servletclass:
· doGet : If the servletsupports HTTP GET requests
· doPost : For HTTP POST requests
· doPut : For HTTP PUT requests
· doDelete : For HTTP DELETE requests
· init & destroy : To manage resources that are held for the life of the servlet
· getServletInfo : Which the servletuses to provide information about itself
· doHead : It receives an HTTP HEAD request from the protected service method and handles the request
· doOptions : Called by the server to handle a OPTIONS request.
· doTrace : Called by the server to handle a TRACE request.
· getLastModified : Returns the time the HttpServletRequestobject was last modified, in milliseconds.
Conclusion:
Servlets have a well-defined life cycle that starts with loading the servlet class into the memory and ends with the destruction of the servlet.
Servlets can be of two types: GenericServlet and HttpServlet. GenericServlet is an abstract class that provides a base implementation for servlets, while HttpServlet is a subclass of GenericServlet that provides additional methods for handling HTTP requests.
Understanding the servlet life cycle and the different types of servlets is essential for developing robust and scalable web applications using Java.
More Blogs related to Java & Adv. Java …