java 中文乱码终极解决方案(斩草除根式消灭中文乱码恶魔)
2008-03-22 21:14
417 查看
前段时间公司做feinar旅行网后台管理系统的开发,我和我的同事都是第一次真正参与这种大型的项目开发,在开发过程中,我们也曾几次遇到中文乱码的问题,很是困惑和苦恼,到网上去查,那些所谓的解决方案都不凑效,几经周折后,终于彻底解决了这个问题。通过查资料和多方面的综合考虑,最终得出了自己的的解决方案:
首先,确保你的数据库是支持中文的。把数据库的编码方式设置能支持中文的,如GB2132,utf-8等,而且经过测试后确实能写入中文且正常显示。这样就就保证了数据库确实是支持中文的,且可以正常输入和保存中文。如果数据库本身都不支持中文字符,那不管你怎么写代码都是无济于事的,肯定会出现中文乱码。
其次,从输入源杜绝中文乱码出现的可能。只有从数据输入源杜绝了中文乱码出现的可能,才能保证输入到数据库中的是正确的字符,加上我们第一步的保障,那么就保证了从数据库中读取的数据的正确性,从而也就保证了程序从数据库中读取的数据不会出现中文乱码成为可能。解决方法:
解决方法一:从页面输入杜绝乱码出现的可能
1、在jsp输入页面中加入:
<%@pagecontentType="text/html;charset=数据库字符集"% >
2、在html标签的<head>的第一行加入
<meta http-equiv="Content-Type" content="text/html; charset=数据库字符集" />
解决方法二:使用javax.servlet.Filter
1、编写web.xml配置。在
这样就可以保证页面发送的数据的编码与数据库编码一致,这样从数据库中读取的数据就是你想要的正确字符了,你可放心的读取了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-name>encoding</filter-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-class>com.morecans.test.EncodingFilter</filter-class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<init-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-name>charset</param-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-value>utf-8</param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</init-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</filter>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-name>encoding</filter-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<url-pattern>/*</url-pattern>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</filter-mapping>
2、编写Filterservlet:com.morecans.test.EncodingFilter
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.morecans.test;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.Filter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.FilterChain;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.FilterConfig;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class EncodingFilter implements Filter ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private FilterConfig config;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void destroy() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("*** destroy EncodingFilter");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doFilter(ServletRequest servletrequest,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ServletResponse servletresponse, FilterChain filterchain)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
throws IOException, ServletException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("*** doFilter EncodingFilter");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String charset = this.config.getInitParameter("charset");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
servletrequest.setCharacterEncoding(charset); //设
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
servletresponse.setCharacterEncoding(charset);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
filterchain.doFilter(servletrequest, servletresponse);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void init(FilterConfig filterconfig) throws ServletException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.config = filterconfig;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("*** init EncodingFilter");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
最后,在输出处避免乱码的出现。只有把输出控制好了,那么我们前面的工作就没有白费,前面的辛苦才会得到承认,才能得到收获。这一步,很简单,甚至都可以不要,因为我们前面的两步基本把这一步的工作也做了,我是说如果编码规范的话。然而,千里之堤,溃于蚁穴!行百里者半九十!希望前面的准备工作不会是徒劳,这是我们不原意看到的,所以在治理我还是建议不要因为这么一小步而错过了收获的季节。解决方法:
现在,你只需要在读取数据之前加上
request.setCharacterEncoding(数据库字符集);
response.setCharacterEncoding(数据库字符集);
而在jsp数据显示页面也像输出页面那样加上
<%@pagecontentType="text/html;charset=数据库字符集"% >
<meta http-equiv="Content-Type" content="text/html; charset=数据库字符集" />
整个过程,从源头到源尾,彻底的监视着数据的一举一动,时刻修正着数据的模样和流向,从而彻底消灭中文乱码这个恶魔的出现。
摘录来源:http://blog.csdn.net/morecans/archive/2007/07/15/1691860.aspx —— Morecans的专栏
首先,确保你的数据库是支持中文的。把数据库的编码方式设置能支持中文的,如GB2132,utf-8等,而且经过测试后确实能写入中文且正常显示。这样就就保证了数据库确实是支持中文的,且可以正常输入和保存中文。如果数据库本身都不支持中文字符,那不管你怎么写代码都是无济于事的,肯定会出现中文乱码。
其次,从输入源杜绝中文乱码出现的可能。只有从数据输入源杜绝了中文乱码出现的可能,才能保证输入到数据库中的是正确的字符,加上我们第一步的保障,那么就保证了从数据库中读取的数据的正确性,从而也就保证了程序从数据库中读取的数据不会出现中文乱码成为可能。解决方法:
解决方法一:从页面输入杜绝乱码出现的可能
1、在jsp输入页面中加入:
<%@pagecontentType="text/html;charset=数据库字符集"% >
2、在html标签的<head>的第一行加入
<meta http-equiv="Content-Type" content="text/html; charset=数据库字符集" />
解决方法二:使用javax.servlet.Filter
1、编写web.xml配置。在
这样就可以保证页面发送的数据的编码与数据库编码一致,这样从数据库中读取的数据就是你想要的正确字符了,你可放心的读取了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-name>encoding</filter-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-class>com.morecans.test.EncodingFilter</filter-class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<init-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-name>charset</param-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<param-value>utf-8</param-value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</init-param>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</filter>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<filter-name>encoding</filter-name>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<url-pattern>/*</url-pattern>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</filter-mapping>
2、编写Filterservlet:com.morecans.test.EncodingFilter
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package com.morecans.test;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import java.io.IOException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.Filter;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.FilterChain;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.FilterConfig;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletException;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletRequest;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import javax.servlet.ServletResponse;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class EncodingFilter implements Filter ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private FilterConfig config;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void destroy() ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("*** destroy EncodingFilter");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void doFilter(ServletRequest servletrequest,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ServletResponse servletresponse, FilterChain filterchain)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
throws IOException, ServletException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("*** doFilter EncodingFilter");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String charset = this.config.getInitParameter("charset");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
servletrequest.setCharacterEncoding(charset); //设
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
servletresponse.setCharacterEncoding(charset);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
filterchain.doFilter(servletrequest, servletresponse);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public void init(FilterConfig filterconfig) throws ServletException ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.config = filterconfig;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("*** init EncodingFilter");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
最后,在输出处避免乱码的出现。只有把输出控制好了,那么我们前面的工作就没有白费,前面的辛苦才会得到承认,才能得到收获。这一步,很简单,甚至都可以不要,因为我们前面的两步基本把这一步的工作也做了,我是说如果编码规范的话。然而,千里之堤,溃于蚁穴!行百里者半九十!希望前面的准备工作不会是徒劳,这是我们不原意看到的,所以在治理我还是建议不要因为这么一小步而错过了收获的季节。解决方法:
现在,你只需要在读取数据之前加上
request.setCharacterEncoding(数据库字符集);
response.setCharacterEncoding(数据库字符集);
而在jsp数据显示页面也像输出页面那样加上
<%@pagecontentType="text/html;charset=数据库字符集"% >
<meta http-equiv="Content-Type" content="text/html; charset=数据库字符集" />
整个过程,从源头到源尾,彻底的监视着数据的一举一动,时刻修正着数据的模样和流向,从而彻底消灭中文乱码这个恶魔的出现。
摘录来源:http://blog.csdn.net/morecans/archive/2007/07/15/1691860.aspx —— Morecans的专栏
相关文章推荐
- java 中文乱码终极解决方案(斩草除根式消灭中文乱码恶魔)
- java中文乱码终极解决方案
- 表单提交后数据中文乱码的终极解决方案
- JSP中文乱码问题终极解决方案
- 转载:Java 关于中文乱码问题的解决方案与经验
- JSP中文乱码问题终极解决方案
- java中路径中参数值是中文,打印到页面是乱码的解决方案
- VIM中文乱码原理详解及终极解决方案[经典]
- 关于地址栏传递中文参数的乱码的终极解决方案
- Java中常遇的中文乱码问题解决方案
- JSP中文乱码问题终极解决方案
- RHEL5.1安装VM TOOL及中文乱码终极解决方案 推荐
- 关于java.util.Properties读取中文出现乱码的解决方案
- 关于java.util.Properties读取中文乱码的正确解决方案(不要再用native2ascii.exe了)
- 学习备忘:Java 中文乱码解决方案汇总
- JQuery中使用Ajax传中文参数乱码解决方案(javaweb开发)
- Java 关于中文乱码问题的解决方案与原理
- 关于Myeclipse出现 java文件中文乱码问题的解决方案.
- JSP中文乱码问题终极解决方案
- SharpMap应用开发——中文乱码问题终极解决方案