jsp代码:
import javax.servlet.*; import javax.servlet.http.*; public class CharsetFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { try { HttpServletRequest httpRequest = (HttpServletRequest)request; String method = httpRequest.getMethod().toLowerCase(); if(method.equals("post")) { //如果是post,即表单方法,直接设置charset即可 request.setCharacterEncoding("UTF-8"); } else if(method.equals("get")) { //如果是get方法 request.setCharacterEncoding("UTF-8"); request = new HttpServletRequestWrapper((HttpServletRequest)request) { public String getParameter(String str) { try { return new String(super.getParameter(str).getBytes("iso-8859-1"),"GBK"); } catch(Exception e) { return null; } } }; } chain.doFilter(request, response); } catch(Exception e){} } public void init(FilterConfig filterConfig) { } }
过滤器配置:
<filter> <filter-name>CharFilter</filter-name> <filter-class>CharsetFilter</filter-class> </filter> <filter-mapping> <filter-name>CharFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping>
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索jsp
, post
, 中文乱码
request
filter 过滤器 乱码、post乱码过滤器、filter 过滤器、java filter过滤器、锥形过滤器kyfilter,以便于您获取更多的相关知识。
时间: 2024-09-23 05:20:56