import javax.servlet.ServletContext; import javax.servlet.ServletContextListener; import javax.servlet.ServletContextEvent; public class AppLifeCycleEventDemo implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { System.out.println("Initializing Application …"); // Load the JDBC driver try { Class.forName("org.gjt.mm.mysql.Driver "); } catch (ClassNotFoundException e) { System.out.println(e.toString()); } // Get the ServletContext object ServletContext servletContext = sce.getServletContext(); // Set a ServletContext attribute servletContext.setAttribute("dbUrl", "jdbc:mysql///Fred"); System.out.println("Application initialized"); } public void contextDestroyed(ServletContextEvent cse) { System.out.println("Application shut down"); } }