Rapidoid - Extremely Fast, Simple and Powerful Java Web Framework!

Session management
  • A HTTP session is a temporary data storage that spans multiple HTTP requests.
  • Data is stored in the session to be accessed later, when handling another HTTP request from the same user (and browsing session).
  • There are 2 different implementations of the session, server-side session and client-side token as a session.

The server-side session is a simple in-memory storage in the web server. While this is not a problem when running only one web server, scaling out is non-trivial, requiring sticky sessions or replicating the session, or storing it into some datastore.

On.req(req -> {
    int counter = req.session("n", 0) + 1;
    req.session().put("n", counter);
    return counter;
});
GET /first
1
GET /second
2
GET /third
3

Server-side Session API (in interface Req):

String sessionId()
Returns the ID of the session (the value of the "JSESSIONID" cookie). If a session doesn't exist, a new session is created.
boolean hasSession()
Does the HTTP request have a server-side session attached?
Map<String, Serializable> session()
Provides read/write access to the server-side session attributes of the HTTP request/response.
T session(String name)
Returns the value of the specified server-side session attribute from the HTTP request/response, or throws a runtime exception if it is not found.
T session(String name, T defaultValue)
Returns the value of the specified server-side session attribute from the HTTP request/response, or the specified default value, if it is not found.

Server-side Session API (in interface Resp):

Map<String, Serializable> session()
Provides read/write access to the server-side session attributes of the HTTP request/response.
Resp session(String name, Serializable value)
Sets a session attribute of the HTTP response.

Token Session API (in interface Req):

boolean hasToken()
Does the HTTP request have a token attached?
Map<String, Serializable> token()
Provides read/write access to the token attributes of the HTTP request/response.
T token(String name)
Returns the value of the specified token attribute from the HTTP request/response, or throws a runtime exception if it is not found.
T token(String name, T defaultValue)
Returns the value of the specified token attribute from the HTTP request/response, or the specified default value, if it is not found.

Token Session API (in interface Resp):

Map<String, Serializable> token()
Provides read/write access to the token attributes of the HTTP request/response.
Resp token(String name, Serializable value)
Sets a token attribute of the HTTP response.