.№愛の冰封
開始懂得逢場作戲,雖然有許多只是霧水情緣; 不過沒關系了,哪來那么多一生一世
BlogJava
首頁
新隨筆
聯系
聚合
管理
posts - 14, comments - 37, trackbacks - 0
j2ee開發中亂碼問題的解決方案匯總
先說我的問題吧, 然后再把各個高手的文章貼出來,
我今天使用hibernate的時候改變了數據的編碼, 不再使用
html encode ,
結果就出現了編碼的問題了, 由于hibernate的本質是jdbc,就去改hibernate的設置, 現在發現原來是jdbc的格式問題,
applicationContext.xml中的數據庫連接必須設置為<property>jdbc:mysql://localhost/dbname?useUnicode=true
&
characterEncoding=gb2312</property>不應該是 jdbc:mysql://localhost/dbname?useUnicode=true
&
characterEncoding=gb2312
這是非常關鍵的!!!
然后我的程序就正確的顯示中文了, 好了, 現在把高手的文章列表出來。
http://www.ziki.cn/blog/archives/1155307798.html
http://www.matrix.org.cn/thread.shtml?topicId=25997&forumId=27 這篇超贊!
http://www.zhuoda.org/lunzi/60912.html
下面把那個非常棒的文章轉來珍惜一下
本文以最常見的JSP+MySQL+Tomcat+Apache亂碼解決為例,望能為你的環境配置起到拋磚引玉之效!
亂碼問題已歷來已久,在開源環境下,亂碼問題更是令程序員措手不及。本人在Unix(Freebsd)下的一次亂碼經歷可謂經典,故撰以此文以滋效尤!
我將本次所遇亂碼歸為三類:
?。保撁孀址麃y碼
?。玻涗涳@示亂碼
?。常畆equest傳遞亂碼
以下將對上述三類亂碼進行解析:
一.頁面字符亂碼:
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)
?。玻g隔不一致:
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之子集。
二.記錄顯示亂碼:
?。保甅ySQL默人語言為latin1_swedish_ci,即拉丁語,所以取出的中文全是亂碼。
*解決方案:
?。保畬harset設為8859_1即:<%@page contentType="text/html;charset=8859_1"%>
這個方法只能暫時緩解字符顯示問題,并權益之計。因為8859_1為字節型字庫,并非字型字庫,故在非全角狀態下,將出現半字亂碼,表現為“?”。
?。玻跀祿爝B接語句中加上?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傳遞亂碼:
?。保苍S,此時你已經可以正常使用系統了,那么恭喜~亂碼問題已經離開你了!但是,大伙通常都沒那么走運,亂碼問題依舊存在。也許,這時你向數據庫添加 了一條記錄以測試系統,可是此時顯示出的還是亂碼。那么可以肯定是Request參數傳遞出錯!那么先寫個測試語句:<%= request.getParameter(“Para”) %>,OK,果然是亂。那么,現在有兩種解決方法。
*解決方案:
?。保由线@條語句:request.setCharacterEncoding("gbk");
在一/兩頁上可行,但此法也非權益之計。
2.注冊SetCharacterEncodingFilter類:
首先,編寫SetCharacterEncodingFilter.java文件,代碼如下:
1
package
cn.com.jsp;
2
3
import
java.io.IOException;
4
import
javax.servlet.Filter;
5
import
javax.servlet.FilterChain;
6
import
javax.servlet.FilterConfig;
7
import
javax.servlet.ServletException;
8
import
javax.servlet.ServletRequest;
9
import
javax.servlet.ServletResponse;
10
import
javax.servlet.UnavailableException;
11
12
public
class
SetCharacterEncodingFilter
implements
Filter
{
13
protected
String encoding
=
null
;
14
protected
FilterConfig filterConfig
=
null
;
15
protected
boolean
ignore
=
true
;
16
17
public
void
destroy()
{
18
this
.encoding
=
null
;
19
this
.filterConfig
=
null
;
20
}
21
22
public
void
doFilter(ServletRequest request, ServletResponse response,
23
FilterChain chain)
throws
IOException,
24
ServletException
{
25
26
//
Conditionally select and set the character encoding to be used
27
if
(ignore
||
(request.getCharacterEncoding()
==
null
))
{
28
String encoding
=
selectEncoding(request);
29
if
(encoding
!=
null
)
{
30
request.setCharacterEncoding(encoding);
31
}
32
}
33
34
//
Pass control on to the next filter
35
chain.doFilter(request, response);
36
37
}
38
39
public
void
init(FilterConfig filterConfig)
throws
ServletException
{
40
41
this
.filterConfig
=
filterConfig;
42
this
.encoding
=
filterConfig.getInitParameter(
"
encoding
"
);
43
String value
=
filterConfig.getInitParameter(
"
ignore
"
);
44
if
(value
==
null
)
{
45
this
.ignore
=
true
;
46
}
else
if
(value.equalsIgnoreCase(
"
true
"
))
{
47
this
.ignore
=
true
;
48
}
else
if
(value.equalsIgnoreCase(
"
yes
"
))
{
49
this
.ignore
=
true
;
50
}
else
{
51
this
.ignore
=
false
;
52
}
53
54
}
55
56
protected
String selectEncoding(ServletRequest request)
{
57
return
(
this
.encoding);
58
}
59
60
}
61
此文件為request過濾類,在全局編譯前需進行注冊。
注冊文件為:<%wwwroot%>/WEB-INF/web.xml。
在此文件中加入如下代碼即可:
1
<
web-app
>
2
<
display-name
>
wwwroot
</
display-name
>
3
<
description
>
MySQL Test App
</
description
>
4
<
filter
>
5
<
filter-name
>
setCharacterEncodingFilter
</
filter-name
>
6
<
display-name
>
setCharacterEncodingFilter
</
display-name
>
7
<
description
>
setCharacterEncodingFilter
</
description
>
8
<
filter-class
>
cn.com.jsp.SetCharacterEncodingFilter
</
filter-class
>
9
<
init-param
>
10
<
param-name
>
encoding
</
param-name
>
11
<
param-value
>
GBK
</
param-value
>
12
</
init-param
>
13
</
filter
>
14
<
filter-mapping
>
15
<
filter-name
>
setCharacterEncodingFilter
</
filter-name
>
16
<
url-pattern
>
/*
</
url-pattern
>
17
</
filter-mapping
>
18
……
19
</
web-app
>
20
OK,現在可以編譯你的SetCharacterEncodingFilter.java文件啦!
至此,亂碼將與你格格不入!
posted on 2007-07-10 10:24
冰封的愛
閱讀(346)
評論(0)
編輯
收藏
所屬分類:
J2EE
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
相關文章:
java日期操作 及 Timer定時器
大型B/S系統的并發問題
定義一個內部類的比較器,比較兩個對象并且進行排序
Spring+hibernate+DWR整合
獲得漢字拼音首字母(java版)
根據拼音首字母查詢人名(C#版)
java開源項目
在 Java 應用程序中計劃重復執行的任務(轉)
如何使用ejb3持久化中callback
EJB3 QL查詢
Copyright ©2025 冰封的愛 Powered By:
博客園
模板提供:
滬江博客
<
2025年5月
>
日
一
二
三
四
五
六
27
28
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(3)
給我留言
查看公開留言
查看私人留言
隨筆檔案
2007年9月 (1)
2007年8月 (6)
2007年7月 (3)
2007年6月 (3)
文章分類
J2EE(64)
Linux(8)
常用工具(9)
開源項目(3)
技術(9)
文章檔案
2009年1月 (1)
2008年4月 (1)
2008年1月 (1)
2007年10月 (2)
2007年9月 (2)
2007年8月 (1)
2007年7月 (73)
2007年6月 (12)
相冊
冰封的相冊
搜索
最新評論
1.?re: 自動生成32位永不重復ID
444
--22
2.?re: 根據拼音首字母查詢人名(C#版)[未登錄]
好用,謝謝
--123
3.?re: java中生成32位隨機ID [未登錄]
手術
--想
4.?re: Jocky混淆JAVA代碼(保護你的JAVA項目)
求支持 JDK 1.7的版本
--pro
5.?re: Jocky混淆JAVA代碼(保護你的JAVA項目)
8530
--ss
閱讀排行榜
1.?學會放下(654)
2.?項目人生(401)
3.?IT外包值得體驗(398)
4.?小型軟件公司如何做大(391)
5.?2007年7月每日一句匯總(1)(363)
評論排行榜
1.?學會放下(0)
2.?2007年7月份 每日一句匯總(2)(0)
3.?2007年7月每日一句匯總(1)(0)
4.?英語每日一句(0)
5.?IT外包值得體驗(0)
主站蜘蛛池模板:
亚洲精品中文字幕无码A片老
|
中文字幕亚洲综合精品一区
|
国产在线观看www鲁啊鲁免费
|
全免费a级毛片免费**视频
|
亚洲成a人无码av波多野按摩
|
亚洲国产一区二区三区
|
亚洲精品在线电影
|
一级做a爰黑人又硬又粗免费看51社区国产精品视
|
一出一进一爽一粗一大视频免费的
|
久久久免费的精品
|
和日本免费不卡在线v
|
凹凸精品视频分类国产品免费
|
亚洲人成网77777亚洲色
|
午夜在线a亚洲v天堂网2019
|
中文字幕免费观看视频
|
99在线视频免费观看视频
|
亚洲女久久久噜噜噜熟女
|
97无码人妻福利免费公开在线视频
|
岛国av无码免费无禁网站
|
久久亚洲精品成人综合
|
久久亚洲AV成人无码国产电影
|
美女视频黄a视频全免费网站一区 美女视频黄a视频全免费网站色
|
精品女同一区二区三区免费站
|
成人伊人亚洲人综合网站222
|
丁香婷婷亚洲六月综合色
|
巨胸狂喷奶水视频www网站免费
|
亚洲熟妇中文字幕五十中出
|
亚洲成人免费在线
|
亚洲色成人四虎在线观看
|
日韩电影免费在线
|
亚洲六月丁香婷婷综合
|
国产一区在线观看免费
|
最新亚洲成av人免费看
|
亚洲区小说区激情区图片区
|
99精品视频在线视频免费观看
|
亚洲第一成年免费网站
|
一级做a毛片免费视频
|
亚洲精品成人久久
|
国产一级一片免费播放i
|
久久er国产精品免费观看2
|
亚洲国产三级在线观看
|