JSP Response containsHeader

JSP Response containsHeader


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

In this tutorial you will learn about the response containsHeader() method in JSP.

JSP Response containsHeader

In this tutorial you will learn about the response containsHeader() method in JSP.

containsHeader() method of HttpServletResponse interface is used for checking whether the specified header is already existed or not. This method may be used when you are trying to add response header and you are confused whether the header you want to set or add is already existed or not.

Syntax :

boolean containsHeader(String name)

Argument of this method is a String that specifies the name of header you want to check for its existence.

Example :

Here I am giving a simple example which will demonstrate you about the use of containsHeader() method. In this example I have created a JSP page where write the java code using the scriptlet and just used the method first for checking the header name for its existence and then added a header using addHeader() method with that name and again checked for the existence of header name using containsHeader() method.

jspResponseContainsHeader.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 containsHeader</title>
</head>
<body>
<%! String hName = "URL"; %>
<p>Whether the header name <b><%=hName %></b> is available :
<% out.println(response.containsHeader(hName));%></p>
<%
response.addHeader(hName, "www.devmanuals.com");
out.println("Header is added to the response");
%>
<p>Whether the header name <b><%=hName %></b> is available :
<% out.println(response.containsHeader(hName));%></p>
</body>
</html>

Output :

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

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics