如何在一个url-pattern中使用俩个过滤器

如何在一个url-pattern中使用俩个过滤器

当不同的过滤器之间的url-pattern范围相同时,第一个过滤器执行doFilter拦截后,第二个过滤器再次执行doFilter是就会报空指针异常。

在调用hello页面时需要CharaterEncodingFilter过滤器解决乱码问题,但是调用时会被上一个过滤器拦截,报出空指针异常。

其他过滤器若设置为/*同样会对当前页面拦截,或者无法显示内容,或者报出空指针。

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小蓝 2023-01-07 10:33:32

同学你好,多个过滤器是按照web.xml中配置url-pattern的顺序自上而下执行的,建议同学将字符过滤器配置在最上面。

另外,仅从描述,老师无法确定问题的原因,建议同学重构一下代码,放弃业务逻辑,仅能演示错误即可。

然后将代码提供到问答区,这样老师可以测试代码,更效率的解决同学的疑问。

祝学习愉快~

  • 提问者 慕粉1712150383 #1

    按照课程代码,我的项目在运行后有时500空指针,有时200但无内容显示

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
        <filter>
            <filter-name>firstFilter</filter-name>
            <filter-class>com.imooc.filter.FirstFilter</filter-class>
        </filter>
        <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>com.imooc.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
        </filter>
        <filter>
            <filter-name>urlPatternFilter</filter-name>
            <filter-class>com.imooc.filter.UrlPatternFilter</filter-class>
        </filter>
        <filter>
            <filter-name>filterA</filter-name>
            <filter-class>com.imooc.filter.chain.FilterA</filter-class>
        </filter>
        <filter>
            <filter-name>filterB</filter-name>
            <filter-class>com.imooc.filter.chain.FilterB</filter-class>
        </filter>
        <filter>
            <filter-name>filterC</filter-name>
            <filter-class>com.imooc.filter.chain.FilterC</filter-class>
        </filter>
    
        <filter>
            <filter-name>deviceAdapterFilter</filter-name>
            <filter-class>com.imooc.filter.DeviceAdapterFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>firstFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <filter-mapping>
            <filter-name>urlPatternFilter</filter-name>
            <url-pattern>/v1/*</url-pattern>
        </filter-mapping>
    
        <filter-mapping>
            <filter-name>filterA</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <filter-mapping>
            <filter-name>filterB</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <filter-mapping>
            <filter-name>filterC</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        <filter-mapping>
            <filter-name>deviceAdapterFilter</filter-name>
            <url-pattern>*.html</url-pattern>
        </filter-mapping>
        <listener>
            <listener-class>com.imooc.WebListener</listener-class>
        </listener>
    </web-app>
    package com.imooc.filter;
    
    import javax.servlet.*;
    import java.io.IOException;
    
    public class FirstFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("FirstFilter已被初始化");
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            System.out.println("firstFilter过滤器已生效");
            //将请求响应向后传递,被后续的filter或servlet进行处理
            chain.doFilter(request,response);
        }
    
        @Override
        public void destroy() {
            System.out.println("FirstFilter已被销毁");
        }
    }
    package com.imooc.filter;
    
    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class CharacterEncodingFilter implements Filter {
        private String encoding;
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            String encoding = filterConfig.getInitParameter("encoding");
            System.out.println("Encoding:" + encoding);
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest req = (HttpServletRequest) request;
            HttpServletResponse resp = (HttpServletResponse) response;
            req.setCharacterEncoding(encoding);
            resp.setContentType("text/html;charset=" + encoding);;
            chain.doFilter(req,resp);
        }
    
        @Override
        public void destroy() {
    
        }
    }
    package com.imooc.servlet;
    
    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("/v1/hello")
    public class HelloServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().println("<h1>你好 世界</h1>");
        }
    }
    HTTP状态 500 - 内部服务器错误
    类型 异常报告
    描述 服务器遇到一个意外的情况,阻止它完成请求。
    例外情况
    java.lang.NullPointerException
    	org.apache.tomcat.util.buf.B2CConverter.getCharset(B2CConverter.java:60)
    	org.apache.catalina.connector.Request.setCharacterEncoding(Request.java:1705)
    	org.apache.catalina.connector.RequestFacade.setCharacterEncoding(RequestFacade.java:329)
    	com.imooc.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:20)
    	com.imooc.filter.FirstFilter.doFilter(FirstFilter.java:16)
    ):注意 主要问题的全部 stack 信息可以在 server logs 里查看
    Apache Tomcat/8.5.66


    2023-01-07 10:45:16
  • 提问者 慕粉1712150383 #2

    如果注释掉FirstFilter中的doFilter,访问页面就是空白页

    https://img1.sycdn.imooc.com//climg/63b8ddcb092e43fb03150452.jpg

    2023-01-07 10:49:58
  • 提问者 慕粉1712150383 #3

    只有URL范围配置全部不同才可以正常显示,有一个/*就无法显示内容或者空指针

    2023-01-07 10:52:38
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师