Preventing session timeout during long processing time in JSF

陌路散爱 提交于 2019-12-28 11:58:48

问题


I've been working on an JSF application. At one place, I've to call an action in managed bean. The action actually process hundreds of records and the session timesout before the processing is finished.

Though all the records are processed successfully, the session expires and the user is sent to login page.

I'd tried adding

session.setMaxInactiveInterval(0);

before the processing of the records with no effect.

How to prevent the session time out during such process.


回答1:


Introduce ajaxical polls to keep the session alive as long as enduser has the page open in webbrowser. Here's a kickoff example with a little help of jQuery.

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
    $(document).ready(function() {
        setInterval(function() {
            $.get('poll');
        }, ${(pageContext.session.maxInactiveInterval - 10) * 1000});
    });
</script>

Here ${pageContext.session.maxInactiveInterval} returns the remnant of seconds the session has yet to live (and is been deducted with 10 seconds -just to be on time with poll- and converted to milliseconds so that it suits what setInterval() expects).

The $.get('poll') should call a servlet which is mapped on an url-pattern of /poll and contains basically the following line in the doGet() method.

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

That's it.




回答2:


To prevent session timeout, try using thread to do processing.

By doing this way, user will be able to do other activities & don't hang up waiting for the process to complete.



来源:https://stackoverflow.com/questions/4525171/preventing-session-timeout-during-long-processing-time-in-jsf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!