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?
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.You should use logging, e.g. java.util.logging or log4j
Example of relevant log4j configuration:
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 istomcat/logs
(or more often referred to asCATALINA_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).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
with
or
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.
Servlet (HttpServlet) has a method log(String s) inherited from GenericServlet class.
So you can just include
in the servlet's code and see output.
If you use Eclipse log goes right into console.
If you use IntellijIdea log goes into Run --> "Tomcat Localhost Log" tab.