Servlet - Client HTTP Request

Last Updated : 11 May, 2026

In Servlet technology, a Client HTTP Request is the request sent by a browser to the web server to access a resource or submit data. This request is handled on the server side using HttpServletRequest, which allows servlets to read all incoming client information like parameters, headers, cookies, and request type.

  • Client HTTP Request is generated when a user interacts with a web page (URL hit or form submit).
  • It carries important data such as headers, parameters, cookies, and request method (GET/POST).
  • Servlets process this request using HttpServletRequest to generate dynamic responses.

Working Flow of Client HTTP Request in Servlet

A Client HTTP Request in Servlet is the process where the browser sends data to the server, and the servlet processes it using HttpServletRequest to generate a dynamic response.

servlet_client_http_request
HTTP Request Flow
  1. Client Sends Request: The browser sends an HTTP request (GET/POST) to the web server by entering a URL or submitting a form.
  2. Request Reaches Web Container: The request is received by the web container (e.g., Tomcat), which identifies the correct servlet based on URL mapping.
  3. HttpServletRequest Object Creation: The container creates an HttpServletRequest object that holds all request data like parameters, headers, cookies, etc.
  4. Servlet Execution: The servlet’s doGet() or doPost() method is called, and the request object is passed as a parameter.
  5. Data Processing: Servlet extracts required information from the request using methods like - getParameter() ,getHeader(), getCookies().
  6. Response Generation: After processing the request data, servlet generates a response using HttpServletResponse.
  7. Response Sent to Client: The generated response is sent back to the browser and displayed to the user.

Header and Descriptions

This section lists the commonly used HTTP request headers to understand what information is sent from the client to the server.

Header NameDescription
AcceptMIME types supported by browser
Accept-CharsetCharacter sets supported
Accept-EncodingCompression formats supported
Accept-LanguagePreferred language of client
AuthorizationAuthentication details
ConnectionConnection type (keep-alive etc.)
Content-LengthSize of request body (POST)
CookieSends stored cookies
HostServer domain and port

Methods Used to Read HTTP Header (HttpServletRequest)

In Servlet, the HttpServletRequest interface provides methods to read HTTP headers, parameters, cookies, session, and request details sent by the client.

MethodDescription
Cookie[] getCookies()Returns all cookies sent by the client in the request.
Enumeration<String> getHeaderNames()Returns all HTTP header names present in the request.
String getHeader(String name)Returns the value of a specific HTTP header.
Enumeration<String> getParameterNames()Returns all request parameter names (form/query data).
String getParameter(String name)Returns the value of a specific request parameter.
HttpSession getSession()Returns current session; creates one if it does not exist.
Object getAttribute(String name)Returns value of a request attribute set on server-side.
Enumeration<String> getAttributeNames()Returns all attribute names stored in request scope.
String getContentType()Returns MIME type of the request body.
String getMethod()Returns HTTP method used (GET, POST, etc.).

Example to Display HTTP Request Headers Using Servlet

Java
package com.headers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Headers")
public class Headers extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Headers() {
        super();
    }

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println(
            "<body bgcolor=\"#FF5732\">"
            + "<h1 align='center'>Display Header Request</h1>"
            + "<b>Request Method:</b> " + request.getMethod() + "<br>"
            + "<b>Request URI:</b> " + request.getRequestURI() + "<br>"
            + "<b>Request Protocol:</b> " + request.getProtocol() + "<br><br>"
            + "<table border='1' align='center'>"
            + "<tr bgcolor=\"#FFAD00\">"
            + "<th>Header Name</th><th>Header Value</th>"
        );

        Enumeration<String> headerNames = request.getHeaderNames();

        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            out.println("<tr><td>" + headerName + "</td>");
            out.println("<td>" + request.getHeader(headerName) + "</td></tr>");
        }

        out.println("</table></body></html>");
    }
}

Explanation: This servlet uses HttpServletRequest to read all HTTP request headers sent by the client and displays them in a formatted HTML table. It also shows basic request details like method, URI, and protocol. This helps in understanding how client request data is received and processed on the server side.

Comment