11
votes

I have a project for school and I have to use Java. Recently I found play framework and I want to try to use it. It's easy enough for a quick start, but I ran into a problem with session.

Being stateless by its nature, play sends entire session to user in cookie and receives it on next request, so it allows only limited amount of data in session.

What I want to do is to fetch User object from DB on user login, and keep it in session, so I could access it from templates and so on (I have some methods in User class that I need to call in order to customize UI), but if I put User object, play calls its toString method and puts that in session.

I found that recommended way is to put larger amount of data into Cache, but I don't know how to access it from template (I guess I could make method with @Before annotation and add user to renderArgs, but that does not seem very clean to me). Another problem is that Cache has limited lifetime, so my object can disappear.

Does anyone has experience with this kind of problems?

Is there any way to use server side session with play? I don't need REST functionality for this project, so, as far as I am concerned, application can save state...

3

3 Answers

4
votes

Use Java Serialization to serialize a Hashmap to a file or to the database. Name the file or id column after a unique identifier you store in the user's cookie. Then put the User object in the hashmap before you serialize the hashmap. Now you have a persistent store that you can access. When the framework forgets the User object or any other session information, you can deserialize the Hashmap. Then write some static helper methods, static Object SessionDB.get(String id, String key) and SessionDB.put(String id, String key, Object value). I use this method on my homemade framework to store session information over a small server farm. Of course I use the database, not a file system.

2
votes

I think you should have a look at

http://groups.google.com/group/play-framework/browse_thread/thread/3d6946ad0b00303b/188e1b272d91408d?lnk=gst&q=store+object+in+session#188e1b272d91408d

you can also search here

http://groups.google.com/group/play-framework

the play framework discussion list at google groups is very active, and you usually get a response in a couple of days, at most...

1
votes

Another option is to convert your objects into JSON. I created a simple Util for this and used FlexJSON to serialise my objects into JSON.

import play.Logger;
import play.mvc.Http;
import play.mvc.Http.Session;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;

public class SessionManager {

private static JSONSerializer s = new JSONSerializer();

    public static void addSession(String key, Object value) {

        if(value != null) {
            Session session = Http.Context.current().session();
            session.put(key, s.deepSerialize(value));
        } else {
            Logger.info("Value for " + key + " is null");
        }
    }

    public static <T> T get(String key) {

        Session session = Http.Context.current().session();
        final String value = session.get(key);

        if (value == null) {
            return null;
        }

        return new JSONDeserializer<T>().deserialize(value);
    }

}

NOTE: You'll have to manage cleaning after your work and keep in mind that since this is stored in the cookie, it is not as secure.