001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.util; 018 019import javax.servlet.ServletContextEvent; 020import javax.servlet.ServletContextListener; 021 022/** 023 * Listener that sets a system property to the web application root directory. 024 * The key of the system property can be defined with the "webAppRootKey" init 025 * parameter at the servlet context level (i.e. context-param in web.xml), 026 * the default key is "webapp.root". 027 * 028 * <p>Can be used for toolkits that support substitution with system properties 029 * (i.e. System.getProperty values), like log4j's "${key}" syntax within log 030 * file locations. 031 * 032 * <p>Note: This listener should be placed before ContextLoaderListener in {@code web.xml}, 033 * at least when used for log4j. Log4jConfigListener sets the system property 034 * implicitly, so there's no need for this listener in addition to it. 035 * 036 * <p><b>WARNING</b>: Some containers, e.g. Tomcat, do NOT keep system properties separate 037 * per web app. You have to use unique "webAppRootKey" context-params per web app 038 * then, to avoid clashes. Other containers like Resin do isolate each web app's 039 * system properties: Here you can use the default key (i.e. no "webAppRootKey" 040 * context-param at all) without worrying. 041 * 042 * <p><b>WARNING</b>: The WAR file containing the web application needs to be expanded 043 * to allow for setting the web app root system property. This is by default not 044 * the case when a WAR file gets deployed to WebLogic, for example. Do not use 045 * this listener in such an environment! 046 * 047 * @author Juergen Hoeller 048 * @since 18.04.2003 049 * @see WebUtils#setWebAppRootSystemProperty 050 * @see Log4jConfigListener 051 * @see System#getProperty 052 */ 053public class WebAppRootListener implements ServletContextListener { 054 055 @Override 056 public void contextInitialized(ServletContextEvent event) { 057 WebUtils.setWebAppRootSystemProperty(event.getServletContext()); 058 } 059 060 @Override 061 public void contextDestroyed(ServletContextEvent event) { 062 WebUtils.removeWebAppRootSystemProperty(event.getServletContext()); 063 } 064 065}