I would like to redirect the output of my application to the PrintWriter
provided in HttpServletRespose, in other words, I would like to send the
output from the application started from my servlet to the client. So I
tried to redirect System.out. But System.setOut(PrintStream) won't
accept a PrintWriter as a parameter, and I know of no way to convert a
PrintWriter to a PrintStream.
Is there any way to send the (standard) output of an application to a
client using an servlet?
--
Pim Lemmens Mathematics & Computing Science
wsi...@win.tue.nl Eindhoven University of Technology
http://wwwis.win.tue.nl/~wsinpim/ P.O. Box 513
tel. (((*31)(0)40)247)3755 5600 MB Eindhoven Netherlands
Getting the output from applications can be a little tricky, especially with
servlets since you're trying to redirect standard out. This may or may not
work in the way you are attempting, but Java does allow you to redirect
standard out to a stream defined by you so you can handle the output any way
you wish. You may have to internally read in the output from your
application and write it back out yourself.
One alternative is to try using the HttpServletResponse's output stream
instead of its PrintWriter. You can get the output stream of the response
object from the method getOutputStream(). You may have to create a
printstream from it like:
PrintStream clientOutput = new PrintStream(response.getOutputStream());
//response is the HttpServletResponse object.
You must remember, however, that all servlets run under the same JVM within
the webserver (or servletrunner). I wouldn't guarantee what would happen if
you redirected standard out to a specific servlet request's outputstream.
Once that request ends, I could only guess what would happen to other
servlet's output to standard out. It may get a little messy and your output
could get corrupted especially if other servlets are trying to write to
standard output at the same time your application is. Remember - there is
only one standard output and multiple servlets (or servlet requests) can be
running at the same time.
You may want to consider having your application write its output to a
designated file which can be read in by the servlet after the application
has completed and subsequently outputted to the client. This can be
accomplished by simply creating a temporary file (per request) and passing
that filename to your application which will write its output to that file.
Writing to a file is a bit more overhead, but at least you know your output
data is complete and correct.
Patricia Connell
Lockheed Martin Missiles & Space
Sunnyvale, CA