views:

47

answers:

4

I have a servlet. But it is not working as desired. Hence for debugging purposes, I want to print statements to the java console(the one that can be opened using the java icon in taskbar). However, if I use System.out.println("message"), it doesnt display in java console.

Is there any alternative way where I can display messages to the console from the servlet? Or can anyone suggest me an alternative way to display messages to any other console?

A: 

You should use logging, e.g. java.util.logging or log4j

Example of relevant log4j configuration:

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;
  <appender name="console" class="org.apache.log4j.ConsoleAppender"> 
    <param name="Target" value="System.out"/> 
    <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/> 
    </layout> 
  </appender> 

  <root> 
    <priority value ="debug" /> 
    <appender-ref ref="console" /> 
  </root>

uthark
I liked your java.util.logging idea. reading more about it
mithun1538
Sure. In production always some logging framework is used. You can log your debug messages with debug level and then just disable debug level in logger configuration.
uthark
+2  A: 

In which console do you expect it to appear?

Depending on the servlet container (I assume Tomcat), the logs are stored in a logs folder. For Tomcat this is tomcat/logs (or more often referred to as CATALINA_HOME/logs). If you are running it from within an IDE - they should be in the IDE console.

As a sidenote, using System.out isn't advisable for a real product - use a logging framework (like log4j).

Bozho
I am using System.out only for debugging. The servlet isnt running as intended, hence need to check the flow. After i fix it, i'll be removing them from the code.
mithun1538
+2  A: 

I want to print statements to the java console(the one that can be opened using the java icon in taskbar)

You need to realize that servlets actually runs at the server side, not at the client side. Those are in fact two physically different environments which communicates with each other through the network using the HTTP protocol. That Java console will only work for Java programs which runs at the client side, such as applets and JNLP.

In your case, you just need to read the server logs. That's where the stdout will by default print to, or use a better configureable logging framework such as logback. The server logs are usually found in a straightforward folder/file in the server installation directory, such as /logs in case of Tomcat.

BalusC
I had a slight feeling that java console was only for client side apps. Got it confirmed now. :)
mithun1538
A: 

Look for the log file in the log-folder of your servlet container (i.e. Tomcat).

As suggested use log4j to generate debug messages. This logging framework provides a configuration file where you can set things like where the logs should be written into or how the log messages should be formatted. As soon it's in place you can replace

System.out.println("message");

with

log.debug("your debug message");

or

log.info("in case of a message with info character");
log.error("routine foo has experienced an exception", e);

Now your code is much cleaner and there is even another benefit - when being placed correctly these logs act as documentation for your code segments.

david