If you like, you can persist data beyond the initialization by creating static variables in your class.
See details here: http://www.java-tips.org/java-ee-tips/java-servlet/how-to-work-with-servletcontextlistener.html
Here's a quick version
package org.peter;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
public class ServletListener implements ServletContextListener {
private ServletContext context = null;
static ArrayListlf = null;
/*This method is invoked when the Web Application has been removed
and is no longer able to accept requests
*/
public void contextDestroyed(ServletContextEvent event) {
//Output a simple message to the server's console
System.out.println("The web app has been removed");
this.context = null;
}
//This method is invoked when the Web Application
//is ready to service requests
public void contextInitialized(ServletContextEvent event) {
this.context = event.getServletContext();
System.out.println("Web app started up");
lf.add("foo");
} // end of Initialized
}
And the entry in web.xml:
<listener>
<listener-class>
org.peter.ServletListener
</listener-class>
</listener>
No comments:
Post a Comment