使用 GWT 的 HTTPSession
我是 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 应用程序连接到另一个网页,如果我单击后退按钮我在会话仍然存在的情况下返回到我的网络应用程序的浏览器的...我该怎么办?
我希望我已经很好地解释了我的问题......
*****新问题***:**
我尝试这样做......
---客户端...... 主要:
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GWT 中的 session 与 servlet 中的 session 类似。不同之处在于您在 servlet 中调用
HTTPSession session = request.getSession();
在 gwt 中您调用
HttpServletRequest request = this.getThreadLocalRequest();
获取请求,然后再次request.getSession();
在您的情况下,您应该调用单击按钮时的 RPC 并在服务器上管理前面的代码中的会话,并在单击另一个按钮时调用另一个 RPC 并使会话无效。这是例子;
此链接可能对您有帮助 在 GWT 中使用 Servlet 会话
编辑:
如果您想测试会话是否
isExist()
,请尝试将此添加到您的界面
boolean test(String attr);
添加到您的 .async add
void test(String attr, AsyncCallbackcallback);
添加到您的 .impl
并调用
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 againrequest.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;
This link maybe helpful to you Using Servlet Sessions in GWT
Edit :
If you want to test whether the session
isExist()
or not try thisadd to your interface
boolean test(String attr);
add to your .async add
void test(String attr, AsyncCallback<Boolean> callback);
add to your .impl
and just call