简体   繁体   中英

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 is installed in the D drive in my system and hence I've hard-coded that path in the API like this

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

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.

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? How do I integrate that into the 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.

The system defines two variables on startup, CATALINA_HOME and CATALINA_BASE. You should be able to get CATALINA_BASE from the system using System.getProperty("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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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