package myPkg;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class InitialCompanyNameListener implements ServletContextListener {
private static final String DEFAULT_NAME = "MISSING-COMPANY-NAME";
public void contextInitialized(ServletContextEvent event) {
ServletContext context = event.getServletContext();
setInitialAttribute(context,"companyName",DEFAULT_NAME);
setInitialAttribute(context,"formerCompanyName","");
}
public void contextDestroyed(ServletContextEvent event) {}
private void setInitialAttribute(ServletContext context,String initParamName,String defaultValue)
{
String initialValue = context.getInitParameter(initParamName);
if (initialValue != null) {
context.setAttribute(initParamName, initialValue);
}
else
{
context.setAttribute(initParamName, defaultValue);
}
System.out.println(initialValue+"PARAM: " + initParamName);
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<context-param>
<param-name>companyName</param-name>
<param-value>not-dot-com.com</param-value>
</context-param>
<context-param>
<param-name>formerCompanyName</param-name>
<param-value>hot-dot-com.com</param-value>
</context-param>
<listener>
<listener-class>coreservlets.listeners.InitialCompanyNameListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>