如何从 Servlet 输出文本到控制台
我有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您希望它出现在哪个控制台中?
根据 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 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).Servlet (HttpServlet) 有一个方法 log(String s) 继承自 GenericServlet 类。
因此,您只需包含
在 servlet 的代码中即可查看输出。
如果您使用 Eclipse,日志会直接进入控制台。
如果您使用 IntellijIdea 日志进入运行 --> “Tomcat 本地主机日志”选项卡。
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.
您需要意识到 servlet 实际上运行在服务器端,不在客户端。实际上,这是两个物理上不同的环境,它们通过使用 HTTP 协议的网络相互通信。该 Java 控制台仅适用于在客户端运行的 Java 程序,例如 小程序 和 JNLP。
就您而言,您只需要阅读服务器日志。这就是
stdout
默认打印到的地方,或者使用更好的可配置日志框架,例如 logback 。服务器日志通常位于服务器安装目录中的简单文件夹/文件中,例如 Tomcat 中的/logs
。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.在 servlet 容器(即 Tomcat)的日志文件夹中查找日志文件。
正如建议使用 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.
您应该使用日志记录,例如 java.util。 logging 或 log4j
相关 log4j 配置示例:
You should use logging, e.g. java.util.logging or log4j
Example of relevant log4j configuration: