Loading data on application startup in ServletContext


Web tier: servlets, JSP, Web frameworks: Loading data on application startup in ServletContext

  1. Hi,

    I would like to load a collection of data into an application scope attribute (ServletContext.setAttribute()) upon startup of my application. Is there an event, handler that I can used to do this upon application startup.

  2. try this[ Go to top ]

    I think you can try this method ,
    config the web.xml use the following:
        <display-name>Start Util Servlet</display-name>

  3. Or you can try the struts 'plug-in' feature
  4. You would add ContextListener class and define it in web.xml
    public class ContextListener implements ServletContextListener {

        private ServletContext context = null;

        public void contextInitialized(ServletContextEvent event) {
            context = event.getServletContext();
        public void contextDestroyed(ServletContextEvent event) {

    In web.xml

    On startup your application method contextInitialized() will be called and you can add to context any attributes or anything else.

    Good luck :)
  5. Probe implement inteface javax.servlet.ServletContextListener