Difference between Filter and Listener in Servlet (Java EE)

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.


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.

Based on @fnt 's responses below, let me try to clarify some more. Listeners are targeted for lifecycle changes, without having to have a client request coming in. So for one client request, there could be many more lifecycle events may happen before the request is disposed of. Example: You want to log all the sessions that timeout. Please note that SesionTimeout is a lifecycle event, which can happen without having the user to do anything. For such a scenario, a listener will be appropriate.

To the question of logging when a request arrives. There is no direct mapping of a new request to an equivalent listener (read lifecycle event) event. And hence for each incoming request if you want to log something, Filter in my opinion is the right thing to use.

This material from Oracle should be able to clarify some more Filters and Listeners

Update 17 Mar 2021 There has been some back and forth in the comments. Trying to clarify. By definition, a filter will always get invoked. So if i need to log the request ALWAYS, keeping it in filters will ensure that i get the logging. If i put it in listeners, i have to make sure the logging code block is executed in ALL possible listeners. Both approaches will get you the logging that you need, using filters will be more efficient.

HTH


One important difference is often overlooked: while listeners get triggered for an actual physical request, filters work with servlet container dispatches. For one listener invocation there may be multiple filters/servlet invocations.

Listeners vs Filters

You can specify dispatcher types with the @WebFilter annotation:

import javax.servlet.DispatcherType;
import javax.servlet.annotation.WebFilter;

@WebFilter(servletNames = { "My Servlet" },
    dispatcherTypes = { DispatcherType.REQUEST, DispatcherType.FORWARD })

See Java EE 7 Tutorial: Filtering Requests and Responses for more information on filters.
If you still have trouble understanding filters, see Mapping filters dispatcher types - this is an older J2EE doc, but it goes into more detail.


Filter is just like a water filter, where incoming (request) and outgoing (response) values will be filtered.

Listener is like listening (trigger) - whenever required, I will be performed.