• Post Reply Bookmark Topic Watch Topic
  • New Topic

out.println in servlets

 
Dhawal Arora
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How in Servlets out.println writes the html text to container's response object where normally in system.out.println() , it writes the text to the console??This is creating a problem for me conceptually. Please help.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What you refer to as "out" is apparently not the same as "System.out". It's probably the ServletOutputStream obtained from the HttpServletResponse. Just because two things are named similarly doesn't mean they're the same (or even related).
 
Dhawal Arora
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So to which outputstream class does the printstream forward the formatted data in case of system.out to print to the console like it does to ServletOutputStream in servlets?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all in the javadoc -- java.io.PrintStream.
 
Tim Holloway
Bartender
Posts: 18417
60
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J2EE/JEE does not support the concept of "write to console". You should not use System.out.print or System.err.print in J2EE apps. Use a logger, instead.

In actual practice, J2EE servers are almost invariably running under JVMs which are launched as traditional executables and therefore in many OS's (specifically Unix and Windows-like OS's), there will be stdio capabilities in the JVM.

But you never know where the stdio data will end up. On the Tomcat server, it's usually redirected to TOMCAT_HOME/logs/catalina.out, but not always. Other webapp servers are even less predictable. Some servers may discard the output altogether.

So, returning the what I said originally: don't use System.out/System.err in a servlet. The servlet class has a log method, although for serious logging, I recommend a full-featured logger such as log4j or the java.util.logging logger (juli).

 
Dhawal Arora
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tim Holloway
Got it. Thanks for clearing it in more detail.

Edit: Also what I understood is that the println is a function of PrintStream or PrintWriter and these classes in turn pass the String we write in println to a different outputstream object which decides the output. Eg in servlets printstream passes the string to ServletOutputStream and in system.out.println , Printstream writes to the console.Thats what makes the difference. These objects are decided at the time of construction of PrintStream or PrintWriter.

Thanks for the help.
 
Tim Holloway
Bartender
Posts: 18417
60
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A PrintStream/PrintWriter is a formatting channel. It understands text lines. Thus, you can use various print functions and it will convert the arguments to Strings (if they aren't alreaady), and - more importantly - it understands line terminators. This insulates you from the OS-specific quirks of Windows/DOS using a CR/LF as a line terminator versus Unix NL character, or even the single CR that the original Macintosh used.

The PrintStream is bound to a Unix-style I/O stream, which can be stdout, stderr, or a custom file-open stream. For non-Unix systems, an equivalent set of channels are used, assuming that there's something that can support them.

As I said previously, webapp servers and their applications are not allowed to presume the existence of the stdio channels, so System.out/System.err should not be used in webapps.

Logging isn't like printing - you're outputting discrete packets of information which are generally accompanied by metadata (for example, a timestamp). So there's no "println" for log APIs. Each log write is, in effect, its own "println".
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!