jsfviewexpiredexception

Keep a session alive for an indefinite amount of time


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.


Solution

  • 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.