【问题标题】:Get path to servlet container deploy/webapps folder获取 servlet 容器 deploy/webapps 文件夹的路径
【发布时间】:2011-06-28 23:13:06
【问题描述】:

从 servlet 的服务方法中,我可以使用 getServletContext.getRealPath("") 获取到 Web 应用程序根文件夹的路径,例如/tomcat/webapps/MyWebApplication,但是有什么方法可以让我获得 /tomcat/webapps/ 的路径或可选的其他特定 Web 应用程序,例如 /tomcat /webapps/MyOtherWebApplication(不诉诸诸如减去上下文路径之类的技巧)?

(仅以 TomCat 为例,应该适用于所有 servlet 容器。)

【问题讨论】:

    标签: servlets


    【解决方案1】:

    ServletContext 有一个 getContext(String) 方法 (javadoc),它返回对应于另一个 webapp 的 ServletContext,然后您可以调用 getRealPath()。但是,应用服务器的安全模型可能会禁止这样做,因此请谨慎使用。

    【讨论】:

      【解决方案2】:

      Tomcat 目录 (tomcat/bin) 的路径:

      File inputFile = new File("file.xml"); // tomcat_dir/bin/file.xml
      

      其他 Tomcat 目录的路径,例如“webapps”:

      File inputFile = new File("../webapps/file.xml"); // tomcat_dir/webapps/file.xml
      

      摆脱绝对路径的好方法:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-01
        相关资源
        最近更新 更多