Board index JavaServer Faces General Partial AJAX calls don't handle container session timeouts

Partial AJAX calls don't handle container session timeouts

Components, Ajax Framework, Utilities and More.

Oleg User avatar
Expert Member

Posts: 3688
Location: Russia, Siberia => Germany, Black Forest
Hello Cagatay,

My answer was for nickw (author of this post) :-) I don't know if my posts will help PrimeFaces. But, sure, you can implement global exception handler in PrimeFaces too. The pronlem is only - you need a redirect to any defined login / logout page if session timed out.
PrimeFaces 4.x, 5.x, Mojarra 2.2.x, JBoss WildFly, WebSphere, Windows 8.1, IntelliJ IDEA
PrimeFaces Cookbook: http://ova2.github.com/primefaces-cookbook/ PrimeFaces Extensions on GitHub: http://primefaces-extensions.github.com/

tandraschko PrimeFaces Core Developer

Posts: 2134
Location: Bavaria, DE

IMO we should have an global handler for the ajax errors!

Maybe something like:
<p:registerAjaxErrorHandler type="ViewExpiredException" redirect="http://google.com" execute="alert('session timeout'); return true;" />


redirect and type should be optional. If type is missing, we should handle all exeptions. If execute returns true, we should redirect if redirect is not empty or defined.
Error type and message should be paramaters in the method for execute.

Also a clientside API would be great:

PrimeFaces.ajax.registerAjaxErrorHandler('javax.faces.application.ViewExpiredException', 'http://google.com', new function(type, message) { alert('session timeout'); return true; });
Always Bet On Prime (+ Extensions)!

PrimeFaces Extensions: https://github.com/primefaces-extensions
OpenWebBeans: http://openwebbeans.apache.org/
DeltaSpike: http://deltaspike.apache.org/
Personal Blog: http://tandraschko.blogspot.de/

Oleg User avatar
Expert Member

Posts: 3688
Location: Russia, Siberia => Germany, Black Forest
Hi Thomas,

p:registerAjaxErrorHandler sounds good, but it's registered on page level. Right? Should I write p:registerAjaxErrorHandler on every page if I want to catch exceptions for all pages? Well, template mechanism can help here, but I meant more or less a programmatic approach for gloabl exception handler and not declarativ one. Your idea is good anyway.
PrimeFaces 4.x, 5.x, Mojarra 2.2.x, JBoss WildFly, WebSphere, Windows 8.1, IntelliJ IDEA
PrimeFaces Cookbook: http://ova2.github.com/primefaces-cookbook/ PrimeFaces Extensions on GitHub: http://primefaces-extensions.github.com/


Posts: 723
Location: United States
A custom exception handler on the server side can handle exceptions in both Ajax and non-Ajax requests so I think that will generally be preferred over relying on the client side JavaScript to interpret an error response tag (handling the error tag would be a nice fall-back mechanism though). The typical response to an exception is to do a redirect so it is extremely important that the client handle the redirect response tag correctly. I have observed that the redirect tag is correctly interpreted in most cases, but in one case in particular it doesn't work: a dataTable row edit request made on an expired exception for example will not honor a redirect sent back in the response.


Posts: 36
Location: switzerland
Same Opinion as bumble.bee

If Browser sends request with expired Session ID, I should define on Server Side if I want answer with redirect to Login Side for traditional http POST/GET/DELETE/INSERT ) and for Ajack Request I might return Prime Face Dialog Box with Message "Session Expired" and Link to configurable URL ... ( URL could be mabe just web context /myappl or login/relogin URL /myappl/relogin )

... So maby some primefaces context parameters in web.xml to predefine the bahavior on ViewExpired?

Something Llike this:

<context-param>
<param-name>primefaces.redirect.javax.faces.application.ViewExpiredException</param-name>
<param-value>/login</param-value>
</context-param>
OSX 10.6.x , Fedora 14, XBuntu 10.x , majora 2.x, primefaces 3.0-snapshot, Tomcat 7.x , weblogic server 11.x, jboss 7.x , glassfish 3.x


Posts: 36
Location: switzerland
Until Optimuse provides nice solution with primefaces

Here What I use for now:

import javax.faces.FacesException;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
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;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LifeCycleListener implements PhaseListener {

      /**
    *
    */
   private static final long serialVersionUID = 1L;
    private static final Log LOG = LogFactory.getLog("wlmp.ui.util");

   public PhaseId getPhaseId() {
           return PhaseId.ANY_PHASE;
   }

   public void beforePhase(PhaseEvent event) {
      if(LOG.isTraceEnabled())  LOG.debug("START PHASE " + 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) {
           if(LOG.isTraceEnabled())  LOG.debug(" Session Time out do redirect to /error.xhtml ");
           Application app = facesCtx.getApplication();
           ViewHandler viewHandler = app.getViewHandler();
           facesCtx.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,"Session Time Out !", ""));
           UIViewRoot view = viewHandler.createView( facesCtx, "/error.xhtml");
           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) {
      if(LOG.isTraceEnabled())  LOG.debug("END PHASE " + event.getPhaseId());
    }


}


in faces-config.xml

 

..
..
<lifecycle>
  <phase-listener>LifeCycleListener</phase-listener>
 </lifecycle>
</faces-config>
OSX 10.6.x , Fedora 14, XBuntu 10.x , majora 2.x, primefaces 3.0-snapshot, Tomcat 7.x , weblogic server 11.x, jboss 7.x , glassfish 3.x


Posts: 723
Location: United States
rodakr wrote:
Same Opinion as bumble.bee


I'm suggesting a JSF 2 custom exception handler (javax.faces.context.ExceptionHandlerWrapper) dishing out redirects is generally a good way to handle errors, but you're suggesting using a Phase Listener and displaying a dialog on ajax error (most likely implemented by handling the error tag in the response).

I think your approach is fine, but it doesn't sound the same as mine.

tandraschko PrimeFaces Core Developer

Posts: 2134
Location: Bavaria, DE

IMO a clientside handler is the better solution because you are flexible. A simple redirect can be handled with a exceptionhandler, too.
If the session expires, you can open modal dialogs with information etc.
Always Bet On Prime (+ Extensions)!

PrimeFaces Extensions: https://github.com/primefaces-extensions
OpenWebBeans: http://openwebbeans.apache.org/
DeltaSpike: http://deltaspike.apache.org/
Personal Blog: http://tandraschko.blogspot.de/


Posts: 723
Location: United States
I agree that handling the error tag allows more flexibility. I just wish the simpler approach of handling the redirect tag worked in all PrimeFaces components!


Posts: 112
optimus.prime wrote:
This is an important one, I'll take this into M3.

great to hear, thanks!

atm i am using idlemonitor to redirect to my timeout page and invalidate the session myself before a timeout occurs, but it doesn't feel like the best solution :-)
never tried to use redirect for this so far thought, will give it a shot (thanks oleg for the nice links)

it looks like most of the problems i run into while developing my first primefaces application are either already solved or will be soon.. i can't even begin to say how happy i am with primefaces! Thanks guys for all the great work!
PrimeFaces (Elite) 4.0.13, Majorra 2.1.28, Tomcat 7.0.53
Testing with Firefox, Chrome and IE9+IE10
<3 Primefaces!!!

PreviousNext

Return to General