views:

1758

answers:

5

I have a series signup pages that build on each other. When the users session expires I have a Listener that cleans everything up on the server and that works great. But, if the user attempts to do anything else I just want to redirect them back to the first page of the series. However, my filter doesn't seem to work correctly. I keep getting javax.faces.application.ViewExpiredException

What is the best practice for handling this expcetion? I can't really just handle in web.xml because that's too global. Plus the Error page is being rendered from some JSF code - it seems like I need to catch that this is happening using a PhaseListener so the exception doesn't happen in the first place, but I haven't been able to find a good model for how to do this. Any ideas?

+1  A: 

I think you are the correct track with a phase listener. Essentially set something up in session on the first page. Then in phase listener look for the value in session. If it doesn't exit then do a redirect. The trick is to do it early in the phase listener process. Not sure exactly where in the process your phase listener is throwing the exception.

JeffJak
A: 

You can check whether you session is invalidate or not

boolean sessionInValid = httpServletRequest.getRequestedSessionId() != null && !httpServletRequest.isRequestedSessionIdValid();

Here the boolean variable sessionInValid will return true if the session is invalidate by any means.

You can add this in a filter or a listener then configure this in your web.xml file.

Umesh Aawte
+1  A: 

The way I handle this is to add a filter to the web.xml only mapped to the urls you want to track. That filter checks if the session is expired, and then forwards to a login page if it is. It should work if the filter is run before any JSF code is run.

Colin Gislason
A: 

Richfaces has their own mechanism for handling ViewExpiredException, look at Richfaces docs.

MarrLiss
but seems like that works only for ajax requests
daedlus
That refers to *session* expiry, not view expiry.
EJP
+1  A: 

The way I have handled this in the past is to use a listener which checks a field in a managed session bean. If the users session times out a listener cleans up the mbean and marks the user as not logged in. Every request is sent through the listener and if the requirements are not met then the user is forced out of the site. I never get ViewExpiredException in my log. The only time this exception has occurred is if the server has been restarted and the user requests a page when having a previous active session.

Thomas Kessler