Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > System.out.println vs out.println?

Reply
Thread Tools

System.out.println vs out.println?

 
 
JGH
Guest
Posts: n/a
 
      02-23-2005
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?
 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      02-23-2005
JGH wrote:

> 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.

--
John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Malte
Guest
Posts: n/a
 
      02-23-2005
JGH wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments