2011-02-17 26 views
0

从servlet的服务方法中,我可以获得带有getServletContext.getRealPath("")的Web应用程序根文件夹的路径,例如, /tomcat的/ webapps /下MyWebApplication,但有什么办法,我可以得到的路径/tomcat的/ webapps /目录或可选的其他特定的Web应用程序一样/tomcat的/ webapps /目录MyOtherWebApplication(而不是诉诸像从其减去黑客上下文路径)?获取servlet容器deploy/webapps文件夹的路径

(仅使用Tomcat作为一个例子,应该在所有的servlet容器的工作。)

回答

1

ServletContextgetContext(String)方法(javadoc)返回的ServletContext对应于另一个Web应用程序,然后你可以调用getRealPath()上。但是,应用程序服务器的安全模型可能会禁止此操作,请谨慎使用。

0

路径到Tomcat目录(Tomcat的/ bin中):

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

路径到其他Tomcat的目录中, “web应用”,例如:

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

很好的方式摆脱绝对路径: )