Wrox Home  
Search P2P Archive for: Go

  Return to Index  

servlets thread: GURUS: How to write output buffered in response object, to a fil- e...


Message #1 by Jay Wright <jwright@o...> on Mon, 30 Jul 2001 11:59:41 -0700
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