The listener wizard helps you create application lifecycle listeners by walking you through the creation process and by providing you with output files that you can use or that you can modify for use with your Web application. The listeners can run on Java EE-compliant Web servers.
To create a listener, complete the following steps:
- From the Java EE perspective, expand your dynamic project in the Project Explorer view.
- Right click on the Listener icon, and select from the pop-up menu. The Create Listener wizard appears.
- Follow the project wizard prompts.
- Application lifecycle listeners
- The Sun Microsystems Java™ Servlet 2.3 Specificationdefines the following interfaces that represent that application lifecycle listeners. You should select at least one of the intefaces.
- javax.servlet.ServletContextListener: This interface is provided by javax.servlet package and can be used to listen to the ServletContext life-cycle events. This interface has two methods: contextInitialized and contextDestroyed. The contextInitialized method is called when the web application is ready to service requests and the contextDestroyed method is invoked when the servlet context is about to be shut down.
- javax.servlet.ServletContextAttributeListener: This interface is provided by javax.servlet package. It can be used to be notified when any attribute is added to the ServletContext or if any of the ServletContext's attributes are changed or removed. This inetface has three methods: attributeAdded, attributeRemoved and attributeReplaced.
- javax.servlet.ServletRequestListener: This interface is provided by javax.servlet package. It can be used to be notified when a request is coming in or out of scope in a Web component. This interface has two methods: requestInitialized and requestDestroyed. The requestInitialized method is called when it is about to enter the first servlet or filter in each Web application. The requestDestroyed method is called when it exits the last servlet or the first filter in the chain.
- javax.servlet.ServletRequestAttributeListener: This interface is provided by javax.servlet package and can be used to be notified when any request attribute is changed. This interface has three methods: attributeAdded, attributeRemoved and attributeReplaced. The attributteAdded method is called when a new attribute was added to servlet request. The attributeRemoved method is called when an existing attribute has been removed from the servlet request. The attributeReplaced method is called when an attribute was replaced on the servlet request.
- javax.servlet.http.HttpSessionListener: This interface is provided by javax.servlet.http package and can be used to listen to a sessions life cycle events. This interface has two methods: sessionCreated and sessionDestroyed. The sessionCreated method is called when an HttpSession object is created. The sessionDestroyed method is called when an HttpSession object is invalidated.
- javax.servlet.http.HttpSessionActivationListener: This interface is provided by javax.servlet.http package. It can be used to listen to a container events notifying that sessions will be passivated or activated. This interface has two methods: sessionDidActivate and sessionWillPasivate. The sessionDidActivate is called when the session has just been activated and the sessionWillPasivate is called when the session is about to be passivated.
- javax.servlet.http.HttpSessionAttributeListener: This interface is provided by javax.servlet.http package and can be used to listen to events related to session attributes. This interface has three methods: attributeAdded, attributeRemoved and attributeReplaced. The attributeAdded method is called when an attribute is added to an HttpSession object. The attributeRemoved and attributeReplaced methods are called when an HttpSession attribute is removed or replaced, respectively.
- javax.servlet.http.HttpSessionBindingListener: This interface is provided by javax.servlet.http package and can be used to be notified when an object is bound to or unbound from a session. This interface has two methods: valueBound and valueUnbound. The valueBound method is called when the object is being bound to a session and identifies the session. The valueUnbound method is called when the object is being unbound from a session and identifies the session.
- Modifiers
- The Sun Microsystems Java Servlet 2.3 Specification states that a Listener class must be public and not abstract. Therefore, you cannot change these modifiers. The only one available for change is the final modifier.
- Interfaces
- The interfaces that you selected in the previous page of the wizard are provided as default values in the Interfaces field. To add additional interfaces, click the Add button next to this field.
- Interface selection dialog
- This dialog appears if you select to add an interface to your servlet. As you type the name of the interface that you are adding, a list of available interfaces listed in the Matching items list box updates dynamically to display only the interfaces that match the pattern. Choose an interface to see the qualifier, and then click OK when finished.