企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
![](https://box.kancloud.cn/bde065a1b8153f615585e56b2e9734da_449x212.png) TestFilterServlet.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被访问了"); } } ~~~ CharsetFilter.java ~~~ package zyw.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; import java.io.IOException; @WebFilter(filterName = "CharsetFilter",urlPatterns = "/*",initParams = {@WebInitParam(name = "encoding",value = "text/html;charset=utf-8")}) public class CharsetFilter implements Filter { private String encoding; public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { if(encoding!=null){ resp.setContentType(encoding); } chain.doFilter(req, resp); } public void init(FilterConfig config) throws ServletException { encoding=config.getInitParameter("encoding"); } } ~~~ 当CharsetFilter.java中有`urlPatterns = "/*"`时,结果 ![](https://box.kancloud.cn/2e3c55cdc102770077bc98840596bed0_202x84.png) 路径不对时,结果 ![](https://box.kancloud.cn/282a4b0ac68a2d6a75a9521923509ec1_526x127.png)