Search This Blog

Thursday, February 4, 2010

Java Servlet API - ServletContextListener Usage Example Code

Whenever the web application got instantiated, it initializes the class which implements ServletContextListener, and calls contextDestroyed() method. Also the application context calls contextDestroyed() method when the application getting shutting down. We can use this type of classes to initialize application sepecific processes.The example for ServletContextListener is given below.

package com.test.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public final class ApplicationListenerContext implements ServletContextListener {

public void contextInitialized(ServletContextEvent scEvent) {
* This method will get called when the application instance got created.
* Initialiaze required process here.
public void contextDestroyed(ServletContextEvent arg0) {
* This method will get called when the application instance is shutting down.
* Shutdown initialined processes here.

Also add below entry to web.xml to register context listener.


No comments:

Post a Comment