Filter过滤器

1.概念:Filter:过滤器
生活中的过滤器:净水器,空气净化器,土匪、
web中的过滤器:当访问服务器的资源时,过滤器可以将请求拦截下来,完成一些特殊的功能。
过滤器的作用:一般用于完成通用的操作。如:登录验证、统一编码处理、敏感字符过滤…

2.快速入门
步骤:
①定义一个类,实现接口Filter
② 复写方法
③配置拦截路径:web.xml, 注解
代码:

package show.cactus.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

//快速入门
@WebFilter("/*")//访问所有资源之前,都会执行该过滤器
public class FilterDemo1 implements Filter {
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}
	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		System.out.println("doFileter被执行了");
		filterChain.doFilter(servletRequest,servletResponse);//放行
	}
	@Override
	public void destroy() {
	}
}

运行结果:
在这里插入图片描述在这里插入图片描述
3. 过滤器细节:
web.xml配置 :

<filter>
                <filter-name>demo1</filter-name>
                <filter-class>show.cactus.web.filter.FilterDemo1</filter-class>
                <!-- 设置Demo1的filter -->
</filter>
<filter-mapping>
                <filter-name>demo1</filter-name>
                <!-- 拦截路径:访问所有资源都会执行Demo1的filter -->
                <url-pattern>/*</url-pattern>
 </filter-mapping>

过滤器执行流程
  1. 执行过滤器
  2. 执行放行后的资源
  3. 回来执行过滤器放行代码下边的代码
index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    index.jsp
    <%
        System.out.println("index.jsp.......");
    %>
  </body>
</html>

FilterDemo2.java

package show.cactus.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/*")
public class FilterDemo2 implements Filter {
	public void destroy() {
	}
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
		System.out.println("FilterDemo2 Coming");
		//对request的请求消息增强
		chain.doFilter(req, resp);//放行
		//对response对象的响应消息增强
		System.out.println("FilterDemo2 Come Back");
	}
	public void init(FilterConfig config) throws ServletException {
	}
}

运行结果:
在这里插入图片描述在这里插入图片描述

过滤器生命周期方法
  1. init:在服务器启动后,会创建Filter对象,然后调用init方法。只执行一次。用于加载资源
  2. doFilter:每一次请求被拦截资源时,会执行。执行多次
  3. destroy:在服务器关闭后,Filter对象被销毁。如果服务器是正常关闭,则会执行destroy方法。只执行一次。用于释放资源

package show.cactus.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/*")
public class FilterDemo3 implements Filter {
	public void destroy() {
		System.out.println("destroy....");
	}
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
		System.out.println("doFilter....");
		chain.doFilter(req, resp);
	}
	public void init(FilterConfig config) throws ServletException {
		System.out.println("init....");
	}

}

在这里插入图片描述在这里插入图片描述
过滤器配置详解

  • 拦截路径配置:
      1. 具体资源路径: /index.jsp 只有访问index.jsp资源时,过滤器才会被执行
      2. 拦截目录: /user/* 访问/user下的所有资源时,过滤器都会被执行
      3. 后缀名拦截: .jsp 访问所有后缀名为jsp资源时,过滤器都会被执行
       4. 拦截所有资源:/
    访问所有资源时,过滤器都会被执行

  • 拦截方式配置:资源被访问的方式
      注解配置:
      设置dispatcherTypes属性
        1. REQUEST:默认值。浏览器直接请求资源
         2. FORWARD:转发访问资源
         3. INCLUDE:包含访问资源
        4. ERROR:错误跳转资源
        5. ASYNC:异步访问资源
    dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST}//可同时设置多种访问方式
    ServletDemo2.java

package show.cactus.web.filter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/servletDemo2")
public class ServletDemo2 extends HttpServlet {
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      System.out.println("ServletDemo2...");
      request.getRequestDispatcher("/index.jsp").forward(request,response);
   }
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      this.doPost(request, response);
   }
}

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    index.jsp
    <%
        System.out.println("index.jsp.......");
    %>
  </body>
</html>

FileterDemo5.java

package show.cactus.web.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
//浏览器直接访问资源时,
@WebFilter(value = "/index.jsp",dispatcherTypes = DispatcherType.REQUEST)
public class FilterDemo5 implements Filter {
   public void destroy() {
   }
   public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
      System.out.println("FilterDemo5....");
      chain.doFilter(req, resp);
   }
   public void init(FilterConfig config) throws ServletException {
   }
}

访问indx.jsp后运行结果:
在这里插入图片描述
将dispatcherTypes = DispatcherType.REQUEST修改为:dispatcherTypes = DispatcherType.FORWARD时,运行结果:在这里插入图片描述

    web.xml配置
      设置<dispatcher></dispatcher>标签即可


5. 过滤器链(配置多个过滤器)
   执行顺序:如果有两个过滤器:过滤器1和过滤器2
    1. 过滤器1
    2. 过滤器2
    3. 资源执行
    4. 过滤器2
    5. 过滤器1

    过滤器先后顺序问题:
      1. 注解配置:按照类名的字符串比较规则比较,值小的先执行
* 如: AFilter 和 BFilter,AFilter就先执行了。
       2. web.xml配置: 谁定义在上边,谁先执行
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值