Open In App

Servlet – Flow Of Execution

Servlets provide a way to communicate with client requests and generate dynamic responses based on the request-response programming model. Servlets can respond to any type of request but in most cases, these are used in web applications hosted by web servers. For these applications, Java Servlets define HTTP – specific servlet classes. For writing Servlets, Java Servlet API provides interfaces and classes in javax.servlet and javax.servlet.http packages. The servlet interface defines the life cycle of the servlet. We must extend GenericServlet class or HTTPServlet class(specific to HTTP request/response) to handle the client requests in web applications.

Flow of Execution of Servlets

In the execution of a servlet for the provided client request, the servlet container plays a crucial role in the Servlet life cycle.



Servlet container:

It is a web container that is part of a web server that interacts with the Java Servlets. Servlet container is responsible for



Before an application client component can be executed, it should be deployed into the container. All these web components and the container runs on the web/application server.

Servlet Program:

To understand the flow of execution of the servlet, we will create a simple web application to display the hello message in the client browser. In this example, we will be using Eclipse IDE and Tomcat server. Create the following files as shown in below package directory structure. 

Example

Files to create:

Project_Structure

index.html:

Create the below form to take the user name as input from the client browser.




<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home</title>
</head>
<body>
    <form action="hello" method="post">
  
        Enter your name and click on submit: 
        <input type="text" name="name" />
        <input type="submit" />
  
    </form>
</body>
</html>

HelloServlet.java:

Create below servlet to accept the client request, process it, and generate the response.




package com;
  
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
  
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
  
    protected void doPost(HttpServletRequest request,
                          HttpServletResponse response)
        throws ServletException, IOException
    {
  
        // Get the client entered data from request object
        String name = request.getParameter("name");
  
        // set the response content type
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
  
        // Print hello message to the client browser in
        // response object
        out.println("<h3>Hello " + name + "!!</h3>");
        out.close();
    }
}

web.xml:

Create deployment descriptor – web.xml file to mention the welcome file and to map the URL to the Servlet class.




<?xml version="1.0" encoding="UTF-8"?>
         xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ServletFlow</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.HelloServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

Program Execution

Step 1:

Following is the conceptual diagram of the servlet container after the deployment of the application.

Servlet Execution Flow – Step 1

As shown in the above diagram, 

At server side:

At client side:

Step 2:

 When we start the server, the job of the servlet container is to

While deploying web applications, 

Servlet Execution Flow – step 2

Now, the container will identify the welcome file page to display on the client browser based on the “welcome file list” mentioned in the “web.xml” file. Here, we specified, “index.html” as a welcome file. So, it displays that HTML page to the browser. URL: http://localhost:8081/ServletFlow/

index.html

Once we enter the name and click on submit, then a request will come to protocol with the data entered by the client.

Step 3:

Servlet Execution Flow – step 3

Step 4: Servlet Life cycle

Servlet Loading:

Servlet Instantiation:

Servlet Initialization:

Servlet Life cycle

Step 5:

Output

Conclusion

Java Servlets don’t have a main() method. It’s the job of Servlet Container for instantiating the servlet or creating a new thread to handle the request and response objects. To process multiple requests in a single servlet the container creates multiple threads. You can also monitor and react to these events in a servlet’s lifecycle by defining listener objects. The methods in listener objects get invoked when lifecycle events occur.


Article Tags :