Filter 拦截路径配置

image.png

Filter 过滤器链

image.png

代码及其运行结果

image.png

  1. package com.itheima.web.filter;
  2. import javax.servlet.*;
  3. import javax.servlet.annotation.*;
  4. import java.io.IOException;
  5. @WebFilter("/*")
  6. public class FilterDemo implements Filter {
  7. @Override
  8. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
  9. // 先执行放行前的,放行前对request数据进行处理
  10. System.out.println("1. FilterDemo...");
  11. // 放行
  12. chain.doFilter(request, response);
  13. // 这是放行后的代码,放行后,对Response 数据进行处理
  14. System.out.println("5. FilterDemo...");
  15. }
  16. public void init(FilterConfig config) throws ServletException {
  17. }
  18. public void destroy() {
  19. }
  20. }
  1. package com.itheima.web.filter;
  2. import javax.servlet.*;
  3. import javax.servlet.annotation.WebFilter;
  4. import java.io.IOException;
  5. //@WebFilter("/hello.jsp")
  6. @WebFilter("/*")
  7. public class FilterDemo2 implements Filter {
  8. @Override
  9. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
  10. // 先执行放行前的,放行前对request数据进行处理
  11. System.out.println("2. FilterDemo...");
  12. // 放行
  13. chain.doFilter(request, response);
  14. // 这是放行后的代码,放行后,对Response 数据进行处理
  15. System.out.println("4. FilterDemo...");
  16. }
  17. public void init(FilterConfig config) throws ServletException {
  18. }
  19. public void destroy() {
  20. }
  21. }