javaWeb(十二)监听器(Listener)和过滤器(Filter)

Listener

1、Listener定义 :

监听器内部实现其实就是接口回调.

就像老板写接口方法,然后员工写实现类,父类接口指向子类对象,多态的一种体现

2、Listener分类 :

总共有8个 划分成三种类型

  1. 【监听三个作用域创建和销毁】
    需要在web.xml里面注册
> > > > >
三个作用域 Web中类的体现 Listener接口 创建 销毁
request httpServletRequest ServletRequestListener 只要访问服务器,就会有请求;html: 会; jsp: 会; servlet : 会 对请求做了响应后
session httpSession HttpSessionListener 调用getSession;html: 不会; jsp: 会; servlet : 会 超时(默认30分钟)、非正常关闭(销毁)、正常关闭服务器(序列化)
application ServletContext ServletContextListener 启动服务器时 关闭服务器、从服务器移除项目
  1. 【监听三个作用域属性状态变更】
    需要在web.xml里面注册

可以监听在作用域中值 添加 | 替换 | 移除的动作。(setAttribute/removeAttribute)

  • request — ServletRequestAttributeListener
    在这里插入图片描述

  • session — HttpSessionAttributeListener
    在这里插入图片描述

  • servletContext — ServletContextAttributeListener
    在这里插入图片描述

  1. 【监听httpSession里面存值的状态变更】

不需要在web.xml里面注册,但是需要Bean实现下面这个接口

  • HttpSessionBindingListener(监听对象与session 绑定和解除绑定 的动作)
 - 让javaBean 实现该接口即可

        @Override
        public void valueBound(HttpSessionBindingEvent event) {
            System.out.println("对象被绑定进来了");
        }

        @Override
        public void valueUnbound(HttpSessionBindingEvent event) {
            System.out.println("对象被解除绑定");
        }
  • HttpSessionActivationListener(用于监听现在session的值 是 钝化 (序列化)还是活化 (反序列化)的动作)

session中的值可能会很多, 并且我们有很长一段时间不使用这 个内存中的值, 那么可以考虑把session的值可以存储到硬盘上【钝化】,等下一次在使用的时候,在从硬盘上提取出来。 【活化】

1. 在tomcat里面 conf/context.xml 里面配置

        对所有的运行在这个服务器的项目生效  

2. 在conf/Catalina/localhost/context.xml 配置

        对 localhost生效。  localhost:8080

3. 在自己的web工程项目中的 META-INF/context.xml

        只对当前的工程生效。

    maxIdleSwap : 1分钟不用就钝化
    directory :  钝化后的那个文件存放的目录位置。 

        D:\tomcat\apache-tomcat-7.0.52\work\Catalina\localhost\ListenerDemo\demo1

    <Context>
        <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
            <Store className="org.apache.catalina.session.FileStore" directory="javaStudy"/>
        </Manager>
    </Context>

Filter

过滤器 , 其实就是对客户端发出来的请求进行过滤。 浏览器发出, 然后服务器派servlet处理。 在中间就可以过滤, 其实过滤器起到的是拦截的作用。

在服务器启动的时候就创建。服务器停止的时候销毁

1、如何使用Filter:

  1. 定义一个类, 实现Filter

public class FilterDemo implements Filter {

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("来到过虑器了。。。");
        chain.doFilter(request, response);
    }

    public void init(FilterConfig fConfig) throws ServletException {
    }

}
  1. 注册过滤器
        <filter>
        <display-name>FilterDemo</display-name>
        <filter-name>FilterDemo</filter-name>
        <filter-class>com.itheima.filter.FilterDemo</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>FilterDemo</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>

2、配置Filter:

  1. 全路径匹配 以 / 开始
/LoginServlet
  1. 以目录匹配 以 / 开始 以 * 结束
/demo01/*
  1. 以后缀名匹配 以 * 开始 以后缀名结束
*.jsp *.html *.do

3、配置参数Dispachers:

REQUEST : 只要是请求过来,都拦截,默认就是REQUEST 
FORWARD : 只要是转发都拦截。 
ERROR : 页面出错发生跳转 
INCLUDE : 包含页面的时候就拦截。

猜你喜欢

转载自blog.csdn.net/weixin_39782583/article/details/85298353