ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Servlet App Event Listeners
Pages: 1, 2, 3, 4, 5, 6, 7

Deploying a Simple Servlet Context Application Lifecycle Event Listener

Application lifecycle event listeners are deployed within the web archive (.war) file and are defined in the deployment descriptor, web.xml, for the web application. The lifecycle event listeners are defined using <listener> tags. The order you define the listener classes does matter;, they will be executed in the order you specify. Listing 2 shows the web.xml file that defines the application lifecycle event listener that was shown in Listing 1.


Listing 2: The web.xml file that defines the application lifecycle event listener listed above (MyContextListener.java)



<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<web-app>

   <!-- Define application events listeners -->
  <listener>
    <listener-class>
     com.listeners.MyContextListener
    </listener-class>
  </listener>

  <!-- Define servlets that are included in the example application -->
  <servlet>
    <servlet-name>
     simple
    </servlet-name>
    <servlet-class>
     com.servlets.SimpleServlet
    </servlet-class>
  </servlet>

  <servlet>
   <servlet-name>
   bank
   </servlet-name>
   <servlet-class>
   com.servlets.BankBalance
   </servlet-class>
  </servlet>

  <!-- Define servlet mappings to urls -->
  <servlet-mapping>
    <servlet-name>
     simple
    </servlet-name>
    <url-pattern>
     /simple
    </url-pattern>
  </servlet-mapping>

  <servlet-mapping>
   <servlet-name>
   bank
   </servlet-name>
   <url-pattern>
   /bank
   </url-pattern>
  </servlet-mapping>
</web-app>


There are a few items to notice. The listener tag comes before the servlet definitions in the web.xml file. Also, the <listener> tag has a child element, <listener-class>, that defines the application lifecycle event listener.

Pages: 1, 2, 3, 4, 5, 6, 7

Next Pagearrow