You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Shravan Shashikant <s0...@unix.tamu.edu> on 2001/04/25 21:15:20 UTC

Cookie Problem

Hi there,
   I have a problem using Cookies in my servlet.I am able to create the
cookie and log in but then I have an option where I ask the user if he
wanst to save his password and there I write a cookie for say 9 days.But
when he wnts to logut ,I execute the following code.
         out= response.getWriter();    

       HttpSession session = request.getSession(true);
       session.invalidate();


            Cookie[] cookies = request.getCookies();

            for(int i=0; i < cookies.length; i++) {
                Cookie thisCookie = cookies[i];
               String name = cookies[i].getName();
               String value = cookies[i].getValue();
               //out.println("  " + name + " : " + value);
                if (thisCookie.getName().equals("user")) {
                    // Delete the cookie by setting its maximum age to
zero
                    //out.println(" Before deleting user " );
                    Cookie killcookie = new Cookie("user",null);
                    //Cookie killcookie = new
Cookie("user",thisCookie.getValue());
                    thisCookie.setMaxAge(0);
                    cookies[i].setMaxAge(0);
                    killcookie.setMaxAge(0);
                    //killcookie.setPath("/");
                    response.addCookie(killcookie);
                    killcookie.setMaxAge(0);
                    response.addCookie(killcookie); //merciless!!!
                    out.println(" new - After deleting user " );
                }
            }       

The weird thing is I am not able to deleet the cookies in IE 5 by using
the above method.I can still see the cookies which I had created,in the
Cookies folder.They are not deleted.I heard somewhere that it takes 4-5
mins to delete cookies in IE.But then its been a day now and I know
Windows's slow but then this just takes the cake.AM I doing something
wrong here(it seems to work fine with NEtscape and in other versions of
IE). If you've an efficient code or mechanism on how to tackle the Logout
problem which's really bugging me,do tell me.
  Also,if setMaxAge is not used while creating the cookie then its not
written to the Cookies folder.DOes it exist only in the browser header in 
this case? 
Thanks and Regards,
Shravan