使用GWT的HTTPSession

Jac*_*els 4 java gwt widget gwt-rpc httpsession

我是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);

   }
}
Run Code Online (Sandbox Code Playgroud)

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

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

public interface MySession extends RemoteService {

    public void setSession();

    public void exitSession();
}
Run Code Online (Sandbox Code Playgroud)

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

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

public interface MySessionAsync {

    void setSession(AsyncCallback<Void> callback);

    void exitSession(AsyncCallback<Void> callback);

}
Run Code Online (Sandbox Code Playgroud)

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

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     
    }

}
Run Code Online (Sandbox Code Playgroud)

我所做的是将我的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);
        }
Run Code Online (Sandbox Code Playgroud)

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

public interface MyServiceAsync {
...

    void exitSession(AsyncCallback<Void> callback);

    void setSession(AsyncCallback<Void> callback);

    void checkSession(AsyncCallback<Boolean> callback); //error!!
Run Code Online (Sandbox Code Playgroud)

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

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

    public void setSession();

    public void exitSession();

    public boolean checkSession();
Run Code Online (Sandbox Code Playgroud)

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

服务器端:

public boolean checkSession() {

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

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

小智 11

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
}
Run Code Online (Sandbox Code Playgroud)

此链接可能对您在GWT中使用Servlet会话很有帮助

编辑:

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

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

@Override
public boolean test(String attr) {
    return session.getAttribute(attr) != null;
}
Run Code Online (Sandbox Code Playgroud)

然后打电话

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());

        }
    });
Run Code Online (Sandbox Code Playgroud)