Servlet Filter
Servlet Listener
Servlet Filter is used for monitoring request and response from client to the servlet, or to modify the request and response, or to audit and log.
Servlet Listener is used for listening to events in a web containers, such as when you create a session, or place an attribute in an session or if you passivate and activate in another container, to subscribe to these events you can configure listener in web.xml, for example HttpSessionListener.
Filter is an object which transform the request and response (header as well as content).
You can monitor and react to events in a servlet’s life cycle by defining listener objects whose methods get invoked when life cycle events occur.
Filters are used for pre and post process requests. Look at the javax.servlet.Filter in your tomcat/jboss/other container javadoc.
Where as the listeners are like triggers that can be attached to events in your app server (let’s use the term container here). With listeners you can track application-level, session-level, life-cycle changes, attribute changes etc. The implemented interfaces are javax.servlet.Listener interface.
Filters work with servlet container dispatches. For one listener invocation there may be multiple filters/servlet invocations. Filters implement javax.servlet.Filter
While listeners get triggered for an actual physical request listeners implements javax.servlet.ServletContextListener and other listed listeners.
Categories: Java