Creating a Listener

See Also

The Java Servlet Specification supports application level events for controlling interactions with the ServletContext and HttpSession objects and for managing the resources that a web module uses.

Application event listeners are classes that implement one or more of the servlet event listener interfaces. Application events notify listener objects 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:

You use the listener element to declare listeners in the deployment descriptor (web.xml). List the listener elements in the order in which they are to be invoked.

To create a listener:

  1. In the Filesystems window, right-click the package node and choose New and choose All Templates from the contextual menu.
  2. Expand JSP & Servlet and expand Listeners.
  3. Choose one of the listener templates and click Next.
  4. Follow the instructions in the New wizard and click Finish. The source code appears in the Source Editor.
tip  If you want to keep your source separate from your class files, you must mount the directory above the package root and you must use the Options window to select WEB-INF/classes as the project's target directory for the External Compiler. Doing so ensures that code completion and compiling are supported by the IDE.
tip  To view the API documentation for servlet listeners, choose View and choose Documentation Indices and choose Overview (Servlet API Documentation) from the main tool bar.

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

See Also
About Servlet Listeners
Setting Listener Properties
Adding a Listener to the Deployment Descriptor
Where to Put Web Module Files

Legal Notices