Servlet - Flow Of Execution

Last Updated : 5 Jun, 2026

The flow of execution in a servlet defines how a client request is processed and a response is generated in a Java web application. It follows a structured lifecycle managed by the servlet container. This flow ensures efficient handling of requests using methods like init(), service(), and destroy().

  • Client request is received and passed to the servlet via the service() method.
  • Based on request type, doGet() or doPost() is executed.
  • The servlet processes data and sends a response back to the client.

Flow of Execution of Servlets

The flow of execution of servlets describes how a client request is processed and how a response is generated in a Java web application. This process is managed by the servlet container and follows a defined lifecycle.

  • Client Request :The client (web browser) sends an HTTP request to the web server.
  • Request Forwarding: The web server forwards the request to the servlet container.
  • Servlet Mapping: The servlet container identifies the appropriate servlet using URL mapping.
  • Servlet Loading & Initialization : If the servlet is not already loaded, the container loads it. The init() method is called (executed only once).
  • Request Processing: The container calls the service() method. Based on the request type, doGet(), doPost(), etc., are invoked.
  • Response Generation: The servlet processes the request and prepares the response (HTML/JSON).
  • Response Sent Back: The response is sent back to the client through the servlet container and web server.
  • Servlet Destruction: When the server shuts down, the destroy() method is called.

Steps to Implement Servlet Execution Example

Step 1: Create Project

  • Open Eclipse -> Create Dynamic Web Application
  • Project name: ServletFlow
Project Structure
Project_Structure

Step 2: Create index.html

  • Create an HTML file to take user input
  • Collect data (user name) from browser
  • Send request to servlet using form

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

HTML
<!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>

Step 3: Create Servlet (HelloServlet.java)

  • Create a servlet class inside src folder
  • Handle client request
  • Process data and generate response

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

Java
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();
    }
}

Step 4: Configure web.xml

  • Create deployment descriptor inside WEB-INF
  • Map URL to servlet
  • Define welcome file

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

XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" 
         xmlns="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html" 
         xsi:schemaLocation="http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html/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>

Step 5: Run Application

  • Right-click project -> Run on Server
  • Server starts and deploys application
  • Browser opens index.html automatically

Step 1: Deployment Overview

conceptual diagram of the servlet container
Servlet Execution Flow - Step 1

Server Side:

  • Servlet container runs inside the server
  • Web application (e.g., ServletFlow) is deployed
  • WEB-INF contains web.xml and servlet .class files

Client Side:

  • Browser sends HTTP requests and receives responses
  • Communication happens via a connection using IP address and port number

Step 2: Server Start & Deployment

  • Servlet container recognizes and deploys all web applications
  • Creates a ServletContext object for each application
  • Locates web.xml inside the WEB-INF folder
  • Loads and parses the web.xml file
  • Stores application-level data in the ServletContext object
servlet context object
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
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: Request Processing

  • Request is divided into header (browser details) and body (user data)
  • Server validates the request and forwards it to the servlet container
Servlet Execution Flow
Servlet Execution Flow - step 3
  • Container identifies the application and resource using URL pattern (/hello)
  • Mapping is checked in web.xml inside WEB-INF
  • Corresponding servlet .class file is located
  • Servlet lifecycle starts after identification

Step 4: Servlet Lifecycle

  • Servlet Loading: Container loads servlet .class file into memory (if not already loaded)
  • Instantiation: Creates servlet object and ServletConfig object
  • Initialization: Calls init() method once to initialize the servlet
Servlet Life cycle
Servlet Life cycle
  • Request Handling:
    1. Creates HttpServletRequest and HttpServletResponse objects
    2. Calls service() → which invokes doPost() / doGet()
    3. Request object holds headers, parameters, and attributes
  • Response Generation: Servlet processes request and prepares response
  • Response Dispatch: Response is sent to server ->protocol -> client (header + body) . Thread is destroyed after execution

Step 5: Response & Destruction

  • Response Sent to Client: Protocol sends the response to the browser
  • Display Output: Browser reads response body and displays result
  • Connection Closed: Connection between client and server is terminated
Servlet Execution Flow
  • Object Cleanup: Container destroys request and response objects
  • Waiting State: Container waits for new requests
  • Servlet Destruction: On server shutdown, destroy() method is called:
    1. Servlet object and ServletConfig are removed
    2. Servlet bytecode is unloaded from memory
    3. ServletContext object is destroyed
Output
Output
Comment