Creating a Listener

See Also

The Java Servlet 2.3 Specification supports application level events which allow you more control over interactions with the ServletContext and HttpSession objects and let you efficiently manage the resources used by a web module.

Application event listeners are classes that implement one or more of the servlet event listener interfaces. Application events let listener objects be notified when servlet contexts and sessions are initialized and destroyed, as well as when attributes are added or removed from a context or session.

The IDE provides a wizard to help you create listener classes. Use the servlet context listener templates to create listener classes that manage resources or state held at a virtual machine level for the web module:

Use the HTTP session listener templates to create listener classes that manage state or resources associated with a series of requests made to a web module from the same client or users:

Listener classes are declared in the deployment descriptor (web.xml) using the listener element. They are listed by class name in the order in which they are to be invoked.

To create a listener:

  1. Choose File and choose New.
  2. In the New Wizard, expand the JSP & Servlet category..
  3. Choose the appropriate listener template from JSP & Servlet and choose Listener.
  4. Follow the instructions in the New Wizard, then click Finish. The source code for the listener you created appears in the source editor.

You can also create a listener class from within the Explorer. Right-click the location where the listener will be stored (typically in WEB-INF/Classes), then choose New and choose JSP & Servlet and choose Listeners and the appropriate listener template from the contextual menu.

For more information about listeners, see the Java Servlet 2.3 Specification available at http://java.sun.com/products/servlet .

See Also
Setting Listener Properties
File Location in a Web Module

Legal Notices