File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes expire cookie Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "expire cookie" Watch "expire cookie" New topic
Author

expire cookie

arona kosari
Greenhorn

Joined: Oct 28, 2006
Posts: 19
hi every body
I want to know how we can expire cookie.
SetMaxAge(0) expires them if browser closes.but I want to expire them when the browser exists.
Thanks a lot
Arona
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41052
    
  43
What do you mean by "when the browser exists"? Is that a typo and you mean "exits"? That would be the same as "closes".


Ping & DNS - my free Android networking tools app
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Clear cookie value before setting max age 0.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
arona kosari
Greenhorn

Joined: Oct 28, 2006
Posts: 19
hi dears
I read somewhere:
A zero value indicates that the cookies are temporary and will be destroyed once the client closes the browser.
But for example in shopping project,user does not want to close the browser and he log out and login again,and I think with setMaxAge(0) the cookies for previous user exist too.
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472


setMaxAge

public void setMaxAge(int expiry)

Sets the maximum age of the cookie in seconds.

A positive value indicates that the cookie will expire after that many seconds have passed. Note that the value is the maximum age when the cookie will expire, not the cookie's current age.

A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.

Parameters:
expiry - an integer specifying the maximum age of the cookie in seconds; if negative, means the cookie is not stored; if zero, deletes the cookie
arona kosari
Greenhorn

Joined: Oct 28, 2006
Posts: 19
Hi Dear D Rog,
I set maxAge to zero, but it does not work.
When user purchase what he selected to buy, the maxAge of all cookies will be set to zero.and when another user login and select some goods, his choice will be added to the chioce of previous user and show all of them.
I have still problem with them.
Thanking you in anticipation
Arona
Tony Burleson
Greenhorn

Joined: Dec 31, 2006
Posts: 6
In addition to expiring a cookie, you can also set the value of the cookie to some useless value, say myvalue=cleared. This is only to add a "peace of mind" for management and has no real business value.

If both the expiration and the value are not being set, then that is out of your control and the problem is with the browser not changing as requested.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you only want the cart to be active for one user's login duration, why are you using cookies? Why aren't you using session objects?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
arona kosari
Greenhorn

Joined: Oct 28, 2006
Posts: 19
Dear Tony,Thanks a lot,I set it's value to empty string too,But it does not work.
Dear Ben,yes,you are right,but it was one of my assignment to work with cookies and it's features.But as you said session is better than cookies.
Regards
Arona
William Janssen
Ranch Hand

Joined: Jan 02, 2007
Posts: 39
Besides the session on the server which gives a better solution.
You can handle more users from one browser and maintain state over "log outs" easily by placing the name of the cookies is some kind of namespace.
e.g. item="My Book" will be A5EE62_item="My Book"
where the namespace is based on a sessionid or the users loginname (BAEE64 encoded ofcourse) or some hash code over the login name or....
But be aware of the possible security issues since everything is handled on the client side which can be manipulated by everyone.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
when another user login and select some goods, his choice will be added to the chioce of previous user and show all of them.


If you are talking about another user on another browser - that sounds like improper use of servlet instance variables instead of sessions.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: expire cookie
 
Similar Threads
setMaxInactiveInterval and <session-timeout> question
browser check alert with cookie
Sessions
Cookie lifetime
Form Resubmissions