Defining a ServletContext Listener for a J2EE Web Application


First create a Java class file like the code below;

ApplicationContextListener.java

package com.listeners;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
* Web application life cycle listener.
* @author tunatore
*/
public class ApplicationContextListener implements ServletContextListener {

public void contextInitialized(ServletContextEvent sce) {
System.out.println(“contextInitialized is called!!”);
}

public void contextDestroyed(ServletContextEvent sce) {
System.out.println(“contextDestroyed is called!!”);
}
}

Edit the web.xml file and add following lines;

<?xml version=”1.0″ encoding=”UTF-8″?>

<xmlns=”http://java.sun.com/xml/ns/javaee&#8221; xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd“>
    <display-name>XMLHttpRequestExample</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <listener>
        <description>ServletContextListener</description>
        <listener-class>com.listeners.ApplicationContextListener</listener-class>
    </listener>
</web-app>

Deploy the application to the Application servers such as Tomcat; and the system.out.log will contain following lines;

Using CATALINA_BASE:   “C:\Documents and Settings\tunat\.netbeans\6.9\apache-tomcat-6.0.26_base”
Using CATALINA_HOME:   “C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.26”
Using CATALINA_TMPDIR: “C:\Documents and Settings\tunat\.netbeans\6.9\apache-tomcat-6.0.26_base\temp”
Using JRE_HOME:        “C:\Program Files\Java\jdk1.6.0_23”
Using CLASSPATH:       “C:\Program Files\Apache Software Foundation\Apache Tomcat 6.0.26\bin\bootstrap.jar”
09.May.2011 20:41:36 org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.1.20.
09.May.2011 20:41:36 org.apache.catalina.core.AprLifecycleListener init
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
09.May.2011 20:41:37 org.apache.coyote.http11.Http11AprProtocol init
INFO: Initializing Coyote HTTP/1.1 on http-8084
09.May.2011 20:41:37 org.apache.coyote.ajp.AjpAprProtocol init
INFO: Initializing Coyote AJP/1.3 on ajp-8009
09.May.2011 20:41:37 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 693 ms
09.May.2011 20:41:37 org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
09.May.2011 20:41:37 org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.26
09.May.2011 20:41:37 org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor FilterExample.xml
09.May.2011 20:41:37 org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor manager.xml
09.May.2011 20:41:37 org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor ROOT.xml
09.May.2011 20:41:37 org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor TestWebServiceMainAppClient.xml
09.May.2011 20:41:37 org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor VaadinExample.xml
contextInitialized is called!!
09.May.2011 20:41:37 org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor XMLHttpRequestExample.xml
09.May.2011 20:41:37 org.apache.coyote.http11.Http11AprProtocol start
INFO: Starting Coyote HTTP/1.1 on http-8084
09.May.2011 20:41:37 org.apache.coyote.ajp.AjpAprProtocol start
INFO: Starting Coyote AJP/1.3 on ajp-8009
09.May.2011 20:41:37 org.apache.catalina.startup.Catalina start
INFO: Server startup in 557 ms

After sending stop request to the Application server;

09.May.2011 20:42:51 org.apache.coyote.http11.Http11AprProtocol pause
INFO: Pausing Coyote HTTP/1.1 on http-8084
09.May.2011 20:42:51 org.apache.coyote.ajp.AjpAprProtocol pause
INFO: Pausing Coyote AJP/1.3 on ajp-8009
09.May.2011 20:42:52 org.apache.catalina.core.StandardService stop
INFO: Stopping service Catalina
contextDestroyed is called!!
09.May.2011 20:42:52 org.apache.coyote.http11.Http11AprProtocol destroy
INFO: Stopping Coyote HTTP/1.1 on http-8084
09.May.2011 20:42:52 org.apache.coyote.ajp.AjpAprProtocol destroy
INFO: Stopping Coyote AJP/1.3 on ajp-8009

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s