Retrieve HTTP Request Headers using JSP

When a HTTP client sends a request, it is required to supply GET or POST. It can send a number of headers.

Retrieve HTTP Request Headers using JSP

Retrieve HTTP Request Headers using JSP

     

This section illustrates you how to retrieve the request headers.

When a HTTP client sends a request, it is required to supply GET or POST. It can send a number of headers. Here are some headers:

 

 

 

 

 

Accept - MIME type, the browser prefers.
Accept-Charset - The Character set the browser expects.
Accept-Language - The language which the browser is accepting.
Connection - If a servlet gets a keep-Alive value or gets a 
request line indicating HTTP 1.1, it may be able to take advantage of connection.
Host (host and port as defined in url).
Content-Length (for POST messages).
User-Agent (type of browser, if servlet is returning browser-specific content).

We are providing you an example which explains you clearly.

Here is the code of headers.jsp

<%@ page import="java.util.*" %>
<html>
<head>
<title>Http Request Headers Example</title>
</head>
<body>
<h2>HTTP Request Headers Received</h2>
<table>
<%
Enumeration enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
String name = (String) enumeration.nextElement();
String value = request.getHeader(name);
%>
<tr><td><%= name %></td><td><%= value %></td></tr>
<%
}
%>
</table>
</body>
</html>

In the above example, we have created an object of class Enumeration by calling the method getHeaderNames() of the request object. This method provides the different header names and their information.

Output will be displayed as

Download Source Code