获取servlet容器deploy / webapps文件夹的路径

时间:2011-02-17 09:31:00

标签: servlets

在servlet的服务方法中,我可以使用getServletContext.getRealPath("")获取Web应用程序根文件夹的路径,例如 / tomcat / webapps / MyWebApplication ,但有什么方法可以获得 / tomcat / webapps / 或者某些特定的其他Web应用程序(如 / tomcat)的路径/ webapps / MyOtherWebApplication (不依赖于像上下文路径这样的黑客攻击)?

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

2 个答案:

答案 0 :(得分:1)

ServletContext有一个getContext(String)方法(javadoc),可以返回与其他网络应用相对应的ServletContext,然后您可以调用getRealPath()。但是,appserver的安全模型可能会禁止此操作,因此请谨慎使用。

答案 1 :(得分:0)

Tomcat dir(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

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

相关问题