javaweb--Filter过滤器快速入门

1.过滤器实现类

package cn.liu.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 destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("FilterDemo1被执行了...");

        //放行
        filterChain.doFilter(servletRequest,servletResponse);
    }
}

2.过滤器细节处理

1.web.xml配置

    <filter>
        <filter-name>demo1</filter-name>
        <filter-class>cn.liu.filter.FilterDemo1</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>demo1</filter-name>
        // 拦截路径
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

2.过滤器执行流程

1.执行过滤器
2.执行放行后的资源
3.回来执行过滤器放行后的资源

3.过滤器的生命周期

package cn.liu.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@WebFilter("/*")
public class FilterDemo3 implements Filter {
    /**
     * 在服务器关闭后,filter对象销毁,如果服务器是正常关闭,则会执行destory  用于释放资源
     */
    public void destroy() {
        System.out.println("destory...");
    }

    /**
     * 每一次请求拦截资源时会执行,执行多次
     * @param requset
     * @param response
     * @param chain
     * @throws ServletException
     * @throws IOException
     */

    public void doFilter(ServletRequest requset, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        System.out.println("defilter...");
        //放行
        chain.doFilter(requset, response);
    }

    /**
     * 服务器启动后,创建Filter对象,调用init方法:用于加载资源
     * @param config
     * @throws ServletException
     */
    public void init(FilterConfig config) throws ServletException {
        System.out.println("init...");
    }

}
  • init:服务器启动后,创建Filter对象,调用init方法:用于加载资源
  • doFilter:每一次请求拦截资源时会执行,执行多次。
  • destory:在服务器关闭后,filter对象销毁,如果服务器是正常关闭,则会执行destory 用于释放资源

4.拦截路径的配置

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

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

//@WebFilter("/index.jsp")    //1.具体资源路径:/index.jsp   :只有访问/index.jsp资源时,过滤器才会执行!
//@WebFilter("/user/*") //2.目录拦截:/user/*:访问/user下的所有资源时,过滤器都会被执行
@WebFilter("*.jsp") //后缀名拦截:	3.*.jsp 访问所有后缀名为jsp资源时,过滤器都会被执行
public class FilterDemo4 implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest requset, ServletResponse response, FilterChain chain) throws ServletException, IOException {

        System.out.println("filterDemo4.。。");
        //放行
        chain.doFilter(requset, response);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
  • 拦截方式配置:资源被访问的方式
    1.注解配置
    *设置dispatcherTypes属性
    1.request:默认值。浏览器直接请求资源
    2.forword:转发访问资源
    3.include:包含访问资源
    4.error:错误跳转资源
    5.async:异步访问资源

代码

//表示浏览器直接请求资源时,该过滤器会被执行
@WebFilter(value = "/index.jsp", dispatcherTypes = DispatcherType.REQUEST)
//只有转发访问index.jsp时,该过滤器才能访问
@WebFilter(value = "/index.jsp", dispatcherTypes = DispatcherType.FORWARD)
//转发访问index.jsp或者直接请求资源时,该过滤器会被执行
@WebFilter(value = "/index.jsp", dispatcherTypes = {DispatcherType.FORWARD,DispatcherType.REQUEST})

2.web.xml配置
设置 标签即可

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

1.过滤器先后顺序问题
1.注解配置:按照类名的字符串名字规则比较,值小的先执行

2.web.xml配置:谁定义在上边,谁先执行!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值