博客专区 > DavidBao 的博客 > 博客详情
DavidBao 发表于3年前
  • 发表于 3年前
  • 阅读 10
  • 收藏 0
  • 点赞 0
  • 评论 0


Session and Flash scopes 


How it is different in Play


If you have to keep data across multiple HTTP requests, you can save them in the Session or the Flash scope. Data stored in the Session are available during the whole user session, and data stored in the flash scope are only available to the next request.

如果你需要在多个HTTP请求中保存数据,你可以把它们保存在Session或Flash域中。保存在Session中的数据,对于整个用户 session 都有效,而保存在Flash域中的数据只对下一次请求有效。

It’s important to understand that Session and Flash data are not stored in the server but are added to each subsequent HTTP Request, using Cookies. This means that the data size is very limited (up to 4 KB) and that you can only store string values.


Cookies are signed with a secret key so the client can’t modify the cookie data (or it will be invalidated). The Play session is not intended to be used as a cache. If you need to cache some data related to a specific session, you can use the Play built-in cache mechanism and use store a unique ID in the user session to associate the cached data with a specific user.

Cookie是被标记的,所以它在客户端不能被修改,否则就会失效。Play session不能用作缓存,如果你需要缓存与某个session相关的数据,你可以使用play内置的缓存机制,在session中保存一个唯一的用户session ID来关联缓存的数据与用户。

There is no technical timeout for the session, which expires when the user closes the web browser. If you need a functional timeout for a specific application, just store a timestamp into the user Session and use it however your application needs (e.g. for a maximum session duration, maxmimum inactivity duration, etc.).

session是没有超时的,当用户关闭浏览器的时候就会失效。如果你需要一个超时功能,只需要在用户的serssion中保存一个时间戳。(例如 session最大有效期,最大失效期等)

Reading a Session value

读取 Session 值

You can retrieve the incoming Session from the HTTP request:


public static Result index() {

    String user = session("connected"); 

    if(user != null) { return ok("Hello " + user);

    } else {

        return unauthorized("Oops, you are not connected");



Storing data into the Session

Session 中存储数据

As the Session is just a Cookie, it is also just an HTTP header, but Play provides a helper method to store a session value:


public static Result index() {

    session("connected", "");

    return ok("Welcome!");


The same way, you can remove any value from the incoming session:


public static Result index() {


   return ok("Bye");


Discarding the whole session


If you want to discard the whole session, there is special operation:


public static Result index() {


    return ok("Bye");


Flash scope

Flash 作用域

The Flash scope works exactly like the Session, but with two differences:


  • data are kept for only one request
  • 数据只保存在一个请求里

  • the Flash cookie is not signed, making it possible for the user to modify it.
  • Flash cookie 没被标记,这使得用户可以修改它。

Important: The flash scope should only be used to transport success/error messages on simple non-Ajax applications. As the data are just kept for the next request and because there are no guarantees to ensure the request order in a complex Web application, the Flash scope is subject to race conditions.


Here are a few examples using the Flash scope:


public static Result index() {

    String message = flash("success");

    if(message == null) {

        message = "Welcome!";


    return ok(message);


public static Result save() {

    flash("success", "The item has been created"); return redirect("/home");


  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 112
博文 264
码字总数 116640
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
* 支付类型