2011-02-17 27 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 

很好的方式擺脫絕對路徑: )