Google AppEngine会话示例 [英] Google AppEngine Session Example
问题描述
我只在我的Google AppEngine 启用Session / Java + GWT应用程序。我该如何使用它?我如何获得会话ID并发挥它所有的好东西?是否有简单登录页面的实例,我只需输入LoginName和Password,然后通过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();
按钮loginBtn =新建按钮(登录);
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 connect to the server。);
}
public void onSuccess(String result){
InfoPanel.show(InfoPanelType.HUMANIZED_MESSAGE,Session ID,Your session id is:+ result);
GWT.log(设置会话,null);
String sessionID = result;
final long DURATION = 1000 * 60 * 60 * 24 * 14; //持续记住登录。 2周
日期到期=新日期(System.currentTimeMillis()+ DURATION);
Cookies.setCookie(sid,sessionID,expires,null,/,false);
}
}
);
}
});
loginVp.add(loginTxt);
loginVp.add(passTxt);
loginVp.add(loginBtn);
返回loginVp;
}
}
RPC Servlet:
public class LoginServiceImpl extends RemoteServiceServlet implements LoginService {
//发回客户端会话ID
public String authenticateUser(String login,String password ){
String sessionId = new String();
// TODO:弄清楚如何在GAE / J
sessionId =如何获取会话ID?中使用会话ID。
return sessionId;
$ b $公共布尔值checkIfSessionIsValid(String sessionId){
// TODO:弄清楚如何检查用户凭证
return true;
$ b 正确方向的任何提示都会有帮助。
Thanks。
解决方案以下是您如何在GAE中获取会话:
this.getThreadLocalRequest()。getSession();
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.
解决方案 Here is how you can get the session in GAE:
this.getThreadLocalRequest().getSession();
这篇关于Google AppEngine会话示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!