2

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

user246114
  • 50,223
  • 42
  • 112
  • 149
  • If you use a client like GWT or a richer client that just plain HTML you can make the server side stateless and get away from the kludgy session mechanism. – Romain Hippeau Apr 20 '10 at 13:22
  • How? You mean the user login state is stored on the client only? Like in GWT, there is only one 'page', no switch between urls - so the javascript stores the logged-in user object for the entire lifetime of the user interaction? Thanks – user246114 Apr 20 '10 at 14:16

1 Answers1

2

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 
systempuntoout
  • 71,966
  • 47
  • 171
  • 241