Hi All,
I want to redirect to a specific ".jsp" file page when the session is timed out in JSF.
I have tried to implement the same using PhaseListener
import javax.faces.FacesException;
import javax.faces.application.Application;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpSession;
public class SessionTimeoutPhaseListener implements PhaseListener {
public void beforePhase(PhaseEvent event) {
// Session detection code goes here
System.out.println(event.getPhaseId());
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) {
Application app = facesCtx.getApplication();
ViewHandler viewHandler = app.getViewHandler();
UIViewRoot view = viewHandler.createView(
facesCtx,
"sample.jsp");
facesCtx.setViewRoot(view);
facesCtx.renderResponse();
try {
viewHandler.renderView(facesCtx, view);
facesCtx.responseComplete();
} catch(Throwable t) {
throw new FacesException(
"Session timed out", t);
}
}
}
public void afterPhase(PhaseEvent event) {
// Do nothing
}
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
}
and I added the following lines in my faces-cofig.xml
<lifecycle>
<phase-listener>
SessionTimeoutPhaseListener
</phase-listener>
</lifecycle>
But Its not working. Whenever session expires i am getting the following exception in my screen.
javax.faces.application.ViewExpiredException: viewId:/welcome.jsf - View /welcome.jsf could not be restored.
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:312)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:43)
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:247)
Have i did anything wrongly in the above mentioned code? Is there any other simple way to handle this situation.
Thanks in Advance,
Antany