Session time out handling through phase listener
796588Oct 30 2009 — edited Nov 2 2009I am trying to handle session timeout using phase listener. am getting RuntimeException of FacesContext cannot be found. My code & error as follows. Pls let me know where i have gone wrong...
my phaselistener code :
public class SessionTimeoutPhaseListener implements PhaseListener{
public void beforePhase(PhaseEvent event)
{
System.out.println("Inside phase listener");
FacesContext facesCtx = event.getFacesContext();
ExternalContext extCtx = facesCtx .getExternalContext();
HttpSession session = (HttpSession)extCtx .getSession(false);
boolean newSession = (session == null) || (session.isNew());
boolean postback = !extCtx.getRequestParameterMap().isEmpty();
boolean timedout = postback && newSession;
if(timedout) {
try {
System.out.println("Inside phase listener-time out");
Application app = facesCtx.getApplication();
String url=extCtx.encodeActionURL(extCtx.getRequestContextPath()+"/jsp/NewLogin.jsp");
System.out.println("Redirect url-"+url);
extCtx.redirect(url);
facesCtx.renderResponse();
System.out.println("redirect");
facesCtx.responseComplete();
}
public void afterPhase(PhaseEvent event) { // Do nothing
}
public PhaseId getPhaseId() { return PhaseId.RESTORE_VIEW; }
}
OutPut:
Inside phase listener
Inside phase listener
Inside phase listener-time out
Redirect url-/cbscrown/jsp/NewLogin.jsp
redirect
StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.RuntimeException: Cannot find FacesContext
at javax.faces.webapp.UIComponentClassicTagBase.getFacesContext(UIComponentClassicTagBase.java:1811)
at javax.faces.webapp.UIComponentClassicTagBase.setJspId(UIComponentClassicTagBase.java:1628)