如何从 Servlet 输出文本到控制台

发布于 2024-08-26 03:29:32 字数 202 浏览 2 评论 0原文

我有一个servlet。但它没有按预期工作。因此,出于调试目的,我想将语句打印到java控制台(可以使用任务栏中的java图标打开的控制台)。但是,如果我使用 System.out.println("message"),它不会显示在 java 控制台中。

有没有其他方法可以将消息从 servlet 显示到控制台? 或者有人可以建议我另一种向任何其他控制台显示消息的方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挖鼻大婶 2024-09-02 03:29:32

您希望它出现在哪个控制台中?

根据 servlet 容器(我假设是 Tomcat),日志存储在 logs 文件夹中。对于 Tomcat,这是 tomcat/logs(或更常称为 CATALINA_HOME/logs)。如果您从 IDE 中运行它 - 它们应该位于 IDE 控制台中。

作为旁注,对于真正的产品,不建议使用 System.out - 使用日志框架(例如 log4j)。

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

久而酒知 2024-09-02 03:29:32

Servlet (HttpServlet) 有一个方法 log(String s) 继承自 GenericServlet 类。

因此,您只需包含

log("output text")

在 servlet 的代码中即可查看输出。

如果您使用 Eclipse,日志会直接进入控制台。

如果您使用 IntellijIdea 日志进入运行 --> “Tomcat 本地主机日志”选项卡。

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.

雾里花 2024-09-02 03:29:32

我想将语句打印到java控制台(可以使用任务栏中的java图标打开的控制台)

您需要意识到 servlet 实际上运行在服务器端,不在客户端。实际上,这是两个物理上不同的环境,它们通过使用 HTTP 协议的网络相互通信。该 Java 控制台仅适用于在客户端运行的 Java 程序,例如 小程序JNLP。

就您而言,您只需要阅读服务器日志。这就是 stdout 默认打印到的地方,或者使用更好的可配置日志框架,例如 logback 。服务器日志通常位于服务器安装目录中的简单文件夹/文件中,例如 Tomcat 中的 /logs

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.

美人骨 2024-09-02 03:29:32

在 servlet 容器(即 Tomcat)的日志文件夹中查找日志文件。

正如建议使用 log4j 生成调试消息。该日志框架提供了一个配置文件,您可以在其中设置日志写入位置或日志消息应如何格式化等内容。一旦到位,您就可以替换

System.out.println("message");

log.debug("your debug message");

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

现在您的代码更加清晰,甚至还有另一个好处 - 当正确放置时,这些日志将充当您的代码段的文档。

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.

深白境迁sunset 2024-09-02 03:29:32

您应该使用日志记录,例如 java.util。 logginglog4j

相关 log4j 配置示例:

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

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文