java-filter and listener

Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层。

使用 Servlet,您可以收集来自网页表单的用户输入,呈现来自数据库或者其他源的记录,还可以动态创建网页。

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.

Servlet Filter

Filter is an object which transform the request and response (header as well as content).
Servlet Filter is used for manipulating the request and response from client to the servlet, or to modify the request and response, or to audit and log. Filters are called on a per-request basis and listeners are not. Filters have access to request object and listeners don’t. Filters can perform many different types of functions. We’ll discuss examples of the italicized items in this paper:

  • Authentication-Blocking requests based on user identity.
  • Logging and auditing-Tracking users of a web application.
  • Image conversion-Scaling maps, and so on.
  • Data compression-Making downloads smaller.
  • Localization-Targeting the request and response to a particular locale.
  • XSL/T transformations of XML content-Targeting web application responses to more that one type of client.

Servlet Listener

You can monitor and react to events in a servlet’s life cycle by defining listener objects whose methods get invoked when life cycle events occur. Servlet Listener is used for listening various events inside a web containers, such as when you create a session, or place an attribute in a session or if you passivate and activate in another container, to manipulate these events you can configure listener in web.xml, for example HttpSessionListener. Below a list of provided Listener classes:

    • HttpSessionAttributeListener
    • HttpSessionBindingListener
    • HttpSessionListener
    • ServletContextAttributeListener
    • ServletContextListener
原文地址:https://www.cnblogs.com/zhouj-happy/p/6000837.html