Last change
on this file since 322 was
322,
checked in by hendrikvanantwerpen, 13 years ago
|
Added Play! framework and application with Jena dependency. Working on
the basic things now (login/register), after that start implementing
our data model.
|
File size:
1.3 KB
|
Line | |
---|
1 | # Using cookies |
---|
2 | |
---|
3 | ## Requestâs cookies |
---|
4 | |
---|
5 | You can retrieve the requestâs cookies using the `cookies()` method of a `Http.Request` object: |
---|
6 | |
---|
7 | ```java |
---|
8 | public class Application extends Controller { |
---|
9 | |
---|
10 | public static Result index() { |
---|
11 | Http.Cookie cookie = request().cookies().get("foo"); |
---|
12 | if (cookie.value().equals("bar")) { |
---|
13 | // ... |
---|
14 | } |
---|
15 | } |
---|
16 | } |
---|
17 | ``` |
---|
18 | |
---|
19 | The `get(String name)` method returns the cookie or `null` if there was no such cookie. See the [API documentation](http://playframework.github.com/api/java/play/mvc/Http.html) of the `Http.Cookie` class to know what information is available on cookies. |
---|
20 | |
---|
21 | ## Responseâs cookies |
---|
22 | |
---|
23 | Setting cookies and discarding them is performed through the `setCookie` and `discardCookies` methods of the `Http.Response` object: |
---|
24 | |
---|
25 | ```java |
---|
26 | public class Application extends Controller { |
---|
27 | |
---|
28 | public static Result index() { |
---|
29 | |
---|
30 | // Set cookies |
---|
31 | response().setCookie("bar", "baz"); |
---|
32 | response().setCookie("bar", "baz", 3600); |
---|
33 | |
---|
34 | // Discard cookies |
---|
35 | response().discardCookies("foo", "bar"); |
---|
36 | |
---|
37 | // ... |
---|
38 | } |
---|
39 | } |
---|
40 | ``` |
---|
41 | |
---|
42 | See the various overloaded versions of the `setCookie` method in the [API documentation](http://playframework.github.com/api/java/play/mvc/Http.Response.html) to know all the possible combinations of parameters. |
---|
Note: See
TracBrowser
for help on using the repository browser.