Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

java - Google AppEngine Session Example

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 Login Form:

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 Servlet:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...