Java-Filter过滤器

白衣少年
2022-11-25 / 0 评论 / 48 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2022年11月25日,已超过523天没有更新,若内容或图片失效,请留言反馈。

Filter 什么是过滤器

  1. Filter 过滤器它是 JavaWeb 的三大组件之一。三大组件分别是:Servlet 程序、Listener 监听器、Filter 过滤器
  2. Filter 过滤器它是 JavaEE 的规范。也就是接口
  3. Filter 过滤器它的作用是:拦截请求,过滤响应。
拦截请求常见的应用场景有:
  1. 权限检查
  2. 日记操作
  3. 事务管理 ……等等

Filter 的初体验 要求:

在你的 web 工程下,有一个 admin 目录。这个 admin 目录下的所有资源(html 页面、jpg 图片、jsp 文件、等等)都必 须是用户登录之后才允许访问。


思考 :根据之前我们学过内容。我们知道,用户登录之后都会把用户登录的信息保存到 Session 域中。所以要检查用户是否 登录,可以判断 Session 中否包含有用户登录的信息即可!!!

Filter开发步骤

  1. 导包

    <?xml version="1.0" encoding="UTF-8"?>
     <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>javaWeb-filter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>13</maven.compiler.source>
        <maven.compiler.target>13</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
        </dependency>
        <!-- jstl表达式的依赖 -->
        <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- standard标签库 -->
        <!-- https://mvnrepository.com/artifact/taglibs/standard -->
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <!-- 连接数据库 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>
     </project>

    注意 :导入jar包时,一定要导入servlet下面的Filter
    过滤器filter的使用.png

  2. 编写过滤器

    package com.yu.filter;
     import javax.servlet.*;
     import java.io.IOException;
     public class CharacterEncodingFilter implements Filter {
    @Override
    //web服务器启动,就已经初始化了
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("初始化...");
        ServletContext context = filterConfig.getServletContext();
    }
    @Override
    //chain 过滤链
    /*
        过滤器中的所有代码,在过滤特定请求的时候,都会执行
        必须要让过滤器继续通行(把请求向后转交
    */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        System.out.println("执行前...");
        chain.doFilter(request,response);
    }
    @Override
    //web服务器关闭时,过滤器会销毁
    public void destroy() {
        System.out.println("已销毁");
    }
     }
  3. 在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">
    <servlet>
        <servlet-name>show</servlet-name>
        <servlet-class>com.yu.servlet.ShowServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>show</servlet-name>
        <url-pattern>/servlet/show</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>show</servlet-name>
        <url-pattern>/show</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>charsetFilter</filter-name>
        <filter-class>com.yu.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>charsetFilter</filter-name>
        <url-pattern>/servlet/*</url-pattern>
        <servlet-name>show</servlet-name>
        <servlet-name>show</servlet-name>
    </filter-mapping>
     </web-app>

    在 filter-mapping 中,可以配置多条 servlet-name,实现对servlet的精确过滤
    群体过滤用 url-pattern,可以过目某个目录的所有 servlet 文件,星号是通配符

Listener: 监听器

  1. 编写一个监听器
    实现一个监听器的接口
    覆盖重写方法

    package com.yu.listener;
     import javax.servlet.ServletContext;
     import javax.servlet.http.HttpSessionEvent;
     import javax.servlet.http.HttpSessionListener;
     //统计网站在线人数
     public class OnlineCountListener implements HttpSessionListener{
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext context = se.getSession().getServletContext();
        System.out.println(se.getSession().getId());
        Integer onlineCount = (Integer) context.getAttribute("OnlineCount");
        if (onlineCount==null){
            onlineCount=1;
        }else {
            int count=onlineCount.intValue();
            onlineCount=count+1;
        }
        context.setAttribute("OnlineCount",onlineCount);
    }
    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext context = se.getSession().getServletContext();
        Integer onlineCount = (Integer) context.getAttribute("OnlineCount");
        if (onlineCount==null){
            onlineCount=1;
        }else {
            int count=onlineCount.intValue();
            onlineCount=count-1;
        }
        context.setAttribute("OnlineCount",onlineCount);
    }
     }
  2. 配置监听器
    在web.xml中对监听器进行配置

    <listener>
    <listener-class>com.yu.listener.OnlineCountListener</listener-class>
     </listener>
4

打赏

评论 (0)

OwO
取消