Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a Servlet Filter and a Servlet Context Listener?

What are the differences between using a Servlet Filter versus a Servlet Context Listener?

When would you use one or the other?

like image 937
BestPractices Avatar asked Mar 21 '12 20:03

BestPractices


People also ask

What is the difference between listener and filter?

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.

What is the difference between filter and servlet?

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.

What is a servlet listener?

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.

What is the servlet filter?

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.

What is a filter in servlet?

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

What is the difference between servletcontextevent and servletcontextlistener?

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.

What is servletrequestlistener in servlet?

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?

What is dofilter in HTTPServlet?

public void doFilter (HttpServletRequest request, HttpServletResponse response): it passes the control to the next filter or resource. We can define filter same as servlet.


Video Answer


1 Answers

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. Every Filter has access to a FilterConfig object from which it can obtain its initialization parameters, and a reference to the ServletContext 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 the addListener methods defined on ServletContext.

Implementations of this interface are invoked at their contextInitialized(javax.servlet.ServletContextEvent) method in the order in which they have been declared, and at their contextDestroyed(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.

like image 175
BalusC Avatar answered Sep 29 '22 00:09

BalusC