Javaweb基础入门(十四)之过滤器Filter及监听器Listener

过滤器Filter

1.概念

Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp,Servlet, 静态图片文件或静态 html文件等进行拦截,从而实现一些特殊的功能。例如实现URL级别的权限访问控制、过滤敏感词汇、压缩响应信息等一些高级功能。

2. 如何编写过滤器

1、编写java类实现Filter接口
2、重写doFilter方法
3、设置拦截的url

3.过滤器的配置

注解式配置

在自定义的Filter类上使用注解@WebFilter(“/*”)

xml配置

<!--过滤器的xml配置  -->
  <filter>
  <!--名称-->
    <filter-name>sf</filter-name>
    <!--过滤器类全称-->
    <filter-class>com.qf.web.filter.SecondFilter</filter-class>
  </filter>
 <!--映射路径配置-->
  <filter-mapping>
     <!--名称-->
    <filter-name>sf</filter-name>
     <!--过滤的url匹配规则和Servlet的一模一样-->
    <url-pattern>/*</url-pattern>
  </filter-mapping>

4.过滤器链以及优先级

过滤器链:通常客户端对服务器请求之后,服务器调用Servlet之前会执行一组过滤器(多个过滤器),那么这组过滤器就称为一条过滤器链。每个过滤器实现某个特定的功能,一个过滤器检测多个Servlet。
执行顺序
如果为注解的话,是按照类名的字符串顺序进行起作用的
如果是xml配置,web服务器根据Filter在web.xml文件中的注册顺序,决定先调用哪个Filter。
优先级: 请求顺序和响应顺序是相反(先进后出)

5.过滤器的初始化参数

注解式:

@WebFilter(value="/*",initParams= {@WebInitParam(name = "version",value = "1.0")})

基于xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>Web_Day</display-name>

  <!--过滤器的xml配置  -->
  <filter>
    <filter-name>myfilter</filter-name>
    <filter-class>com.qf.web.filter.SecondFilter</filter-class>
     <!--过滤器的初始化参数  -->
    <init-param>
      <param-name>version</param-name>
      <param-value>1.0</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>myfilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

6.怎么获得初始化参数

    public void init(FilterConfig fConfig) throws ServletException {
        suffix = fConfig.getInitParameter("suffix");
        System.out.println(suffix);
        System.out.println(fConfig.getInitParameter("encoding"));
    }

7.过滤器的典型应用

1 处理请求乱码过滤器
2 脏话过滤器
3 防止js攻击过滤器
4 没有登录不能访问某些页面

监听器Listener

1.概念

监听器用于监听web应用中某些对象、信息的创建、销毁、增加,修改,删除等动作的发生,然后作出相应的响应处理。当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法。常用于统计在线人数和在线用户,系统加载时进行信息初始化,统计网站的访问量等等

2.监听器类型

监听ServletContext的变化
a.监听生命周期

扫描二维码关注公众号,回复: 3024637 查看本文章
ServletContextListener接口
        内部方法:
        初始化:contextInitialized
        销毁:contextDestroyed

b.监听属性内容变化

ServletContextAttributeListener接口
        内部的方法:
        attributeAdded:监听属性的添加
        attributeRemoved:监听属性的移除
        attributeReplaced:监听属性的修改

监听HttpSession变化
a.监听生命周期

HttpSessionListener
        内部方法:
        sessionCreated:监听Session对象的创建
        sessionDestroyed:监听Session对象的销毁

b.监听属性内容变化

HttpSessionAttributeListener
        监听HttpSession的内容的变化
        内部的方法:
        attributeAdded:监听属性的添加
        attributeRemoved:监听属性的移除
        attributeReplaced:监听属性的修改

c.监听服务器的Session的钝化和活化

HttpSessionActivationListener
        监听服务器的钝化和活化
        内部方法:
        sessionWillPassivate:监听Session内部存储对象的钝化-存储
        sessionDidActivate:监听Session内部存储对象的活化---读取
        对应类需要实现序列化接口Serializable

d.监听对象的添加和移除

HttpSessionBindingListener
        监听对象的添加和移除
        内部方法:
        valueBound:监听对象的绑定
        valueUnbound:监听对象的解除绑定

监听ServletRequest的变化
a.监听生命周期

ServletRequestListener
        监听request对象的初始化和销毁
        内部方法:
            1、requestInitialized:监听request对象的初始化
            2、requestDestroyed:监听request对象的销毁

b.监听属性内容变化

ServletRequestAttributeListener
        监听属性内容变化
        内部方法:
        attributeAdded:监听属性的添加
        attributeRemoved:监听属性的移除
        attributeReplaced:监听属性的修改

3.监听器两种配置

在web.xml中进行配置

<listener> <!--直接写出自定义的监听器的类名即可-->
<listener-class>com.qf.web.listener.RequestLeftListener</listener-class>
</listener>

注解式配置

Servlet3.0之后新增的,使用注解@WebListener进行监听器的注册

猜你喜欢

转载自blog.csdn.net/xueer_z/article/details/82259261
今日推荐