Session GWT-需要提示-检查并设置会话上的参数。这条路对吗?
Im使用GWT在我的web应用程序上进行了一种自动验证。因此,我在GWTServiceImpl类中创建了以下函数:Session GWT-需要提示-检查并设置会话上的参数。这条路对吗?,session,gwt,Session,Gwt,Im使用GWT在我的web应用程序上进行了一种自动验证。因此,我在GWTServiceImpl类中创建了以下函数: public class PageMenuLogin extends FlowPanel { public PageMenuLogin() { PageMenuLogin.getService().isSetSession(new AsyncCallback<String>() { @Override
public class PageMenuLogin extends FlowPanel {
public PageMenuLogin() {
PageMenuLogin.getService().isSetSession(new AsyncCallback<String>() {
@Override
public void onFailure(Throwable caught) {
InlineLabel err=new InlineLabel();
err.setText("Errore Di Connessione");
PageMenuLogin.this.add(err);
}
@Override
public void onSuccess(String result) {
if(result.compareTo("")==0) {
designLogin();
} else {
designLogout(result);
}
}
});
}
public final void designLogin() {
final InlineLabel menu_err=new InlineLabel("");
menu_err.setStyleName("menu_err");
this.add(menu_err);
Button menu_login_button=new Button("Login");
this.add(menu_login_button);
menu_login_button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
getService().checkLogin("nickname", "password", new AsyncCallback<Boolean>() {
@Override
public void onFailure(Throwable caught) {
menu_err.setText("Comunicazione Fallita");
}
@Override
public void onSuccess(Boolean result) {
if (result) {
// I LOAD THE PROFILE PAGE
} else {
menu_err.setText("Username e password non validi");
}
}
});
}
});
}
}
********************************************************************************
public class GWTServiceImpl extends RemoteServiceServlet implements GWTService {
HttpServletRequest request;
HttpSession session;
public String isSetSession() {
this.request = this.getThreadLocalRequest();
this.session=this.request.getSession();
if(this.session.getAttribute("nickname")==null) {
return "";
} else {
return (String)this.session.getAttribute("nickname");
}
}
public boolean checkLogin(String username, String password) {
if("i check on the database if the user exist") {
this.session.setAttribute("nickname", value);
return true;
}
return false;
}
}
public类PageMenuLogin扩展流程面板{
公共页面menulogin(){
PageMenuLogin.getService().isSetSession(新的AsyncCallback()){
@凌驾
失败时的公共无效(可丢弃){
InlineLabel err=新的InlineLabel();
错误设置文本(“连接错误”);
PageMenuLogin.this.add(err);
}
@凌驾
成功时的公共void(字符串结果){
if(result.compareTo(“”==0){
designLogin();
}否则{
设计注销(结果);
}
}
});
}
公共最终作废设计登录(){
最终InlineLabel菜单\u err=新InlineLabel(“”);
菜单错误设置样式名称(“菜单错误”);
添加(菜单错误);
按钮菜单\登录\按钮=新按钮(“登录”);
添加(菜单\登录\按钮);
菜单\登录\按钮。添加ClickHandler(新建ClickHandler(){
公共作废一次点击(点击事件){
getService().checkLogin(“昵称”、“密码”,新的AsyncCallback()){
@凌驾
失败时的公共无效(可丢弃){
菜单错误设置文本(“Comunicazione Fallita”);
}
@凌驾
成功时的公共void(布尔结果){
如果(结果){
//我加载配置文件页面
}否则{
菜单错误设置文本(“用户名e密码无效”);
}
}
});
}
});
}
}
********************************************************************************
公共类GWTServiceImpl扩展RemoteServiceServlet实现GWTService{
HttpServletRequest请求;
http会议;
公共字符串isSetSession(){
this.request=this.getThreadLocalRequest();
this.session=this.request.getSession();
if(this.session.getAttribute(“昵称”)==null){
返回“”;
}否则{
返回(字符串)this.session.getAttribute(“昵称”);
}
}
公共布尔校验登录(字符串用户名、字符串密码){
if(“如果用户存在,我检查数据库”){
this.session.setAttribute(“昵称”,值);
返回true;
}
返回false;
}
}
在客户端,我调用GWTServiceImpl函数(服务器端),检查返回值并执行一些操作
这是使用GWT会话的正确方法吗?如有任何建议/提示/帮助,将不胜感激:)
谢谢你的时间
编辑
新的GWTServiceImpl:
public class GWTServiceImpl extends RemoteServiceServlet implements GWTService {
HttpSession session;
public String isSetSession() {
HttpSession session=getThreadLocalRequest().getSession();
if(session.getAttribute("nickname")==null) {
return "";
} else {
return (String)session.getAttribute("nickname");
}
}
public boolean checkLogin(String nickname, String password) {
HttpSession session=getThreadLocalRequest().getSession();
Database mydb=Configuration.getDatabase();
mydb.connetti();
// faccio md5 ed escape
String log_check_user=nickname;
String log_check_pass=password;
// controllo che l'utente esista
ArrayList<String[]> db_result=null;
db_result=mydb.selectQuery("SELECT nickname FROM users WHERE nickname='"+log_check_user+"' AND password='"+log_check_pass+"'");
mydb.disconnetti();
if(!db_result.isEmpty()) {
session.setAttribute("nickname", nickname);
return true;
}
return false;
}
public boolean checkLogout() {
HttpSession session=getThreadLocalRequest().getSession();
session.invalidate();
return true;
}
}
公共类GWTServiceImpl扩展RemoteServiceServlet实现GWTService{
http会议;
公共字符串isSetSession(){
HttpSession session=getThreadLocalRequest().getSession();
if(session.getAttribute(“昵称”)==null){
返回“”;
}否则{
return(String)session.getAttribute(“昵称”);
}
}
公共布尔校验登录(字符串昵称、字符串密码){
HttpSession session=getThreadLocalRequest().getSession();
数据库mydb=Configuration.getDatabase();
mydb.connetti();
//faccio md5 ed escape
字符串log\u check\u user=昵称;
字符串log\u check\u pass=密码;
//埃西斯塔管制员
ArrayList db_result=null;
db_result=mydb.selectQuery(“从昵称='”+log_check_user+“'和密码='“+log_check_pass+”)的用户中选择昵称”;
mydb.disconnetti();
如果(!db_result.isEmpty()){
session.setAttribute(“昵称”,昵称);
返回true;
}
返回false;
}
公共布尔签出(){
HttpSession session=getThreadLocalRequest().getSession();
session.invalidate();
返回true;
}
}
看起来应该能用。我使用GWT做了很多工作,尽管我经常放弃使用RemoteServiceServlet,而是通过JSON来回传递数据
不过,还是有一些建议。在同一类中调用方法或字段时,不需要包含this
关键字。这不会造成伤害,但会使代码比需要的时间更长。尽管如此,如果你发现它能让事情变得更清楚的话,请随意保留它
另外,除非您有实际使用请求的方法,否则不需要创建请求对象;你可以这么做
session=getThreadLocalRequest().getSession()代码>
最后一个建议:由于您在多个方法中使用会话,因此最好立即初始化它;因此,您可以只编写HttpSession session=getThreadLocalRequest().getSession(),而不是在isSetSession()中初始化它代码>,或在类的构造函数中初始化它。现在,如果您在isSetSession()
之前调用checkLogin()
,您将得到一个NullPointerException
,因为session
尚未初始化 谢谢你的帮助,我真的很感谢你的帮助:)所以,关于这一点,是的,我用它,因为它让我的事情更清楚。关于请求,是的,我可以直接使用getSession trought getThreadLocalRequest(),好主意!关于最后一个建议,你完全正确。但是如果我在构造上写它(检查上面的代码),它就不起作用了。我怎么了?:)附言(OT)我怎样才能在评论中添加这样的文字?我想我给出的关于sess的一些建议