什么是Filter过滤器?
Filter 过滤器它是 JavaWeb 的三大组件之一。(三大组件分别是:Servlet 程序、Listener 监听器、Filter 过滤器)
Filter 过滤器它是 JavaEE 的规范,也就是接口
Filter 过滤器它的作用是:
拦截请求
,过滤响应。
Filter 过滤器的使用步骤:
案例:
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//解决响应的乱码问题
response.setContentType("text/html; charset=UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
//简略模拟数据库判断用户名密码是否正确
if("mike".equals(username)&&"123456".equals(password)){
request.getSession().setAttribute("username",username);
response.getWriter().write("登录成功");
}else {
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
}
}
Filter:
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class AdminFilter implements Filter {
//doFilter 方法,专门用于拦截请求。可以做权限检查
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpSession session = httpServletRequest.getSession();
Object username = session.getAttribute("username");
// 如果等于 null,说明还没有登录
if (username == null) {
servletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest, servletResponse);
return;
} else {
// 让程序继续往下访问用户的目标资源
filterChain.doFilter(servletRequest, servletResponse);
}
}
}
web.xml:
<servlet>
<servlet-name>LoginServletservlet-name>
<servlet-class>com.fox.servlet.LoginServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>LoginServletservlet-name>
<url-pattern>/loginServleturl-pattern>
servlet-mapping>
<filter>
<filter-name>AdminFilterfilter-name>
<filter-class>com.fox.Filter.AdminFilterfilter-class>
filter>
<filter-mapping>
<filter-name>AdminFilterfilter-name>
<url-pattern>/admin/*url-pattern>
filter-mapping>
此时如果输入错误的用户名或密码则会跳回到登录页面,并且此时访问admin目录下的资源也会跳回到登录页面:
只有成功登录后才能访问admin目录下的资源。
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("构造方法执行了");
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("初始化方法执行了");
}
@Override
public void destroy() {
System.out.println("销毁方法执行了");
}
//doFilter 方法,专门用于拦截请求。可以做权限检查
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
HttpSession session = httpServletRequest.getSession();
Object username = session.getAttribute("username");
// 如果等于 null,说明还没有登录
if (username == null) {
servletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest, servletResponse);
System.out.println("doFilter方法执行了");
return;
} else {
// 让程序继续往下访问用户的目标资源
filterChain.doFilter(servletRequest, servletResponse);
}
}
}
见名知义,FilterConfig 类是 Filter 过滤器的配置文件类。
Tomcat 每次创建 Filter 的时候,也会同时创建一个 FilterConfig 类,这里包含了 Filter 配置文件的配置信息。
FilterConfig 类的
作用
——获取 Filter 过滤器的配置内容:
filterConfig.getFilterName()
获取 Filter 的名称 ,即
里的内容filterConfig.getInitParameter(String 参数名)
获取在 Filter 中配置的
里的初始化参数filterConfig.getServletContext()
获取 ServletContext 对象案例:
web.xml:
<filter>
<filter-name>AdminFilterfilter-name>
<filter-class>com.fox.Filter.AdminFilterfilter-class>
<init-param>
<param-name>usernameparam-name>
<param-value>rootparam-value>
init-param>
<init-param>
<param-name>urlparam-name>
<param-value>jdbc:mysql://localhost:3306/testparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>AdminFilterfilter-name>
<url-pattern>/admin/*url-pattern>
filter-mapping>
AdminFilter:
import javax.servlet.*;
import java.io.IOException;
public class AdminFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// 1、获取 Filter 的名称 filter-name 的内容
System.out.println("filter-name 的值是:" + filterConfig.getFilterName());
// 2、获取在 web.xml 中配置的 init-param 初始化参数
System.out.println("初始化参数 username 的值是:" + filterConfig.getInitParameter("username"));
System.out.println("初始化参数 url 的值是:" + filterConfig.getInitParameter("url"));
// 3、获取 ServletContext 对象
System.out.println(filterConfig.getServletContext());
}
//doFilter 方法,专门用于拦截请求。可以做权限检查
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
}
}
FilterChain 就是过滤器链(它向我们展示了多个过滤器如何一起工作)
FilterChain.doFilter()方法的
作用
:
在多个Filter过滤器在执行的时候,它们执行的优先顺序是由它们在web.xml中从上到下配置的顺序决定
多个Filter过滤器执行的
特点
:
案例:
Filter1:
import javax.servlet.*;
import java.io.IOException;
public class Filter1 implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("Filter1前置代码");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("Filter1后置代码");
}
}
Filter2:
import javax.servlet.*;
import java.io.IOException;
public class Filter2 implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("Filter2前置代码");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("Filter2后置代码");
}
}
web.xml:
<filter>
<filter-name>Filter1filter-name>
<filter-class>com.fox.Filter.Filter1filter-class>
filter>
<filter-mapping>
<filter-name>Filter1filter-name>
<url-pattern>/a.jspurl-pattern>
filter-mapping>
<filter>
<filter-name>Filter2filter-name>
<filter-class>com.fox.Filter.Filter2filter-class>
filter>
<filter-mapping>
<filter-name>Filter2filter-name>
<url-pattern>/a.jspurl-pattern>
filter-mapping>
<url-pattern>/target.jspurl-pattern>
以上配置的路径,表示请求地址为:http://ip:port/工程路径/target.jsp
<url-pattern>/admin/*url-pattern>
以上配置的路径,表示请求地址为:http://ip:port/工程路径/admin/*
<url-pattern>*.htmlurl-pattern>
以上配置的路径,表示请求地址必须以.html 结尾才会拦截到
<url-pattern>*.dourl-pattern>
以上配置的路径,表示请求地址必须以.do 结尾才会拦截到
<url-pattern>*.actionurl-pattern>
以上配置的路径,表示请求地址必须以.action 结尾才会拦截到
注意:Filter 过滤器它只关心请求的地址是否匹配,不关心请求的资源是否存在