Skip to content
Guillaume Bort edited this page Feb 6, 2012 · 1 revision

Using cookies

Request’s cookies

You can retrieve the request’s cookies using the cookies() method of a Http.Request object:

public class Application extends Controller {

    public static Result index() {
        Http.Cookie cookie = request().cookies().get("foo");
        if (cookie.value().equals("bar")) {
          // ...
        }
    }
}

The get(String name) method returns the cookie or null if there was no such cookie. See the API documentation of the Http.Cookie class to know what information is available on cookies.

Response’s cookies

Setting cookies and discarding them is performed through the setCookie and discardCookies methods of the Http.Response object:

public class Application extends Controller {

    public static Result index() {

        // Set cookies
        response().setCookie("bar", "baz");
        response().setCookie("bar", "baz", 3600);

        // Discard cookies
        response().discardCookies("foo", "bar");

        // ...
    }
}

See the various overloaded versions of the setCookie method in the API documentation to know all the possible combinations of parameters.

Clone this wiki locally