JSP Delete Cookie

JSP Delete Cookie


Posted in : Java Posted on : May 8, 2012 at 7:30 PM Comments : [ 0 ]

In this tutorial you will learn about how to deleted cookie in JSP.

JSP Delete Cookie

In this tutorial you will learn about how to deleted cookie in JSP.

After how long a cookie will be expired/deleted is depend on the age of cookie. Age of cookie can be set by a setMaxAge() method of Cookie class. Argument of this method specifies the age of cookie expiry. Age of cookie is set in seconds. To delete an existing cookie you may have to first get an existing cookie from the request object and need to be stored in the Cookie object then age of cookie is required to set to 0 (zero) using the setMaxAge() method and again set this cookie into response header.

Syntax  : setMaxAge()

public void setMaxAge(int expiry)

Example :

Here an example is given below will demonstrate you about how to delete cookies in JSP. For this at first I have created two JSP pages one of them named addCookie.jsp for adding and retrieving cookies and the another one named deleteCookie.jsp for retrieving the existed cookies and then delete that existing cookies. To delete an existed cookie at first I have retrieve the cookies using getCookies() method of HttpServletRequest interface then kept them into cookie's object and then set the maximum age of cookie is 0 (zero) using setMaxAge() method of Cookie class, this will delete the existed cookie and then again set the cookie to the response header.

addCookie.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="javax.servlet.http.Cookie" %>
<!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>Add Cookies In JSP</title>
</head>
<body>
<form>
<table>
<tr>
<td>Enter Your Name :</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td>Enter Your Age :</td>
<td><input type="text" name="age"/></td>
</tr>
<tr>
<td></td><td><input type="submit" value="submit"/></td>
</tr>
</table>
</form>
<%
String nm = request.getParameter("name");
String ag = request.getParameter("age");

if(nm != null)
{
String trim = ag.trim();
int a = Integer.parseInt(ag);
%>
<table>
<tr>
<td>Name = </td> <td><%=nm %></td>
</tr>
<tr>
<td>Age = </td> <td><%=a %></td>
</tr>
</table>
<%
Cookie nCookie = new Cookie("nam", nm);
//Name cookie add to the response header
response.addCookie(nCookie);
}
%>

<%
Cookie cookie = null;
Cookie[] cookieArray = request.getCookies();
if(cookieArray != null)
{
%>
<p><b>Cookies Information</b></p>
<%
for(int i=0; i<cookieArray.length; i++)
{
cookie = cookieArray[i];
out.println("CookieName : " + cookie.getName()+"<br>");
out.println("CookieValue : " + cookie.getValue()+"<br>"); 
}
}
else
{
out.println("Cookies is not added in the response");
}
%>
</body>
</html>

deleteCookie.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="javax.servlet.http.Cookie" %>
<!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>Delete Cookies In JSP</title>
</head>
<body>
<%
Cookie cookie = null;
// Cookie deleteCookie = null;
Cookie[] cookieArray = request.getCookies();
if( cookieArray != null )
{

for (int i = 0; i < cookieArray.length; i++)
{
cookie = cookieArray[i];
// deleteCookie = new Cookie(cookie.getName(),null);
// deleteCookie.setMaxAge(0);
// response.addCookie(deleteCookie);
// out.print("Deleted cookie: " +
// deleteCookie.getName( ) + "<br/>"); 
cookie.setMaxAge(0);
response.addCookie(cookie);
out.print("Deleted cookie : "+ cookie.getName()+"<br>");
}
}
else
{
out.println("cookies Not Found");
}
%>
</body>
</html>

Output :

1. When you will made the request first time for addCookie.jsp page output will be as follows :

2. When you will click on submit button after filling the value in text box like above then the output will be as follows :

3. When you will execute the deleteCookie.jsp page first time output will be as follows :

4. When you will again made request for the deleteCookie.jsp page output will be as follows :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics