Listeners and Events

Listeners and events for Web applications are another new feature introduced in the Servlet API 2.3 specification. The event-listener model was introduced from JDK 1.1 in the AWT package. You can define a listener for any AWT UI (user interface) element. Any actions or events occurring on the UI element are processed by the AWT listener. The same event-listener model has been incorporated in the servlet specification. Now you can define listeners for two kinds of events:

  • ServletContext events

  • HttpSession events

Depending on the event to be listened to, the listener class has to override the appropriate interface and implement the methods required.

Now look in detail at the two main events.

ServletContext Events

Five events of the ServletContext can have listeners:

  • Create ServletContext This event occurs when the servlet is created for the first time. To write a listener for this event, you must override the javax.servlet.ServletContextListener interface and implement the contextInitialized() method. The method is passed a ServletContextEvent object by the WebLogic Server.

  • Destroy ServletContext This event occurs when the servlet is destroyed.To write a listener for this event, you must override the javax.servlet.ServletContextListener interface and implement the contextDestroyed() method. The method is passed a ServletContextEvent object by the WebLogic Server.

  • Attribute added in ServletContext This event occurs when an attribute is added to the ServletContext. You must write a listener that implements the attributeAdded() method of the ServletContextAttributesListener interface to intercept this event. The method is passed a ServletContextAttributeEvent object by the WebLogic Server.

  • Attribute removed from ServletContext This event occurs when an attribute is removed from the ServletContext. You must write a listener that implements the attributeRemoved() method of the ServletContextAttributesListener interface to intercept this event. The method is passed a ServletContextAttributeEvent object by the WebLogic Server.

  • Attribute replaced in ServletContext This event occurs when an attribute is replaced in the ServletContext. You must write a listener that implements the attributeReplaced() method of the ServletContextAttributesListener interface to intercept this event. The method is passed a ServletContextAttributeEvent object by the WebLogic Server.

HttpSession Events

The HttpSession events are described here. Five events of the HttpSession event can have listeners:

  • Create HttpSession This event occurs when an HttpSession object is created. To write a listener for this event, you must override the javax.servlet.HttpSessionListener interface and implement the sessionCreated() method. The method is passed an HttpSessionEvent object by the WebLogic Server.

  • Destroy HttpSession This event occurs when the servlet is destroyed. To write a listener for this event, you must override the javax.servlet.HttpSessionListener interface and implement the sessionDestroyed() method. The method is passed an HttpSessionEvent object by the WebLogic Server.

  • Attribute added in HttpSession You must write a listener that implements the attributeAdded() method of the HttpSessionAttributesListener interface to intercept this event. The method is passed an HttpSessionBindingEvent object by the WebLogic Server.

  • Attribute removed from HttpSession This event occurs when an attribute is removed from the HttpSession object. You must write a listener that implements the attributeRemoved() method of the HttpSessionAttributesListener interface to intercept this event. The method is passed an HttpSessionBindingEvent object by the WebLogic Server.

  • Attribute replaced in HttpSession This event occurs when an attribute is replaced in the HttpSession. You must write a listener that implements the attributeReplaced() method of the HttpSessionAttributesListener interface to intercept this event. The method is passed an HttpSessionBindingEvent object by the WebLogic Server.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net