Filter 拦截路径配置
Filter 过滤器链
代码及其运行结果
package com.itheima.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebFilter("/*")
public class FilterDemo implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
// 先执行放行前的,放行前对request数据进行处理
System.out.println("1. FilterDemo...");
// 放行
chain.doFilter(request, response);
// 这是放行后的代码,放行后,对Response 数据进行处理
System.out.println("5. FilterDemo...");
}
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
}
package com.itheima.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
//@WebFilter("/hello.jsp")
@WebFilter("/*")
public class FilterDemo2 implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
// 先执行放行前的,放行前对request数据进行处理
System.out.println("2. FilterDemo...");
// 放行
chain.doFilter(request, response);
// 这是放行后的代码,放行后,对Response 数据进行处理
System.out.println("4. FilterDemo...");
}
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
}