Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP output to file

 
Josh Hudgins
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way to capture the HTML output stream of a JSP in a file?
Thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Save the file from your browser?
Create an application that opens a URLConnection to the server,
sends a request and saves the data from the response.
Bill

 
Josh Hudgins
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need it to be entirely server side. Preferably something in JSP itself or its Servlet controller.
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can save the output in a outputstream, and then write the content of outputstream through both the responses' outputstream and a FileOutputStream to write to server's local disk.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The environment a JSP runs in is rather complex, so I don't think there is anything you can do to the JSP that could divert the output to another stream. (Unless you want to hack the server code - Tomcat comes with complete code) You could have a servlet create a URLconnection to the server (maybe as localhost?) and grab the output that way.
Bill
------------------
author of:
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Josh Hudgins:
I need it to be entirely server side. Preferably something in JSP itself or its Servlet controller.

Do you want to redirect the output to a file, or do you want to log the output in addition to sending it as a normal response to the client? If the former, then URL.getContent()/URLConnection is your friend. If the latter...
A very simple way would be to create your own JspWriter subclass which acts as a wrapper class around the "real" JspWriter. At the top of your JSP you would say something like
<% out = new MyLoggingWriter(out); %>
Not sure if this will still work with complex pages which use dynamic includes or custom tags, but in simple pages it probably does.
Along the same lines but slightly more sophisticated would be a custom tag (using the BodyContent class) which would log everything produced between the start and end tags.
Finally, a servlet 2.3 filter could probably do it.
- Peter

[This message has been edited by Peter den Haan (edited April 29, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic