package servlet; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebFilter(filterName = "FilterCoding",urlPatterns = "/*") public class FilterCoding implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; // 解决post方式提交中文乱码问题, // 在tomcat8及以上版本,get方式提交不存在中文乱码问题 if ("post".equalsIgnoreCase(request.getMethod())){ request.setCharacterEncoding("utf-8"); } // 解决响应中文乱码问题 response.setContentType("text/html;charset=utf-8"); // 放行请求与响应 chain.doFilter(request, response); } public void init(FilterConfig config) throws ServletException { } }
原文地址:https://www.cnblogs.com/heqiuyong/p/9768577.html
时间: 2024-11-04 04:29:21