filter过滤器五种拦截行为

该文章展示了如何在web.xml中配置过滤器(filter)以处理不同类型的请求,包括开启异步支持。同时,它也设置了全局错误页面,用于捕获JavaException和HTTP404错误,将用户重定向到定制的错误信息页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 web.xml

 <filter>
        <filter-name>filterDemo05</filter-name>
        <filter-class>com.itheima.filter.FilterDemo05</filter-class>
        <!--配置开启异步支持,当dipatcher配置ASYNC时,需要配置此行-->
        <async-supported>true</async-supported>
    </filter>
    <filter-mapping>
        <filter-name>filterDemo05</filter-name>
<!--        <url-pattern>/error.jsp</url-pattern>-->
        <url-pattern>/index.jsp</url-pattern>
        <!--过滤请求:默认值-->
        <dispatcher>REQUEST</dispatcher>
        <!--过滤全局错误页面,当由服务器调用全局页面时,过滤器工作-->
        <dispatcher>ERROR</dispatcher>
        <!--过滤请求转发:当请求转发时,过滤器工作-->
        <dispatcher>FORWARD</dispatcher>
        <!--过滤请求包含:当请求包含时,过滤器工作,它只能过滤动态包含,jsp的include指令是静态包含,过滤器不会-->
        <dispatcher>INCLUDE</dispatcher>
        <!--过滤异步类型:它要求我们在filter标签中配置开启异步支持-->
        <dispatcher>ASYNC</dispatcher>
    </filter-mapping>

    <!--配置全局错误页面-->
    <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/error.jsp</location>
    </error-page>
    <error-page>
        <error-code>404</error-code>
        <location>/error.jsp</location>
    </error-page>
<%--
  Created by IntelliJ IDEA.
  User: hqg
  Date: 2023/3/30
  Time: 6:34
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>自定义错误页面</title>
</head>
<body>
    不好意思出错了。。。。
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: hqg
  Date: 2023/3/30
  Time: 4:28
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  $END$
  </body>
</html>
package com.itheima.filter;

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

/*过滤器拦截行为*/
//@WebFilter("/*")
public class FilterDemo05 implements Filter {




    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("filterDemo05执行了。。。。。");
        servletResponse.setContentType("text/html;charset=UTF-8");
        //放行
        filterChain.doFilter(servletRequest,servletResponse);
    }


}
package com.itheima.serlvet;

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("/servletDemo03")
public class servletDemo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servletDemo03 执行了");
        /*int i=1/0;*/
        //请求转发
       /* req.getRequestDispatcher("index.jsp").forward(req,resp);*/

        //请求包含
        req.getRequestDispatcher("index.jsp").include(req,resp);

        //resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("servletDemo03 执行了");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值