如何在Java中获取Tomcat版本号 [英] How to get Tomcat version number in Java
问题描述
如何在JAVA中获取Tomcat/Catalina版本号?
How do I get the Tomcat/Catalina version number in JAVA?
我已经看到了很多通过命令行等方法来做的事情,但这不是我可以使用的代码,我也不能使用catalina.path,因为已经从路径中剥离了版本号.
I've seen lots of how to do it via command line etc. but that's not code I can use, I also cannot use catalina.path as the version number has been stripped from the path.
还请注意,我想在代码中使用该版本,因此我看过的各种JSP解决方案也对我不起作用.
Please also note I want to use the version in code, so the various JSP solutions that I've looked at also do not work for me.
谢谢
推荐答案
来自JSP
在一个jsp文件中,您可以打印出这样的版本:
From a JSP
In a jsp file you can print out the version like this:
Tomcat Version : <%= application.getServerInfo() %>
输出:
Tomcat Version : Apache Tomcat/8.0.14
JSP之外(任何Java代码)
如果您希望在JSP之外使用它(例如在Servlet,侦听器或w/e中),请查看
Outside of JSP (Any Java Code)
If you want it outside of a JSP (e.g. in a Servlet or listener or w/e) take a look at the org.apache.catalina.util.ServerInfo
class, it has some nice static methods:
System.out.println(ServerInfo.getServerBuilt());
System.out.println(ServerInfo.getServerInfo());
System.out.println(ServerInfo.getServerNumber());
输出:
Sep 24 2014 09:01:51
Apache Tomcat/8.0.14
8.0.14.0
所以您真正想要的是ServerInfo.getServerNumber()
.
注意:
ServerInfo
类还具有一个main()
方法,因此它也可以作为独立的应用程序运行:
The ServerInfo
class also has a main()
method so it can be run as a standalone application too:
java -cp lib/catalina.jar org.apache.catalina.util.ServerInfo
示例输出:
Server version: Apache Tomcat/8.0.14
Server built: Sep 24 2014 09:01:51
Server number: 8.0.14.0
OS Name: Windows 7
OS Version: 6.1
Architecture: x86
JVM Version: 1.7.0_55-b13
JVM Vendor: Oracle Corporation
这篇关于如何在Java中获取Tomcat版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!