Efficiently using Session & Flash in Play framework


Play framework is being the love of every Scala developer now a days. In this post we’ll learn about using Session & Flash scopes in Play framework.

Play has a stateless architecture so in order to keep the data across multiple HTTP request we can use Session & Flash. Session and Flash data are not stored by the server but are added to each subsequent HTTP request, using the cookie mechanism. This result in to the limitation of data size that we want to keep in Session or Flash. Generally we can store data up to 4KB in Session or Flash.

Session :

- Data stored in the Session are available during the whole user session.
- Cookie values are signed with a secret key so the client can’t modify the cookie data.

Storing the data in Session :

For the very first request we can store the values in Session as

 Ok("Success").withSession("Org"-> "Knoldus Software LLP")

This’d start a new session and store the values.

For the subsequent requests in order to store more data in session you can use it like :

val sesssionToMaintain = request.session + ("location" -> "New Delhi")
Ok("Success").withSession(sesssionToMaintain)

This’d keep the previously stored values in session and would add more data in Session.

Retrieving the data from session:

"Value of 'Org' from Session : " + request.session.get("Org")

Discarding the whole session :

Ok("Bye").withNewSession

Session Expiry : Session expires when the user closes the web browser.

Flash :

- Data stored in the Flash scope are available to the next request only.
- Cookie is not signed, making it possible for the user to modify it.

Storing the data in Flash :

Redirect("/").flashing("Org"->"Knoldus Software LLP")

Retrieving the data from Flash:

"Value of 'Org' from Flash : " + request.flash.get("Org")

Retrieving the values on views :

To retrieve the Flash scope value in your view, just add an implicit with Flash:

@()(implicit flash: Flash)
 ...
 ...
 @flash.get("Org").getOrElse("No Company Found")
 ...

About Neelkanth Sachdeva

Software Consultant @ Knoldus Software LLP Co-Founder @ My Cell Was Stolen
This entry was posted in Architecture, Cloud, Scala, Web and tagged , , , . Bookmark the permalink.

6 Responses to Efficiently using Session & Flash in Play framework

  1. rohit says:

    how can i get the session id (similar to jsessionid concept) in play scala?

  2. As play is stateless , the session is maintained by browser’s cookie mechanism so there is no session id kind of things are required.

    http://www.playframework.com/documentation/2.2.x/ScalaSessionFlash

    It should make the concept clear.

    • Rohit says:

      If I need to manage session, how can I do that?
      I am building an android app and integrating it with REST services (Play + Scala). I need to check whether the requests are Ok or not

    • Rohit says:

      Is there a way we can access request or use request.session.get(“”) something in some normal method and not an action? I need to access request but want to call some other method from it and not return some result

  3. Gaurav kwatra says:

    Session never expire by just closing browser or just using logout button. As HTTP is a stateless protocol, so we use session-id to keep track of multiple HTTP request of authenticated user and differentiate from other users as well. So, session-id have same importance as password have. After logout, if session-id is not expire by the server end. Then, user don’t need their credentials again to log-in. IF any how anyone get session-id, HE/SHE log-in into the application. So, Session – id must be expire by server end.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s