Question

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?

How to do it step by step?

Is it okay this code?:

Main (client-side):

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     
    }

}

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?

I hope I have explained well what my problem ...

*****NEW PROBLEM***:**

I tried to do so ...

---client side.... MAIN:

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

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

server-side:

public boolean checkSession() {

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

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

Solution

session in GWT is similar to session in servlet. The difference is in servlet you call
HTTPSession session = request.getSession();

in gwt you call

HttpServletRequest request = this.getThreadLocalRequest(); to get request and then again request.getSession();

in your situation you should call RPC when click the button and manage the session on server the previous code and call another RPC when clicking another button and invalidate session. Here is example;

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
}

This link maybe helpful to you Using Servlet Sessions in GWT

Edit :

If you want to test whether the session isExist() or not try this

add to your interface boolean test(String attr);
add to your .async add void test(String attr, AsyncCallback<Boolean> callback);
add to your .impl

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

and just call

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

        }
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top