What are the differences between using a Servlet Filter versus a Servlet Context Listener?
When would you use one or the other?
In short, Filter is for the Servlet, intercepting the requests and responses. Listener is for the Web Application, doing important tasks on events in context-level, session-level etc.
Filter provides functionality which can be “attached” to any web resource. Servlet used for performing action which needs for particular request as user login, get response based on user role, interacts with database for getting data, business logic execution, and more.
Servlet API provides different kind of listeners for different types of Events. Listener interfaces declare methods to work with a group of similar events, for example we have ServletContext Listener to listen to startup and shutdown event of context. Every method in listener interface takes Event object as input.
Servlet Filters are Java classes that can be used in Servlet Programming for the following purposes − To intercept requests from a client before they access a resource at back end. To manipulate responses from server before they are sent back to the client.
A filter is an object that is invoked at the preprocessing and postprocessing of a request. It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and decryption, input validation etc. The servlet filter is pluggable, i.e. its entry is defined in the web.xml file,...
ServletContextEvent class provides alerts/notifications for changes to a web application’s servlet context. ServletContextListener is a class that receives alerts/notifications about changes to the servlet context and acts on them. When the context is initialized and deleted, the ServletContextListener is utilized to conduct crucial tasks.
ServletRequestListener listens to ServletRequestEvent which is an event triggered for every incoming request. If I want to log the user-agent for every request to my web app, should I use this listener or a filter? @BalusC Is defining a listener is mandatory?
public void doFilter (HttpServletRequest request, HttpServletResponse response): it passes the control to the next filter or resource. We can define filter same as servlet.
A Filter
intercepts on HTTP requests matching its URL pattern and allows you to modify them. See also its javadoc:
A filter is an object that performs filtering tasks on either the request to a resource (a servlet or static content), or on the response from a resource, or both.
Filters perform filtering in the
doFilter
method. EveryFilter
has access to aFilterConfig
object from which it can obtain its initialization parameters, and a reference to theServletContext
which it can use, for example, to load resources needed for filtering tasks.Filters are configured in the deployment descriptor of a web application.
Examples that have been identified for this design are:
- Authentication Filters
- Logging and Auditing Filters
- Image conversion Filters
- Data compression Filters
- Encryption Filters
- Tokenizing Filters
- Filters that trigger resource access events
- XSL/T filters
- Mime-type chain Filter
A ServletContextListener
intercepts on webapp's startup and shutdown and allows you to execute some code on startup and/or shutdown. See also its javadoc:
Interface for receiving notification events about
ServletContext
lifecycle changes.In order to receive these notification events, the implementation class must be either declared in the deployment descriptor of the web application, annotated with
WebListener
, or registered via one of theaddListener
methods defined onServletContext
.Implementations of this interface are invoked at their
contextInitialized(javax.servlet.ServletContextEvent)
method in the order in which they have been declared, and at theircontextDestroyed(javax.servlet.ServletContextEvent)
method in reverse order.
When to use the one or the other should now be obvious. Use a Filter
if you want to intercept on HTTP requests maching a specific URL pattern because you want to check/modify the HTTP request/response. Use a ServletContextListener
if you want to intercept on webapp's startup and/or shutdown.
Please know where to find the javadocs and how to interpret them. They contain all answers to this kind of trivial questions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With