9

I use the Cookies class of GWT to generate cookies.

When I use the following

Cookies.setCookie(LOGIN_COOKIE_NAME, value, expires);

everything works fine. Checking the cookie in the browser leads to mydomay.com as it should.

But, when I use the following:

String path = "/"
String domain = "mydomain.com"
Cookies.setCookie(LOGIN_COOKIE_NAME, value, expires, domain, path, secure);

I can see a dot before the domain when I check it in my browser:

.mydomain.com

Where does the dot comes from?

It turns out that Cookies.removeCookie(LOGIN_COOKIE_NAME) does not work for me if .mydomain.com is given. Why is it not possible to delete this cookie when there is a dot in front?

4 Answers 4

11

The dot means that the cookie also holds for any subdomain to mydomain.com, such as example.mydomain.com. Think of it as *.mydomain.com where * is a wildcard.

You can find a highly similar question here: What is the cookie dot rule?

4
  • 1
    @confile That could be another issue which has been resolved here: stackoverflow.com/questions/8049467/remove-cookie-issue
    – erb
    Commented Dec 14, 2013 at 9:31
  • When the cookie has domain: .mydomain.com I tried Cookies.removeCookie("cookieName", "/") but still I cannot remove the cookie. It must be because of the dot in front of the domain. Any idea?
    – Michael
    Commented Feb 9, 2014 at 23:54
  • @confile I'm sorry but I have no idea what could be the issue here. Could you give a complete code example that reproduces the unexpected behavior?
    – erb
    Commented Feb 11, 2014 at 15:20
  • 2
    How do I fix my problem I want to have String domain = "mydomain.com"?
    – Michael
    Commented Feb 19, 2014 at 16:15
2

You have to set the expire date to now such that the cookie expires imidiately.

Cookies.setCookie(COOKIE_NAME, "", new Date(), domain, path, false);
0
2

If you passed a domain when it was created then, when you clear a cookie, you need to pass the same domain (e.g. How do you remove a Cookie in a Java Servlet?); however, there's no API here to allow that.

This sounds a lot like a bug reported as RFE: Provide a Cookies.removeCookie(name, domain, path) method:

A cookie is set on the server for a given URL and the domain name and path are explicitly set on the cookie as part of the returned result. Cookies.remove(name) and Cookies.remove(name, path) will not remove the cookie on FF3.0.14. (The path based remove does work on IE 6).

The workaround suggested in that issue is to patch the GWT Cookies class.

1
  • 1
    How do I fix my problem I want to have String domain = "mydomain.com"?
    – Michael
    Commented Feb 19, 2014 at 16:12
1

Dot here signifies that cookie also holds for a sub-domain, such as abcd.mydomain.com. IT is very similar to say that you can add an entirely different website onto your domain, say registration segment separated in a different sub-domain so in general it can be substituted with a wildcard say *.mydomain.com. This is referred to as Cookie Dot Rule. Refer to the IETF Page for the same. You can let the same cookie refer to multiple token values as well.

1
  • 1
    How do I fix my problem I want to have String domain = "mydomain.com"?
    – Michael
    Commented Feb 19, 2014 at 16:12

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.