保持会话无限期的存活 [英] Keep a session alive for an indefinite amount of time
问题描述
是否有一种方法可以使页面的会话保持活动状态,而无需诉诸状态发送给客户端?我无法将STATE_SAVING_METHOD
设置为client
,并且我不想使用a4j:keepalive
.
Is there a way to keep a page's session active without resorting to sending the state to the client? I'm not able to set the STATE_SAVING_METHOD
to client
and I'd prefer to not use the a4j:keepalive
.
我尝试使用一个简单的隐藏iframe提交给有问题的Bean,但是它会使主页无效.
I've tried using a simple hidden iframe that submits to the Bean in question but it invalidates the main page.
我正在使用JSF 1.2和myfaces.
I am using JSF 1.2 and myfaces.
这是为了避免在不需要用户登录的页面上发生ViewExpiredException.大多数现有站点都需要用户登录.
This is to get around a ViewExpiredException on a page that does not require the user to log in. The majority of the existing site requires the user to log in.
推荐答案
将ajax轮询作为心跳"实施,以使会话保持活动状态.最简单的方法是,在一些 jQuery 的帮助下,可以达到以下目的,从而避免了100行的样板代码才能使其正常工作世界各地的所有不同浏览器都知道:
Implement an ajax poll as a "heartbeat" to keep the session alive. At its simplest you can achieve this as follows with help of a little jQuery to avoid boilerplate code of 100 lines to get it to work across all different browsers the world is aware of:
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function() {
setInterval(function() {
$.get("${pageContext.request.contextPath}/poll");
}, ${(pageContext.session.maxInactiveInterval - 10) * 1000});
});
</script>
${pageContext.session.maxInactiveInterval}
根据服务器端的配置(会话可以通过web.xml
中的<session-timeout>
控制的方式)显示会话尚待运行的剩余秒数,并扣除10秒,只是为了准时它会自动过期,并转换为毫秒,以适应 setInterval()
的期望.
The ${pageContext.session.maxInactiveInterval}
prints the remaining seconds the session has yet to live according to the server side configuration (which is by the way controllable by <session-timeout>
in web.xml
) and is been deducted with 10 seconds, just to be on time before it automatically expires, and converted to milliseconds so that it suits what setInterval()
expects.
$.get()
在给定的URL上发送ajax GET请求.对于上面的示例,您需要在/poll
的URL模式上映射servlet,并且基本上在doGet()
方法中执行以下操作:
The $.get()
sends an ajax GET request on the given URL. For the above example, you need to map a servlet on the URL pattern of /poll
and does basically the following in the doGet()
method:
request.getSession(); // Keep session alive.
应该的.
这篇关于保持会话无限期的存活的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!