Servlet Life Cycle
Tags: java, webdev
Published: 2023-01-13 20:18:36

## What are the Servlets?
Servlets are Java Programming language classes that dynamically process requests and construct responses. Servlets are on the server side. Those are pure java classes and unlike JSPs, only Java codes can be written in the servlets with the file extension of .java.  In Java web applications, servlets are suitable to work with the database, and session management, use the persistence API, invoke a web service, and do RMI call. In Enterprise Java Beans (EJB) applications, Servlets are used to invoke Java beans. Servlets contain the back-end logic of a java web application in most cases.


![A sample Servlet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nttk6duvkqo4j6svil8o.png)

## Class Hierarchy
Following are the parent classes of _HttpServlet_ class.


![Class hierarchy of _HttpServlet_ class depicting only the important servlet methods.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o0rkyl4i2nw8un51j5qf.png)

## Life Cycle
From the creation of a Servlet to the termination of it is called the “Servlet life cycle”.


![Servlet life cycle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y6hou3yfq8qzcdda8mc5.png)

Let’s consider the very first request received by a Servlet. Since this is the first request to the Servlet there are not any instances of that Servlet. Then the Servlet gets compiled to a class file and the web container creates a new instance and the _init(ServletConfig config)_ method is invoked. Then the _init()_ method is invoked. These _init()_ methods can be used to initialize resources such as JDBC connections. The _init()_ methods are called once in the life cycle by the web container.

After the invocation of the _init_ methods, three objects are created. First, a thread object is created to handle this process afterward (to serve the received request); A _HTTPServletRequest_ object is created as the request; Lastly, A _HTTPServletResponse_ object is created as the response. This is the starting point for the requests which are not the very first request (if there is already a servlet instance available). This means that for every request, a thread is created, an _HTTPServletRequest_ is created and _HTTPServletResponse_ is created. 

Then the _Service(ServletRequest request, ServletResponse response)_ method and then the _Service(HTTPServletRequest request, HTTPServletResponse response)_ is invoked. Previously created _HTTPServletRequest_ object and _HTTPServletResponse_ object are passed to these methods as the parameter list. These Service methods are invoked by the web container whenever a request from a client is received. The latter method checks the received HTTP header for the HTTP method type. Then it invokes the _doGet()_, _doPost()_, etc., methods accordingly. The method can be overridden and write program code if required.

The service method invokes 7 methods considering the type of the HTTP request received. 
1.	_doGet()_ for GET
2.	_doPost()_ for POST
3.	_doDelete()_ for DELETE
4.	_doUpdate()_ for UPDATE
5.	_doOptions()_ for OPTIONS
6.	_doHead()_ for HEAD
7.	_doTrace()_ for TRACE 

Most all the time the program logic goes here by overriding these methods according to the need. The argument lists of these methods are types of _HTTPServletRequest_ and _HTTPServletResponse_. But those are interfaces therefore they can’t be instantiated. The methods are receiving objects of types of _RequestFacade_ and _ResponseFacade_ as their parameter list. These are classes and they can be instantiated. 

Finally, the _destroy()_ method is invoked by the web container to terminate the servlet. It is called once in the servlet lifecycle. This method can be overridden and suitable to close database connections and files, etc.