JSP Response addHeader

JSP Response addHeader


Posted in : Java Posted on : May 10, 2012 at 10:36 AM Comments : [ 0 ]

In this tutorial you will learn about how to add header to the response in JSP.

JSP Response addHeader

In this tutorial you will learn about how to add header to the response in JSP.

Header is an useful part of response. It consists various informations using which it does various tasks like specifying cookies, instructing the browser to reload the page into a specified interval and many more tasks. Headers can be specified using the addHeader() method of HttpServletResponse interface however it can also be specified by the setHeader() method of same interface. In case of addHeader() method a new header is added if a header is already existed with that name. To check whether a header is set or not use containsHeader().

Syntax :

void addHeader(String name, String value)

First argument specifies the name of header and the second argument specifies the value of that header.

Example :

Here the example is being given will demonstrate you about how to add response header. In this example I have created a JSP page where designed a form to take the input from user where user can add header and their value. Then used the containsHeader() method to check whether the header name is given by user is already existed or not, if this statement returns false then the specified header name will be added using addHeader() method. Further I have checked again for the existence of specified header name.

jspResponseAddHeader.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.net.URL, 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 addHeader</title>
</head>
<body>
<form>
<table>
<tr>
<td>Enter name of header to add : </td>
<td><input type="text" name="hName"/></td>
</tr>
<tr>
<td>Enter value for the above header :</td>
<td><input type="text" name="hValue"/></td>
</tr>
<tr>
<td></td><td><input type="submit" value="submit"/></td>
</tr>
</table>
</form>
<%
String hName = request.getParameter("hName");
String hValue = request.getParameter("hValue");

if(hName != null)
{
boolean bol = response.containsHeader(hName);
%>
<p>Whether header name <b><%=hName %></b> is available :
<% out.println(bol);%></p>
<%
if(bol== false)
{
response.addHeader(hName, hValue);
out.println("Header is added to the response");
}
else
{
out.println("Specified header is already existed");
} %>
<p>Whether header name <b><%=hName %></b> is available :
<% out.println(response.containsHeader(hName));%></p>
<%
}
%>
</body>
</html>

Output :

When you will execute the above JSP page output will be as follows :

1. At first a page will be displayed to you to input the values

2. When you will click on submit button after input the value like above then the output will be :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics