Search code examples
javaplayframework-2.0session-cookiessetcookie

Play framework: Why the cookie isn't showing


I'm trying to set a simple cookie with a domain but it seems that it doesn't get added to the browser's cookie store.

Here's how I added the cookie

response().setCookie("clientauthtoken", "asdasd", 5000, "/test", "test.com", false, false);

And if I check in the cookie manager (a plugin for Firefox to manage cookies) it doesn't show the cookie I added.

If I check in the session, yes it's there

    for (play.mvc.Http.Cookie cockie: response().cookies()) {
        Logger.info(" name " + cockie.name());
        Logger.info(" value " + cockie.value());
        Logger.info(" domain " + cockie.domain());
    }

This happens only if I added the domain (test.com). If I set the domain null or an empty string and try to add the cookie again, then it is showing both the browser's cookie store and in the session. So what I'm missing here? Or is it not possible to add a cookie with a domain.

Thanks.


Solution

  • If you're adding a cookie with a domain, you can't test it when browsing to localhost. What you need to do in order to test it is to edit the hosts settings of your OS (for linux and OS X it's /etc/hosts) and add:

    127.0.0.1 test.com
    

    Then you can test things via http://test.com:9000