Sunteți pe pagina 1din 10

Servlet Listeners

Presented By: Ashish Tiwari


Application Events and
Listeners

• Application events provide notifications of a change in state of the servlet context


(each Web Application uses its own servlet context) or of an HTTP session object.
You write event listener classes that respond to these changes in state and you
configure and deploy Application event and listener classes in a Web Application.
• For servlet context events, the event listener classes can receive notification when
the Web Application is deployed or is being undeployed (or when WebLogic Server
shuts down), and when attributes are added, removed, or replaced.
• For HTTP session events, the event listener classes can receive notification when
an HTTP session is activated or is about to be passivated, and when an HTTP
session attribute is added, removed, or replaced.
• Use Web Application events to:
• Manage database connections when a Web Application is deployed or shuts down
• Create counters
• Monitor the state of HTTP sessions and their attributes.

© Copyrights aurionPro Solutions Ltd.


Servlet Context Events

• The following table lists the types of Servlet context


events, the interface your event listener class must
implement to respond to the event, and the methods
invoked when the event occurs.

© Copyrights aurionPro Solutions Ltd.


HTTP Session Events

• The following table lists the types of HTTP session events, the interface your
event listener class must implement to respond to the event, and the methods
invoked when the event occurs.

• Note: The Servlet 2.3 specification also contains the


javax.servlet.http.HttpSessionBindingListener and the
javax.servlet.http.HttpSessionActivationListener interfaces.
• These interfaces are implemented by objects that are stored as session
attributes and do not require registration of an event listener in web.xml.

© Copyrights aurionPro Solutions Ltd.


Configuring an Event Listener

• To configure an event listener:


• In web.xml add an event declaration using the <listener> element. The event
declaration defines the listener class that is invoked when the event occurs.
The <listener> element must directly follow the <filter> and <filter-mapping>
elements and directly precede the <servlet> element.
• Can specify more than one listener class for each type of event.
• WebLogic Server invokes the event listeners in the order that they appear in
the deployment descriptor (except for shutdown events, which are invoked in
the reverse order).
• For Example:
<listener>
  <listener-class>myApp.myContextListenerClass</listener-class>
</listener>
<listener>
  <listener-class>myApp.mySessionAttributeListenerClass</listener-
class>
</listener>
• Write and deploy the Listener class.

© Copyrights aurionPro Solutions Ltd.


Writing a Listener Class

• To write a listener class:


 Create a new class that implements the appropriate interface for the type
of event your class responds to.
 Create a public constructor that takes no arguments.
 Implement the required methods of the interface.
 Copy the compiled event listener classes into the WEB-INF/classes
directory of the Web Application, or package them into a jar file and copy
the jar file into the WEB-INF/lib directory of the Web Application.

• The following useful classes are passed into the listener methods in a listener class:
 javax.servlet.http.HttpSessionEvent
 provides access to the HTTP session object
 javax.servlet.ServletContextEvent
 provides access to the servlet context object.
 javax.servlet.ServletContextAttributeEvent
 provides access to servlet context and its attributes
 javax.servlet.http.HttpSessionBindingEvent
 provides access to an HTTP session and its attributes

© Copyrights aurionPro Solutions Ltd.


Templates for Listener Classes

• Servlet Context Listener Example


package myApp;
import javax.servlet.*;
public final class myContextListenerClass implements  ServletContextListener
{
    public void contextInitialized(ServletContextEvent event)
{
     /* This method is called when the servlet context is initialized(when the Web
Application is deployed).
         We can initialize servlet context related data here. */
}
public void contextDestroyed(ServletContextEvent event)
{
      /* This method is invoked when the Servlet Context is undeployed or
         WebLogic Server shuts down.  */
    }
}

© Copyrights aurionPro Solutions Ltd.


HTTP Session Attribute Listener
Example

package myApp;
import javax.servlet.*;
public final class mySessionAttributeListenerClass implements
HttpSessionAttributeListener
{
    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is added to a session.  */
    }   
public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session. */
}   
  public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.  */
}
}

© Copyrights aurionPro Solutions Ltd.


• package myApp;
import javax.servlet.*;
public final class mySessionAttributeListenerClass implements
HttpSessionAttributeListener
{
    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is added to a session.  */
    }   
public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session. */
}   
  public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.  */
}
}

© Copyrights aurionPro Solutions Ltd.


Additional Resources

• http://java.sun.com/j2ee/tutorial/1_3-fcs/index.html
• http://www.jcp.org/aboutJava/communityprocess/final/jsr053/
• http://www.google.com

© Copyrights aurionPro Solutions Ltd.

S-ar putea să vă placă și