Mysql與JSP網頁中文亂碼問題的解決方案
2024-09-05 00:19:06
供稿:網友
自從以前學習jsp開始,中文亂碼問題就一直不斷,苦不堪言。這次在項目開始之前,我們要解決的第一個問題就是把mysql的中文亂碼問題搞定。經過多天的努力,終于成功的解決了中文亂碼問題,特寫在這里,以備后用。
軟件及環境:windows xp(2000), j2sdk1.4.2, tomcat 5.0.25, mysql 4.1, ems mysql manager 2(方便建表,版本2.8.5.1),驅動為mysql-connector-java-3.1.4-beta-bin.jar。
目標:在該環境下,實現中文的正常顯示,讀取與插入數據庫。
注:我只在此環境下測試通過,別的系統及不同版本未測試
要點:統一字符集(jsp頁面編碼,mysql建庫時字符集選擇,連接數據庫url,request設定等)
下面我以gbk為例講解。如果要使用utf-8,只要在相應的gbk處換成utf-8即可
--------------------------- 步驟1 以gbk字符集建庫建表 -------------------------------------
我使用ems來建mysql的數據庫及表,因為它是圖形界面,方便操作(就像sql server 2000中的企業管理器一樣)。
建庫時,從ems菜單中選create database...新建一個數據庫,characterset選gbk_bin(另一個gbk_chinese_ci不知道與這個有什么區別,我找資料也沒有找到。如果你知道,請告訴我,我補充在這里)。不要把工具欄上有一個加號和數據庫模樣的圖標當成新建數據庫了,那個新注冊一個已經存在的數據庫。
后面建表時,也要選擇同樣的字符集。
建好后,此時不要用ems向里面插入數據,否則你看到的中文依然是亂碼。
--------------------------- 步驟2 連接數據庫的url后加些參數 -------------------------------
假設我新建的數據庫是testdb,那么我連接數據庫的url應該為:
jdbc:mysql://localhost:3306/testdb?useunicode=true&characterencoding=gbk
此時要注意:如果我是把這個url寫在java代碼中,就直接這樣寫。但如果是在xml配置文件中(如struts-config.xml,web.xml等),要把其中的&改為&才行,否則會出錯。也就是:
jdbc:mysql://localhost:3306/testdb?useunicode=true&characterencoding=gbk
--------------------------- 步驟3 每個jsp頁面都要聲明該中文字符集 ----------------------------
在每個jsp頁面的最上面都加上一句
<%@ page language="java" contenttype="text/html;charset=gbk" %>
這樣才能保證jsp頁面中的中文顯示正常
--------------------------- 步驟4 加一個傳遞參數時設定request字符集的filter類 -----------------------
因為網絡中字符在傳遞的時候,都是統一以iso-8859-1的編碼傳遞,所以我們必須對request重新設定字符集,才能正常顯示中文。如果采用filter類來實現,我們不用在每次取中文參數時都要重新設定。
filter類的內容:
/*
* ====================================================================
*
* javawebstudio 開源項目
*
* struts_db v0.1
*
* ====================================================================
*/
package com.strutslogin.util;
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 {
// ----------------------------------------------------- instance variables
/**
* the default character encoding to set for requests that pass through
* this filter.
*/
protected string encoding = null;
/**
* the filter configuration object we are associated with. if this value
* is null, this filter instance is not currently configured.
*/
protected filterconfig filterconfig = null;
/**
* should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;
// --------------------------------------------------------- public methods
/**
* take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterconfig = null;
}
/**
* select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request the servlet request we are processing
* @param result the servlet response we are creating
* @param chain the filter chain we are processing
*
* @exception ioexception if an input/output error occurs
* @exception servletexception if a servlet error occurs
*/
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);
}
/**
* place this filter into service.
*
* @param filterconfig the filter configuration object
*/
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
/**
* select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. if no character encoding should be set, return
* <code>null</code>.
* <p>
* the default implementation unconditionally returns the value configured
* by the <strong>encoding</strong> initialization parameter for this
* filter.
*
* @param request the servlet request we are processing
*/
protected string selectencoding(servletrequest request) {
return (this.encoding);
}
}//eoc
該代碼來自于www.javawebstudio.com,特此感謝!
然后我們在web.xml中加一些配置,就可以了,配置如下:
<filter>
<filter-name>set character encoding</filter-name>
<filter-class>javawebstudio.struts_db.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>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>set character encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
放在web.xml的合適位置。一般在最后,<jsp-config>標簽之前(如果有的話)
經過以上步驟,jsp和mysql的中文顯示及插入就都正常了。在struts中也正常。
但是,此時如果你用ems或mysql的命令行控制臺來看表中的數據,卻發現它們都是????。這是怎么回事呢?
不用擔心,只要我們運行下面的這幾行命令,就能看到正常的中文了!
set character_set_client = gbk;
set character_set_connection = gbk;
set character_set_database = gbk;
set character_set_results = gbk;
set character_set_server = gbk;
set collation_connection = gbk_bin;
set collation_database = gbk_bin;
set collation_server = gbk_bin;
如果你用的是mysql的命令行,則直接輸入就好。
如果是ems,則在工具欄中有一個show sql editor按鈕,點一下,把上面的命令輸入,再按一個"execute"的按鈕,就行了!
而且在這種情況下,你可以甚至可以用中文名來建數據庫,表名和字段名!!!!
----------------------------------------------------------------------------------------------------
但是有一點要特別注意!
像gbk,utf-8這樣的名字,在mysql與java中有不同的規定,寫的時候要格外注意,否則會出錯。
比如gbk,在java中要寫成gbk,但在mysql中要寫成gbk(連接數據庫的url)
比如utf-8,在java中要寫成utf-8,但在mysql中要寫成utf8
其它的字集符也有類似的區別