Saturday, 12 September 2020

Life Cycle of Servlet. | How container handles Servlet request and response?

The following topics are covered in the post:

  • What is HTTP?
  • What is Server? Difference between Web Server and Application Server.
  • What is Servlet and what is the Servlet container?
  • Life Cycle of Servlet.
  • How container handles Servlet request and response?


It is the data communication protocol used to establish communication between client and server.

HTTP Requests

It is the request send by the computer to a web server that contains all sorts of potentially interesting information.


Server is a device or a computer program that accepts and responds to the request made by other program, known as client. It is used to manage the network resources and for running the program or software that provides services.

There are two types of servers:

1.      Web Server

2.      Application Server

Web Server

web server accepts and fulfills requests from clients for static content (i.e., HTML pages, files, images, and videos) from a website. Web servers handle HTTP requests and responses only.

An application server exposes business logic to the clients, which generates dynamic content. It is a software framework that transforms data to provide the specialized functionality offered by a business, service, or application. Application servers enhance the interactive parts of a website that can appear differently depending on the context of the request.


A servlet is a Java technology that is managed by a container called a servlet engine. It generates dynamic content and interacts with the client through Request and Response. Servlet extends the functionality of a web server.


Java Servlet is a platform-independent, container-based Web component used to generate dynamic content in a Web page. It is one of the stable technologies to share server-side resources in client-server programming.

Though servlets can respond to many types of requests, they generally implement web containers for hosting any website on web server.


The client can request only static pages from web server. If a client required dynamic pages then server further forwards this request to container where the corresponding dynamic web page is generated on server side.

Container is a part of web server which interacts with the servlet for handling the dynamic web pages from the client.

Examples of Servlet Container:

  1. Apache Tomcat
  2. Resin


Container Operations:

  • Communication Support
  • Lifecycle Management
  • Security
  • Object Pooling
  • Multithreading
  • JSP Support



The web container maintains the life cycle of a servlet instance. A Servlet is executed in 5 steps. Let's see the life cycle of the servlet. 

  1. Servlet class is loaded.
  2. Servlet instance is created.
  3. init method is invoked.
  4. service method is invoked.
  5. destroy method is invoked.

1) Servlet class is loaded:

The classloader is responsible to load the servlet class. The servlet class is loaded when the first request for the servlet is received by the web container.

2) Servlet instance is created:

The web container creates the instance of a servlet after loading the servlet class. The servlet instance is created only once in the servlet life cycle.

3) init method is invoked:

The web container calls the init method only once after creating the servlet instance. The init method is used to initialize the servlet. It is the life cycle method of the javax.servlet.Servlet interface. Syntax of the init method is given below:

public void init(ServletConfig config) throws ServletException 

4) service method is invoked:

The web container calls the service method each time when request for the servlet is received. If servlet is not initialized, it follows the first three steps as described above then calls the service method. If servlet is initialized, it calls the service method. Notice that servlet is initialized only once. The syntax of the service method of the Servlet interface is given below:

public void service(ServletRequest request, ServletResponse response)  

  throws ServletException, IOException 

5) destroy method is invoked:

The web container calls the destroy method before removing the servlet instance from the service. It gives the servlet an opportunity to clean up any resource for example memory, thread etc. The syntax of the destroy method of the Servlet interface is given below:

public void destroy()


As we have studied that web server understands only static pages. Container is a part of web server which understands static pages.

When a request is bypassed by the Web Server to the Container in order to generate the dynamic response, Container creates two objects, HttpServletRequest and HttpServletResponse for handling the request as well as response.  The HTTP request is converted by container into a format which is acceptable by the servlets and associated to the HttpServletRequest object. This request goes to Servlet. Then response (dynamic content) is created by Servlet send back to the web server in HTTP form by the container through HttpServletResponse object. 



No comments:

Post a comment

All about journals and research paper | What is impact factor? | how the impact factor is calculated? | who calculate the impact factor? Scopous journals

Figure 1: One of my Research Papers When the Scholars are in their Master or PhD or in any research field. They are supposed to writ...