File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Setting cookie takes time? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Setting cookie takes time?" Watch "Setting cookie takes time?" New topic

Setting cookie takes time?

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
In my login servlet I make a test if it is possible to set add a cookie on the users computer. Only if it is possible to add the cookie the user is allowed to login the system.
If I have turned on the cookie settings on my computer and I try to login, the I a�m rejected first time but second time I can login the system.
I use this code to test if the users computer has enabled cookie settings.
cookie = new Cookie("test" , "test" );
cookie.setMaxAge( 100 );
cookie.setPath ("/");
response.addCookie( cookie );

for ( int i = 0 ; i < cookies.length ; i++ ){

cookie = cookies[ i ];
c_name = cookie.getName();
if ( c_name.equals( sheme_test ) ) {
cookieSettings = true;

if ( cookieSettings ){
else {
It seems that the problem is that the the program is to slowly with ading the cookie and because of that it can�t find the cookie first time but only second time.
What is the problem and how can I solve it?
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
your understanding of the entire mechanism seems flawed.
A cookie is NOT set on the client the instant you set it in your servlet code.
The actual cookie is only set when the servlet returns to the client the result of the request.
You should not test if the cookie can be written during login, it's pointless.
If the client doesn't let the cookie be written that's its problem, not yours.
They'll just fail to give back the cookie content during the next request at which point you can tell them what's going on.

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Yes you are right ;-) Thank you
I agree. Here's the link:
subject: Setting cookie takes time?
It's not a secret anymore!