Servlet App Event Listeners

Servlet App Event Listeners

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.

Prev  [1] [2] [3] [4] [5] [6] [7] Next

Close    To Top
  • Prev Article-Java:
  • Next Article-Java:
  • Now: Tutorial for Web and Software Design > Java > Java Servlets > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction