首先保证所有文件编码一致
如果还有就写个过滤器吧,一劳永逸
三、字符编码的过滤器
import javax.servlet.*;
import java.io.IOException;
/**
* 用于设置 HTTP 请求字符编码的过滤器,通过过滤器参数encoding指明使用何种字符编码,用于处理Html Form请求参数的中文问题
*/
public class CharacterEncodingFilter
implements Filter
{
protected FilterConfig filterConfig = null;
protected String encoding = "";
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException
{
if(encoding != null)
servletRequest.setCharacterEncoding(encoding);
filterChain.doFilter(servletRequest, servletResponse);
}
public void destroy()
{
filterConfig = null;
encoding = null;
}
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
}
}
具体介绍和使用方法教程看这里
http://blog.chinaunix.net/u1/55983/showart_713077.html
把接收到的中文进行如下转换操作即可。
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK"); //将字符串转换为GBK编码
doget
response.setContentType(text/html;charset=gb2312);
dopost 设置
strvalue = new String(strvalue.getBytes("ISO8859_1"), "GBK");
request,response都需要进行设置