In this tutorial you will learn about the JSP response getHeaders().
JSP Response getHeaders
In this tutorial you will learn about the JSP response getHeaders().
getHeaders() method of HttpServletResponse interface returns the value of the headers that you have set/add using the setter/adder method of this interface for example setHeader(), addHeader(). In case if the specified header is not set then it may returns the empty values. This method is introduced in the Servlet 3.0.
Syntax :
public java.util.Collection<java.lang.String> getHeaders(java.lang.String name)
Argument of this method is the name of header which values have to be found. It returns the values as a collection.
Example :
An example is being given here for you to demonstrate about the use of getHeaders() method. In this example I have created a JSP page where created a Date object to display the date. Then format these date objects into a specified format using SimpleDateFormat constructor. Then added the headers where I have used one setHeader() method to add the Refresh header, and rest of the other headers is added through the addHeader() method. setHeader() method replaces the existed header value with the current value of header is added and the addHeader() method adds a new value to the header it doesn't matter the value of header is to be added has already been assigned a value or not.
jspGetHeaders.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" import="java.util.*, java.text.Format, java.text.SimpleDateFormat"%> <!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 getHeaders</title> </head> <body> <% Date date = new Date(); Format formatter= new SimpleDateFormat("dd.MM.yyyy.HH.mm.ss"); Format formatter1= new SimpleDateFormat("dd/MM/yyyy"); Format formatter2= new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); String newDate = formatter.format(date); String newDate1 = formatter1.format(date); String newDate2 = formatter2.format(date); response.setHeader("Refresh", "1"); response.addHeader("Date", newDate); response.addHeader("Date", newDate1); response.addHeader("Date", newDate2); Collection<String> collection = response.getHeaders("Date"); Iterator<String> itr= collection.iterator(); %> <table border="1"> <tr><td align="center"><b>Date</b></td></tr> <% while(itr.hasNext()) { %> <tr><td> <%=itr.next()%> </td></tr> <% } %> </table> <% out.close(); %> </body> </html>
Output :
When you will execute the above JSP page an output will displayed as follows :
In the above output you can see clearly there are three different format of date. These different formats of date are added into the header with the same header name but with different values. So this method returned different values. In case you will use the setHeader() method then it will display the last value you have set using setHeader() method.
[ 0 ] Comments