[web]Servlet中的Listener和Filter

建议先看看 ——> Servlet工作原理

一、Listener

在Tomcat服务中,Listener的设计是基于观察者模式的,目前在Servlet中提供6中两类事件的观察者接口,它们分别是:

实际上这六个Listener都继承了EvenListener,每个Listener由分别定义了自己需要实现的接口:

ServletContextAttributeListener:

public interface ServletContextAttributeListener extends EventListener {
    
    // 设置属性时
    public default void attributeAdded(ServletContextAttributeEvent scae) {
    }

    // 删除属性时
    public default void attributeRemoved(ServletContextAttributeEvent scae) {
    }
    
    // 、更改属性时
    public default void attributeReplaced(ServletContextAttributeEvent scae) {
    }
}
View Code

ServletRequestAttributeListener:

public interface ServletRequestAttributeListener extends EventListener {

    // 更改Request属性时
    public default void attributeAdded(ServletRequestAttributeEvent srae) {
    }

    // 更改Request属性时
    public default void attributeRemoved(ServletRequestAttributeEvent srae) {
    }

    // 更改Request属性时
    public default void attributeReplaced(ServletRequestAttributeEvent srae) {
    }
}
View Code

HttpSessionAttributeListener:

public interface HttpSessionAttributeListener extends EventListener {

    // 添加 Session 属性时
    public default void attributeAdded(HttpSessionBindingEvent se) {
    }

    // 删除 Session 属性时
    public default void attributeRemoved(HttpSessionBindingEvent se) {
    }

    // 更改 Session 属性时
    public default void attributeReplaced(HttpSessionBindingEvent se) {
    }
}
View Code
ServletRequestAttributeListener:
public interface ServletRequestAttributeListener extends EventListener {

    public default void attributeAdded(ServletRequestAttributeEvent srae) {
    }

    public default void attributeRemoved(ServletRequestAttributeEvent srae) {
    }

    public default void attributeReplaced(ServletRequestAttributeEvent srae) {
    }
}
View Code

ServletContextListener:

public interface ServletContextListener extends EventListener {

    // StandardConetxt 创建时
    public default void contextInitialized(ServletContextEvent sce) {
    }

    // StandardConetxt 销毁时
    public default void contextDestroyed(ServletContextEvent sce) {
    }
}
View Code

HttpSessionListener:

public interface HttpSessionListener extends EventListener {

    public default void sessionCreated(HttpSessionEvent se) {
    }

    public default void sessionDestroyed(HttpSessionEvent se) {
    }
}
View Code

 二、Filter

Filter可以完成和Servlet同样的工作,除了request和response两个对象外,它还提供了一个FilterChain对象,它可以帮助我们更加灵活地控制请求的流程,该接口定义如下:

Filter:

public interface Filter {

    // 初始化接口,在用户定义的Filter初始化时被调用,它与Servlet的init方法是一样的,
    // FilterConfig也和ServletConfig类似,除了可以获取ServletContext之外,还可以在配置文件中声明参数
    public default void init(FilterConfig filterConfig) throws ServletException {}

    // 每个用户的匹配请求进来时,这个方法都被调用,并且是在Servlet的service方法之前被调用,
    // 而 FilterChain代表了当前的整个请求链,通过调用FilterChain.doFilter可以将请求继续传递下去,
    // 如果需要拦截该请求,那么就不调用FilterChain,就可以直接返回了,所以它是一种责任链模式
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException;


    // Filter被销毁时,会调用这个方法,web容器调用这个方法之后,容器会再调用一次doFilter方法
    public default void destroy() {}
}
View Code

除此之外还有两个接口的定义,分别FilterConfig 和FilterChain

FilterConfig:

public interface FilterConfig {

    /**
     * Get the name of the filter.
     *
     */
    public String getFilterName();

    /**
     * Returns a reference to the {@link ServletContext} in which the caller is
     * executing.
     *
     */
    public ServletContext getServletContext();

    /**
     * Returns a <code>String</code> containing the value of the named
     * initialization parameter, or <code>null</code> if the parameter does not
     * exist.
     *
     */
    public String getInitParameter(String name);

    /**
     * Returns the names of the filter's initialization parameters as an
     */
    public Enumeration<String> getInitParameterNames();

}
View Code

FilterChain:

public interface FilterChain {

    /**
     * Causes the next filter in the chain to be invoked, or if the calling
     * filter is the last filter in the chain, causes the resource at the end of
     * the chain to be invoked.
     *
     */
    public void doFilter(ServletRequest request, ServletResponse response)
            throws IOException, ServletException;

}
View Code

三、<url-pattern>匹配规则

    • 精确匹配:如/foo/.html,只能匹配/foo.html
    • 路径匹配:如/foo/*,将匹配/foo/路径下所有的URL
    • 后缀匹配:如*.html,将匹配所有所有的后缀为.html的路径

 匹配的顺序也是:精确匹配——> 最长路径匹配——>后缀匹配

原文地址:https://www.cnblogs.com/ytuan996/p/10627550.html