什么是Filter过滤器
1. Filter过滤器是什么?
- Filter过滤器是JavaWeb的三大组件之一, 三大组件分别是: Servlet程序、Listener监听器、Filter过滤器
- Filter过滤器,它是javaEE的规范,也就是接口
- Filter过滤器他的作用是拦截请求过滤响应
一图分析Filter过滤器在JavaWeb中的使用
那么我们来根据分析的图来搭建页面
2. Filter过滤器的使用
1、首先创建web工程
2、在web工程下的webapp目录中创建admin目录,在admin目录下放入一些有权限才能访问的文件
3、创建登录页面、登录成功页面
index.jsp 登录页面
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
<base href="http://localhost:8080/15_filter/">
</head>
<body>
<form method="get" action="login_success.jsp">
用户名:<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
login_success.jsp页面 登录成功后的页面,会把登录信息存储进session域中
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//获取到用户信息,表示已登录
String username = request.getParameter("username");
//将用户信息存储到session域中
session.setAttribute("username",username);
%>
登录成功
</body>
</html>
4、创建Filter过滤器,创建好后需要在web.xml页面配置Filter过滤器的信息
public class LoginFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest) request;
//获取session域中的username对象
Object username = req.getSession().getAttribute("username");
System.out.println("用户信息是否为空: " +username);
if (username == null ){
//如果用户信息为空则,跳转去登录界面
req.getRequestDispatcher("/index.jsp").forward(request,response);
}else{
//如果用户信息不为空,则执行访问的目标资源
chain.doFilter(request,response);
}
}
}
web.xml配置Filter过滤器
<?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过滤器-->
<filter>
<!--filter-name标签用于创建filter的别名-->
<filter-name>LoginFilter</filter-name>
<!--filter-class标签用于指定filter过滤器的全类名-->
<filter-class>com.example.filter.LoginFilter</filter-class>
</filter>
<!--filter-mapping标签用于配置过滤器的拦截路径-->
<filter-mapping>
<!--filter-name标签是指定filter过滤器别名-->
<filter-name>LoginFilter</filter-name>
<!--url-pattern标签
/ : 斜线表示指定到http://ip/port/工程路径/ 映射到idea的web工程下
admin/* : 是只要拦截的地址, 当客户端访问此路径时要检测是否已登录
-->
<url-pattern>/admin/*</url-pattern>
</filter-mapping>
</web-app>
测试Filter过滤器的使用
1、不输入用户名情况下,直接通过地址栏搜索admin目录内的文件,Filter过滤经过检测发现这个访问者的用户信息为空的,把他转发去了登录页面
2、输入用户名情况下,跳转到登录成功页面,页面会将登录信息保存在session域里,这时访问admin目录内的文件,Filter过滤器检测后当前访问者的用户信息不为空,则跳转进访问的目标文件内
3. Filter的生命周期
Filter的生命周期有几个方法
1、构造器方法
2、init()方法
第1、2步在web工程启动时执行
3、doFilter()方法
在拦截到请求时执行
4、destroy()方法
停止web工程时执行
4. FilterConfiig类
- 见名思意,FilterConfig类就是获取Filter过滤器的配置信息
- 当web工程启动时,随着Filter过滤器的创建,FilterConfig也会创建,这里一个Filter过滤器对象对应着一个FilterConfig对象
FilterConfig常用的方法:
1、获取Filter过滤器的别名 : filterConfig.getFilterName()
2、获取filter的初始化参数 : filterConfig.getInitParameter()
3、获取ServletContext对象 : filterConfig.getServletContext()
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter过滤器的名称为: " + filterConfig.getFilterName());
//获取Init参数需要在web.xml文件里配置好
System.out.println("Filter过滤器的url参数为: " + filterConfig.getInitParameter("url"));
System.out.println("Filter过滤器的ServletContext对象: " + filterConfig.getServletContext());
}
5、过滤器链
Filter 过滤
Chain 链
过滤器链就是在访问一个目标资源上由多个过滤器组成的链,能够进一步增加访问权限
6、Filter过滤器的三种匹配方式
– 精确匹配
<url-pattern>/target.jsp</url-pattern>
以上配置的路径,必须是: http://ip:port/工程路径/target.jsp
– 目录匹配
<url-pattern>/admin/*</url-pattern>
以上配置的路径,必须是: http://ip:port/工程路径/admin/*
–后缀匹配
<url-pattern>.jsp</url-pattern>
以上配置的路径,必须是带有包含.jsp的文件,不能以左斜线开头
可以看出Fliter过滤器匹配只看地址符不符要求,不看资源存不存在