首先在 web.xml文件里面配置
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>web.root</param-value>
</context-param>
然后在java类里面可以通过
System.getProperty("web.root")
例如有项目A部署在tomcat里面,那么通过这样得到的路径就是
D:/tomcat/webapps/A/