• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cookie expiry

 
Karan Tyagi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating a cookie through a servlet for a html page and am specifying setMaxAge(120) i.e 120 seconds or two minutes, so accordingly after two minutes the cookie should automatically expire. But it persists and does not expire. Please tel me why is this so?
 
Preethi M
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karan!
When we set the expiry time for a cookie it will definetly expire.when u try to retrive the cookie ,if the cookie is not existing it will give an exception.Can u give me in detail how u have checked out that the cookie still persists.
Regards
Preethi M
 
Karan Tyagi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Preethi,
Well after i have created the cookie and accessed the HTML page, I again access another HTML page in which I am retrieving the value of the cookie. I have put in the relevent try/catch blocks to catch an exception, in case no cookie is retrieved. But even after two minutes (wrt setMaxAge(120)) or even after 20 minutes the cookie does not expire. And when I access the HTML page in which I am retrieving the cookie value, it does so. Hence the cookie is not destroying automatically. Help me out!!
Karan
 
Preethi M
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Karan,
When the cookie's expiry time is set it should and will expire.I have tried it out and it worked fine.If u could send me ur code maybe i can help u out.
Thanx
Preethi M
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Ranch has a naming policy, described here and "Preethi M" is not a valid name. Please choose one which meets the requirements.
Thanks.
 
Karan Tyagi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Preethi,
Here is the code..

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class CookieExample extends HttpServlet {
private String names[] = { "C", "C++", "Java",
"Visual Basic 6" };
private String isbn[] = {
"0-13-226119-7", "0-13-528910-6",
"0-13-012507-5", "0-13-528910-6" };
public void doPost( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException
{
PrintWriter output;
String language = request.getParameter( "lang" );
Cookie c = new Cookie( language, getISBN( language ) );
c.setMaxAge( 120 ); // seconds until cookie removed
response.addCookie( c ); // must precede getWriter

response.setContentType( "text/html" );
output = response.getWriter();
// send HTML page to client
output.println( "<HTML><HEAD><TITLE>" );
output.println( "Cookies" );
output.println( "</TITLE></HEAD><BODY>" );
output.println( "<P>Welcome to Cookies!<BR>" );
output.println( "<P>" );
output.println( language );
output.println( " is a great language." );
output.println( "</BODY></HTML>" );
output.close(); // close stream
}
public void doGet( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException
{
PrintWriter output;
Cookie cookies[];

cookies = request.getCookies(); // get client's cookies
response.setContentType( "text/html" );
output = response.getWriter();
output.println( "<HTML><HEAD><TITLE>" );
output.println( "Cookies II" );
output.println( "</TITLE></HEAD><BODY>" );
if ( cookies != null ) {
output.println( "<H1>Recommendations</H1>" );
// get the name of each cookie
for ( int i = 0; i < cookies.length; i++ )
output.println(
cookies[ i ].getName() + " How to Program. " +
"ISBN#: " + cookies[ i ].getValue() + "<BR>" );
}
else {
output.println( "<H1>No Recommendations</H1>" );
output.println( "You did not select a language or" );
output.println( "the cookies have expired." );
}
output.println( "</BODY></HTML>" );
output.close(); // close stream
}
private String getISBN( String lang )
{
for ( int i = 0; i < names.length; ++i )
if ( lang.equals( names[ i ] ) )
return isbn[ i ];
return ""; // no matching string found
}
}
And these are the corresponding HTML codes.. for creating and retrieving the Cookie values...
<HTML>
<HEAD>
<TITLE>Cookies</TITLE>
</HEAD>
<BODY>
<FORM ACTION="http://localhost:8080/servlet/CookieExample"
METHOD="POST">
<STRONG>Select a programming language:<br>
</STRONG><BR>
<PRE>
<INPUT TYPE="radio" NAME="lang" VALUE="C">C<BR>
<INPUT TYPE="radio" NAME="lang" VALUE="C++">C++<BR>
<INPUT TYPE="radio" NAME="lang" VALUE="Java"
CHECKED>Java<BR>
<INPUT TYPE="radio" NAME="lang"
VALUE="Visual Basic 6">Visual Basic 6
</PRE>
<INPUT TYPE="submit" VALUE="Submit">
<INPUT TYPE="reset"> </P>
</FORM>
</BODY>
</HTML>

Retrieving:
<HTML>
<HEAD>
<TITLE>Cookies</TITLE>
</HEAD>
<BODY>
<FORM ACTION="http://localhost:8080/servlet/CookieExample"
METHOD="GET">
Press "Recommend books" for a list of books.
<INPUT TYPE=submit VALUE="Recommend books">
</FORM>
</BODY>
</HTML>
Thanx for your time.
Karan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic