Listeners are classes that will wait for certain life cycle events and the container will invoke certain methods of these classes when that life cycle event happens. There will be listener interfaces, with methods that need to be invoked when a life cycle event occur. The listener implementation class will need to implement this interface.
You can specify a listener in either of the ways:
@WebListener annocation over the listener implementation class
By configuring in the deployment descriptor (web.xml). In the deployment descriptor, you need to place a <listener> element under the root element, and within the <listener> element, embed a <listener-class> element. The value held in the <listener-class> element should be the fully qualified class name of a listener class. There is no need to specify which type of listener you’re talking about, as the web container will find this out using Java’s reflection capabilities.
We have not covered topics required for some of the listeners mentioned here; you can come back and look into those listeners once those topics are covered.
Responds to the life and death of each request.
public void requestInitialized(ServletRequestEvent requestEvent)
Called at the beginning of any request’s scope. This is either at the start of the servlet’s service() method or at the start of the doFilter() method for the first filter in a chain.
public void requestDestroyed(ServletRequestEvent requestEvent)
Called for each request that comes to an end—either at the end of the servlet’s service() method or at the end of the doFilter() method for the first filter in a chain.
Responds to any change to the set of attributes attached to a request object.
Called whenever a new attribute is added to any request (as a result of any call to ServletRequest.setAttribute() for a new attribute).
Called whenever an attribute is removed from a request (as a result of any call to ServletRequest.removeAttribute()).
Called whenever an attribute is replaced (as a result of any call to ServletRequest.setAttribute() for an attribute name already in use on that request).
Note: Responses don't have or need listeners.
Responds to the life and death of the context for a web application.
Called at the beginning of context scope. Gets called before any servlet’s init() method or any filter’s doFilter() method.
Called at the end of context scope. And every filter and servlet destroy() method must have executed before the contextDestroyed() method is called.
Responds to any change to the set of attributes attached to the context object.
Responds to the life and death of the session.
Called by the web container whenever a new session is provided.
Called by the web container at the moment a session is about to be invalidated—within the call to HttpSession.invalidate(), but before the session becomes invalid and unusable.
Responds to any change to the set of attributes attached to the session object.
Called whenever a new attribute is added to any session.
Called whenever an attribute is removed from any session
Called whenever an attribute is replaced
HttpSessionBindingListener interface should be implemented by classes that may be bound into sessions as attributes and if they want to know when they were bound.
When an object which implements this interface is bound to a session or unbound from the session, the container will call its valueBound and valueUnbound methods respectively.
Since the interface is added by the attributes that are added to the session themselves and cannot listen to any other attributes, you should not mention it in the web.xml file or have the @WebListener annotation. For more details, refer to http://javajee.com/demo-httpsessionbindinglistener-an-object-knowing-whe....
The valueBound and valueUnbound methods of this interface are passed with a HttpSessionBindingEvent event that contains the below important methods:
getName – Name of the attribute added to session.
getValue – Value of the attribute will be this object itself.
GetSession – Current session object.
It also contain the inherited methods from EventObject: getSource() and toString().
HttpSessionBindingEvent is sent to methods of both objects that implements HttpSessionBindingListener as well as HttpSessionAttributeListener.
Called whenever the object implementing the HttpSessionBindingListener interface is the value object passed to an HttpSession.setAttribute() call.
Called whenever the object implementing the HttpSessionBindingListener interface is removed from the session as a result of an HttpSession.removeAttribute() call.
Receives events when a value object is transported across JVMs. This happens when the object is an attribute of a session in a distributed environment.
Called on each HttpSessionActivationListener implementing object bound to the session just prior to the serialization of the session (and all its attributes).
Called on each HttpSessionActivationListener implementing object bound to the session just after deserialization of the session (and all its attributes).
Receives events when session id gets changed.
Added since Servlet 3.1 (Java EE 7)
The order in which implementations of this interface are invoked is unspecified.
sessionIdChanged(HttpSessionEvent event, String oldSessionId
Receives event object and old session id when session id gets changed.
Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!