Filter接口
要成为一个Filter需要实现Filter接口
Filter生命周期
容器启动就创建Filter实例,先执行无参构造器,然后执行init()方法,以后每次要过滤的请求会执行doFilter()方法,容器关闭时,执行destroy()方法
public interface Filter {
public void init(FilterConfig filterConfig) throws ServletException;
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException;
public void destroy();
}
一、xml配置
1.web.xml
在web.xml中配置Filter,<filter>指定对应的Filter类,<filter-mapping>映射信息,<url-pattern>指定过滤哪些请求,/hello.jsp,请求hello.jsp时,先执行过滤器,<init-param>设置初始化参数,<dispatcher>告诉服务器都拦截哪些方式到达的资源
<url-pattern>:
1)精确匹配:/路径名/资源名,要拦截资源的详细信息,/hello.jsp
2)路径匹配:/路径名/*,/*拦截所有
3)后缀匹配:*.后缀,所有一给定后缀结尾的都拦截
<dispatcher>:
1)forward:拦截转发过来的
2)include:拦截包含的(动态包含会被拦截)
栗:hello.jsp页面包含test.jsp页面
静态包含<%@include file="test.jsp" %>;动态包含<jsp:include page="test.jsp"/>
3)request:(默认)拦截直接请求的
4)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注解配置
name等同于<filter-name>,urlPatterns等同于<url-pattern>,initParams 等同于<init-param>
@WebFilter(filterName = "HelloFilter",
urlPatterns = "/hello.jsp",
initParams = {
@WebInitParam(name = "user", value = "root")
})
2.Servlet类
@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() {
}
}