System.out.println vs out.println?

Discussion in 'Java' started by JGH, Feb 23, 2005.

  1. JGH

    JGH Guest

    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?
     
    JGH, Feb 23, 2005
    #1
    1. Advertising

  2. 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
     
    John C. Bollinger, Feb 23, 2005
    #2
    1. Advertising

  3. JGH

    Malte Guest

    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.
     
    Malte, Feb 23, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joona I Palaste
    Replies:
    3
    Views:
    4,139
    Will Clark
    Aug 28, 2003
  2. dan
    Replies:
    1
    Views:
    618
    Joona I Palaste
    Aug 28, 2003
  3. Mashrur Mia

    System.out.println() synchronized?

    Mashrur Mia, Oct 9, 2003, in forum: Java
    Replies:
    1
    Views:
    3,744
    Christophe Vanfleteren
    Oct 9, 2003
  4. Olivier Mallet

    using System.out.println in an applet

    Olivier Mallet, Nov 4, 2003, in forum: Java
    Replies:
    1
    Views:
    799
    Andrew Thompson
    Nov 4, 2003
  5. gajo
    Replies:
    2
    Views:
    979
    Roedy Green
    May 10, 2004
Loading...

Share This Page