Updated December 30, 2023
Table of Content
Introduction to Servlet Life Cycle
This tutorial gives you an understanding of Java Servlets and their life cycle.Before delving into the Servlet Life Cycle, let’s first gain insight into what a Servlet is and its process. A Java Servlet is a class in the Java programming language defined in Java Enterprise Edition, also known as Java EE. Sun Microsystems developed it in 1997. After the initial release of Servlet 1.0 in 1997, subsequent versions were introduced, with the latest being Servlet 4.0.
What is Servlet?
Servlet Technology is very useful in creating web applications as it generates dynamic web pages while residing at the server-side.Java servlets replaced Common Gateway Interface (CGI), a scripting language commonly used as a server-side programming language.
Servlet is platform-independent, robust and it can access all the Java APIs like the JDBC (Java Database Connectivity) API for accessing the databases of any enterprise.
Java Servlet is used to create web applications that are dynamic in nature. In order to do so, it extends the server capability. It is capable of running on any web server which has a Servlet container integrated with it.
The Java Servlet process can be easily understood from the steps mentioned below :
- The client sends a request to a servlet container. The client here refers to any browser such as Chrome, IE, Mozilla, etc., in use.
- The container or the Web Server looks for the servlet. As soon as the server finds the servlet, it initiates the servlet.
- Now, the servlet processes the client request, and then the server sends back a response.
- The server sends this response to the client.
Before we move to the life cycle of a servlet, you should be clear with terminologies used until now in this article. This will be helpful while understanding the Servlet Life Cycle.
- Web Server: The Web Server or HTTP Server handles HTTP Requests and HTTP Responses. The requests sent by clients are handled, and a response is sent based on the request made by this server.
- Web Container: Web Container or Servlet Container or Servlet Engine interacts with the Servlets. It is an important component of a web server as it manages a servlet’s life cycle.
Life Cycle of Servlet
You can understand the life cycle of a Servlet as a sequence of steps that a servlet undergoes from initiation to destruction. Servlet Engine manages the life cycle of the servlet, as told earlier.
Sum up the life cycle of a servlet in the following five points:
- The Servlet class loads.
- Creates a Servlet instance.
- Invokes the init() method to initialize the servlet.
- Invokes the service() method repeatedly for each client request placed.
- Destroys the servlet using the destroy() method.
Note: Instances are also called objects.
1. Loading of the Servlet Class
The classloader loads the servlet class. When the web container receives a request for a servlet, it loads the servlet class.
2. Creating a Servlet Class Instance
An instance of the servlet is created by the web container as soon as the servlet class gets loaded. Do keep in mind that the creation of a servlet instance is once in a lifetime process for every servlet, which means that it will be instantiated only once in the life cycle of the servlet.
3. Invoking the Init() Method
After creating the servlet instance, the web container’s task is to invoke the init() method. The init() method initializes the servlet and is called only once. If any user invokes a servlet, only one instance of the servlet will be created. Every single request results in a generation of a new thread. The data created or loaded by invoking the init() method remains throughout the servlet’s life.
Please make a note that the init() method will be called only once during the entire life of the servlet.
The syntax given below will invoke the init method –
public void init()throws ServletException
{
//code
}
4. Invoking the Service() Method
The service() method is a crucial method that the servlet calls every time it receives a request. The web container is the component that will call the service() method to perform some real work, i.e., to receive requests sent by the client browser and to handle it by sending an appropriate response to the client. Upon servlet initialization, the service method invokes and analyzes all HTTP request types, such as GET, POST, PUT, DELETE, etc. Once it identifies the request types, the service() method dispatches the request to its handler method accordingly.
Consider the case of a POST request made by the client. The job of the service() method is to call the doPost() method and dispatch the request to it. All the requests have their own handler method to which a call will be made by the service() method based on the type of request made. For example, for getting an exception, there is the doGet() handler method; for Put, there is a doPut() method, and so on.
Please note that the service() method is invoked every time a client request happens. This means that unlike the init() and destroy() method, the service() method can be invoked innumerable times during the servlet life cycle.
The syntax that you find below will invoke the service() method for Servlet :
public void service(ServletRequest req, ServletResponse response) throws IOException,ServletException
{
//code
}
5. Invoking a Destroy() Method
Upon shutting down the web server, the servlet has the opportunity to unload all created servlets. The destroy() method will remove all the initialized servlets, thus cleaning up the memory.
The syntax for it is:
public void destroy()
Servlet Life Cycle Web Container
The essential part of the servlet life cycle is a web container. It interacts with the servlet page using the web container.
A servlet life cycle’s web container has two primary duties:
- Servlet lifecycle management
- Mapped URLs
The web container is a server-side component that manages and processes all requests from the servlet page.
Servlet deployment takes occurs in a container. The server delivers the request to the container and then the servlet directly when a client sends a request to a web server that hosts a servlet. After determining the requested servlet, the container loads the servlet’s doGet() or doPost() methods. These methods pass the servlet the HTTP request and response ().
Architecture
The following figure shows you the basic architecture of the servlet life cycle and its methods.
It shows the servlet life cycle operation with each method and servlet container.
- The servlet container routes HTTP requests arriving at the server.
- The init method comes from a web container to initialize each object.
- The servlet container loads the servlet before running the service() method.
- The servlet container handles numerous requests by generating many threads. It executes the service() method of a single object of the life cycle of the servlet.
- The destroy() method closes the connections and cleans up the instance of the servlet.
Recommended Articles
We hope that this EDUCBA information on “Servlet Life Cycle” was beneficial to you. You can view EDUCBA’s recommended articles for more information.