Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle session expiration and ViewExpiredException in JSF 2?

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:

ViewExpiredException: 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.

like image 638
Selvin Avatar asked Feb 14 '11 13:02

Selvin


People also ask

What happens when a session expires?

When the session expires, or session timeout occurs, the Session_End event in global. asax is raised (except when session is handled by the DB) and the session collection is finally cleared. If any objects are NOT holding a reference to any of values in the session collection, then GC will collect it.

What is automatic session expiration?

Session Expire itself means that your web page has lost its connectivity to internet and it is no more active page(user data also expired) so it cannot re-direct to login page itself. Once a request is made to site again, it will automatically redirect request to login page.


1 Answers

To handle the exception whenever the user invokes a synchronous POST request on a page while the HTTP session has been expired and the JSF view state saving method is set to server, 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 handle the exception on asynchronous (ajax) requests as well, you need to implement a custom ExceptionHandler as answered in Session timeout and ViewExpiredException handling on JSF/PrimeFaces ajax request

See also:

  • javax.faces.application.ViewExpiredException: View could not be restored
like image 69
BalusC Avatar answered Sep 29 '22 19:09

BalusC