Saturday, March 7, 2009

Servlet Application Event Listeners

Sun Microsystems released the proposed final draft of the Servlet 2.3 specification on October 20, 2000. Although the Servlet 2.3 specification is not in its final version yet, we do not have to wait any longer to play with the exciting new features defined by it. Apache's Tomcat has released a build, version 4.0 beta 1 that fully supports the Servlet 2.3 proposed final draft specification.

Link: Writing a Simple Listener
Link: Deploying Servlet Listeners

Code Snippets
Listing 1: An example of a very simple Servlet Event Listener(MyContextListener.java)


package com.listeners;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributesListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public final class MyContextListener
implements ServletContextListener {

private ServletContext context = null;
public MyContextListener() {}

//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 Simple 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();
//Output a simple message to the server's console
System.out.println("The Simple Web App. Is Ready");
}
}



Link: Link to Complete Article

No comments:

Post a Comment