11. What is a servlet event listener?
Listeners are used to perform some logic on trigger of certain events. Listeners are java classes that is used to implement the event handling mechanism. Example for Listener:
- Listener class can be used for auditing user login information’s.
- Listener classes can be used to clean up session objects and free memory when user logs off.
12. Classify the type of events in an web application?
There are three kind of events:
- Servlet Context Events: These are events triggered when there is a change in the ServletContext object.
- HTTP Session Events: These are events triggered when there is a change in the Session object.
- Servlet Request Events: These are events triggered when there is a change in the request object.
13. When are events triggered?
The events are triggered under two circumstances:
- Lifecycle changes: If there is a lifecycle change on the object. For example, when session created (or) initialized (or) destroyed.
- Attribute changes: Any attribute stored /changed in the session. For example, when a attribute “message” is stored in the session.
14. Who triggers the events?
Whenever one of the changes in lifecycle or attribute happens the events are triggered by the web container which can be captured by listeners to perform a logic.
15. What are the steps to develop a servlet listener?
- Step 1: Create the listener class. Listener class is done by implementing the required servlet listener interface by a normal java class and overriding all the methods.
- Step 2: Listener class is registered with the deployment descriptor file.