Notes on Filter&Listener in JavaWeb

Filter&Listener

  • Concept: Filter represents a filter, which is one of the three major components of JavaWeb (Servlet, Filter, Listener).

  • Filters can intercept requests for resources to achieve some special functions.

  • Filters generally complete some common operations, such as: permission control, consent encoding processing, sensitive character processing, etc...

filter

Filter operation

  1. Define a class, implement the Filter interface, and override all other methods

    public class FilterDemo implements Filter {
          
          
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
          
          }
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
          
          }
        @Override
        public void destroy() {
          
          }
    }
    
  2. @WebFilterConfigure the path for Filter to intercept resources: define annotations on the class

    @WebFilter("/*")
    public class FilterDemo implements Filter
    
  3. Output a sentence in the doFilter method and let it go

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
          
          
          System.out.println("FilterDemo...");
          //放行
          filterChain.doFilter(servletRequest,servletResponse);
        }
    

Filter execution process

Execute pre-release logic -> release -> access resources -> execute post-release logic

Filter interception path configuration

  • Filter can configure different interception resource paths according to requirements
    • Intercept specific resources: /index.jsp: only when accessing index.jsp will be intercepted.
    • Directory interception: /user/*: access to all resources under /user will be intercepted
    • Suffix interception: *.jsp: Access to resources with the suffix jsp will be intercepted
    • Intercept all: /*:: Access to all resources will be intercepted

Filter filter chain

  • A web application can configure multiple filters, and multiple filters become a filter chain
  • Annotation configured Filter, the priority is in accordance with the natural order of the filter class name (string)

Listener

  • The listener can listen to the functional components that automatically execute code when the three objects of application, session, and request are created, destroyed, or added, modified, or deleted.

Listeners for object creation and destruction

The Servlet specification defines listeners for the creation and destruction events of three objects: ServletContext, HttpSession, and HttpServletRequest, as shown in the following table.

event source listener listener description create and destroy methods call timing
ServletContext ServletContextListener Used to monitor the creation and destruction process of the ServletContext object void contextInitialized (ServletContextEvent sce) When creating a ServletContext object
void contextDestroyed (ServletContextEvent sce) When the ServletContext object is destroyed
HttpSession HttpSessionListener Used to monitor the creation and destruction process of the HttpSession object void sessionCreated (HttpSessionEvent se) When creating an HttpSession object
void sessionDestroyed (HttpSessionEvent se) When destroying the HttpSession object
ServletRequest ServletRequestListener Used to monitor the creation and destruction process of the ServletRequest object void requestInitialized (ServletRequestEvent sre) When creating a ServletRequest object
void requestDestroyed (ServletRequestEvent sre) When the ServletRequest object is destroyed

Listener for property changes

The Servlet specification defines listeners for monitoring attribute change events in the three objects of ServletContext, HttpSession, and HttpServletRequest. These three listener interfaces are ServletContextAttributeListener, HttpSessionAttributeListener, and ServletRequestAttributeListener. Three methods are defined in these three interfaces, which are used to handle the addition, deletion and replacement events of attributes in the monitored object. The method names corresponding to the same event in the three interfaces are exactly the same, but the parameter types are different, as shown in the following table.

event source listener listener description method call timing
ServletContext ServletContextAttributeListener Add, remove, and replace properties for listening to ServletContext objects public void attributeAdded (ServletContextAttributeEvent scae) When a new attribute is added to the ServletContext object
public void attributeRemoved (ServletContextAttributeEvent scae) When deleting an attribute in the ServletContext object
public void attributeReplaced (ServletContextAttributeEvent scae) When an attribute in the ServletContext object is replaced
HttpSession HttpSessionAttributeListener Add, remove, and replace properties for listening to the HttpSession object public void attributeAdded (HttpSessionBindingEvent hsbe) When a property is added to the HttpSession object
public void attributeRemoved (HttpSessionBindingEvent hsbe) When deleting an attribute in the HttpSession object
public void attributeReplaced (HttpSessionBindingEvent hsbe) When an attribute in the HttpSession object is replaced
HttpServletRequest ServletRequestAttributeListener Add, remove, and replace properties for listening to HttpServletRequest objects public void attributeAdded (ServletRequestAttributeEvent srae) When a property is added to the HttpServletRequest object
public void attributeRemoved (ServletRequestAttributeEvent srae) 当删除 HttpServletRequest 对象中的一个属性时
public void attributeReplaced (ServletRequestAttributeEvent srae) 当 HttpServletRequest 对象中的某个属性被替换时

监听 Session 中对象状态改变的监听器

Session 中的对象可以有多种状态:绑定到 Session 中、从 Session 中解除绑定、随 Session 对象持久化到存储设备中(钝化)、随 Session 对象从存储设备中恢复(活化)。

Servlet 规范中定义了两个特殊的监听器接口,用来帮助对象了解自己在 Session 中的状态:HttpSessionBindingListener 接口和 HttpSessionActivationListener 接口 ,实现这两个接口的类不需要进行注册。

事件源 监听器 监听器描述 方法 调用时机
HttpSession HttpSessionBindingListener 用于监听 JavaBean 对象绑定到 HttpSession 对象和从 HttpSession 对象解绑的事件 void valueBound (HttpSessionBindingEvent event) 当对象被绑定(添加)到 HttpSession 对象中时
void valueUnbound (HttpSessionBindingEvent event) 当对象从 HttpSession 对象中解除绑定(移除)时
HttpSessionActivationListener 用于监听 HttpSession 中对象活化和钝化的过程 void sessionWillPassivate (HttpSessionBindingEvent event) When the object bound to the HttpSession object is about to be passivated with the HttpSession object
indingEvent event) When the object bound to the HttpSession object is about to be passivated with the HttpSession object
void sessionDidActive (HttpSessionBindingEvent event) When the object bound to the HttpSession object will be activated with the HttpSession object

Guess you like

Origin blog.csdn.net/qq_48778364/article/details/127775326