1

I have a site name www.goo.com and i used to save cookie without declaring domain name when creating the cookie. Now, i want to add sub domain foo.goo.com I save the cookie in the domain and not in the sub domain. There are 2 problems:

  1. I want to delete the old cookie. I tried to do 'cookie.setMaxAge(0)' already and the problem is that in Firefox and chrome it do not work. it work only in IE. why? and what i can do about it?

  2. When i do

    Cookie[] cookie = request.getCookies();
    if (cookie != null) {
       for (int i = 0; i < cookie .length; i++) {
           cookie[i].getDomain();   //here i get null.why?
       }
    }
    

Why i get null in the getDomain() line?

Edit:

i tried to add this line:

response.setContentType("text/html");

to send the content type.

I also tried to send the cookie through the response.

Someone have another idea, for the 2 problems the presents here?

4
  • this cookie is set... so he have the domain.
    – lolo
    Feb 19, 2012 at 16:10
  • Tried sending the cookie through the response? response.setHeader("Set-Cookie", name + "=" + value + "; Max-Age=" + maxAge + "; expires=" + expiresTime); Feb 19, 2012 at 16:22
  • its the same like to do addcookie right? what is expires?
    – lolo
    Feb 19, 2012 at 16:37
  • Yes, it's the same. About max-age and expiresen.wikipedia.org/wiki/HTTP_cookie#Expires_and_Max-Age, just use one of them.. Feb 19, 2012 at 16:49

1 Answer 1

3
  1. Answered here: How do you remove a Cookie in a Java Servlet (you need to send a text/html content type)

  2. When a client sends cookies to the server, it only sends the name/value. The other fields are only available when setting the cookie.

1
  • The second answer should be correct. As for the first, I have never used java servlets and I have no clue why Firefox and Chrome disobey the expire field. A possible issue (IIRC) is that all fields (path, domain, secure, etc) must match for the cookie to be erased rather than creating another cookie that is deleted immediately.
    – AlexanderZ
    Feb 21, 2012 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.