| title | Session | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| slug | session | ||||||||||
| draft | false | ||||||||||
| menu |
|
If you need to store data between HTTP requests, you can store it in the session. The data stored in the session is available throughout the user's session.
{{< alert "info" >}}
Note that the session is not stored on the server side, but on the client side via the use of a digitally signed Cookie.
Because session is implemented using cookies, there are some implications.
- The data size is limited to 4 KiB
- Only string can be stored
{{< newline >}}
The following configuration keys can be used in the configuration file of your application.
voidframework.web.session.cookieNamethe name of the cookie containing the current session. The default value isVOID_SESS.voidframework.web.session.cookieHttpOnlyis the cookie only be accessed via HTTP? The default value istrue.voidframework.web.session.cookieSecureis the cookie secured? If true, sent only for HTTPS requests. The default value isfalse.voidframework.web.session.signatureKeythe key used to digitally sign the session content.voidframework.web.session.timeToLivethe session TTL. The default value is7 days.
{{< newline >}}
You can access the session data via the Context. Methods get or getOrDefault can be used.
@Singleton
@WebController
public class Controller {
@RequestRouting
public Result sessionExample(final Context ctx) {
final String userId = ctx.getSession().get("USER_ID");
return Result.of(userId);
}
}{{< newline >}}
To store value(s), methods put, putAll and putIfAbsent can be used.
@Singleton
@WebController
public class Controller {
@RequestRouting
public Result sessionExample(final Context ctx) {
final String userId = ctx.getSession().put("USER_ID", "e90b88d4-3c15");
return Result.of(userId);
}
}