○ 概念:Filter表示过滤器,是JavaWeb三大组件(Servlet、Filter、Listener)之一。
○ 过滤器可以把对资源的请求拦截下来,从而实现一些特殊的功能。
○ 过滤器一般完成一些通用的操作,比如:权限控制、统一编码处理、敏感字符处理等待。。。
Filter快速入门
○ 定义类,实现FIlter接口,并重写其所有方法@Overridepublic void init(FilterConfig filterConfig)throws ServletException{}@Overridepublic void doFilter(ServletRequest servletRequest,ServletResponse servletResponse,FilterChainfilterChain)throws IOException, ServletException{}@Overridepublic void destroy(){}○ 配置Filter拦截资源的路径:在类上定义@WebFilter("/*") 注解@WebFilter("/*")public class FilterDemo implements Filter○ 在doFilter方法中输出一句话,并放行§ filterChain.doFilter(servletRequest,servletResponse);
Filter执行流程
pom.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> < project xmlns = " http://maven.apache.org/POM/4.0.0" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion> 4.0.0</ modelVersion> < properties> < maven.compiler.source> 8</ maven.compiler.source> < maven.compiler.target> 8</ maven.compiler.target> </ properties> < dependencies> < dependency> < groupId> mysql</ groupId> < artifactId> mysql-connector-java</ artifactId> < version> 8.0.27</ version> </ dependency> < dependency> < groupId> javax.servlet</ groupId> < artifactId> javax.servlet-api</ artifactId> < version> 3.1.0</ version> < scope> provided</ scope> </ dependency> < dependency> < groupId> org.mybatis</ groupId> < artifactId> mybatis</ artifactId> < version> 3.5.5</ version> </ dependency> < dependency> < groupId> javax.servlet.jsp</ groupId> < artifactId> jsp-api</ artifactId> < version> 2.2</ version> < scope> provided</ scope> </ dependency> < dependency> < groupId> jstl</ groupId> < artifactId> jstl</ artifactId> < version> 1.2</ version> </ dependency> </ dependencies> < groupId> org.example</ groupId> < artifactId> filter-demo</ artifactId> < version> 1.0-SNAPSHOT</ version> < build> < plugins> < plugin> < groupId> org.apache.tomcat.maven</ groupId> < artifactId> tomcat7-maven-plugin</ artifactId> < version> 2.2</ version> </ plugin> </ plugins> </ build> < packaging> war</ packaging> </ project>
访问的页面的jsp文件
< % @ page contentType= "text/html;charset=UTF-8" language= "java" % >
< html>
< head> < title> Title< / title>
< / head>
< body> < h1> Hello JSP~ ~ < / h1> < % System. out. println ( "3.hello Jsp" ) ; % >
< / body>
< / html>
创建一个过滤器FilterDemo实现Filter接口
@WebFilter ( "/*" )
public class FilterDemo 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 ( "1.FilterDemo..." ) ; / / 放行filterChain. doFilter ( servletRequest, servletResponse) ; / / 2. 放行后,对response数据进行处理System. out. println ( "5.FilterDemo..." ) ; } @Override public void destroy ( ) { }
}
运行结果
输入链接,按下回车
页面显示Hello JSP~~
控制台输出,对应上述的Filter执行流程
• Filter使用细节
○ Filter拦截路径配置§ 拦截具体的资源:/index.jsp:只有访问index.jsp时才会被拦截§ 目录拦截:/user/*:访问/user下的所有资源都会被拦截§ 后缀名拦截:*.jsp:访问后缀名为jsp的资源都会被拦截§ 拦截所有:/*:访问所有资源都会被拦截○ 过滤器链§ 一个web应用,可以配置多个过滤器,这多个过滤器称为过滤器链