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.
In Java, including Advanced Java, System.out
and System.err
are both instances of PrintStream
that represent standard output and standard error streams, respectively.
In the context of a Servlet:
System.out
(Standard Output):- It is generally used for normal output, such as regular log messages or responses.
- The data sent to
System.out
is typically directed to the console or log files. - In a servlet, writing to
System.out
may result in the output being captured by the servlet container’s logging mechanism.
System.err
(Standard Error):- It is used for error messages or exceptional situations.
- The data sent to
System.err
is typically directed to the console or log files as well, but it is intended for error-related information. - In a servlet, writing to
System.err
is often used to report errors or exceptional conditions.
In summary, the key difference lies in the intended use of these streams. System.out
is for regular output, while System.err
is for error-related output. When working with servlets, it’s a good practice to use logging frameworks like Log4j or SLF4J for more fine-grained control over logging and to avoid direct use of System.out
or System.err
.