JSF2 What Happens When Session Timeout?

30-11-2013
Question: Consider the following scenario. I am clicking the submit button of a JSF form, after the session has timed out(expired). The browser displays some exception message.
"view context could not be restored"...
What I want to do is, to automatically redirect to the homepage of the website after the session has expired. What is the mechanism to do this? any help would be much appreciated. Thanks.

To handle the exception whenever the user invokes a POST request on a page while the HTTP session has been expired, add an <error-page> to the web.xml which catches the JSF ViewExpiredException and shows the home page.
<error-page>
    <exception-type>javax.faces.application.ViewExpiredException</exception-type>
    <location>/home.xhtml</location>
</error-page>

To avoid the exception by redirecting to the home page immediately when the HTTP session has been expired, add a HTML meta refresh tag to the <head>/<h:head> which goes to the home page when the session has been timed out.

<meta http-equiv="refresh" content="#{session.maxInactiveInterval};url=home.xhtml" />

(the HttpSession#getMaxInactiveInterval() returns the session timeout in seconds)

I would however display some informal message to the enduser that the enduser ended in the home page because the session has been expired so that it's clear to the enduser why the website behaved like that. You could achieve this by adding a request parameter to the home URL like so home.xhtml?reason=expired and then intercept on that like so:
<h:outputText value="Your session has been expired" rendered="#{param.reason == 'expired'}" />

© 2019 All rights reserved. Codesenior.COM