tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jay Wright <jwri...@once.com>
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

THE SERVLET CODE: 

            MyServletResponse wrapper1 = new MyServletResponse (response,
"1_content.txt"); 
            MyServletResponse wrapper2 = new MyServletResponse (response,
"2_content.txt"); 

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

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

request.getRequestDispatcher(getScreenFlowManager().getTemplate(locale)).for
ward(request, response); 

MY WRAPPER CLASS: 

public class MyServletResponse extends HttpServletResponseWrapper { 

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

    public ServletOutputStream getOutputStream() throws java.io.IOException
{ 
   return servletOutputStream; 
    } 

    public void setOutputStream(ServletOutputStream sos) throws
java.io.IOException { 
        servletOutputStream = sos; 
    } 

    public PrintWriter getWriter() throws java.io.IOException { 
        return printWriter; 
    } 

    public void close() { 
        this.printWriter.close(); 
    } 
} 

--
To unsubscribe, e-mail:   <mailto:tomcat-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-dev-help@jakarta.apache.org>


Mime
View raw message