Redirecting in @PostConstruct method resulted in IllegalStateException
illegalstateexception, jsf-2, redirect
Solution
As mentioned in my update, the following line printed `INIT true`:
System.out.println("INIT " + FacesContext.getCurrentInstance().getExternalContext().isResponseCommitted());
I finally found why the above situation occurred. On my page, before the `ManagedBean X`, which contains the redirect function, was referenced by `UIComponent A`, another `ManagedBean Y` was referenced beforehand by `UIComponent B` that is placed above `UIComponent A` in the page structure. As a consequence, the response was partially committed by `ManagedBean Y`, which makes it impossible for `ManagedBean X` to send a `redirect` request.
Problem
According to the answer of BalusC, I used ``` FacesContext.getCurrentInstance().getExternalContext().redirect(url); ``` in my `@PostConstruct` method to stop JSF from rendering the view and redirect users. However, when I try to run the code, I still ran into the `java.lang.IllegalStateException` exception at the above line. ``` WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:524) at StudentManagedBean.CourseSummary.init(CourseSummary.java:55) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ``` UPDATE: I added the following line in my `@PostConstruct` method: ``` System.out.println("INIT " + FacesContext.getCurrentInstance().getExternalContext().isResponseCommitted()); ``` and what I saw was `INIT true`. I wonder if the response is supposed to be committed BEFORE the `@PostConstruct` method is called? I'd be very grateful if you could give me an advice.