日期:2014-05-18  浏览次数:20764 次

问个很菜的问题!急……
过滤器怎样处理中文?

------解决方案--------------------
自动.....

------解决方案--------------------
request.setCharacterEncoding("GBK")就可以了
------解决方案--------------------
第一步:写个fiter类SetCharacterEncodingFilter.java

Java code

package com.niexiong.fiter; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class SetCharacterEncodingFilter implements Filter { protected String encoding = "GB2312"; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void destroy() { this.encoding = null; this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException { if (!ignore) { String encoding = selectEncoding(request); request.setCharacterEncoding(encoding); } chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig.getInitParameter("ignore"); if (value == null) this.ignore = true; else if (value.equalsIgnoreCase("true")) this.ignore = true; else if (value.equalsIgnoreCase("yes")) this.ignore = true; else this.ignore = false; } protected String selectEncoding(ServletRequest request) { return (this.encoding); } }



第二步:web.xml中添加

XML code

<filter> <filter-name>SetCharacterEncoding</filter-name> <filter-class>com.niexiong.fiter.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> <init-param> <param-name>ignore</param-name> <param-value>false</param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>



第三步:jsp页面都添加

Java code

<%@ page contentType="text/html;charset=GBK" %>


------解决方案--------------------
<%@ page contentType= "text/html;charset=GBK " %> 

在每个页面添加
------解决方案--------------------
同意pc144818

------解决方案--------------------
插入数据库在转啊,GBK转成ISO8859
------解决方案--------------------
过滤器核心就是doFilter(),提供的参数是ServletRequest,ServletResponse.记得转换就可以了
------解决方案--------------------
3楼的方法好像不行