sendRedirect in JSF 2.2

3.8k views Asked by At

I am upgrading JSF from 1.2 to 2.2 version.

I have a simple response.sendRedirect() in my backing bean method. With JSF2.2, it started giving "java.lang.IllegalStateException: Cannot change buffer size after data has been written at org.apache.catalina.connecto" exception.

After adding "FacesContext.getCurrentInstance().responseComplete();", it worked!

Can anyone help me in understanding how the implementation is upgraded in JSF2.2 that redirect is not working without explicity saying response is completed?

Thanks!

2

There are 2 answers

0
BalusC On

You're supposed to use ExternalContext#redirect() for the job.

public void submit() throws IOException {
    // ...

    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
    ec.redirect(ec.getRequestContextPath() + "/otherpage.jsf");
}

This has always been the case since the beginning, also in JSF 1.x. It will under the covers automatically call FacesContext#responseComplete() after performing the HttpServletResponse#sendRedirect(). The responseComplete() will basically instruct JSF that the response is already manually completed, and that JSF basically doesn't need to proceed to render response phase then (i.e. writing the navigation outcome into the response).

Moreover, any attempt to grab and downcast the raw javax.servlet.* API from under JSF's covers should be taken as a hint to think twice if there isn't already a JSF-ish way to achieve the same. In JSF 2.x there's an additional new way to perform a redirect: append faces-redirect=true query parameter to the (implicit) navigation outcome:

public String submit() {
    // ...

    return "otherpage?faces-redirect=true";
}

As to the illegal state exception you faced, JSF 2.2 just postpones setting the response headers to the point when it actually needs to render response. It will be too late if the response is already committed.

0
send2ashok On

Java.lang.IllegalStateException: Cannot change buffer size after data has been written at org.apache.catalina.connecto" exception.

This may occur because of you had set response buffer size manually for reducing memory reallocation at rendering time but your page has more size than buffer size

For example

<context-param>    
    <param-name>    javax.faces.FACELETS_BUFFER_SIZE    </param-name>
    <param-value>    55555    </param-value>
</context-param>