您能否告诉我如何在GWT项目中捕获会话超时。我使用gwt dispatch lib。 我想知道我可以做一些事情,比如实现一个过滤器,然后检查会话是否存在,但我想在gwt项目中有不同的方法。 欢迎任何帮助。
由于
答案 0 :(得分:2)
客户端:所有回调都会扩展一个抽象回调,您可以在其中实现onFailur()
public abstract class AbstrCallback<T> implements AsyncCallback<T> {
@Override
public void onFailure(Throwable caught) {
//SessionData Expired Redirect
if (caught.getMessage().equals("500 " + YourConfig.ERROR_MESSAGE_NOT_LOGGED_IN)) {
Window.Location.assign(ConfigStatic.LOGIN_PAGE);
}
// else{}: Other Error, if you want you could log it on the client
}
}
服务器:所有ServiceImplementations都扩展了AbstractServicesImpl,您可以访问SessionData。覆盖onBeforeRequestDeserialized(String serializedRequest)并检查那里的SessionData。如果SessionData已过期,则将空间错误消息写入客户端。此错误消息在您的AbstrCallback中获取checkt并重定向到登录页面。
public abstract class AbstractServicesImpl extends RemoteServiceServlet {
protected ServerSessionData sessionData;
@Override
protected void onBeforeRequestDeserialized(String serializedRequest) {
sessionData = getYourSessionDataHere()
if (this.sessionData == null){
// Write error to the client, just copy paste
this.getThreadLocalResponse().reset();
ServletContext servletContext = this.getServletContext();
HttpServletResponse response = this.getThreadLocalResponse();
try {
response.setContentType("text/plain");
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
try {
response.getOutputStream().write(
ConfigStatic.ERROR_MESSAGE_NOT_LOGGED_IN.getBytes("UTF-8"));
response.flushBuffer();
} catch (IllegalStateException e) {
// Handle the (unexpected) case where getWriter() was previously used
response.getWriter().write(YourConfig.ERROR_MESSAGE_NOT_LOGGED_IN);
response.flushBuffer();
}
} catch (IOException ex) {
servletContext.log(
"respondWithUnexpectedFailure failed while sending the previous failure to the client",
ex);
}
//Throw Exception to stop the execution of the Servlet
throw new NullPointerException();
}
}
}
另外,您还可以覆盖doUnexpectedFailure(Throwable t)以避免记录抛出的NullPointerException。
@Override
protected void doUnexpectedFailure(Throwable t) {
if (this.sessionData != null) {
super.doUnexpectedFailure(t);
}
}