开发者

How to ouput text to console from Servlet

开发者 https://www.devze.com 2022-12-23 08:28 出处:网络
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 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 开发者_Python百科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?


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).


Servlet (HttpServlet) has a method log(String s) inherited from GenericServlet class.

So you can just include

log("output text")

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.


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.


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.


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/">
  <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

精彩评论

暂无评论...
验证码 换一张
取 消