Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play framework handling session state

I have a Webapp that is built on top of the Play framework and Scala. It is about presenting the user with a set of questions with each question having a set of answers. Some of the questions have radio button types an answers and some have check boxes as answers. When the user clicks start test, I call the controller, fetch the list of questions with its answers and return the result as a case class to the view template. I now need to maintain the state of the test as the user answers each question. He can go previous, next and I need to keep track of all the questions that he answered.

Coming from a Java EE background, I thought I can store the case class in the session and manipulate it in my controller. But that unfortunately does not look like that as the Play framework's session is a key value pair of String, String and not a String, Object. I'm now stuck with my app and since my experience with the Play framework is limited, I would like to ask for suggestions.

like image 659
joesan Avatar asked Dec 25 '13 12:12

joesan


People also ask

Is Play framework asynchronous?

Internally, Play Framework is asynchronous from the bottom up. Play handles every request in an asynchronous, non-blocking way. The default configuration is tuned for asynchronous controllers.

Is Play framework still used?

Play is rock-solid and used by hundreds of thousands of Java and Scala developers every month. Play is still extremely relevant to today's application and web development and has a passionate and very capable community around it ensuring that it has many good years left.

What is the default session cookie name set by the Play framework?

The default name for the cookie is PLAY_SESSION . This can be changed by configuring the key session. cookieName in application. conf.”

What is activator in play framework?

The activator command can be used to create a new Play application. Activator allows you to select a template that your new application should be based off. For vanilla Play projects, the names of these templates are play-scala for Scala based Play applications, and play-java for Java based Play applications.


2 Answers

Play is a framework designed to be stateless, so you do not find any concrete concept of server side session. Of course, this is not strictly prohibited, if you need it, as in this case, you can use a server-side technology (cache or database) as a container for your items and Play-cookie session for storing the access key string.

This is an example of storing object using Play Cache:

import play.api.cache.Cache
import play.api.Play.current

val key = UUID.randomUUID.toString
Cache.set(key, yourModel, 0)
Ok.withSession("answer_key" -> key)

and this is an example of retrieval:

import play.api.cache.Cache
import play.api.Play.current

val key = session.get("answer_key").getOrElse("none")
val yourModel = Cache.getAs[ModelClass](key).getOrElse(//whatever you want if not exists)
like image 132
Marco Avatar answered Sep 21 '22 13:09

Marco


Play only supports Strings for sessions because it stores all session state in cookies - this means Play nodes can scale without the need for any sort of clustering/state sharing tech.

If the data you want to store is small (less than 2k), then just serialise it into JSON, or it sounds like in your case, even simpler would be to serialise it into a comma separated list of answers.

Otherwise, you can store it in a cache such as memcached or redis.

like image 27
James Roper Avatar answered Sep 19 '22 13:09

James Roper