I am new to GWT ... I would like to implement sessions in my web application Basically, I want the session to start with the click of a button (handle the event) and end with the click of another button (another event descriptor). Is it possible?
How to do it step by step?
Is this code ok ?:
Main (client):
Button b1 = new Button("b1"); b1.addClickHandler(new ClickHandler) { public voin onClick(){ ... rpc.setSession(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();
What am I doing - I am connecting to my web application on another web page if I click the back button of the browser, which I will return to my web application with a still live session ... How can I do this?
I hope I have well explained that my problem ...
***** NEW PROBLEM ***: **
I tried to do this ...
--- 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);
//////////////////////////////////////////////////// //////////////////////
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; }