Filter&Listener

Filter&Listener

Filter:过滤器

概述

  • 在生活中的过滤器:净水器,空气净化器,土匪

  • web中的过滤器:当访问服务器的资源时,过滤器可以将请求拦截下来,完成一些特殊的功能

  • 过滤器的作用

    • 一般用于完成通用的操作。如:登录操作、统一编码处理、敏感字符过滤…

快速入门

步骤
  1. 定义一个类,实现接口Filter
  2. 复写方法
  3. 配置拦截路径
    1. web.xml
    2. 注解
代码实现
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @author zhuxianglong
 * @version 1.0
 * @date 2021/8/30 22:25
 * 过滤器快速入门
 */
@WebFilter("/*")//访问所有资源之前,都会执行该过滤器
public class FilterDemo1 implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("filterdemo1被执行了...");
        //放行
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}

过滤器细节

web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <filter>
        <filter-name>demo1</filter-name>
        <filter-class>net.seehope.web.filter.FilterDemo1</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>demo1</filter-name>
        <%--拦截路径--%>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
过滤器执行流程
  1. 执行过滤器
  2. 执行放行后的资源
  3. 回来执行过滤器放行代码下边的代码
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * @author zhuxianglong
 * @version 1.0
 * @date 2021/8/30 22:41
 */
@WebFilter("/*")
public class FilterDemo2 implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        //对request对象的请求消息增强
        System.out.println("filterDemo2执行了");
        //放行
        chain.doFilter(req, resp);
        //对response对象的响应消息增强
        System.out.println("filterDemo2回来了");
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
过滤器生命周期方法
  1. init:在服务器启动后,会创建Filter对象调用init方法。执行一次。用于加载资源
  2. doFilter:每一次请求被拦截资源时,会执行。执行多次
  3. destroy:在服务器关闭后,Filter对象被销毁,如果服务器是正常关闭,则会执行destroy方法。执行一次。用于释放资源
过滤器配置详解
  • 拦截路径配置:

    1. 具体资源路径:/index.jsp 只有访问index.jsp资源时,过滤器才会被执行
    2. 拦截目录:/user/* 访问/user下的所有资源时,过滤器都会被执行
    3. 后缀名拦截:*.jsp 访问所有后缀名为jsp资源时,过滤器都会被执行
    4. 拦截所有资源:/* 访问所有资源时,过滤器都会被执行
  • 拦截方式配置:资源被访问的方式

    • 注解配置

      • 设置dispatcheTypes属性:

        1. REQUEST:默认值。浏览器直接请求支援
        2. FORWARD:转发访问资源

        FilterDemo4.java

        import javax.servlet.*;
        import javax.servlet.annotation.WebFilter;
        import java.io.IOException;
        
        /**
         * @author zhuxianglong
         * @version 1.0
         * @date 2021/8/30 22:56
         */
        //浏览器直接请求index.jsp资源时,该过滤器会被执行
        //@WebFilter(value="/index.jsp",dispatcherTypes = DispatcherType.REQUEST)
        //只有转发访问index.jsp时,该过滤器才会被执行
        //@WebFilter(value="/index.jsp",dispatcherTypes = DispatcherType.FORWARD)
        //浏览器直接请求index.jsp资源时或者转发访问index.jsp时,该过滤器才会被执行
        @WebFilter(value="/index.jsp",dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST})
        public class FilterDemo4 implements Filter {
        
            public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
                System.out.println("filterDemo4...");
                chain.doFilter(req, resp);
            }
        
            public void init(FilterConfig config) throws ServletException {
        
            }
        
            public void destroy() {
            }
        
        }
        

        ServletDemo1.java

        import javax.servlet.ServletException;
        import javax.servlet.annotation.WebServlet;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import java.io.IOException;
        
        /**
         * @author zhuxianglong
         * @version 1.0
         * @date 2021/8/30 23:02
         */
        @WebServlet("/servletDemo1")
        public class ServletDemo1 extends HttpServlet {
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                //转发到index.jsp
                request.getRequestDispatcher("/index.jsp").forward(request,response);
            }
        
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                this.doPost(request, response);
            }
        }
        
        1. INCLUDE:包含访问资源

        2. ERROR:错误跳转资源

        3. ASYNC:异步访问资源

    • web.xml配置

      • 设置<dispatcher></dispatcher>标签即可
过滤器链(配置多个过滤器)
  • 执行顺序:如果有两个过滤器:过滤器1和过滤器2
    1. 过滤器1
    2. 过滤器2
    3. 资源执行
    4. 过滤器2
    5. 过滤器1
  • 过滤器先后顺序问题:
    1. 注解配置:按照类名的字符串比较规则比较,值小的先执行
      • 如:AFilter和BFilter,AFilter先执行了
    2. web.xml:<filter-mapping>谁定义在上边,谁先执行

案例

案例一:登录验证
需求
  1. 访问case案例的资源。验证其是否登录
  2. 如果登陆了,则直接放行
  3. 如果没有登录,则跳转到登录页面,提示“您尚未登录,请先登录”
分析

在这里插入图片描述

案例二:敏感词汇过滤
需求
  1. 对case案例录入的数据进行敏感词汇过滤
  2. 如果是敏感词汇,替换为***

Listener监听器

概述

  • 概念:web的三大组件之一
    • 挤时间监听机制
      • 事件:一件事情
      • 事件源:事件发生的地方
      • 监听器:一个对象
      • 注册监听:将事件、事件源、监听器绑定在一起。当事件源上发生某个事件后,执行监听器代码

ServletContextListener

  • 监听ServletContext对象的创建和销毁
方法
  1. void contextDestriyed(ServletContextEven sce):ServletContext对象被销毁之前会调用该方法
  2. void contextInitialized(ServletContextEven sce):ServletContext对象创建后会调用该方法
步骤
  1. 定义一个类,实现ServletContextListener接口

  2. 复写方法

  3. 配置

    1. web.xml

      <listener>
              <listener-class>net.seehope.web.listener.ContextLoaderListener</listener-class>
      </listener>
      
      • 指定初始化参数<context-param>
    2. 注解

      • @WebListener
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @author zhuxianglong
 * @version 1.0
 * @date 2021/8/30 23:50
 */
@WebListener
public class ContextLoaderListener implements ServletContextListener {

    /**
     * 监听ServletContext对象创建的。ServletContext对象服务器启动后启动创建
     * @param sce
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //加载资源文件
        //1.获取ServletContext对象
        ServletContext servletContext = sce.getServletContext();

        //2.加载资源文件
        String initParameter = servletContext.getInitParameter("contextConfigLocation");

        //3.获取真实路径
        String realPath = servletContext.getRealPath("contextConfigLocation");

        //4.加载进内存
        try {
            FileInputStream fis = new FileInputStream(realPath);
            System.out.println(fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        System.out.println("ServletContext对象被创建了...");
    }

    /**
     * 在服务器关闭后,ServletContext对象被销毁。当服务器正常关闭后该方法调用
     * @param sce
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("ServletContext对象被销毁了...");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZXLzhuzhu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值