JSP Header Request Example

JSP Header Request Example


Posted in : Java Posted on : May 8, 2012 at 7:10 PM Comments : [ 0 ]

In this tutorial you will learn about how to get the header information the client has sent as a request.

JSP Header Request Example

In this tutorial you will learn about how to get the header information the client has sent as a request.

Header Request is a request information that the user make request to the server. To find out the header names a getHeaderNames() method of request object is used. And to find out the values of header a getHeaders() method of request object is used. Both of these methods returned the value as an Enumeration so, to take the actual values we will required to iterate over all of them returned as Enumeration. Here I am giving a simple example which will demonstrate you about getting the header information.

Example :

jspHeaderRequest.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.Enumeration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Header Request</title>
</head>
<body>
<h4>Header Request Information</h4>
<table border="1">
<%! Enumeration enm, header;
String hName, hValue;
%>
<%
enm = request.getHeaderNames();
while (enm.hasMoreElements()) {
hName = (String) enm.nextElement();
header = request.getHeaders(hName);
if(header != null)
{
while(header.hasMoreElements())
{
hValue= (String) header.nextElement();
}
}
%>
<tr><td><%= hName %></td><td><%= hValue %></td></tr>
<%
}
%>
</table>
</body>
</html>

Output :

When you will execute the above JSP page the header information may display as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics