aspose file tools*
The moose likes Servlets and the fly likes Reading a PrintWriter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Reading a PrintWriter" Watch "Reading a PrintWriter" New topic
Author

Reading a PrintWriter

Martin Lefebvre
Greenhorn

Joined: Nov 02, 2006
Posts: 9
Hi!
Is there a way that I could read the content of a PrintWriter, before it is sent to the browser??
If yes how?
Thanks
Martin
sudip ghosh
Greenhorn

Joined: Nov 14, 2006
Posts: 3
Hi Martin,

First of all nice to meet you & I should appreciate you for asking that kind question.
Yah. Its possible to have stored the PrintWriter's output to a seperate file into your hard drive.
For that I would like to suggest you to try this piece of code.....

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("file.out")));

Remember to specify the hard drive path of your comp within " ", i.e. something like "C:\\myproject\file.out" and this file has to be created under this folder before compiling your code, otherwise you will be welcome with a compilation error like "file.out does not exist".

Best of Luck !!!
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by sudip ghosh:
Remember to specify the hard drive path of your comp within " ", i.e. something like "C:\\myproject\file.out" and this file has to be created under this folder before compiling your code, otherwise you will be welcome with a compilation error like "file.out does not exist".

I do not have a JDK in the computer that I am using right now..So I sould not test by doubts.

I think that if the file is not found then it would create the file and even if it needs the file to be present before writing then also I do not think that compilation would fail.It might fail at runtime.


Rahul Bhattacharjee
LinkedIn - Blog
Rashid Mayes
Ranch Hand

Joined: Jan 11, 2006
Posts: 160
Hi Martin, Although I understand the literal meaning of the question asked I am not certain of the larger issue that is addressed by finding a solution to the question.

Can you provide more information about the nature of the issue that has lead up this question?

A servlet filter can be used to post-process a response before it reaches the client. We do have control over what is written to the outputstream and we control how and if a printwriter is used. Does this question really pertain to JSPs?

If so, filters will work with JSPs in addition to servlets. But, again, before traveling to far down this road a discussion of the larger issue is needed.

Next, as far as FileWriters, Checked Exceptions, and compilations are concerned, you will receive an error when a declared exception is not handled. You will not, however, receive compile time errors for conditions that can only be determined at runtime. In fact, in this case, you will not even receive a warning.


Rashid Mayes
http://www.hostj2me.com/ - http://www.worlddeveloper.org/
Martin Lefebvre
Greenhorn

Joined: Nov 02, 2006
Posts: 9
Thanks for replying,
In fact, I have a servlet, and I would like to read the content of the outputstream, before it is flushed to the browser client. So that I could change the content on certain conditions...
Martin
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Maybe you can think to do not generate this content at certain conditions? You can't control buffering of output stream in servlet container much, so subclass servlet output stream with your stream and use it as it was recommended.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Martin Lefebvre
Greenhorn

Joined: Nov 02, 2006
Posts: 9
Hi thanks, for the info!
I could sublass the servlet class, but how can I read, the content that would be rendered, in a normal way?
>Any exemple?
Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you subclass HttpServletResponseWrapper, you can override the getWriter method and plug your own writer in.

Then, you can use a filter to wrap the response object from the server with your own wrapper class.

If you want an example, grab http://simple.souther.us/capture.war and run it.
This app does what was mentioned above to write the output to standard out.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Martin Lefebvre
Greenhorn

Joined: Nov 02, 2006
Posts: 9
HI!
In your example, the filter i must create, does it have to be the first filter called if my application has several filters??
When I'll call responseWrapper.toString(), I will have the html code of the page??
I will have to try it soon!!!
Thanks for helping, hope this will solve my problem...
Martin

Does'nt seem to work SystemOut always print ""!

I'll check further tomorrow...
[ November 20, 2006: Message edited by: Martin Lefebvre ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Martin Lefebvre:
...
I will have to try it soon!!!
...



Martin Lefebvre
Greenhorn

Joined: Nov 02, 2006
Posts: 9
Well,
It does'nt work
I'm always getting "" from the SytemOut
 
 
subject: Reading a PrintWriter