Disney web site - */ public void contextInitialized( ServletContextEvent event ){ this.context

*/ public void contextInitialized( ServletContextEvent event ){ this.context = event.getServletContext( ); // Initialize the logging service here // Log a message that the listener has started log( “LoggingListener initialized” ); } /** * Called by the container when the ServletContext is about * ready to be removed. This is a good time to clean up * any open resources. */ public void contextDestroyed( ServletContextEvent event ){ // Clean up the logging service here // Log a message that the LoggingListener has been stoppedlog( “LoggingListener destroyed” ); } /** * Log a message to the servlet context application log or * system out if the ServletContext is unavailable. */ protected void log( String message ) { if (context != null){ context.log(”LoggingListener: ” + message ); }else{ System.out.println(”LoggingListener: ” + message); } } } The event-listener class in Example 15-3 contains two methods that are invoked by the web container: public void contextInitialized( ServletContextEvent event ); public void contextDestroyed( ServletContextEvent event ); The web container calls the contextInitialized( )method before the first request is processed. You should initialize any needed resources in this method. For example, this is an ideal location to initialize a logging service. The contextDestroyed( )method is called when the web application is taken out of service. This is where any open resources that the listener class is using should be closed. Because there can be multiple event-listener classes for the same event, let’s use another ServletContext event-listener class to make our example more realistic. Example 15-4 shows the DBConnectionPoolListener class. Both the LoggingListener and the
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Leave a Reply