【问题标题】:HTTPSession with GWT带有 GWT 的 HTTPSession
【发布时间】:2011-06-09 13:52:44
【问题描述】:

我是 GWT 的新手...我想在我的 Web 应用程序中实现会话 基本上我希望会话在单击一个按钮(处理一个事件)时开始,并在单击另一个按钮(另一个处理一个事件)时结束。 有可能吗?

如何一步一步来?

这段代码可以吗?:

主要(客户端):

Button b1 = new Button("b1");
b1.addClickHandler(new ClickHandler) {
      public voin onClick(){
              ...
             rpc.setSession(callback); //rpc call the service...

   }
}

Button b2 = new Button("b2");
b1.addClickHandler(new ClickHandler) {
      public voin onClick(){
              ...
             rpc.exitSession(callback);

   }
}

//--------------------------------------------- ---------------------------------------

import com.google.gwt.user.client.rpc.RemoteService;

public interface MySession extends RemoteService {

    public void setSession();

    public void exitSession();
}

//--------------------------------------------- ---------------------------------------

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface MySessionAsync {

    void setSession(AsyncCallback<Void> callback);

    void exitSession(AsyncCallback<Void> callback);

}

//--------------------------------------------- ---------------------------------------

import de.vogella.gwt.helloworld.client.MySession;

public class MySessionImpl extends RemoteServiceServlet implements MySession {

    HttpSession httpSession;
    @Override

    public void setSession() {
        httpSession = getThreadLocalRequest().getSession();

        httpSession = this.getThreadLocalRequest().getSession();
        httpSession.setAttribute("b", "1");

    }

    @Override
    public void exitSession() {
          httpSession = this.getThreadLocalRequest().getSession();
          httpSession.invalidate(); // kill session     
    }

}

如果我单击浏览器的后退按钮,我会用我的 Web 应用程序将我的 Web 应用程序连接到另一个网页,我会返回我的 Web 应用程序,而会话仍然存在......我该怎么办?

我希望我已经很好地解释了我的问题......

*****新问题***:**

我试图这样做......

---客户端.... 主要:

        MyServiceAsync service = (MyServiceAsync) GWT.create(MyService.class);
        ServiceDefTarget serviceDef = (ServiceDefTarget) service;
        serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()+ "rpc");

        boolean b=false;;

        b=service.checkSession(new AsyncCallback<Boolean>() {

            @Override
            public void onSuccess(Boolean result) {
                // here is the result
                if(result){
                        // yes the attribute was setted
                   }
            }

            @Override
            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());

            }
        });

        if (b==false){ // se non esiste una sessione
        RootPanel.get().add(verticalPanel); 
        RootPanel.get().add(etichetta); 
        RootPanel.get().add(nameField);
        RootPanel.get().add(sendButton);
        RootPanel.get().add(horizontalPanel); 

        }

        else{ //esiste già una sessione attiva (pagina da loggato)
            welcome.setText("Ciao "+userCorrect+"!!");
            RootPanel.get().add(verticalPanelLog);
            RootPanel.get().add(etichetta);
            RootPanel.get().add(nameField);
            RootPanel.get().add(cercaLog);
            RootPanel.get().add(horizontalPanel);
        }

/////////////////////////////////////// //////////////////////

public interface MyServiceAsync {
...

    void exitSession(AsyncCallback<Void> callback);

    void setSession(AsyncCallback<Void> callback);

    void checkSession(AsyncCallback<Boolean> callback); //error!!

/////////////////////////////////////// //////////////////////

public interface MyService extends RemoteService {
    /.....

    public void setSession();

    public void exitSession();

    public boolean checkSession();

/////////////////////////////////////// //////////////////////

服务器端:

public boolean checkSession() {

      httpSession = this.getThreadLocalRequest().getSession();

      //se la sessione esiste già
      if (httpSession.getAttribute("b")!= null){
          return true;
      }
      else{ .
          return false;
      }

【问题讨论】:

    标签: java gwt widget gwt-rpc httpsession


    【解决方案1】:

    GWT 中的会话类似于 servlet 中的会话。不同之处在于您调用的 servlet
    HTTPSession session = request.getSession();

    在你调用的 gwt

    HttpServletRequest request = this.getThreadLocalRequest(); 获取请求,然后再次request.getSession();

    在您的情况下,您应该在单击按钮时调用 RPC 并在服务器上管理会话之前的代码,并在单击另一个按钮并使会话无效时调用另一个 RPC。这是示例;

    Button b1 = new Button("b1");
    b1.addClickHandler(new ClickHandler) {
        // call RPC and 
       // session = this.getThreadLocalRequest().getSession();
      // session.setAtribute("b", "1");
    }
    
    
    Button b2 = new Button("b2");
    b1.addClickHandler(new ClickHandler) {
        // call RPC and 
       // session = this.getThreadLocalRequest().getSession();
      // session.invalidate(); // kill session
    }
    

    此链接可能对您有帮助Using Servlet Sessions in GWT

    编辑:

    如果你想测试会话isExist()是否可以试试这个

    添加到您的界面boolean test(String attr);
    添加到您的 .async 添加 void test(String attr, AsyncCallback&lt;Boolean&gt; callback);
    添加到您的 .impl

    @Override
    public boolean test(String attr) {
        return session.getAttribute(attr) != null;
    }
    

    然后打电话

    Rpc.test(attribute, new AsyncCallback<Boolean>() {
    
            @Override
            public void onSuccess(Boolean result) {
                // here is the result
                if(result){
                        // yes the attribute was setted
                   }
            }
    
            @Override
            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
    
            }
        });
    

    【讨论】:

    • 嗨 hilal,感谢您的回复...但我不明白: HttpSession httpSession = getThreadLocalRequest().getSession() 必须包含在“impl”服务器端对吗?如果不是太麻烦,你能给我一个“RPC调用”按钮的例子吗?谢谢
    • 例如...我在客户端有接口“MySession”、“MySessionAsync”,在服务器端有“MySessionImpl”。我应该在这些容器中放什么?
    • 是的,它必须包含在“impl”中。你不能在客户端做到这一点。对于每个服务器操作,您应该调用 rpc
    • 这与会话管理无关。它给出了“这要么是错误配置,要么是黑客尝试”。您是否对异步服务执行此 @RemoteServiceRelativePath("/*greet*/") 并且您的 web.xml 是否正确?小服务程序映射?请检查这些错误。这与会话管理无关
    • 不客气。我很高兴你学到了一些新东西
    猜你喜欢
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 2017-04-02
    • 1970-01-01
    相关资源
    最近更新 更多