4、FilterConfig 类
FilterConfig类见名知义,它是Filter过滤器的配置文件类。
Tomcat每次创建Filter的时候,也会同时创建一个FilterConfig类,这里包含了Filter配置文件的配置信息。
FilterConfig类的作用是获取fiter过滤器的配置内容
1、获取Filter的名称filter-name的内容
2、获取在Filter 中配置的init-param初始化参数
3、获取ServletContext对象
修改 web.xml
<filter> <!-- 给filter起一个别名 --> <filter-name>AdminFilter</filter-name> <!-- 配置filter的全类名 --> <filter-class>com.filter.AdminFilter</filter-class> <init-param> <param-name>username</param-name> <param-value>root</param-value> </init-param> <init-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306/test</param-value> </init-param> </filter>
修改 AdminFilter
package com.filter; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import java.io.IOException; public class AdminFilter implements Filter { public AdminFilter(){ System.out.println("1.Filter构造器方法AdminFilter()"); } @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("2.Filter的init(FilterConfig filterConfig) 初始化"); // 1、获取Filter的名称filter-name的内容 System.out.println("filter-name的值是:"+filterConfig.getFilterName()); // 2、获取在Filter 中配置的init-param初始化参数 System.out.println("初始化参数username的值是:"+filterConfig.getInitParameter("username")); System.out.println("初始化参数username的值是:"+filterConfig.getInitParameter("url")); // 3、获取ServletContext对象 System.out.println(filterConfig.getServletContext()); } /** * doFilter方法,专门用于拦截请求。可以做权限检查 * @param servletRequest * @param servletResponse * @param filterChain * @throws IOException * @throws ServletException */ @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("3.Filter的doFilter() 过滤"); HttpServletRequest httpServletRequest= (HttpServletRequest) servletRequest; HttpSession session=httpServletRequest.getSession(); Object user=session.getAttribute("user");//导入service包 //如果等于null,说明还没有登录 if (user==null){ servletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest,servletResponse); return; }else { //让程序继续往下访问用户的目标资源 filterChain.doFilter(servletRequest,servletResponse); } } @Override public void destroy() { System.out.println("4.Filter的destroy() 销毁方法"); } }
结果
5、FilterChain 过滤器链
新建filter/Filter1
package com.filter; import javax.servlet.*; import java.io.IOException; public class Filter1 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("Filter1 前置代码"); System.out.println("Filter1线程"+Thread.currentThread().getName()); System.out.println("Filter1 "+servletRequest.getParameter("username")); servletRequest.setAttribute("key1","value1"); filterChain.doFilter(servletRequest,servletResponse); System.out.println("Filter1线程"+Thread.currentThread().getName()); System.out.println("Filter1 后置代码"); } @Override public void destroy() { } }
新建filter/Filter2
package com.filter; import javax.servlet.*; import java.io.IOException; public class Filter2 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("Filter2 前置代码"); System.out.println("Filter2线程"+Thread.currentThread().getName()); System.out.println("Filter2 "+servletRequest.getParameter("username")); System.out.println("Filter2取Filter1中保存的数据是:"+servletRequest.getAttribute("key1")); filterChain.doFilter(servletRequest,servletResponse); System.out.println("Filter2线程"+Thread.currentThread().getName()); System.out.println("Filter2 后置代码"); } @Override public void destroy() { } }
配置web.xml 执行顺序和配置顺序一样
<filter> <filter-name>Filter1</filter-name> <filter-class>com.filter.Filter1</filter-class> </filter> <filter-mapping> <filter-name>Filter1</filter-name> <url-pattern>/target.jsp</url-pattern> </filter-mapping> <filter> <filter-name>Filter2</filter-name> <filter-class>com.filter.Filter2</filter-class> </filter> <filter-mapping> <filter-name>Filter2</filter-name> <url-pattern>/target.jsp</url-pattern> </filter-mapping>
新建web/target.jsp
<%-- Created by IntelliJ IDEA. User: lenovo Date: 2021/9/5 Time: 下午 08:48 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% System.out.println("target.jsp页面执行了"); System.out.println("target.jsp线程"+Thread.currentThread().getName()); System.out.println("target.jsp "+request.getParameter("username")); %> </body> </html>
结果
6、Filter的拦截路径
-精确匹配
<url-pattern>/target.jsp</url-pattern>
以上配置的路径,表示请求地址必须为:http:/ip:port/工程路径/target.jsp
-目录匹配
<url-pattern>/admin/*</url-pattern>
以上配置的路径,表示请求地址必须为:http://ip:port/工程路径/admin/*
-后缀名匹配
<url-pattern>*.html</url-pattern>
以上配置的路径,表示请求地址必须以.html结尾才会拦截到。
<url-pattern>*.do</url-pattern>
以上配置的路径,表示请求地址必须以.do结尾才会拦截到。
<url-pattern>*.action</url-pattern>
以上配置的路径,表示请求地址必须以.action结尾才会拦截到。
注意:不能以/打头
Filter过滤器它只关心请求的地址是否匹配,不关心请求的资源是否存在!!
演示
访问http://localhost:8080/15_filter/target.abc
修改web.xml
<filter-mapping> <filter-name>Filter2</filter-name> <url-pattern>*.abc</url-pattern> </filter-mapping>