JSP Response getHeaderNames

JSP Response getHeaderNames


Posted in : Java Posted on : May 15, 2012 at 11:51 AM Comments : [ 0 ]

In this section we will discussed about the getHeaderNames() method.

JSP Response getHeaderNames

In this section we will discussed about the getHeaderNames() method.

getHeaderNames() method of HttpServletResponse interface returns the name of headers as a collection. In this collection names of the headers, added either by a setter or adder method of HttpServletResponse interface, are held. In case if headers are not added then the collection may return empty. This method had been introduced in Servlet 3.0 version.

Syntax :

public java.util.Collection<java.lang.String> getHeaderNames()

This method returns the name of header as a String.

Example :

Example in this section describes how to use getHeaderNames() in JSP. In this example I have created a JSP page where added headers to the response using addHeader() and setHeader() method then used the getHeaderNames() to get the header's name. If no any header is added then it may returns the empty.

jspResponseGetHeaderNames.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" import="java.util.*"%>
<!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 Response getHeaderNames</title>
</head>
<body>
<%
response.addHeader("Date", "14/05/2012");
response.addHeader("URL", "www.devamanuals.com");
response.setHeader("Author", "Deepak");
Collection<String> collection= response.getHeaderNames();
Iterator<String> itr= collection.iterator();
%>
<table border="1">
<tr><td align="center">Header Names</td></tr>
<%
while(itr.hasNext())
{
%>
<tr><td><%=itr.next()%></td></tr>
<%
}
%>
</table>
</body>
</html>

Output :

When you will execute the above JSP page you will get the following output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics