Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: RE: GURUS: How to write output buffered in respon- se o bject, to a fil e...


Message #1 by Jay Wright <jwright@o...> on Wed, 1 Aug 2001 09:39:45 -0700
Actually this has resulted in some interesting problems.  Using resin-2.0.0,
my implementation works flawlessly.  I send in the wrapped response, use the
RequestDispatched to include layers of jsp templates, and it writes the file
pefectly.

With tomcat, running standalone on solaris, this fails, throwing an
exception:

javax.servlet.ServletException: OutputStream is already being used for this
request

I'm not sure I understand how the tomcat container implements the
ServletOutputStream and PrintWriter, so I'm not sure where to start in
debugging this...

-----Original Message-----
From: Jay Wright [mailto:jwright@o...]
Sent: Wednesday, August 01, 2001 9:40 AM
To: Servlets
Subject: [servlets] RE: GURUS: How to write output buffered in respon se
o bject, to a fil e...


Thank you.  I've implemented this (sort of) and it works. 

What I did was write the HttpServletResponseWrapper, which had private
objects: ServletOutputStream and PrintWriter.  Then I overrode the
getOutputStream() and getWriter() to return these objects.  I didn't
subclass ServletOutputStream.  I assumed I didn't need to.  I made the
assumption (and this works, but it may still be the wrong assumption) that I
didn't need to subclass it because I didn't want to change it's
functionality.

Jay

------My Wrapper Class--------

public class MyServletResponseWrapper extends HttpServletResponseWrapper {

 private PrintWriter printWriter;
  private ServletOutputStream servletOutputStream;

 public AtOnceServletResponse(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 PrintWriter getWriter() throws java.io.IOException {
    return printWriter;
  }
}

-----Original Message-----
From: Richard Huss [mailto:richardh@w...]
Sent: Tuesday, July 31, 2001 12:53 AM
To: Servlets
Subject: [servlets] RE: GURUS: How to write output buffered in response
o bject, to a fil e...


Whilst I've not experimented with this, in version 2.3 the Servlet spec you
can pass a RequestDispatcher.include() or RequestDispatcher.forward() call
wrappers around the original request and response objects, rather than those
objects themselves.

The cunning trick is to write your own HttpServletResponseWrapper subclass
that overrides getOutputStream() and getWriter() to return your own custom
subclass of ServletOutputStream, so that the response from the included
servlet is captured rather than being sent directly to the client. (This is
the same model used if you want to capture or transform servlet output in a
filter.)

Richard Huss
Technical Architect, Wrox Press Ltd
richardh@w...
www.wrox.com


-----Original Message-----
From: Jay Wright [mailto:jwright@o...]
Sent: Monday, July 30, 2001 8:00 PM
To: Servlets
Subject: [servlets] GURUS: How to write output buffered in response
object, to a fil e...



Here's a question that has been misunderstood and is therefore still
unanswered on other newsgroups. 

I have a servlet whose purpose is to generate HTML.  To do this, it
"includes" jsp templates by invoking the RequestDispatcher's include method
to process the template and product the resultant HTML.  

As I understand it, this include method generates HTML that is written to a
buffer in the response object that will be sent to the client (browser in
this case). I would like to capture this resultant HTML or otherwise direct
it to a text file.   

As far as I can tell, because the RequestDispatcher's include method uses
the response object to store the output stream, I cannot simply create a new
PrintWriter and print to a file. Likewise, I see no way to access the
buffered output or change the respone object's output stream to write to a
file. Is this correct?

In short, I am relying on the JSP engine to process templated JSP's to
generate HTML. But I am not returning it to the client. I want to write it
to a file.

Any ideas on how to intercept this output stream and write it to a file? 

Many thanks,
Jay


  Return to Index