Client HTTP Request in Servlet

In this article, we will discuss the Client HTTP Request. As we know Client(browser) requests the web page from the server it not only sends the request but also sends lots of information along with it. So this information is sent in the header part of the request. so, we will see what are these headers.

What is HTTP?

HTTP is a stateless, TCP/IP protocol that is used to deliver data over the World Wide Web. It is used as an extension of the request method, error, and headers. It is used to established communication between client and server over the web.

What is HTTP Request?

The message sent by the client to the server is HTTP Request. The request is included within the first line of the message, the method to be applied, the identifier, the protocol used.

Request Line: The Request-Line begins with a method Followed by Request URI and protocol version here SP is used for space and CRLF is used to terminate a line in HTTP.

Request-Line = Method SP Request-URI SP HTTP-Version CRLF

Request Method: This identifies the method to be used on the request.

  • GET
  • HEAD
  • POST
  • PUT

Request URI: It is the Uniform Resource Identifier for the request.

  • Request URI= '*'|absoluteURI|abs_path|authority

What is Client HTTP Request?

When a client(web browser) sends a request to a server it not only sends the request but a lot of information in the header of the HTTP Request. The following are the header and information related to it.

  • Accept: The MIME type the browser prefers.
  • Accept charset: The character set browser prefers.
  • Accept-Encoding: The type of data encoding the browser prefers.
  • Accept-Language: The language browser/client uses.
  • Authorization: It is used by the client to identify themselves while accessing secured pages.
  • Connection: It indicates whether the client can handle the persistent request
  • Content-length: It gives the size of the post data in is applicable to POST requests only.
  • Cookie: This Header returns cookies that were previously sent to them.
  • Host: This header gives us the host and port in the URL.
  • If modified-since: It indicates that the client wants the pages only if has been modified after a specified date.
  • If Unmodified-since: It is the opposite of If modified-since. it tells that the operation should succeed only if the document is older than the specified date.
  • Referrer: This indicates the URL of the referring page.
  • User-Agent: This header identifies the client making the request and is used to send different contents to different browsers.

Methods for HTTP Request Header

Let’s see the following methods that can be used to read the HTTP header in servlet.

  • Cookie[] getCookies(): This method returns the array of cookie that is sent along with the request.
  • Enumeration getAttributeNames():This method returns that name of attribute of request.
  • Enumeration getHeaderNames(): This method returns the name of all the headers that requests contain.
  • Enumeration getparametersNames(): This method returns the names of all the parameters that the request contains.
  • HttpSession getSesion():This method returns the current session of the request.if it doesn’t have any it create one for it.
  • HttpSession getSession(boolean create):This method return HTTP session along with request.
  • Locale getlocale(): This method returns the locale that client uses.
  • Object getAtrribute(String name): This returns the object of the named attribute.
  • ServletInputStream getInputStream(): This returns the body of the request in the form of binary data.
  • Sring getAuthType(): This method returns the authorization used to protect the servlet.
  • String getCharacterEncoding(): This returns the Character Encoding name used in the request body.
  • String getContentType(): This returns the MIME type of request.
  • String getContextPath(): This returns the path of URI that indicates the context of the request.
  • String getHeader(String name): This returns the specified request header.
  • String getMethod():This returns the HTTP method of request.
  • String getParameter(String name): This returns the value of the request parameter as a string.
  • String getPathInfo(): This returns path information associated with the URL.
  • String getprotocol(): This returns the name and version of the protocol used in the request.
  • String getQueryString(): This method returns the query string.
  • String getRemoteAddr(): This method returns the IP address of the client who made the request.
  • String getRemoteHost(): This method returns the fully qualified name of the request.
  • String getRemoteUser(): This method returns the login of the client who made the request.
  • String getRequestURI(): This method returns the request is present in the first line of the request.
  • String getRequestedSessionId(): This method returns the session id of the client.
  • String getServletPath(): This method returns the path of the request URL.
  • String [] getParameterValues(String name): This method returns the array of parameter values associated with the request.
  • boolean isSecure(): This method returns boolean, indicates whether the request was using a secured channel like https.
  • int getContentLength(): This method returns the length of the request body in bytes.
  • int getIntHeader(String name): This method returns the values of request Header as an integer.
  • int getServerPort(): This method returns the port number on which request.

 Example of HTTP Request Headers

Following is the Example of HTTP Request Headers. Here we have used getHeaderNames() method which will return the names of the header as an array. So we have used Enumeration so that we can loop it in the standard format.

package com.headers;

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;

 * Servlet implementation class RequestHeaders
public class RequestHeaders extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public RequestHeaders() {
    // TODO Auto-generated constructor stub

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    // response.getWriter().append("Served at:
    // ").append(request.getContextPath());
    PrintWriter out = response.getWriter();
    String str = "Display Request Header";
    out.println("<BODY BGCOLOR=\"#FF5733\">\n" + "<H1 ALIGN=CENTER>" + str + "</H1>\n"
        + "<B><center>Request Method: <center></B>" + request.getMethod() + "<BR>\n" + "<B>Request URI: </B>"
        + request.getRequestURI() + "<BR>\n" + "<B>Request Protocol: </B>" + request.getProtocol()
        + "<BR><BR>\n" + "<TABLE BORDER=1 ALIGN=CENTER>\n" + "<TR BGCOLOR=\"#FFAD00\">\n"
        + "<TH>Header Name<TH>Header Value");

    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
      String headerName = (String) headerNames.nextElement();
      out.println("<TR><TD>" + headerName);
      out.println("<TD>" + request.getHeader(headerName));

In the next article, we will see what we get as a response in Servlet i.e Server HTTP Response.