javaservletsservlet-filtersservlet-listeners

Difference between Filter and Listener in Servlet (Java EE)


There are Filters and Listeners functionality in Servlet. I want to know exact difference between Filter and Listener.


Solution

  • 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.