作用:对url进行统一拦截处理,用于应用程序层面进行全局处理。
(https://blog.csdn.net/yuzhiqiang_1993/article/details/81288912)
- 任何过滤器都要实现javax.servlet.Filter接口
- 在Filter接口的doFilter()方法中编写过滤器的功能代码
- 在web.xml中对过滤器进行配置,说明拦截URL的范围
第一个过滤器
MyFirstFilter.java
public class MyFirstFilter implements Filter {
@Override
public void destroy() {
// TODO 自动生成的方法存根
Filter.super.destroy();
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO 自动生成的方法存根
System.out.println("过滤器已经生效");
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO 自动生成的方法存根
Filter.super.init(filterConfig);
}
}
web.xml配置(或者使用注解配置)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>first-filter</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>MyFirstFilter</filter-name>
<filter-class>com.ulikeme.filter.MyFirstFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFirstFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
(注解形式配置)
@WebFilter(filterName="MyAnnoationFilter",urlPatterns="/*")
创建index.html并且访问,控制台输出
字符集过滤器(解决get,post请求乱码)
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO 自动生成的方法存根
HttpServletRequest req = (HttpServletRequest) request;
req.setCharacterEncoding("UTF-8");
HttpServletResponse res = (HttpServletResponse) response;
res.setContentType("text/html;charset=utf-8");
chain.doFilter(request, response);
}
过滤器参数化
(字符集过滤器优化)
web.xml中配置
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.ulikeme.filter.CharacterEncodingFilter</filter-class>
<!-- 过滤器参数 -->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>encoding2</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
public class CharacterEncodingFilter implements Filter {
private String encoding;
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO 自动生成的方法存根
HttpServletRequest req = (HttpServletRequest) request;
req.setCharacterEncoding(encoding);
HttpServletResponse res = (HttpServletResponse) response;
res.setContentType("text/html;charset="+encoding);
chain.doFilter(request, response);
}
@Override
public void destroy() {
// TODO 自动生成的方法存根
Filter.super.destroy();
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO 自动生成的方法存根
encoding=filterConfig.getInitParameter("encoding");
}
}
注解形式配置
@WebFilter(filterName="MyAnnoationFilter",urlPatterns="/*",initParams= {
@WebInitParam(name="p1",value="v1"),
@WebInitParam(name="p2",value="v2")
})
url-pattern设置过滤范围
过滤链
- 每个过滤器具有独立的职能
- 过滤器执行顺序以 <filter-mapping>顺序为准(注解形式则按照类名字母表升序顺序排序)
- 调用chain.doFilter()将请求向后传递