DEV Community

Rajitha Yasasri
Rajitha Yasasri

Posted on

Servlet Life Cycle

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

Class Hierarchy

Following are the parent classes of HttpServlet class.

Class hierarchy of _HttpServlet_ class depicting only the important servlet methods.

Life Cycle

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

Servlet life cycle

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.

Top comments (0)