【问题标题】:How to get the full URL from deployed WAR file in a tomcat server如何从 tomcat 服务器中部署的 WAR 文件中获取完整 URL
【发布时间】:2018-05-03 13:24:52
【问题描述】:

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

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

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

【问题讨论】:

    标签: java maven war tomcat8


    【解决方案1】:

    网址:“http://localhost:8080/

    网址:“http://localhost:8080/test/

    Uri: "/test/" 在 java 代码中

    String uri = request.getContextPath();
    

    Request 是类 HttpServletRequest
    部署服务器 tomcat 时,如果文件 .war 名称为 ROOT.war --> URL 为 http://localhost:8080/
    使用文件 abc.war --> URL 是 http://localhost:8080/abc/ ---> abc 是上下文路径

    【讨论】:

    • 是否可以在您的 java 代码中的 war 文件中实例化 HttpServletRequest?我试过了(new HttpServletRequest()),但它需要我实现一堆方法..
    • code java : new HttpServletRequest() 不正确,使用 java servlet jsp 时方法 doGet(HttpServletRequest request) 或 doPost(HttpServletRequest request) 中的 HttpServletRequest 请求
    猜你喜欢
    • 2012-10-29
    • 2018-07-25
    • 1970-01-01
    • 2018-10-24
    • 2012-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    相关资源
    最近更新 更多