控制台会打印两次

来源:2-8 过滤链

mixiaofan

2019-11-27 14:44:09

http://img.mukewang.com/climg/5dde1ad1091be24a01770355.jpg

启动Tomcat时,控制台会打印两次ABC ABC

第三次是http://localhost:8080/filter_chain/hello时的反馈

<?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_4_0.xsd"
         version="4.0">
    <filter>
        <filter-name>FilterA</filter-name>
        <filter-class>com.imooc.filter.FilterA</filter-class>
    </filter>
    <filter>
        <filter-name>FilterB</filter-name>
        <filter-class>com.imooc.filter.FilterB</filter-class>
    </filter>
    <filter>
        <filter-name>FilterC</filter-name>
        <filter-class>com.imooc.filter.FilterC</filter-class>
    </filter>
    <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>
</web-app>
package com.imooc.filter;

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 javax.xml.bind.SchemaOutputResolver;
import java.io.IOException;

@WebServlet(urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().println("Hello World!!!");
        System.out.println("Hello World");
    }
}
package com.imooc.filter;

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

@WebFilter(filterName = "FilterC")
public class FilterC implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("I`m Filter C");
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
package com.imooc.filter;

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

@WebFilter(filterName = "FilterB")
public class FilterB implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("I`m Filter B");
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}
package com.imooc.filter;

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

@WebFilter(filterName = "FilterA")
public class FilterA implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        System.out.println("I`m Filter A");
        chain.doFilter(req, resp);
    }

    public void init(FilterConfig config) throws ServletException {

    }

}


写回答

2回答

hexuhao

2020-06-26

这是因为你同时使用了web.xml和注解两种方式进行配置,把其中一种注释掉就可以了

0

好帮手慕酷酷

2019-11-27

同学你好,老师这边测试同学的代码编写的没有问题,启动Tomcat时,控制台并没有打印两次ABC的情况哦!如:

http://img.mukewang.com/climg/5dde40a0097395db04030272.jpg

建议同学清除一下Tomcat缓存和idea缓存,重新运行一下项目哦!如:

http://img.mukewang.com/climg/5dde41d1093d92e502670447.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0

0 学习 · 9666 问题

查看课程