如何从 tomcat 服务器中部署的 WAR 文件中获取完整的 URL

How to get the full URL from deployed WAR file in a tomcat server

我想在服务器上部署 war 应用程序时获得完整的 URL。 假设我的应用程序 war 文件是 "test.war",我的 tomcat 安装在“http://localhost:8080/”。

我想要的是从此 war 文件中的 Java 代码获取“http://localhost:8080/test/”值,因为我必须将它们传递给其他服务。

我正在使用 maven 构建 war 文件,因此也欢迎任何特定于 maven 的解决方案。

URL: "http://localhost:8080/"

URL: "http://localhost:8080/test/"

Uri: java 代码中的“/test/”

String uri = request.getContextPath();

请求是 class HttpServletRequest
当部署服务器 tomcat if file .war with name is ROOT.war --> URL is http://localhost:8080/
使用文件 abc.war --> URL 是 http://localhost:8080/abc/ ---> abc 是上下文路径