Http Request Headers Display in a JSP Page


Http Request Headers Display in a JSP page

A JSP page can access the information in the request header by using the implicit object request. An Http client sends a request, it sends the request in the form of get or post method or any other Http Request methods. It can also sends the headers with it.

Here is the list of most commonly used headers are
 

Accept The MIME types the browser prefers.
Accept- Encoding The types of data encodings the browser knows how to decode.
Accept- Charset The character set the browser expects.
Content-Length It is mostly used for POST messages, it tells how much data is attached.
Cookie It is one of the most frequently used headers, it returns the cookies.
Accept- Language The language the browser is expecting.

There are many another headers , here we are going to develop an application in JSP to show the request headers.
For developing the application we follow the following steps

Steps 1: Create a new web application

First we use an web application in java web and click on the next button

selet new web page.jpg

Steps 2: Name and Location

In this step we use a specific name and location to the project

name and location.jpg

Step 3: Select server and setting

In this step we select a specific server GlassFish 3.1 and java EE 6 version

select serrver and setting it.jpg

Step 4: Select Framework

There is no need to select any framework for this application

selectFramework.jpg


Step 5: Create a new jsp file

In this step we create a new jsp file index.jsp.

create new jsp file.jpg

Index.jsp

<%@ page import="java.util.*" %>
<html>
      <body bgcolor="cyan">
           <h1>HTTP Request Headers Received</h1>
           <table border="1" cellpadding="4" cellspacing="0">
           <%
               Enumeration eNames = request.getHeaderNames();
               while (eNames.hasMoreElements())
               {
               String name = (String) eNames.nextElement();
               String value = normalize(request.getHeader(name));
            %>
           <tr><td><%= name %></td><td><%= value %></td></tr>
           <%
               }
           %>
           </table>
       </body>
</html>

           <%!
                private String normalize(String value)
               {
                 StringBuffer sb = new StringBuffer();
                 for (int i = 0; i < value.length(); i++) {
                 char c = value.charAt(i);
                 sb.append(c);
                 if (c == ';')
                 sb.append("<br>");
                }
                return sb.toString();
                }
            %>

Compiling and Running the application

Now we first compile it and run it on server and then we will see the output as:

Output

index.jsp

In this application we will get the information about all the headers, and the information which we will get from this example. Through this example we are going to retrieve all the headers available in the request object. We can retrieve these by using the Enumeration interface of the java.util package. Use while condition to check whether there are more headers or not. And at last print the headers on the browsers by using the out implicit object. 

index.jsp.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all