Java googleappengine会话示例

Java googleappengine会话示例,java,google-app-engine,session,Java,Google App Engine,Session,我只是在我的googleappengine/Java+GWT应用程序中。我如何使用它?我如何获得会话ID并从中播放所有精彩内容?有没有简单登录页面的真实示例,我只需输入登录名和密码,然后通过RPC调用将其发送到服务器,根据数据库进行身份验证,并将会话ID发送回客户端 我已经有以下代码,但不知道下一步要做什么: GWT登录表单: public class LoginForm { private final LoginServiceAsync loginService = GWT.creat

我只是在我的googleappengine/Java+GWT应用程序中。我如何使用它?我如何获得会话ID并从中播放所有精彩内容?有没有简单登录页面的真实示例,我只需输入登录名和密码,然后通过RPC调用将其发送到服务器,根据数据库进行身份验证,并将会话ID发送回客户端

我已经有以下代码,但不知道下一步要做什么:

GWT登录表单:

public class LoginForm {
    private final LoginServiceAsync loginService = GWT.create(LoginService.class);

    VerticalPanel loginVp = new VerticalPanel();
    TextBox loginTxt = new TextBox();
    TextBox passTxt = new TextBox();

    Button loginBtn = new Button("Login");

    public Widget getLoginWidget(){

        loginBtn.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent arg0) {

                loginService.authenticateUser(loginTxt.getText(), passTxt.getText(), 
                        new AsyncCallback<String>(){

                            public void onFailure(Throwable caught) {
                                InfoPanel.show(InfoPanelType.HUMANIZED_MESSAGE, "No Connetion", "Problem conneting to the server.");
                            }

                            public void onSuccess(String result) {
                                InfoPanel.show(InfoPanelType.HUMANIZED_MESSAGE, "Session ID", "Your session id is: " + result);

                                GWT.log("Setting up session", null);
                                String sessionID = result;
                                final long DURATION = 1000 * 60 * 60 * 24 * 14; //duration remembering login. 2 weeks
                                Date expires = new Date(System.currentTimeMillis() + DURATION);
                                Cookies.setCookie("sid", sessionID, expires, null, "/", false);
                            }
                        }
                );  
            }   
        });

        loginVp.add(loginTxt);
        loginVp.add(passTxt);
        loginVp.add(loginBtn);

        return loginVp;
    }
}
任何方向正确的提示都会有帮助。
谢谢。

启用会话支持将为您提供一个标准的Servlet HttpSession

这将通过cookie(称为JSESSONID)进行跟踪,cookie由servlet容器在封面下管理。您不需要关心会话id

然后,您可以设置将与会话关联的属性(服务器端)(以便以后可以检索它们)

这也适用于来自GWT的Ajax请求。 有关更多详细信息,请参阅任何Servlet教程


GAE上的会话(与其他servlet引擎相比)的缺点是,它们每次都被序列化并从数据库中加载,这可能非常昂贵,特别是如果您在其中放入大量数据。

以下是如何在GAE中获取会话:

this.getThreadLocalRequest().getSession();

如果您只是使用基于cookie的sessionId进行身份验证,请小心,因为它会让您面临跨站点脚本攻击:虽然它们是memcached的。但是在servlet上下文中使用request.getSession()仍然可以,对吗?getThreadLocalRequest()是GWT访问传递到服务和doGet、doPut等中的请求的方式。。。因为我通常不使用GWT,所以我依赖于request.getSession()。很好。
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.getThreadLocalRequest().getSession();