How to ouput text to console from Servlet

2019-04-06 23:32发布

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?

5条回答
Deceive 欺骗
2楼-- · 2019-04-07 00:07

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.

查看更多
迷人小祖宗
3楼-- · 2019-04-07 00:07

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>

查看更多
Luminary・发光体
4楼-- · 2019-04-07 00:20

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

查看更多
Viruses.
5楼-- · 2019-04-07 00:21

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.

查看更多
混吃等死
6楼-- · 2019-04-07 00:21

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.

查看更多
登录 后发表回答