How To Create, Manage, Associate A Session In Rest Jersey Web Application
A HTML5 UI is connected to the backend (REST Jersey to business logic to Hibernate and DB). I need to create and maintain a session for each user login until the user logs out. Can
Solution 1:
Using JAX-RS for RESTful web services is fairly straightforward. Here are the basics. You usually define one or more service classes/interfaces that define your REST operations via JAX-RS annotations, like this one:
@Path("/user")publicclassUserService{
// ...
}
You can have your objects automagically injected in your methods via these annotations:
// Note: you could even inject this as a method parameter@Context private HttpServletRequest request;
@POST@Path("/authenticate")
public String authenticate(@FormParam("username") String username,
@FormParam("password") String password) {
// Implementation of your authentication logicif (authenticate(username, password)) {
request.getSession(true);
// Set the session attributes as you wish
}
}
HTTP Sessions are accessible from the HTTP Request object via getSession()
and getSession(boolean)
as usual. Other useful annotations are @RequestParam
, @CookieParam
or even @MatrixParam
among many others.
For further info you may want to read the RESTEasy User Guide or the Jersey User Guide since both are excellent resources.
Post a Comment for "How To Create, Manage, Associate A Session In Rest Jersey Web Application"