How to output text to the console from a servlet

I have a servlet. But he does not work as he wishes. Therefore, for debugging purposes, I want to print statements in the java console (the one that can be opened using the java icon in the taskbar). However, if I use System.out.println ("message"), it does not appear in the java console.

Is there an alternative way to display messages on the console from a servlet? Or can someone suggest me an alternative way to display messages on any other console?

+6
java logging console servlets jconsole
source share
5 answers

Which console do you expect to appear on?

Depending on the servlet container (I assume Tomcat) the logs are stored in the logs folder. For Tomcat, this is tomcat/logs (or more commonly called CATALINA_HOME/logs ). If you run it from the IDE, they should be in the IDE console.

As a side element, using System.out not practical for a real product - use a registration framework (for example, log4j ).

+7
source share

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

You need to understand that servlets are actually running on the server side, not the client side. These are actually two physically different environments that communicate with each other over the network using the HTTP protocol. This Java console will only work for Java programs that run on the client side, such as applets and JNLP .

In your case, you just need to read the server logs. Where stdout will print by default or use a more convenient custom logging framework such as logback . Server logs are usually located in a simple folder / file in the server installation directory, for example /logs in the case of Tomcat.

+2
source share

Locate the log file in the log folder of your servlet container (i.e. Tomcat).

As suggested using log4j to generate debugging messages. This logging structure provides a configuration file in which you can set options such as where the logs should be written or how the log messages should be formatted. Once it is in place, you can replace

 System.out.println("message"); 

from

 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 another advantage - when placed correctly, these logs act as documentation for your code segments.

+1
source share

The servlet (HttpServlet) has a method log (String s) inherited from the GenericServlet class.

So you can just turn on

 log("output text") 

in the servlet code and see the output.

If you use the Eclipse log, it goes straight to the console.

If you use the IntellijIdea log, go to Run → "Tomcat Localhost Log".

+1
source share

You should use logging, for example. java.util.logging or log4j

An example of the appropriate log4j configuration:

 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <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> 

0
source share

All Articles