tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan Luehe <Jan.Lu...@Sun.COM>
Subject Response not flushed before RD.forward() returns
Date Fri, 20 Jan 2006 01:57:19 GMT
Consider the following code snippet of a servlet's service() method:

  public class DispatcherServlet extends HttpServlet {

    public void service(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException {

      request.getRequestDispatcher("/target").forward(request, response);

      try {
      } catch (Exception ex) { }

where "target" prints some output to the response.

The code currently returns the output printed by "target" only after
DispatcherServlet's service() method has finished, instead of right
before RD.forward() returns.

This seems to be in violation of the Servlet Spec, which has this:

SRV.8.4 ("The Forward Method"):

  Before the forward() method of the RequestDispatcher interface
  returns, the response content must be sent and committed, and closed
  by the servlet container.

The code at the end of o.a.c.core.ApplicationDispatcher.doForward()
looks like this:

        // This is not a real close in order to support error processing
        if ( log.isDebugEnabled() )
            log.debug(" Disabling the response for futher output");

        if  (response instanceof ResponseFacade) {
            ((ResponseFacade) response).finish();
        } else {
            // Servlet SRV.6.2.2. The Resquest/Response may have been
            // and may no longer be instance of RequestFacade
            if (log.isDebugEnabled()){
                log.debug( " The Response is vehiculed using a wrapper: "
                           + response.getClass().getName() );

            // Close anyway
            try {
                PrintWriter writer = response.getWriter();
            } catch (IllegalStateException e) {
                try {
                    ServletOutputStream stream = response.getOutputStream();
                } catch (IllegalStateException f) {
                } catch (IOException f) {
            } catch (IOException e) {

In the above code sample, response will be an instance of
ResponseFacade, meaning it will be suspended instead of being flushed
and closed right away.

Does anyone remember why the "response instanceof ResponseFacade"
check is there? I would have expected the "else" case to always be

Any hints appreciated.



To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message