This discussion is archived
6 Replies Latest reply: Jul 24, 2012 6:12 AM by gimbal2 RSS

Cannot forward after response has been committed error after a sendRedirect

873078 Newbie
Currently Being Moderated
Hi,

I have been looking for on Google and other forums information about this error, but nothing solution works with my application.

In my JSF Tool, I effort an java.lang.NumberFormatException. When this error is catched, the flow goes to my SecurityFilter class and in the doFilter method I declare a sendRedirect method. But when this is executed I'm only obtain *"java.lang.IllegalStateException: Cannot forward after response has been committed"*

The error is launched when the application is trying lo load a table into a form.

The code is the next:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
HttpServletResponse resp = (HttpServletResponse) response;

try{
chain.doFilter( request, response );
}
catch(ServletException e){
resp.sendRedirect("/app/error/errorPage.jsf") ; // This line is the executed
return ;
}
catch(Exception e){
resp.sendRedirect("/app/error/errorPage.jsf") ;
     }
}

If I write

FacesContext.getCurrentInstance().getExternalContext().redirect("/app/error/errorPage.jsf");

An NullPointerException is catched.

Some clue to solve? Thanks
  • 1. Re: Cannot forward after response has been committed error after a sendRedirect
    gimbal2 Guru
    Currently Being Moderated
    You'll have to do things differently as the way you've chosen now simply does not work. You can't generate a response (meaning data is already sent to the client / browser) and then half-way through decide that you want to do something else - HTTP does not work that way. One request, one response. So if you really must redirect to an error page you had better make sure that you can do that before you decide to start dumping data to the client.

    Note that your description is a bit vague, so it may just be that I misunderstand the order of things as they are happening.
  • 2. Re: Cannot forward after response has been committed error after a sendRedirect
    905019 Newbie
    Currently Being Moderated
    Handling errors in the response-rendering phase is a bit tricky. Wonder if cacheing the reponse (either by configuration or by writing to a bytebuffer or something) until you're sure everything is OK could work?
    Adding some javascript at the end of the page that redirects on error is a hack (and depending how much the rendering broke, it might not execue anyway).
  • 3. Re: Cannot forward after response has been committed error after a sendRedirect
    873078 Newbie
    Currently Being Moderated
    Hi gimbal2,

    I hope to better explain.

    In my JSF code I have the next:

    <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions">
              
              
         <h:body>
              <ui:composition template="/template.xhtml">
                   ...
                   <ui:define name="page">
                        <h:head>
                             <h:outputStylesheet ...
                        </h:head>

                        <h:form id="form1">
                             <rich:dataTable      id="table"
                                            value="#{listBeanRequest.elements}"
                                            var="element">
                                  ...
         </h:body>
    </html>

    In the listBeanRequest class, the method getElements() has a line, Integer h = Integer.valueOf("a");, which efforts the java.lang.NumberFormatException

    An in my web.xml I have:

    <filter>
         <filter-name>SecurityFilter</filter-name>
         <filter-class>my.filter.SecurityFilter</filter-class>
    </filter>

    I'm using JSF 1.2 running on Apache Tomcat 6
  • 4. Re: Cannot forward after response has been committed error after a sendRedirect
    gimbal2 Guru
    Currently Being Moderated
    Yes - redesign you code such that the getElements() no longer throws an exception. You could try by creating a @PostConstruct annotated method in which you initialize the elements up front and deal with exceptions there, but don't delay it until the moment where the page is already rendering.
  • 5. Re: Cannot forward after response has been committed error after a sendRedirect
    873078 Newbie
    Currently Being Moderated
    And could it work the next tag in web.xml?

    <error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/app/error//errorPage.jsf</location>
    </error-page>
  • 6. Re: Cannot forward after response has been committed error after a sendRedirect
    gimbal2 Guru
    Currently Being Moderated
    Not for your particular problem, for the reasons I already explained. You cannot magically start a new response when one is already "committed" - the server can't do it for you either.

    This is really the moment where you have to understand the WHY, which you can figure out by Googling for the "response has already been committed" exception message.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points