我有一个应用程序,其中用户经常让页面整天在浏览器中运行。该页面有一个 intervalrendered,每 30 秒更新一次数据。
但是,如果用户有一段时间没有与页面交互,它仍然会过期。只要浏览器打开并发出预定请求,我希望它自动扩展。
有没有一种方法可以在每次为其中一个页面触发预定渲染器时自动延长 session ?
最佳答案
当我的代码已经被 ICEFaces 每 30 秒调用一次时,我真的不想用 Javascript hack 来点击按钮。 ICEFaces 内部计时器的以下被黑变通办法似乎有效:
private void updateSessionExpiration () {
HttpSession sess = getSession();
if (sess != null) {
try {
Field fld = SessionDispatcher.class.getDeclaredField("SessionMonitors");
fld.setAccessible(true);
Map map = (Map)fld.get(null);
String sessID = sess.getId();
if (map.containsKey(sessID)) {
log.info("About to touch session...");
SessionDispatcher.Monitor mon =
(SessionDispatcher.Monitor)map.get(sessID);
mon.touchSession();
}
} catch (Exception e) {
log.error("Failed to touch session");
}
}
}
此外,还有 ICEFaces 1.8.2RC1 (并且大概最终也会随着 ICEFaces 1.8.2 的发布版本一起发布),有两个新的解决方法可用:
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpSession session = request.getSession();
if (session != null) {
SessionDispatcher.Monitor.lookupSessionMonitor(session).touchSession();
}
或者,将它放在 web.xml 中以更新对特定模式中 URL 的任何命中:
<filter>
<filter-name>Touch Session</filter-name>
<filter-class>com.icesoft.faces.webapp.http.servlet.TouchSessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Touch Session</filter-name>
<url-pattern>/seam/remoting/*</url-pattern>
</filter-mapping>
关于java - ICEFaces session 自动扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/738642/