1.字符集介绍
(1)ASCLL码:最早的字符集,美国信息交换标准码。它使用一个字节中的7位来表示一个字符,最多可以表示128个字符;
(2)LATIN1:英语国家和西欧各国的编码标准;
(3)gb2312:我国针对汉字最早提出的一套编码标准,总共可以表示6556个字符;
(4)GBK:gb2312的扩充;
(5)UNICODE:统一了全球的字符编码,支持超过650中语言的国际字符集;
(6)UTF-8:是UNICODE的其中一种使用方式。UTF就是Unicode Translation Format,即把Unicode转换为某种格式。
2.产生乱码的原因
(1)操作系统环境:windows已经能够很好地支持GBK了,如果是linux或Unix则要检查其字符集环境的设置;
(2)数据库环境:库的字符集、表的字符集、字段的字符集;
(3)编程环境:在java中使用response.setContentType()方法可以进行设置;
(4)中间件环境(Tomcat、Weblogic、JBoss):tomcat默认编码是iso-8859-1,可以在server.xml中进行修改;
(5)JSP页面编译时:在头部设置contentType属性值来设置所用的字符集。
3.解决办法
(1)在tomcat中修改server.xml;
<Connector port="8080" maxThreads="150" minSpare="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding='GBK' />
(2)修改java编程环境的字符集;
response.setContentType("text/html;charset=GBK");
(3)mysql数据库所采用的字符集;
创建数据库时加上default charset=GBK
(4)JSP页面编辑时。
连接数据库时:
con=DriverManager.getconnection("jdbc:mysql://localhost:3306/sample_db?user=&password=&useUnicode=true&characterEncoding=GBK");
4.小结
(1)如果怕出现乱码,尽量在JSP页面中加上如下代码:
<%@page contentType="text/html;charaset=GBK"%>
<META http-equiv=Content-Type content="text/html;charset=GBK">
(2)最为糟糕的是进行了以上的设置还不能解决乱码问题,就需要为Tomcat服务器添加一个设置字符集的Filter类。
package filters;
import java.io.IOException;
import javax.servlet.*;
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
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 || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if(encoding != null)
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.ignure = true;
else
this.ignore = false;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
然后再web.xml中进行相应的配置:
<filter>
<filter-name>Set Character Encoding</filter-class>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>