Servlet--Listener&Filter

Listener监听器

监听三个作用域创建和销毁

1. ServletContextListener  [aapplication  --- ServletContext]
        servletcontext创建:
            1. 启动服务器的时候
        servletContext销毁:
            2. 关闭服务器. 从服务器移除项目     
    2. ServletRequestListener [ request  ---httpServletRequest]
        request创建:
            访问服务器上的任意资源都会有请求出现。
            访问 html: 会
            访问 jsp:    会
            访问 servlet : 会          
        request销毁:
            服务器已经对这次请求作出了响应。
    3. HttpSessionListener [  session  ---httpSession]
        session的创建
            调用getSession才会创建
            html:        不会
            jsp:        会      getSession();
            servlet:     会
        session的销毁
            超时  30分钟           
            非正常关闭 销毁
            正常关闭服务器(序列化)


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

servletContext --- ServletContextAttributeListener

request --- ServletRequestAttributeListener

session --- HttpSessionAttributeListener

监听httpSession里面存值的状态变更

 这一类监听器不用注册。

HttpSessionBindingListener

监听对象与session 绑定和解除绑定 的动作

* HttpSessionActivationListener

> 用于监听现在session的值 是 钝化 (序列化)还是活化 (反序列化)的动作
钝化 (序列化)
> 把内存中的数据 存储到硬盘上
活化 (反序列化)
> 把硬盘中的数据读取到内存中。


Filter过滤器

 实现Filter接口

注册过滤器

> 在web.xml里面注册,注册的手法与servlet基本一样。


              <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>

##Filter细节:
1. init方法的参数 FilterConfig , 可以用于获取filter在注册的名字 以及初始化参数。  其实这里的设计的初衷与ServletConfig是一样的。
2. 如果想放行,那么在doFilter 方法里面操作,使用参数 chain
        chain.doFilter(request, response); 放行, 让请求到达下一个目标。
3.  <url-pattern>/*</url-pattern> 写法格式与servlet一样。
    1. 全路径匹配  以 /  开始
           /LoginServlet
    2. 以目录匹配 以 / 开始  以 * 结束
        /demo01/* 
    3. 以后缀名匹配  以 * 开始 以后缀名结束
        *.jsp  *.html *.do
4. 针对 dispatcher 设置
        REQUEST : 只要是请求过来,都拦截,默认就是REQUEST 
        FORWARD : 只要是转发都拦截。 
        ERROR : 页面出错发生跳转 

        INCLUDE : 包含页面的时候就拦截。


猜你喜欢

转载自blog.csdn.net/kikock/article/details/80765045