esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

What happens to System.out and System.err output in a Servlet?
It depends on your servlet engine.

Usually (in JServ or Tomcat or Weblogic) System.out goes to 'client side' and is seen in browser, while System.err goes to 'server side' and is visible in error logs and/or on console.

A piece of servlet code shows the idea:

 
public void doGet(HttpServletRequest req, HttpServletResponse res) {
// let client browser expect plain text
resp.setContentType("text/plain");

// this line will be seen in the browser
System.out.println("Hey, browser!");

// this line goes to error logs or such
System.err.println("Hey, error log!");
}


This behaviour is not consistent across servlet engines, so portable calls should be used.
To output text to client, use:


 
public void doGet(HttpServletRequest req, HttpServletResponse res) {
// get proper PrintWriter
PrintWriter pw = resp.getWriter();

// this line will be seen in the browser
w.println("Hey, browser!");
}


To output text to servlet engine error logs, use ServletContext.log():


 
public void doGet(HttpServletRequest req, HttpServletResponse res) {
// get ServletContext
ServletContext sc = getServletContext();

// this line will be seen in servlet engine logs
sc.log("Hey, browser!");
}




Further Information
Author of answer: hal9000

Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.