System.out.println vs out.println?

J

JGH

Possibly a stupid question...

I'm trying to write code that will be usable in both scripts run at the
linux command line and via jsp executed by tomcat.

When run at the command line, I can do this:
System.out.println ("Hello world!");


But in the jsp it has to be
out.println ("Hello world!");

Why is that and what do I do about it?
 
J

John C. Bollinger

JGH said:
I'm trying to write code that will be usable in both scripts run at the
linux command line and via jsp executed by tomcat.

When run at the command line, I can do this:
System.out.println ("Hello world!");


But in the jsp it has to be
out.println ("Hello world!");

Why is that and what do I do about it?

System.out is a PrintStream, whereas the implicit "out" object in a JSP
is a JSPWriter. The nearest common ancestor is Object, which is not
useful to you. You need an object of a consistent type to which you can
direct all output; the most likely candidate appears to be a PrintWriter:

// command-line program
PrintWriter outputPrinter = new PrintWriter(System.out);

or

// JSP
PrintWriter outputPrinter = new PrintWriter(out);

Then in both cases

outputPrinter.println("Hello world!");

There is probably more that you could do to encapsulate this business,
but what I've presented is a good starting point, and may be sufficient
in itself to meet your needs.
 
M

Malte

JGH said:
Possibly a stupid question...

I'm trying to write code that will be usable in both scripts run at the
linux command line and via jsp executed by tomcat.

When run at the command line, I can do this:
System.out.println ("Hello world!");


But in the jsp it has to be
out.println ("Hello world!");

Why is that and what do I do about it?

AFAIK, this is not the same 'out'. In a JSP it is the stream going back
to the browser.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top