Filter四种拦截方式

        我们来做个测试,写一个过滤器,指定过滤器的资源为b.jsp,然后我们在浏览器中直接访问b.jsp,你会发现过滤器执行了。

        但是,当我们在a.jsp中request.getRequestDispathcer("/b.jsp").forward(request, response)时,就不会再执行过滤器了,也就是说,默认情况下,只能直接访问目标资源才会执行过滤器,而forward执行目标资源,不会执行过滤器。

1.第一种默认dispatcher标签配置为REQUEST

2.forward服务器端跳转默认是不经过拦截器的是需要配置,如下:

 protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
   <filter-mapping>
        <filter-name>helloFilter</filter-name>

        <url-pattern>/index.jsp</url-pattern>
           <!--拦截    -->
        <dispatcher>FORWARD</dispatcher>

    </filter-mapping>

3.INCLUDE前端跳转

<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
<%--  包含--%>
    <jsp:include page="/index.jsp"></jsp:include>
  </body>
</html>
<filter-mapping>
        <filter-name>helloFilter</filter-name>

        <url-pattern>/index.jsp</url-pattern>

        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>

4.ERROR跳转

注意:1除0是错误的浏览器会500error,所以会跳转到配置的error的界面当中,这种方式跳转也是不经过filter的,所以需要配置ERROR.

<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <!--1除0会报500-->
    <%1/0%>
  </body>
</html>
<filter-mapping>
        <filter-name>helloFilter</filter-name>

        <url-pattern>/error.jsp</url-pattern>

        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

附带:

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值