Difference between Filter and Listener in Servlet (Java EE)

前端 未结 10 1052
有刺的猬
有刺的猬 2020-12-22 19:05

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

相关标签:
10条回答
  • 2020-12-22 19:10

    After reading all the answers and blogs this is what I got

    Filter

    A filter is an object that dynamically intercepts requests and responses to transform or use the information contained in the requests or responses.

    Filters typically do not themselves create responses but instead provide universal functions that can be "attached" to any type of servlet or JSP page.

    The filter is run before rendering view but after controller rendered response.

    A Filter is used in the web layer only as it is defined in web.xml.

    Filters are more suitable when treating your request/response as a black box system. They'll work regardless of how the servlet is implemented.

    Filters are used to perform filtering tasks such as login authentication ,auditing of incoming requests from web pages, conversion, logging, compression, encryption and decryption, input validation etc.

    A Servlet Filter is used in the web layer only, you can't use it outside of a web context.

    For more detail on filter http://array151.com/blog/servlet-filter/

    Listener

    Servlet Listener is used for listening to events in a web container, such as when you create a session or place an attribute in a 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.

    Listeners get triggered for an actual physical request that can be attached to events in your app server .With listeners, you can track application-level, session-level, life-cycle changes, attribute changes etc.

    You can monitor and react to events in a servlet's life cycle by defining listener objects whose methods get invoked when lifecycle events occur.

    For more detail : http://array151.com/blog/servlet-listener/

    and here is the difference http://array151.com/blog/difference-between-servlet-filter-and-servlet-listener/

    0 讨论(0)
  • 2020-12-22 19:10

    Filter:Filter is simply Filtering the Response and request coming from the clients to the servlet.

    Listener:is like a trigger when any trigger is occur it take the action.

    0 讨论(0)
  • 2020-12-22 19:14

    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.

    0 讨论(0)
  • 2020-12-22 19:16

    You can easily have a rough idea with the English meaning of those two.

    Filter is there to filter the content/resource which coming to/going out from a Servlet. In the other hand, Listener is there, to do some related things when something happen to the web application(listening).

    0 讨论(0)
  • 2020-12-22 19:23

    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.

    0 讨论(0)
  • 2020-12-22 19:25

    While you can modify the current event object within a listener, you cannot halt the execution of the current event handler in a listener. You also cannot clear the event queue from within a listener. Besides the imposed differences in capabilities, they are also intended for different purposes. Listeners tend to focus on interacton between the event handler and the model, while filters tend to focus on interaction between the event handler and the controller.

    Source : web

    0 讨论(0)
提交回复
热议问题