您的位置:首页 > 运维架构 > Tomcat

Tomcat5+Mysql中文乱码问题的解决方法

2007-05-24 09:19 986 查看
Tomcat 下最容易到的问题就是中文乱码问题了,下面就来谈谈我在Windows +Tomcat5下调试JSP程序时,所碰到的问题以及解决办法。
这次调试所遇到的乱码问题主要有三类:
一、页面字符乱码。
即整个页面出现汉字乱码(不管是用HTML格式写的,还是用print()方法输出的汉字,全部显示为乱码)。
二、Request 传递乱码。
在用post,get等方法,传递变量时出现乱码;需要进行cookies或者session调用时出现乱码。
三、数据库存取乱码。
在进行数据库存取时,或者存入数据库,或者读出数据时出再乱码。

下面就这三类乱码进行分析并谈谈我的解决方法:
一、页面字符乱码。

我们先写下这样的一个jsp文件:
//testPagErr.jsp
<%
out.println(“能显示中文吗?”);
%>
如果我们直接按照默认的配置启动Tomcat5,然后访问这一个页面的时,将输出的中文将显示为乱码。其原因很简单,这是由于在Tomcat5 默认的设置下,将按照ISO-8859-1进行编码。解决这个问题最直接的做法是,有每个有中文输出的jsp文件的开头加一句:
<%@ page contentType="text/html;charset=GBK" //(改成GB2312亦可,本文中均以GBK为例)%>
但是这样做存在很多问题,首先,这一句开指令在include的时候,不能够被子文件继承,因此,我们必须在子文件中重新加入这一句话,但如果大小写不话,就会出现类似于以下的错误:
org.apache.jasper.JasperException: /top.jsp(1,1) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html;charset=gbk, new: text/html;charset=GBK).
那么,这样子定义就有点恐怖了,假如有一个要作Head.jsp的头文件,我们试着在其中加入<%@ page contentType="text/html;charset=GBK" %>,要在众多的文件中被包含,而这些文件是由不同的程序只写的,有的习惯用“gbk”,有的用“GBK”,还有喜欢用“GB2312”,那怎么办啊?难到要一个文件一个文件地去改过来?就算你可以用一些编辑器的Replace in Files功能把所有的文件更正过来,但这样做必然会影响系统的可移植性。
同样,如果"text/html;charset=GBK"的间隔不一致的话,也会出错,而且,一旦程被修改成这样的模式,你的程序就不能在旧的jsp/servlet container上执行了,因为旧的规格是不允许在include文档中再次中现<%@ page ... %>这样的定义的。
因此上,如果采用上述方法的话,在一两个页面上也可行,但此法仅为权益之计。
在J2EE1.4中,关于jsp的部分里有一个<jsp-config> 的 XML Tag,这个区块是用来定义jsp的相关特殊属性的,它包含所采用的taglib和下面我们要用到的<jsp-property-group>,定义字符编码的方法就定义在<jsp-property-group>之中,找到你正在开发的webapps目录(一般在$TOMCAT_HOME/webapps/your_web_site/)下的WEB-INF,打开web.xml(如果没的话,就创建一个),作如下修改:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!— 这一段是xml head的定义,特别注意,最后的version="2.4",Tomcat5会去检测这个版本的信息,只有2.4的才会去处理jsp-config中的参数 -->
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4">
<!— ------------------------------------------------------------------------------------------ -->

<!— 这一部分就是我们新加入的,起作用的区块 -->
<jsp-config>
<jsp-property-group>
<description>
Special property group for JSP Configuration JSP example.
</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>GBK</page-encoding>
<scripting-invalid>flase</scripting-invalid>
<include-prelude></include-prelude>
<include-coda></include-coda>

<description>
Special property group for JSP Configuration JSP example.
</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.html</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>GBK</page-encoding>
<scripting-invalid>flase</scripting-invalid>
<include-prelude></include-prelude>
<include-coda></include-coda>
</jsp-property-group>
</jsp-config>
<!— ------------------------------------------------------------------------------------------ -->
</web-app>
另外,如果Tomcat5与Apache整和,最好把httpd.conf中的AddDefaultCharset的值设为GBK.
二、Request传递乱码。
按照Tomcat的默认设置,在通过表单提交的数据将出现乱码,其原因是中文字符的高位被丢失,如:
成龙的unicode是 6210 9f99 ,但是从request里面读出来的是: 10 99。
其原因是form表单和request方法中设置的编码格式不一致,在上面,我们已经将页面编码改成了GBK,因此上,我们只须将request的编码改为GBK即可。
比较简单的解决方法是,在要用到request方法的地方加上这条语句:request.setCharacterEncoding("GBK");
但这也将遇到与第一种方法同样,当几个人同时写程序时,或者,引用别人已写的程序的时候,也会存在移植不便的问题。
更好的方法是,注册SetCharacterEncodingFilter类:
在$TOMCAT_HOME/webapps/your_web_site/WEB-INF/classes目录下新建一个名为filters的文件夹,然后把$TOMCAT_HOME/webapps/jsp-examples/WEB-INF/classes/filters的SetCharacterEncodingFilter.class文件拷到新建的filters下面,最后在web.xml中加入如下的两个区块:
<!-- Example filter to set character encoding on each request -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>

<!-- Example filter mapping to apply the "Set Character Encoding" filter
to *all* requests processed by this web application -->

<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
如果找不到SetCharacterEncodingFilter.class,也可以自己编写SetCharacterEncodingFilter.java文件,代码如下:
/*
* filename: SetCharacterEncodingFilter.java
*/
package filters;

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;
import javax.servlet.UnavailableException;

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 {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}

// Pass control on to the next filter
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 Methods

protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}

}
用任意一个java编译器编译即可,编译的时候注意包含servlet-api.jar包,它位于$TOMCAT_HOME/common/lib/下面。
然后删除work中的内容,然后启动Tomcat,即可显示中文了。
三、数据库存取乱码
当然,在写数据库时,也要保正数据库的编码与其它一致:
我们可以在系统盘windows目录下的my.ini文件,在文件中插入一行default-character-set=GBK,但上面说了这么多,大家也应该明白些了吧,改动太多的默认设置不是我的风格,因此上,这一行还是不要加的好。
但不管怎么样,我们还是要创建一个基于中文编码的数据库,当然,用客户端登录的时候,某些客户用自动把字体编码转换成中文编码。在这里,我想说一下在DOS下创建中文编码数据库的方法:
在进入数据库的时候,用mysql --default-character-set=gbk -u root -p 这句话进入mysql,然后创建数据库,如:create database admin;这样创建起来的数据库就是基于中文编码的了。
用连接数据库的时候,读出的数据也可能是乱码,解决这个问题的方法非常简单,只要在你建立数据库连接的时候把URL设置成下面这个样子就可以了:URL= jdbc:mysql://localhost:3306/my_database?useUnicode=true&characterEncoding=GBK
好了,说了这么多,总结一句话结束吧,把各种地方的编码统一起来,那么,所在的乱码问题就都解决了!
【来源】
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: