JavaWeb中的filter拦截方式的配置
拦截方式配置:资源被访问的方式
- 注解配置:
设置dispatcherTypes属性- REQUEST:默认值。浏览器直接请求资源
- FORWARD:转发访问资源
- INCLUDE:包含访问资源
- ERROR:错误跳转资源
- ASYNC:异步访问资源
- web.xml配置
设置标签即可
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
//拦截方式配置
@WebFilter(value = "/index.jsp", dispatcherTypes = {DispatcherType.REQUEST,DispatcherType.FORWARD})
public class FilterDemo5 implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
System.out.println("拦截请求或者转发到index.jsp的资源");
chain.doFilter(req, resp);
}
public void init(FilterConfig config) throws ServletException {
}
}