tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jay Wright <>
Subject HttpServletResponseWrapper error.
Date Mon, 21 Jan 2002 19:37:40 GMT

I am writing to this list after inquiries elsewhere have turned up no
adequate responses.  I hope it is the correct forum for the question, since
I am having trouble on Tomcat whereas I didn't on Resin and need to know how
the Tomcat container handles the HttpServletResponseWrapper.

I have a servlet which needs to write a JSP page's resultant HTML to a file
instead of sending it back to the browser. 

I do this by wrapping the response with HttpServletResponseWrapper, calling
RequestDispatcher and forward on the wrapped response, which runs the JSP
and writes the resultant HTML to a file, then forward control to a
thankyou.html page. 

This works with Resin. However, in Tomcat, when I wrap the response, I
"commit" the original response object and can not forward to a thank you
page. I catch a "java.lang.IllegalStateException: Cannot forward after
response has been committed". In my debug tests, the response is committed
during the RequestDispatchers forward method.

I need to avoid this.  The templates (1 and 2) are all JavaServer Pages
whose output (HTML) is intercepted and written to file by the
MyServletResponse wrapper. How do I do this without "committing" the
response object. I could find no way of duplicating or cloning the object
before passing it to MyServletResponse's constructor.  Nor could I close the
ServletOutputStreams.  Any ideas?  This worked wonderfully on Resin, but not
Tomcat 4.0.1.  I prefer to use Tomcat. 

If this cannot work, I need to know so I can move on.  If there is a better
way to handle it, I'm open to that as well.  Additionally it is ENTIRELY
reasonable to assume that I have implemented the wrapper incorrectly. 

Thank you,
Jay Wright


            MyServletResponse wrapper1 = new MyServletResponse (response,
            MyServletResponse wrapper2 = new MyServletResponse (response,

            rd = request.getRequestDispatcher(template1); 
            rd.include(request, wrapper1); 

            rd = request.getRequestDispatcher(template2); 
            rd.include(request, wrapper2 ); 
            wrapper2 .close(); 

ward(request, response); 


public class MyServletResponse extends HttpServletResponseWrapper { 

    private PrintWriter printWriter; 
    private ServletOutputStream servletOutputStream; 
* Constructor 
    public MyServletResponse (HttpServletResponse response, String filename)
throws { 
      servletOutputStream = response.getOutputStream(); 
      File file = new File(filename); 
      String fileCreated = file.toString(); 
   printWriter = new PrintWriter(new BufferedWriter(new

    public ServletOutputStream getOutputStream() throws
   return servletOutputStream; 

    public void setOutputStream(ServletOutputStream sos) throws { 
        servletOutputStream = sos; 

    public PrintWriter getWriter() throws { 
        return printWriter; 

    public void close() { 

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

View raw message