Filter接口
要成为一个Filter需要实现Filter接口
Filter生命周期
容器启动就创建 Filter 实例,先执行无参构造器,然后执行 init() 方法,以后每次要过滤的请求会执行 doFilter() 方法,容器关闭时,执行 destroy() 方法
一、xml配置
1.web.xml
在 web.xml 中配置 Filter,<filter>
指定对应的 Filter 类,<filter-mapping>
映射信息,<url-pattern>
指定过滤哪些请求,/hello.jsp
,请求 hello.jsp
时,先执行过滤器,<init-param>
设置初始化参数,<dispatcher>
告诉服务器都拦截哪些方式到达的资源
<url-pattern>
:
- 精确匹配:/路径名/资源名,要拦截资源的详细信息,/hello.jsp
- 路径匹配:/路径名/,/拦截所有
- 后缀匹配:*.后缀,所有一给定后缀结尾的都拦截
<dispatcher>
:
- forward:拦截转发过来的
- include:拦截包含的(动态包含会被拦截),栗:
hello.jsp
页面包含test.jsp
页面,静态包含<%@include file="test.jsp" %>
;动态包含<jsp:include page="test.jsp"/>
- request:(默认)拦截直接请求的
- error:发生错误(全局配置的一个错误页面会被拦截,例如,发生错误404,500转到hello.jsp)
<error-page>
<error-code>404</error-code>
<location>/hello.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/hello.jsp</location>
</error-page>
<filter>
<filter-name>HelloFilter</filter-name>
<filter-class>com.example.filter.HelloFilter</filter-class>
<init-param>
<param-name>user</param-name>
<param-value>root</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>HelloFilter</filter-name>
<url-pattern>/hello.jsp</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
2.Filter类
当请求为 /hello.jsp?pwd=123
时通过过滤器,可以访问 hello.jsp
,否则,不能够范围 /hello.jsp
chain.doFilter()
方法,请求传递到下一层
public class HelloFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String user = filterConfig.getInitParameter("user");
System.out.println(user);
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String pwd = request.getParameter("pwd");
if (pwd != null && "123".equals(pwd)){
chain.doFilter(request, response);
}else {
response.getWriter().write("failed");
}
}
@Override
public void destroy() {
}
}
二、@WebFilter 注解配置
// WebFilter.java
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebFilter {
String description() default "";
String displayName() default "";
WebInitParam[] initParams() default {};
String filterName() default "";
String smallIcon() default "";
String largeIcon() default "";
String[] servletNames() default {};
String[] value() default {};
String[] urlPatterns() default {};
DispatcherType[] dispatcherTypes() default {DispatcherType.REQUEST};
boolean asyncSupported() default false;
}
name 等同于 <filter-name>
,urlPatterns 等同于 <url-pattern>
,initParams 等同于 <init-param>
@WebFilter(filterName = "HelloFilter",
urlPatterns = "/hello.jsp",
initParams = {
@WebInitParam(name = "user", value = "root")
})
2.Filter类
@WebFilter(filterName = "HelloFilter",
urlPatterns = "/hello.jsp",
dispatcherTypes = {
DispatcherType.FORWARD, DispatcherType.INCLUDE
},
initParams = {
@WebInitParam(name = "user", value = "root"),
})
public class HelloFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String user = filterConfig.getInitParameter("user");
System.out.println(user);
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String pwd = request.getParameter("pwd");
if (pwd != null && "123".equals(pwd)){
chain.doFilter(request, response);
}else {
response.getWriter().write("failed");
}
}
@Override
public void destroy() {
}
}