Introduction
I have been trying to come up with a way to highlight these fascinating classes in a way that demonstrates their power and usefulness. In my post Java EE 7 Tip of the Day: Programmatic Control of Session Tracking with ServletContextListener I highlighted the use of theServletContextListener
to set session tracking programmatically. However, there are a number of listener classes that can help you. Here they are in generally decreasing scope:ServletContainerInitializer
ServletContextListener
ServletContextAttributeListener
HttpSessionListener
HttpSessionAttributeListener
HttpSessionIdListener
HttpSessionBindingListener
ServletRequestListener
ServletRequestAttributeListener
Web Application Lifecycle Listeners
ServletContainerInitializer
TheServletContainerInitializer
is a special case that must be registered with the Service Provider Interface (SPI) loading mechanism. Please see the Javadocs for more details on its usage. That is a topic for a different post.
ServletContextListener
TheServletContextListener
has been previously mentioned in another post as noted above. This listener is notified of lifecycle changes, e.g. context initialized and destroyed. This gives the developer the opportunity to perform application setup such as setting a logger, or enabling other listeners. It also provides a clean mechanism to handle application cleanup when an application is shutdown, or disabled.
ServletContextAttributeListener
TheServletContextAttributeListener
listens for events that occur when attributes are added, modified, or removed from the ServletContext
. This could be used to modify those attributes, or perhaps log them. Interestingly, the order in which the implementations are invoked is not specified.
HttpSessionListener
TheHttpSessionListener
listens for session creation, and destruction events. I have found this to be one of the most useful classes in a web application. This can be used to set up a persistent data connection while the session is valid, and close the connection when the session expires, or is invalidated.The listener implementations are invoked in the order of declaration, and destroyed in reverse order. A way to think of this is like the layers of filo dough. If you go from top to bottom, then you must reverse order from bottom to top.
HttpSessionAttributeListener
The sibling interfaceHttpSessionAttributeListener
is the second most used class in my toolbox for listeners behind the ServletContextListener and HttpSessionListener. I have found that I often need to examine when attributes are added, modified, or removed from a session. This is the tool I use to manage that.Keep in mind that the HttpSessionAttributeListener behaves like the ServletContextAttributeListener in that the order in which the implementations are invoked is unspecified. This means that you can not rely on a specified ordering to take place across containers. Usually, the ordering is consistent on a container basis, but the contract is explicit that the ordering is undefined.
HttpSessionIdListener
This is a new to Java EE 7. TheHttpSessionBindingListener
is used to handle when a HttpSession
ID changes. This is usually the result of a HttpServletRequest.changeSessionId()
command. This was added in Java EE 7 to help solve a security issue called Session Fixation. Typically, you would change the session id after the user successfully authenticates. This can help you with this transition. Otherwise, you need to do a lot of work to make it happen. In this case, it is just great to be able to let the API handle it.This interface will allow you to modify your application based on the ID change. Please note that the order in which the implementations are invoked is not specified.
HttpSessionBindingListener
TheHttpSessionBindingListener
listens for binding events. This occurs when an object is bound, or unbound from a session. There are a couple of examples from BalusC on Stackoverflow. Here are a couple: Getting SessionScoped bean from HttpSessionListener? and How to access HTTP sessions in Java. These are just a couple of examples on its usage. I personally have never used it directly. I was just thinking I should come up with my own example for it.
ServletRequestListener
TheServletRequestListener
is another listener that I use. However its usage is not as frequent as the other aforementioned ones. I usually end up checking the ServletRequest
with an instanceof
for HttpServletRequest
and casting it. This listener will allow you to modify an individual request.Since it has every request pass through it, you should make sure that there is no excessive overhead in the listener such as database lookups, or excessive logging. Abuse of this listener can result in massive performance issues.
ServletRequestAttributeListener
Finally, we have theServletRequestAttributeListener
that listens for request attribute changes. The same warning as noted in the ServletRequestListener
applies here. Any excessive overhead will result in dramatic performance decreases.Like the other attribute listeners, this listener does not have a specific order in which they are invoked.
Code Examples
The code for this project was developed using NetBeans 7.4, Apache Maven, Mercurial, and is hosted on Bitbucket. The code in the examples is somewhat contrived, but does demonstrate the functionality of the listeners.I strongly recommend downloading the code, executing it, and looking at the output. It will give you a better picture of how the code works, and also show you some hidden "features" of your application that you may be unaware of.
The source code can be downloaded from here: web-application-listeners.