This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading a PrintWriter

 
Martin Lefebvre
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rashid Mayes
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Lefebvre
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Lefebvre
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martin Lefebvre
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Martin Lefebvre:
...
I will have to try it soon!!!
...



 
Martin Lefebvre
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,
It does'nt work
I'm always getting "" from the SytemOut
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic