在web.xml中加入监听器,用spring框架的 WebAppRootListener类
<listener>
<listener-class>
org.springframework.web.util.WebAppRootListener
</listener-class>
</listener>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>
然后在java类中可以用System.getProperty("webapp.root")来获取绝对路径