使会话无限期地保持活动状态

发布于 2024-12-26 02:05:59 字数 287 浏览 1 评论 0原文

有没有一种方法可以保持页面会话处于活动状态,而无需将状态发送给客户端?我无法将 STATE_SAVING_METHOD 设置为 client,并且我不想使用 a4j:keepalive

我尝试过使用一个简单的隐藏 iframe 提交到有问题的 Bean,但它使主页无效。

我正在使用 JSF 1.2 和 myfaces。

这是为了绕过不需要用户登录的页面上的 ViewExpiredException。大多数现有站点都需要用户登录。

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.

I've tried using a simple hidden iframe that submits to the Bean in question but it invalidates the main page.

I am using JSF 1.2 and myfaces.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红衣飘飘貌似仙 2025-01-02 02:05:59

实现 ajax 轮询作为“心跳”以保持会话处于活动状态。最简单的是,您可以通过一点 jQuery 的帮助来实现此目的,以避免 100 行样板代码使其正常工作跨世界所知的所有不同浏览器:

<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}<​​/code>根据服务器端配置打印会话尚未存活的剩余秒数(顺便说一句,这可以通过 < web.xml中的code>)并扣除10秒,只是为了在自动过期之前准时,并转换为毫秒,以便它符合 setInterval() 的期望。

$.get() 发送一个 ajax GET 请求给定的网址。对于上面的示例,您需要将 servlet 映射到 /poll 的 URL 模式上,并在 doGet() 方法中基本上执行以下操作:

request.getSession(); // Keep session alive.

应该是这样。

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>

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.

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.

That should be it.

黑白记忆 2025-01-02 02:05:59

BalusC 的回答帮助我在我的应用中满足了这一要求,但由于我使用的是 PrimeFaces,我想分享一下 BalusC 的做法答案启发了我用来执行此操作的代码。

xhtml 页面

<p:poll listener="#{pf_usersController.keepUserSessionAlive()}"
        interval="#{session.maxInactiveInterval - 10}" />

bean

public void keepUserSessionAlive() {
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
    request.getSession();
}

一如既往,谢谢你,BalusC!

编辑:最终用户今天早上对此进行了测试,效果非常好!我的应用程序通常会在全页面刷新后 15 分钟强制会话超时(通过基于 session.maxInactiveInterval 和 web.xml 中的会话超时值的元刷新重定向到 sessionExpired.xhtml);如果用户在一个页面上执行一堆 AJAX 请求,会话将超时,因为 AJAX != 全页面刷新,但此代码允许最终用户“保持会话活动”,而最终用户位于应用程序的工资单页面上,并且会话保持活动状态1到2小时! :)

BalusC's answer helped me to meet this requirement in my app, but since I'm using PrimeFaces, I wanted to share how BalusC's answer inspired the code i'm using to do this.

xhtml page

<p:poll listener="#{pf_usersController.keepUserSessionAlive()}"
        interval="#{session.maxInactiveInterval - 10}" />

bean

public void keepUserSessionAlive() {
    FacesContext context = FacesContext.getCurrentInstance();
    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
    request.getSession();
}

as always, thank you, BalusC!

EDIT: An enduser put this to the test this morning, and it is working great! my app usually forces session timeout 15 minutes after full page refresh (redirect to sessionExpired.xhtml via meta refresh based on session.maxInactiveInterval and session timeout value in web.xml); if user is on one page doing a bunch of AJAX requests, session will timeout, since AJAX != full page refresh, but this code allowed enduser to 'keep session alive' while enduser was on payroll page in the app, and session stayed alive for 1 to 2 hours! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文