Servlet App Event Listeners

Servlet App Event Listeners

Deploying All Of The Above Listeners and Servlets


Listing 6: The deployment descriptor, web.xml, which contains the definitions for the listener classes and Servlet defined above.

<?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>

  <listener>
    <listener-class>
      com.listeners.ServletContextAttribListener
    </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>

  <servlet>
    <servlet-name>
      context attribs
    </servlet-name>
    <servlet-class>
      com.servlets.ServletContextAttrib
    </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>

  <servlet-mapping>
    <servlet-name>
      context attribs
    </servlet-name>
    <url-pattern>
      /servletcontextattrib
    </url-pattern>
  </servlet-mapping>

</web-app>


Working With Session Information

The application event listeners can also make notifications when an HttpSession object is created, destroyed, or has its attributes modified. This is another way the Servlet 2.3 specification is empowering the web application programmer.

It is possible to write one application event listener that handles all notifications for changes to the HttpSession objects. This could be done with ServletContext notifications, but the earlier examples kept them separate to show that method.

To receive lifecycle notifications for HttpSession objects write a Java class that implements the javax.Servlet.http.HttpSessionListener interface. This interface defines two methods and their signatures are (taken from the JavaDocs):

void sessionCreated (HttpSessionEvent se)
  Notification that a session was created.

void sessionDestroyed (HttpSessionEvent se)
  Notification that a session was invalidated.

To receive notifications that the attributes of an HttpSession object have been modified implement the javax.servlet.http.HttpSessionAttributesListener interface. This interface defines three methods and their signatures are (taken from the JavaDocs):

void attributeAdded (HttpSessionBindingEvent se)
  Notification that an attribute has been added to a session.

void attributeRemoved (HttpSessionBindingEvent se)
  Notification that an attribute has been removed from a session.

void attributeReplaced (HttpSessionBindingEvent se)
  Notification that an attribute has been replaced in a session.

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