企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
FirstFilter.java ~~~ package zyw.filter; import javax.servlet.*; import java.io.IOException; public class FirstFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("doFilter被访问了"); filterChain.doFilter(servletRequest,servletResponse);//放行 } @Override public void destroy() { } } ~~~ HttpServlet.java ~~~ package zyw.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(name = "TestFilterServlet",urlPatterns = "/testfilter") public class TestFilterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.getWriter().write("testfilter被访问了"); } } ~~~ web.xml ~~~ <?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>first</filter-name> <filter-class>zyw.filter.FirstFilter</filter-class> </filter> <filter-mapping> <filter-name>first</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> ~~~ 在 FirstFilter.java中加入` filterChain.doFilter(servletRequest,servletResponse);//放行`时,才有下图结果 ![](https://box.kancloud.cn/1d92cae5c41c97758d5a2da460ab50e8_578x124.png)