我是 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);
}
}
//------------------------------------------------ ----------------------------------
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
}
}
我所做的是将我的 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);
}
///////////////////////////////////////// ////////////////////
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();
///////////////////////////////////////// ////////////////////
服务器端:
public boolean checkSession() {
httpSession = this.getThreadLocalRequest().getSession();
//se la sessione esiste già
if (httpSession.getAttribute("b")!= null){
return true;
}
else{ .
return false;
}