Google app engine sessions now supported?

2019-02-19 18:04发布

I thought google app engine did not support sessions (last time I checked was a few months ago). Now I was searching again for it and saw this:

http://code.google.com/appengine/docs/java/config/appconfig.html#Enabling_Sessions

says it supports:

javax.servlet.http.HttpSession

does this mean we have servlet session support now? If so, does anyone have an example of using this? I wanted to create my own User class and support user login and session management (I know app engine already supports this for google users, but wanted my own users for various requirements)

Thanks!

------------ Update -------------------------------------------

I put this in my GreetingServiceImpl, just to give it a shot:

public void login(String username, String password) {
    HttpSession session = getThreadLocalRequest().getSession(false);
    session.setAttribute("username", username);
}

then I'm trying to see if the session can be discovered in my landing jsp page, after refreshing the page in my browser:

<body>
<%
  String username = null;
  HttpSession mysession = request.getSession(false);
  if (mysession.getAttribute("username") != null) {
      username = (String)mysession.getAttribute("username");
  }
%>

but it seems the jsp page cannot resolve the fact that the user is logged in. I'd like to be able to figure out that the user is logged in on the landing page when the user refreshes, if possible.

Thanks

------------ Update 2 -------------------------------------------

It works,

Thanks

1条回答
放我归山
2楼-- · 2019-02-19 18:41

Session support is a feature out of the box on GAEJ.

You could code a login servlet that store your user on session:

HttpSession session = request.getSession();
if(ProvidedUserParametersAreOK){
   session.setAttribute("user", "name");

and then, code a control like this in your dispatcher servlet:

HttpSession session = request.getSession(false);
 if (session.getAttribute("user") != null){
    User user=(User)session.getAttribute("user");
    //user logged in 
查看更多
登录 后发表回答