JavaWeb的过滤器(Filter)的两种配置

Filter接口

要成为一个Filter需要实现Filter接口

Filter生命周期

容器启动就创建Filter实例,先执行无参构造器,然后执行init()方法,以后每次要过滤的请求会执行doFilter()方法,容器关闭时,执行destroy()方法

public interface Filter {

    public void init(FilterConfig filterConfig) throws ServletException;

    public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException;

    public void destroy();
}

一、xml配置

1.web.xml

在web.xml中配置Filter,<filter>指定对应的Filter类,<filter-mapping>映射信息,<url-pattern>指定过滤哪些请求,/hello.jsp,请求hello.jsp时,先执行过滤器,<init-param>设置初始化参数,<dispatcher>告诉服务器都拦截哪些方式到达的资源

<url-pattern>:

1)精确匹配:/路径名/资源名,要拦截资源的详细信息,/hello.jsp

2)路径匹配:/路径名/*,/*拦截所有

3)后缀匹配:*.后缀,所有一给定后缀结尾的都拦截

<dispatcher>:

1)forward:拦截转发过来的

2)include:拦截包含的(动态包含会被拦截)

栗:hello.jsp页面包含test.jsp页面

静态包含<%@include file="test.jsp" %>;动态包含<jsp:include page="test.jsp"/>

3)request:(默认)拦截直接请求的

4)error:发生错误(全局配置的一个错误页面会被拦截,例如,发生错误404,500转到hello.jsp)

  <error-page>
        <error-code>404</error-code>
        <location>/hello.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/hello.jsp</location>
    </error-page>

 

 <filter>
        <filter-name>HelloFilter</filter-name>
        <filter-class>com.example.filter.HelloFilter</filter-class>
        <init-param>
            <param-name>user</param-name>
            <param-value>root</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>HelloFilter</filter-name>
        <url-pattern>/hello.jsp</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

2.Filter类

当请求为/hello.jsp?pwd=123时通过过滤器,可以访问hello.jsp,否则,不能够范围/hello.jsp

chain.doFilter()方法,请求传递到下一层

public class HelloFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String user = filterConfig.getInitParameter("user");
        System.out.println(user);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String pwd = request.getParameter("pwd");
        if (pwd != null && "123".equals(pwd)){
            chain.doFilter(request, response);
        }else {
            response.getWriter().write("failed");
        }
    }

    @Override
    public void destroy() {

    }
}

二、@WebFilter注解配置

name等同于<filter-name>,urlPatterns等同于<url-pattern>,initParams 等同于<init-param>

@WebFilter(filterName = "HelloFilter",
        urlPatterns = "/hello.jsp",
        initParams = {
                @WebInitParam(name = "user", value = "root")
        })

2.Servlet类

@WebFilter(filterName = "HelloFilter",
        urlPatterns = "/hello.jsp",
        dispatcherTypes = {
            DispatcherType.FORWARD, DispatcherType.INCLUDE
        },
        initParams = {
                @WebInitParam(name = "user", value = "root"),
        })
public class HelloFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String user = filterConfig.getInitParameter("user");
        System.out.println(user);
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String pwd = request.getParameter("pwd");
        if (pwd != null && "123".equals(pwd)){
            chain.doFilter(request, response);
        }else {
            response.getWriter().write("failed");
        }
    }

    @Override
    public void destroy() {

    }
}

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值