Saurabh
Saurabh

Reputation: 159

How to initialise a session in Play

Well, this is from a developer newly using Play. When it came to using session, I found its not at all like I have been doing in servlets or jsps. I have tried reading documentation and found session in Play are stored in HTTP cookies rather. I have tried importing HTTP class of play. My problem however is I am unable to initialise a new session to set values in it. I have obviously tried using 'new' session as in Java and that obviosly didnt work out.

Session session = new session();

Also after looking somewhere I have used:

Session session = Http.Context.current().session();

which shows me error in identifying context and current

I have tried looking at sample codes and codes on net. each of them however is different and I don't get the basic way of using sessions in Play, so that after that I can use put and get to keep and retrieve.

I know the question seems too basic but believe me there is no exact answer available anywhere to what I need. So please help me regarding this. Any answer, any piece of code, or any Link on this will be highly appreciated.

Upvotes: 1

Views: 3363

Answers (2)

Visceras
Visceras

Reputation: 101

In play 2.8 the Http.Context was deprecated. This means, among other things, that the method "session()" is no longer available in a controller.

This is the updated way of doing it:

public Result info(Http.Request request) {
    //This is the equivalent to the old session()
    request.session() ... 
}

The Http.Request needs to be passed down through the route defined in routes. More information here.

Upvotes: 2

Daniel Olszewski
Daniel Olszewski

Reputation: 14401

Forget everything about the sessions from the jsp and servlets world while working with the Play's session. Play doesn't store anything on the server side and by design it's completely stateless. The Play session is just a cookie attached to every http request and it's stored on the client side. Word 'session' may be misleading in your case.

Working with the session is pretty straight forward. All you need is inherited from play.mvc.Controller which you have to extend when creating your own controller. To put a value in it you simply call the session(String key, String value) method from within a controller. For example:

public class Application extends Controller {
  public static Result login() {
    session("key", "example value");
    return ok("Welcome!");
  }
}

If there is no session cookie stored on client side this method will create new one and attach it to the HTTP response. Otherwise it will modify the existing one.

To read stored value use:

String value = session("key");

You can also remove value from the session:

session().remove("key");

or completely destroy it:

session().clear();

These are helper methods to work with the particular cookie witch in Play's terminology is called session. Nothing stops you from creating another cookie with similar purpose. But it'll require more writing. These helper methods saves your time and in many cases are more than enough.

You can specify session cookie name in your application.conf by setting session.cookieName property.

Upvotes: 3

Related Questions