JSP+MySQL+Tomcat+Apache亂碼解決一例
Saturday, 17. June 2006, 18:34:17
轉自:http://www.matrix.org.cn/thread.shtml?topicId=25997&forumId=27
本文以最常見的JSP+MySQL+Tomcat+Apache亂碼解決爲例,望能爲你的環境配置起到拋磚引玉之效!
亂碼問題已歷來已久,在開源環境下,亂碼問題更是令程序員措手不及。本人在Unix(Freebsd)下的一次亂碼經歷可謂經典,故撰以此文以滋效尤!
我將本次所遇亂碼歸爲三類:
1.頁面字符亂碼
2.記錄顯示亂碼
3.request傳遞亂碼
以下將對上述三類亂碼進行解析:
一.頁面字符亂碼:
1.大小寫不一致:
org.apache.jasper.JasperException: /top.jsp(1,1) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html;charset=gb2312, new: text/html;charset=GB2312)
2.間隔不一致:
org.apache.jasper.JasperException: /top.jsp(1,1) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html; charset=GB2312, new: text/html;charset=GB2312)
*解決方案:
首先,在Apache中增加AddDefaultCharset GB2312或AddDefaultCharset GBK
其次,統一使用頁面編碼定義,如:<%@page contentType="text/html;charset=GB2312"%>
*注:GB2312爲GBK之子集。
二.記錄顯示亂碼:
1.MySQL默人語言爲latin1_swedish_ci,即拉丁語,所以取出的中文全是亂碼。
*解決方案:
1.將charset設爲8859_1即:<%@page contentType="text/html;charset=8859_1"%>
這個方法只能暫時緩解字符顯示問題,並權益之計。因爲8859_1爲字節型字庫,並非字型字庫,故在非全角狀態下,將出現半字亂碼,表現爲“?”。
2.在數據庫連接語句中加上?useUnicode=true;characterEncoding=GBK,如:
jdbc:mysql://localhost/dbname?useUnicode=true;characterEncoding=GBK
*注:一般教科書上都會加上localhost:3306,因爲默認端口爲3306,故可捨去!同時,請使用連接池的朋友注意,在註冊xml文件時,是不可以單獨出現“;”的,所以必須使用“&”,即:jdbc:mysql://localhost/dbname?useUnicode=true&characterEncoding=GBK。
否則提示出錯:
Parse Fatal Error at line 213 column 91: The reference to entity "characterEncoding" must end with the ';' delimiter.
org.xml.sax.SAXParseException: The reference to entity "characterEncoding" must
end with the ';' delimiter.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Un
known Source)
也曾有人提意:在MySQL的my.ini文件中加入default-character-set=gbk,本人不贊同此法,因爲這樣破壞了原有的環境,除非這是MySQL的第一個站點。
三.request傳遞亂碼:
1.也許,此時你已經可以正常使用系統了,那麼恭喜~亂碼問題已經離開你了!但是,大夥通常都沒那麼走運,亂碼問題依舊存在。也許,這時你向數據庫添加了一條記錄以測試系統,可是此時顯示出的還是亂碼。那麼可以肯定是Request參數傳遞出錯!那麼先寫個測試語句:<%= request.getParameter(“Para”) %>,OK,果然是亂。那麼,現在有兩種解決方法。
*解決方案:
1.加上這條語句:request.setCharacterEncoding("gbk");
在一/兩頁上可行,但此法也非權益之計。
2.註冊SetCharacterEncodingFilter類:
首先,編寫SetCharacterEncodingFilter.java文件,代碼如下:
package cn.com.jsp;
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 String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
此文件爲request過濾類,在全局編譯前需進行註冊。
註冊文件爲:<%wwwroot%>/WEB-INF/web.xml。
在此文件中加入如下代碼即可:
<web-app>
<display-name>wwwroot</display-name>
<description>MySQL Test App</description>
<filter>
<filter-name>setCharacterEncodingFilter</filter-name>
<display-name>setCharacterEncodingFilter</display-name>
<description>setCharacterEncodingFilter</description>
<filter-class>cn.com.jsp.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
……
</web-app>
OK,現在可以編譯你的SetCharacterEncodingFilter.java文件啦!
至此,亂碼將與你格格不入!
本文以最常見的JSP+MySQL+Tomcat+Apache亂碼解決爲例,望能爲你的環境配置起到拋磚引玉之效!
亂碼問題已歷來已久,在開源環境下,亂碼問題更是令程序員措手不及。本人在Unix(Freebsd)下的一次亂碼經歷可謂經典,故撰以此文以滋效尤!
我將本次所遇亂碼歸爲三類:
1.頁面字符亂碼
2.記錄顯示亂碼
3.request傳遞亂碼
以下將對上述三類亂碼進行解析:
一.頁面字符亂碼:
1.大小寫不一致:
org.apache.jasper.JasperException: /top.jsp(1,1) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html;charset=gb2312, new: text/html;charset=GB2312)
2.間隔不一致:
org.apache.jasper.JasperException: /top.jsp(1,1) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html; charset=GB2312, new: text/html;charset=GB2312)
*解決方案:
首先,在Apache中增加AddDefaultCharset GB2312或AddDefaultCharset GBK
其次,統一使用頁面編碼定義,如:<%@page contentType="text/html;charset=GB2312"%>
*注:GB2312爲GBK之子集。
二.記錄顯示亂碼:
1.MySQL默人語言爲latin1_swedish_ci,即拉丁語,所以取出的中文全是亂碼。
*解決方案:
1.將charset設爲8859_1即:<%@page contentType="text/html;charset=8859_1"%>
這個方法只能暫時緩解字符顯示問題,並權益之計。因爲8859_1爲字節型字庫,並非字型字庫,故在非全角狀態下,將出現半字亂碼,表現爲“?”。
2.在數據庫連接語句中加上?useUnicode=true;characterEncoding=GBK,如:
jdbc:mysql://localhost/dbname?useUnicode=true;characterEncoding=GBK
*注:一般教科書上都會加上localhost:3306,因爲默認端口爲3306,故可捨去!同時,請使用連接池的朋友注意,在註冊xml文件時,是不可以單獨出現“;”的,所以必須使用“&”,即:jdbc:mysql://localhost/dbname?useUnicode=true&characterEncoding=GBK。
否則提示出錯:
Parse Fatal Error at line 213 column 91: The reference to entity "characterEncoding" must end with the ';' delimiter.
org.xml.sax.SAXParseException: The reference to entity "characterEncoding" must
end with the ';' delimiter.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Un
known Source)
也曾有人提意:在MySQL的my.ini文件中加入default-character-set=gbk,本人不贊同此法,因爲這樣破壞了原有的環境,除非這是MySQL的第一個站點。
三.request傳遞亂碼:
1.也許,此時你已經可以正常使用系統了,那麼恭喜~亂碼問題已經離開你了!但是,大夥通常都沒那麼走運,亂碼問題依舊存在。也許,這時你向數據庫添加了一條記錄以測試系統,可是此時顯示出的還是亂碼。那麼可以肯定是Request參數傳遞出錯!那麼先寫個測試語句:<%= request.getParameter(“Para”) %>,OK,果然是亂。那麼,現在有兩種解決方法。
*解決方案:
1.加上這條語句:request.setCharacterEncoding("gbk");
在一/兩頁上可行,但此法也非權益之計。
2.註冊SetCharacterEncodingFilter類:
首先,編寫SetCharacterEncodingFilter.java文件,代碼如下:
package cn.com.jsp;
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 String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
此文件爲request過濾類,在全局編譯前需進行註冊。
註冊文件爲:<%wwwroot%>/WEB-INF/web.xml。
在此文件中加入如下代碼即可:
<web-app>
<display-name>wwwroot</display-name>
<description>MySQL Test App</description>
<filter>
<filter-name>setCharacterEncodingFilter</filter-name>
<display-name>setCharacterEncodingFilter</display-name>
<description>setCharacterEncodingFilter</description>
<filter-class>cn.com.jsp.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
……
</web-app>
OK,現在可以編譯你的SetCharacterEncodingFilter.java文件啦!
至此,亂碼將與你格格不入!
Comments
靈狼兄。
您的文章很管用。
用您的介紹的以下方法 1。 亂碼問題解決了。既然您建議這個方法並非權益之計,故嘗試方法2,結果總是顯示亂碼。
您能多指導一點兒麼?
1.將charset設爲8859_1即:<%@page contentType="text/html;charset=8859_1"%>
這個方法只能暫時緩解字符顯示問題,並權益之計。因爲8859_1爲字節型字庫,並非字型字庫,故在非全角狀態下,將出現半字亂碼,表現爲“?”。
2.在數據庫連接語句中加上?useUnicode=true;characterEncoding=GBK,如:
jdbc:mysql://localhost/dbname?useUnicode=true;characterEncoding=GBK
多謝。
import java.io.UnsupportedEncodingException;
{
}
{
if(s==null){s="";}
try
{
byte[] b=s.getBytes("gb2312");
s=new String(b);
}
catch(Exception e)
{
s="";
return s;
}
{
try
{
return new String(str.getBytes("iso-8859-1"), "gbk");
}
catch (UnsupportedEncodingException ex)
{
return null;
}
}
{
try
{
return new String(str.getBytes("gb2312"), "iso-8859-1");
}
catch (UnsupportedEncodingException ex)
{
return null;
}
}
{
try
{
return new String(str.getBytes("gbk"), "iso-8859-1");
}
catch (UnsupportedEncodingException ex)
{
return null;
}
}
public static String getISOToUTF8(String str)
{
try
{
return new String(str.getBytes("iso-8859-1"), "utf-8");
}
catch (UnsupportedEncodingException ex)
{
return null;
}
}
public static String getEncodingString(String str, String encoding)
{
String[] encodings = encoding.toLowerCase().replaceAll("//s", "").split("//|");
if (encodings.length == 2)
{
String encodingStr = "";
try
{
encodingStr = new String(str.getBytes(encodings[0]), encodings[1]);
}
catch (UnsupportedEncodingException ex)
{
}
finally
{
return encodingStr;
}
}
return str;
}
}
String user_name=CharsetT.getISOToGBK(request.getParameter("user_name"));