Scala next play Framework learning notes (Session and Flash scopes)

Source: Internet
Author: User

The difference between session and Flash scope

If you need to transfer data over multiple HTTP requests, then the session and flash scope will be used. When stored in session, the entire session is valid, stored in Flash scope, only valid for the next request. The server does not store data in session and flash scope, they are only passed to the next request, using the cookie mechanism. This means that you can only store string types and have a limited size of up to 4KB. The default name of the cookie is play_session, which can be modified by the Session.cookiename in the application.conf.

The cookie data is marked with a key, so the client cannot modify the cookie data, otherwise the data will be invalidated.

The play Session is not used as a cache, and if you need to cache some of the session-related data, use the play's built-in caching mechanism to store a unique ID in the user's session to maintain the association with a particular user.

The session defaults to no expiration time unless the user switches off the browser. If you need to set the expiration time, you should store a timestamp in the user session, regardless of whether the application is required. The maximum expiration time can be set through play.http.session.maxAge in application.conf, but it does not prevent hackers from recovering from the expiry date of the cookie.


storing data in session

Since the session is only a cookie and is also an HTTP header, you can process the session data in the same way as the request results:

Ok ("welcome!"). Withsession ("Connected", "[email protected]")

Add an element to the existing session and create a new session in the following way:

Ok ("Hello world!"). Withsession (Request.session + ("Saidhello", "yes")

Deleting the elements in the session is also in a similar way:

Ok ("Theme reset!"). Withsession (Request.session-"theme")


Read the value of Session

To search for the session from the HTTP request:

def index = Action {request = = Request.session.get ("Connected"). map {user = OK ("Hello" + user)}.getorelse {Unauthorized ("Oops, you is not Connected")}}

Discard the entire session

The following actions can discard the entire session:

Ok ("Bye"). Withnewsession


Flash Scope

Flash scope works differently than the session two points:

The data is retained only in one request, and the other is that the Flash cookie is not tagged and the user can modify it.

      flash scope can only transmit success/error information Because the data is only in one request and cannot guarantee the order of the request in a complex web application, so it is constrained by the race condition.

      using flash scope are as follows:

def index = Action {Implicit request = = {Request.flash.get ("Success"). Getorelse ("welcome!") }}def save = Action {Redirect ("/home"). Flashing ("Success", "the item has been created")}


Search in view flash the value of scope and add an implicit Flash parameter, as follows:

@ () (implicit flash:flash) [email protected] ("Success"). Getorelse ("welcome!") ...


The implicit request => is then implicitly specified in the action , by the following way:

def index = Action {implicit request = = Ok (Views.html.index ())}

An implicit flash is provided to the view based on an implicit request.


If the following error is reported:

' could not find implicit value for parameter Flash:play.api.mvc.Flash '

That means your action does not have an implicit request within the scope.


Scala next play Framework learning notes (Session and Flash scopes)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.