title: HTTPSession with GWT
body: <p>I'm new in GWT ... I would like to implement sessions in my Web App
  Basically I want that a session starts at the click of a button (handle an event) and ends at the click of another button (other handle an event).
  It's possible?</p>
  
  <p>How to do it step by step?</p>
  
  <p>Is it okay this code?:</p>
  
  <p><strong>Main (client-side):</strong></p>
  
  <pre><code>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);
  
     }
  }
  </code></pre>
  
  <p>//------------------------------------------------------------------------------------</p>
  
  <pre><code>import com.google.gwt.user.client.rpc.RemoteService;
  
  public interface MySession extends RemoteService {
  
      public void setSession();
  
      public void exitSession();
  }
  </code></pre>
  
  <p>//------------------------------------------------------------------------------------</p>
  
  <pre><code>import com.google.gwt.user.client.rpc.AsyncCallback;
  
  public interface MySessionAsync {
  
      void setSession(AsyncCallback&lt;Void&gt; callback);
  
      void exitSession(AsyncCallback&lt;Void&gt; callback);
  
  }
  </code></pre>
  
  <p>//------------------------------------------------------------------------------------</p>
  
  <pre><code>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     
      }
  
  }
  </code></pre>
  
  <p>What I do is I connect with my Web application to another web page, if I click the back button of the browser that I return to my web app with the session still alive ... How can I do?</p>
  
  <p>I hope I have explained well what my problem ...</p>
  
  <p>*****NEW PROBLEM***:**</p>
  
  <p>I tried to do so ...</p>
  
  <p><strong>---client side....
  MAIN:</strong></p>
  
  <pre><code>        MyServiceAsync service = (MyServiceAsync) GWT.create(MyService.class);
          ServiceDefTarget serviceDef = (ServiceDefTarget) service;
          serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()+ "rpc");
  
          boolean b=false;;
  
          b=service.checkSession(new AsyncCallback&lt;Boolean&gt;() {
  
              @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);
          }
  </code></pre>
  
  <p>////////////////////////////////////////////////////////////////////////</p>
  
  <pre><code>public interface MyServiceAsync {
  ...
  
      void exitSession(AsyncCallback&lt;Void&gt; callback);
  
      void setSession(AsyncCallback&lt;Void&gt; callback);
  
      void checkSession(AsyncCallback&lt;Boolean&gt; callback); //error!!
  </code></pre>
  
  <p>////////////////////////////////////////////////////////////////////////</p>
  
  <pre><code>public interface MyService extends RemoteService {
      /.....
  
      public void setSession();
  
      public void exitSession();
  
      public boolean checkSession();
  </code></pre>
  
  <p>////////////////////////////////////////////////////////////////////////</p>
  
  <p><strong>server-side:</strong></p>
  
  <pre><code>public boolean checkSession() {
  
        httpSession = this.getThreadLocalRequest().getSession();
  
        //se la sessione esiste già
        if (httpSession.getAttribute("b")!= null){
            return true;
        }
        else{ .
            return false;
        }
  </code></pre>