HTTPSession with GWT

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); //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 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); //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; } 
+4
source share
1 answer
Session

in GWT is like a session in a servlet. The difference is the servlet you call
HTTPSession session = request.getSession();

in gwt that you are calling

HttpServletRequest request = this.getThreadLocalRequest(); to receive the request, and then again request.getSession();

in your situation, you must call RPC when you click a button and manage the session on the server of the previous code and call another RPC when you click another button and cancel the session. Here is an 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 may be useful for you. Using servlet sessions in GWT

Edit:

If you want to check if the session isExist() or not try this

add boolean test(String attr); to your interface boolean test(String attr);
add to .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()); } }); 
+11
source

Source: https://habr.com/ru/post/1335646/


All Articles