Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes JSP output to file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "JSP output to file" Watch "JSP output to file" New topic
Author

JSP output to file

Josh Hudgins
Greenhorn

Joined: Apr 28, 2001
Posts: 2
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

Joined: Mar 22, 2000
Posts: 12783
    
    5
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

Joined: Apr 28, 2001
Posts: 2
I need it to be entirely server side. Preferably something in JSP itself or its Servlet controller.
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
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

Joined: Mar 22, 2000
Posts: 12783
    
    5
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

Joined: Apr 20, 2000
Posts: 3252
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP output to file