• Post Reply Bookmark Topic Watch Topic
  • New Topic

Redirect System.out to ServletOutputStream

 
Majid Al-Fifi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a web application without the source code.
There are several lengthy processes done by the app and it prints logs using System.out.print() statements to the console.

example of output generated:
done walking 30 steps (10%)
done jumping 45 jumps (13%)
fell down stairs
and so on

I want to redirect this output to response.getWriter() or response.getOutputStream() so that the user can see what is going on.

I will have a dedicated browser window which will keep showing whatever it receives line after line just as it was the console.

Any idea?

Thanks,
Majid
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best thing to do would be to use a log4j.Or if you do not want to do a lot of code changes then before your code start writing to the consolse , set another writer to System class . Like System.setWriter(yourWriter)
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of potential for trouble if you take the redirecting System.out approach - for one thing ALL Threads can address System.out but a given ServletOuputStream is valid only for the duration of a single request-response cycle.

You might create a custom object which permanently redirects System.out to some sort of buffer system which holds (say) the last 200 lines of output. Provide a method for the user to get a snapshot of this buffer. Use the HTML <pre> tag to simplify formatting.

Bill
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!