简体   繁体   English

动态获取Apache Tomcat服务器路径和端口

[英]Getting Apache Tomcat server path and port dynamically

I have developed an API in Eclipse deployed on Apache Tomcat Server which modifies an XML file kept in the ROOT folder of the server. 我在Apache Tomcat Server上部署了Eclipse中API ,它修改了保存在服务器ROOT文件夹中的XML文件。

Apache Tomcat is installed in the D drive in my system and hence I've hard-coded that path in the API like this Apache Tomcat安装在我系统的D驱动器中,因此我在API中对此路径进行了硬编码

new File("D:\\Program Files\\Apache Software Foundation\\Tomcat 7.0\\webapps\\ROOT\\example\\example.xml")

Also I've configured the server to use port 8080 hence I've also hard-coded the URI path in the API like this 此外,我已将服务器配置为使用端口8080,因此我还在API中对URI路径进行了硬编码

String uriPath = "http://localhost:8080/example/example2";

And it works fine. 它工作正常。

But now I want to deploy the same API in the form of a WAR file on different systems having Apache Tomcat. 但现在我想在具有Apache Tomcat的不同系统上以WAR文件的形式部署相同的API。

How do I obtain the Apache Tomcat ROOT folder path and the port number for those systems programmatically so that one API works for all systems? 如何以编程方式获取这些系统的Ap​​ache Tomcat ROOT文件夹路径端口号 ,以便一个API适用于所有系统? How do I integrate that into the API? 如何将其集成到API中?

There are a number of reasons within the JEE specification that state why you shouldn't do this but, that aside, you can get a platform agnostic way of determining the tomcat location. JEE规范中有许多原因说明了为什么不应该这样做,但除此之外,您可以获得一种确定tomcat位置的平台无关的方法。

The system defines two variables on startup, CATALINA_HOME and CATALINA_BASE. 系统在启动时定义了两个变量,CATALINA_HOME和CATALINA_BASE。 You should be able to get CATALINA_BASE from the system using System.getProperty("catalina.base") 您应该能够使用System.getProperty(“catalina.base”)从系统获取CATALINA_BASE

From there you could hypothetically build up a path like this: 从那里你可以假设建立一个这样的路径:

final String catalinaBase= System.getProperty("catalina.base");
final File catalinaBaseDir= new File(catalinaBase);
final File exampleXML= new File(catalinaBase, "webapps/root/example/example.xml");

As for the URI path, this article should provide you with enough reference to do what you want. 至于URI路径, 本文应该为您提供足够的参考来做您想做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM