Preventing session timeout during long processing time in JSF
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 s开发者_StackOverflow中文版uch process.
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.
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.
精彩评论