Javaweb | 过滤器、配置、过滤器链、优先级
过滤器在这里插入图片描述
概念过滤器(Filter)是处于客户端与服务器目标资源之间的一道过滤技术
用户的请求和响应都需要经过过滤器过滤器作用执行地位在Servlet之前,客户端发送请求是,会先经过Filter,再到达目标Servlet中;响应时,会根据执行流程再次反向执行Filter可以解决多个Servlet共性代码的冗余问题过滤器可以对请求前和请求后进行逻辑处理编写过滤器Servlet API中提供了一个Filter接口,开发人员编写一个Java类实现了这个接口即可,这个Java类称为过滤器
实现过程编写Java类实现Filter接口在doFilter方法中编写拦截逻辑设置拦截路径FilterChain 过滤链 作用:
它可以指向下一个资源,如果还有过滤器,则继续进行下一次的过滤,如果没有过滤器,则会指向资源
MyFilter类package com.woniu.Filter;
import javax.servlet.*;import javax.servlet.annotation.WebFilter;import java.io.IOException;
@WebFilter(value = "/t")public class MyFilter implements Filter {
//初始化方法 @Override public void init(FilterConfig filterConfig) throws ServletException { } //doFilter方法中编写拦截逻辑 @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("---MyFilter---"); //FilterChain过滤链,让请求继续 filterChain.doFilter(servletRequest, servletResponse); System.out.println("---End---"); /* 输出结果: ---MyFilter--- ---Target--- ---End--- */ } //销毁方法 @Override public void destroy() { }
}
123456789101112131415161718192021222324252627282930313233343536373839TargetServlet类package com.woniu.Filter;
import javax.servlet.; import javax.servlet.http.;import javax.servlet.annotation.*;import java.io.IOException;
//注解过滤器,指定过滤目标资源@WebServlet(name = "TargetServlet", value = "/t")public class TargetServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("---Target---"); /* 输出结果: ---MyFilter--- ---Target--- */ }
}
1234567891011121314151617181920212223242526过滤器配置注解过滤器在自定义的Filter类上使用注解
@WebServlet(value="/过滤目标资源");
@WebServlet(name = "TargetServlet", value = "/t")1xml配置<!--<!DOCTYPE web-app PUBLIC--><!-- "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"--><!-- "http://java.sun.com/dtd/web-app_2_3.dtd" >-->
<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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version="3.1"metadata-complete="false">
<!--起始页面--> <!--<welcome-file-list>--> <!--<welcome-file></welcome-file>--> <!--</welcome-file-list>--> <filter> <filter-name>xml</filter-name> <filter-class>com.woniu.Filter.XmlFilter</filter-class> </filter> <filter-mapping> <filter-name>xml</filter-name> <url-pattern>/test</url-pattern> </filter-mapping>
</web-app>
12345678910111213141516171819202122232425262728过滤器链 & 优先级过滤器链概念:客户端对服务器请求之后,服务器调用Servlet之前会执行一组过滤器(多个过滤器),那么这组过滤器就称为一条过滤器链
流程:每个过滤器实现某个特定的功能,当第一个Filter的doFilter方法被调用时,Web服务器会创建一个代表Filter链的FilterChain对象传递给该方法,在doFilter方法中,开发人员如果调用了FilterChain对象的doFilter方法,则Web服务器会检查FilterChain对象中是否还有Filter,如果有,则调用第二个Filter,如果没有,则调用目标资源
在这里插入图片描述
过滤器优先级一个Web应用中,可以开发编写多个Filter,这些Filter组合起来称为一个Filter链
优先级:
如果为注解的话,是按照类全名称的字符串顺序决定作用顺序如果web.xml,按照filter-mapping注册顺序,从上往下web.xml配置高于注解方式如果注解和web.xml同时配置,会创建多个过滤器对象,造成过滤多次过滤器拦截路径精确拦截匹配:/index.jsp后缀拦截匹配:.jsp 通配符拦截匹配:/ 表示拦截所有过滤器解决编码问题Login.html页面<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport"content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Document</title></head><body><form action="/Servlet_Projects_war/login" method="post">用户名:<input type="text" name="username"/><br>密码:<input type="password" name="password"/><br><input type="submit" value="登录"></form></body></html>
1234567891011121314151617LoginServlet类package com.woniu.Filter;
import javax.servlet.; import javax.servlet.http.;import javax.servlet.annotation.*;import java.io.IOException;
@WebServlet(name = "LoginServlet", value = "/login")public class LoginServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //根据表单组件名称获取提交数据 String username = request.getParameter("username"); String password = request.getParameter("password"); System.out.println(username + "\t" + password); }
}
123456789101112131415161718192021222324EncodingFilter过滤器类package com.woniu.Filter;
import javax.servlet.*;import javax.servlet.annotation.WebFilter;import java.io.IOException;
@WebFilter(value = "/login")public class EncodingFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {
} @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { //统一处理请求和响应的乱码 servletRequest.setCharacterEncoding("UTF-8"); servletResponse.setContentType("text/html;charset=utf-8"); //FilterChain过滤链,让请求继续 filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { }
}
12345678910111213141516171819202122232425262728结果张三 123456