I wish to set up a few application wide variables with servletContext.setAttributes on servlet context initialization phase .How can I achieve this.
Implement javax.servlet.SevletContextListener
which gets a callback when javax.servlet.ServletContext
is initialized.
Here is the example:
public class MyServletContextListener implements ServletContextListener
{
public void contextInitialized(ServletContextEvent sce)
{
ServletContext sc = sce.getServletContext();
//do your initialization here.
sc.setAttribute(.....);
}
public void contextDestroyed(ServletContextEvent sce)
{
ServletContext sc = sce.getServletContext();
//do your cleanup here
}
}