July 26, 2009

Servlet Listener

« Servlet Filter | Main | HTMLParser »
The Servlet API Specification defines the javax.servlet.ServletContextListener as a lifecycle event listener interface. It reacts to basic javax.servlet.ServletContext events:
//ready to process requests
void contextInitialized (ServletContextEvent sce);
//shutdown event of servlet context
void contextDestroyed (ServletContextEvent sce);
Basic Listener class:
public class ExampleContextListener
    implements ServletContextListener {
  public void contextDestroyed(ServletContextEvent event) {
    System.out.println("Web app was removed.");
  }
  public void contextInitialized(ServletContextEvent event) {
    System.out.println("Web app is ready.");
  }
}

ServletContextListeners will be used as pre or post hook handlers for example to initialize configurations or other things. For example the Spring ContextLoaderListener is used for initialization of the configured Spring dependency injection on start of the web application.

After you implemented the ServletContextListener interface you have only add your class in the web.xml file.

...
<listener>
  <listener-class>ExampleContextListener</listener-class>
</listener>
...
Regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 5:34 PM in Uncategorized

 

[Trackback URL for this entry]

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
« July »
SunMonTueWedThuFriSat
   1234
567891011
12131415161718
19202122232425
262728293031