I have a servlet which uses a HttpServletResponseWrapper to wrap a response,
write data to a file, then eventually forwards to a jsp to display the
typical "thank you" page.

The problem is that when I wrap the response, I "commit" the response object
and can not forward to a thank you page.  I catch a
"java.lang.IllegalStateException: Cannot forward after response has been
committed".

I need to avoid this.   The templates (1 thru 3) 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 need to use Tomcat.

THE SERVLET CODE:

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

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

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

            // TEXT
            rd = request.getRequestDispatcher(template3);
            rd.include(request, wrapper3 );
            wrapper3 .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, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to