Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect to index page if session time out happened in jsf application

Tags:

jsf

I am using JSF RI 1.1. How to redirect to index page if session time out happens?

like image 586
Paul Avatar asked Mar 30 '10 05:03

Paul


People also ask

How does JSF handle session timeout?

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.


1 Answers

There are two ways which can be combinied:

  1. Make use of the <meta> refresh header in the HTML <head> element in combination with HttpSession#getMaxInactiveInterval() which returns the remnant of seconds the session has yet to live.

    <meta http-equiv="refresh" content="${pageContext.session.maxInactiveInterval};url=index.jsf"> 

    This approach will automatically redirect the page to the given url when the session expires.

  2. Catch ViewExpiredException in web.xml:

    <error-page>     <exception-type>javax.faces.application.ViewExpiredException</exception-type>     <location>/index.jsf</location> </error-page> 

    This approach will automatically forward the request to the given <location> when a POST request has been fired (h:commandButton, h:commandLink, etc) while the session is expired.

Note that I personally would prefer an intermediate "Session Expired" warning page or alert to avoid "wtf?" experiences and thus improve the user experience. Even more, I would as well prefer firing an ajaxical poll every minute when the client has shown activity by listening on click and keypress, so that the session's lifetime can be postponed more.

like image 171
BalusC Avatar answered Sep 28 '22 16:09

BalusC