一、 web.xml配置 javascript
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>java
"webapp.root"这个字符串能够随便写任何字符串。若是不配置默认值是"webapp.root"。
能够用System.getProperty("webapp.root")来动态获项目的运行路径。
通常返回结果例如:/usr/local/tomcat6/webapps/项目名
二、解决如下报错web
部署在同一容器中的Web项目,要配置不一样的<param-value>,不能重复,不然报相似下面的错误:
Web app root system property already set to different value: 'webapp.root' = [/home/user/tomcat/webapps/project1/] instead of [/home/user/tomcat/webapps/project2/] - Choose unique values for the 'webAppRootKey' context-param in your web.xml files!
意思是“webapp.root”这个key已经指向了项目1,不能够再指向项目2.
三、加载方式spring
Spring经过org.springframework.web.util.WebAppRootListener 这个监听器来运行时的项目路径。
可是若是在web.xml中已经配置了 org.springframework.web.util.Log4jConfigListener这个监听器,
则不须要配置WebAppRootListener了。由于Log4jConfigListener已经包含了WebAppRootListener的功能
通常配置类型下面的例子:tomcat
Xml代码
<!-- 加载Log4J 配置文件 --> webapp
<context-param> spa
<param-name>log4jConfigLocation</param-name> orm
<param-value>WEB-INF/conf/log4j.properties</param-value> xml
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>3000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
四、在运行时动态的找出项目的路径
在log4j.properties配置文件,就能够按下面的方式使用${webapp.root}: log4j.appender.file.File=${webapp.root}/WEB-INF/logs/sample.log 就能够在运行时动态的找出项目的路径