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 "Stephen Hobdell" <stephen.hobdell@e...> on Fri, 8 Feb 2002 16:08:02
Okay. This is fine if you have an environment that supports J2EE.

Unfortunately, even some commercial products do not, as of 8th of 
February, 2002, support J2EE 1.3 spec.

Can a similar approach be used in servlet chains under the J2EE 1.2 
(Servlets 2.2) spec??

Stephen.


> 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