Google AppEngine Session Example

前端 未结 2 447
耶瑟儿~
耶瑟儿~ 2020-12-04 18:55

I just enabled Session in my Google AppEngine/Java + GWT application. And how do I use it? How do I get session ID and play will all good stuff from it? Are there any real e

相关标签:
2条回答
  • 2020-12-04 18:59

    Here is how you can get the session in GAE:

    this.getThreadLocalRequest().getSession();
    
    0 讨论(0)
  • 2020-12-04 19:01

    Enabling session support gives you a standard Servlet HttpSession.

    This will be tracked by means of a cookie (called JSESSONID), which is managed by the servlet container under the covers. You do not need to care about the session id.

    You can then set attributes (server-side) that will be associated with the session (so that you can retrieve them later).

    HttpServletRequest request = this.getThreadLocalRequest();
    
    HttpSession session = request.getSession();
    
    // in your authentication method
    if(isCorrectPassword)
       session.setAttribute("authenticatedUserName", "name");
    
    // later
     if (session.getAttribute("authenticatedUserName") != null)
    

    This should also work with Ajax requests from GWT. Please refer to any Servlet tutorial for more details.

    The drawback of sessions on GAE (compared to other servlet engines) is that they are serialized in and loaded from the database every time, which could be expensive, especially if you put a lot of data in there.

    0 讨论(0)
提交回复
热议问题