Google AppEngine 会话示例 [英] Google AppEngine Session Example

查看:35
本文介绍了Google AppEngine 会话示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在我的 Google AppEngine 中启用会话/Java + GWT 应用程序.我该如何使用它?我如何获得会话 ID 并从中播放所有好东西?是否有任何简单登录页面的真实示例,我只是在其中输入登录名和密码,然后通过 RPC 调用进入服务器,对数据库进行身份验证并将会话 ID 发送回客户端.

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 examples of simple login page where I'm just entering LoginName and Password, then it goes to the server over RPC call, authenticates against database and sends Session ID back to the client.

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

I have following code already but don't know what to do next:

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;
    }
}

RPC 服务程序:

public class LoginServiceImpl extends RemoteServiceServlet implements LoginService{ 
    //Sends back to the client session id
    public String authenticateUser(String login, String password){
        String sessionId = new String();

        // TODO: figure out how to work with session id in GAE/J
        sessionId = "How to get session id?";

        return sessionId;
    }

    public Boolean checkIfSessionIsValid(String sessionId){

        //TODO: figure out how to check user's credentials  
        return true;
    }
}

任何正确方向的提示都会有所帮助.谢谢.

Any hints in the right direction would be helpful. Thanks.

推荐答案

以下是在 GAE 中获取会话的方法:

Here is how you can get the session in GAE:

this.getThreadLocalRequest().getSession();

这篇关于Google AppEngine 会话示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆