概述
Filter的基本功能是对Servlet容器调用Servlet的过程进行拦截,从而在Servlet进行响应处理的前后实现一些特殊的功能。
在Servlet API中定义了三个接口来供开发人员编写Filter程序:Filter,FilterChain,FilterConfig。
Filter程序是一个实现了Filter接口的Java类,与Servlet程序相似,它由Servlet容器进行调用和执行。
Filter程序需要在web.xml文件中进行注册和设置它所能拦截的资源:Filter程序可以拦截JSP,Servlet,静态图片文件和静态html文件。
Filter是什么?
JavaWeb的一个重要组件,可以对发送到Servlet的请求进行拦截,并对相应也进行拦截。
Filter是实现了Filter接口的Java类,Filter需要在web.xml文件中进行配置。
如何创建一个Filter,并把它跑起来?
1.创建一个Filter类,实现Filter接口
2.在web.xml文件中配置并映射该Filter
其中url-pattern指定该Filter可以拦截哪些资源,即可以通过那些url访问到该Filter。
3.Filter接口:
init(FilterConfig filterConfig):类似Servlet的init方法,在创建Filter对象后,立即被调用,且只被调用一次;Filter对象在Servlet容器加载当前WEB应用时即被创建;该方法用于对当前的Filter进行初始化操作。Filter实例是单例的。
FilterConfig :类似于ServletConfig
可以在web.xml文件中配置当前Filter的初始化参数,配置方式也和Servlet类似。
doFilter(ServletRequest request,ServletResponse response,FilterChain chain):真正Filter的逻辑代码需要编写在该方法中,每次拦截都会调用该方法。
>FilterChain :Filter链,多个Filter可以构成一个Filter链。
>chain.doFilter(request,response);//放行
>多个Filter拦截的顺序和<filter-mapping>配置的顺序有关,靠前的先被调用。
destroy:释放当前Filter所占用资源的方法,在Filter被销毁之前被调用且只被调用一次。
Demo
目录结构
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>javaWeb_27</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- 当前web应用的初始化参数 -->
<context-param>
<param-name>userName</param-name>
<param-value>admin</param-value>
</context-param>
<!-- 用户名过滤器 -->
<filter>
<filter-name>UserNameFilter</filter-name>
<filter-class>com.dao.chu.UserNameFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UserNameFilter</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping>
<!-- 密码过滤器 -->
<filter>
<filter-name>PasswordFilter</filter-name>
<filter-class>com.dao.chu.PasswordFilter</filter-class>
<!-- 当前Filter的初始化参数 -->
<init-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PasswordFilter</filter-name>
<url-pattern>/index.jsp</url-pattern>
</filter-mapping>
</web-app>
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>login.jsp</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/index.jsp">
<input type="text" name="userName"><br><br>
<input type="password" name="password"><br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
UserNameFilter.java
package com.dao.chu;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class UserNameFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String userName = request.getParameter("userName");
String initUserName = request.getServletContext().getInitParameter("userName");
if (null!=userName&&userName.equals(initUserName)) {
chain.doFilter(request, response);
return;
}
request.setAttribute("message", "用户名错误");
request.getRequestDispatcher("/message.jsp").forward(request, response);;
}
public void init(FilterConfig arg0) throws ServletException {
}
}
PasswordFilter.java
package com.dao.chu;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class PasswordFilter implements Filter {
private FilterConfig filterConfig;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String password = request.getParameter("password");
String initPassword = filterConfig.getInitParameter("password");
if (null!=password&&password.equals(initPassword)) {
chain.doFilter(request, response);
return;
}
request.setAttribute("message", "密码错误");
request.getRequestDispatcher("/message.jsp").forward(request, response);
}
public void init(FilterConfig config) throws ServletException {
this.filterConfig=config;
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index.jsp</title>
</head>
<body>
<h2>欢迎来到主页!</h2>
</body>
</html>
message.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>message.jsp</title>
</head>
<body>
<h3>
<%=request.getAttribute("message")==null?"":request.getAttribute("message") %>
</h3>
</body>
</html>
运行效果
账户:admin 密码:123456
账户:admin1 密码:123456
账户:admin 密码:111