Request header display in a jsp page

This is detailed jsp code that shows how to display request header information in a jsp page. When user request to the server, the request defines an object to provide client request information to a jsp.

Request header display in a jsp page

Request header display in a jsp page

     

This is detailed jsp code that shows how to display request header information in a jsp page. When user request to the server, the request defines an object to provide client request information to a jsp. In this example, request_header_jsp.jsp is used to display header information on the page. Request object's getMethod() method returns request method type i.e. GET or POST and getHeaderNames() method returns header names as an enumeration which can be iterated to get each of them.

request_header_jsp.jsp

<HTML>
<HEAD>
    <TITLE>Information about request header</TITLE>
</HEAD>
<BODY bgcolor="#ffffcc">
  <font size="+3" color="green"><br>Welcome in www.roseindia.net !</font>
  <font size="+2" color="#7E354D"><br>Information about request header</font>
  <br>
    <TABLE style="background-color: #ECE5B6;" WIDTH="30%" border="1">
	<tr>
	    <th>method used to send request</th>
           <!-- getMethod() returns the name of the HTTP 
method with which this request was made,
	     for example, GET, POST, or PUT -->
	     <td><%= request.getMethod() %></td>
	</tr>
	<tr>
	    <th>URI of the request</th>
      	    <!-- getRequestURI() returns the part of this request's URL -->
	    <td><%= request.getRequestURI() %></td>
	</tr>
	<%
	 /*This method returns an enumeration of all the header names this 
          request contains.*/	    
         java.util.Enumeration names = request.getHeaderNames();
         while (names.hasMoreElements()) {
	 String hname = (String)names.nextElement();
	%>
	<tr>
	    <th> <%= hname %> </th>
     		<!-- This method returns the value of the 
specified request header as a String. -->
		<td><%= request.getHeader(hname) %></td>
	</tr>
   <%
        }
   %>
 </body> 
</html>

Save this code as a .jsp file named "request_header_jsp.jsp" in your application directory in Tomcat , 'user' for this example, and run this jsp page with url http://localhost:8080/user/request_header_jsp.jsp in address bar of the browser.

Download Source Code