When and How to redirect to a view in a JSF Application
By jayashri on Feb 06, 2006
This blog discusses the reason and fix for the IllegalStateException (IAE) that you may run into when you call FacesContext.getExternalContext().redirect() in your JSF application.
During RenderResponse phase, the JSF framework (ViewHandler in particular) does a RequestDispatcher.forward() to the specified view and the response is sent to the client. So if you do a redirect after the response has been committed, (say in the afterRenderResponse() , you will get an IAE. In other words, you can call redirect() instead of rendering the page and not after rendering the page. If you do so, Servlet Container will throw an exception because the call to forward() during RenderResponse phase would have already committed the response. So the right time to do the redirect would be anytime before the RenderResponse phase executes namely preRender(), (if you are in JavaStudio Creator) or beforeRenderResponse(). You also need to call FacesContext.responseComplete() after you call redirect() to indicate to the JSF framework that the response has been committed, so RenderResponse phase can be skipped.