Client HTTP Request in JSP

In the previous article, we have learned about custom tags means how we can customize a tag for our own operation. In this article, we will discuss client requests in JSP.

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 JSP.

  • 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

We will be using different methods using request object in JSP. Following is the Example of HTTP Request Headers. Here we have used the 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.

<%@page import="java.util.Enumeration"%>
<%@page import="java.util.Locale"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>HTTP Client Request</title>
  <h3 align="center">JSP- HTTP Client Request</h3>
  <table border="1" bgcolor="#8EEBEC">
      <th>Header value</th>
      HttpSession httpSession = request.getSession();
      out.print("<tr><td> Session Name </td><td>" + httpSession + "</td></tr>");
      Locale locale = request.getLocale();
      out.print("<tr><td> Locale Name </td><td>" + locale + "</td></tr>");
      String path = request.getPathInfo();
      out.print("<tr><td>Path Name is</td><td>" + path + "</td></tr>");
      String cpath = request.getContextPath();
      out.print("<tr><td>Context path is</td><td>" + cpath + "</td></tr>");
      String sname = request.getServerName();
      out.print("<tr><td>Server Name is </td><td>" + sname + "</td></tr>");
      int portname = request.getServerPort();
      out.print("<tr><td>Server Port is </td><td>" + portname + "</td></tr>");
      Enumeration hnames = request.getHeaderNames();
      while (hnames.hasMoreElements()) {
        String paramName = (String) hnames.nextElement();
        out.print("<tr><td>" + paramName + "</td>");

        String paramValue = request.getHeader(paramName);
        out.println("<td> " + paramValue + "</td></tr>");



Thus, in this article, we have discussed what happens when a client requests a web page to server. We have seen all the important header information.

In the next article of this tutorial, we will cover what we will get a response in JSP i.e HTTP response in JSP with a simple example.