我今天使用hibernate的時候改變了數據的編碼, 不再使用html encode ,
結果就出現了編碼的問題了, 由于hibernate的本質是jdbc,就去改hibernate的設置, 現在發(fā)現原來是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亂碼解決為例,望能為你的環(huán)境配置起到拋磚引玉之效!
亂碼問題已歷來已久,在開源環(huán)境下,亂碼問題更是令程序員措手不及。本人在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
其次,統(tǒng)一使用頁面編碼定義,如:<%@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為字節(jié)型字庫,并非字型字庫,故在非全角狀態(tài)下,將出現半字亂碼,表現為“?”。
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,本人不贊同此法,因為這樣破壞了原有的環(huán)境,除非這是MySQL的第一個站點。
三.request傳遞亂碼:
1.也許,此時你已經可以正常使用系統(tǒng)了,那么恭喜~亂碼問題已經離開你了!但是,大伙通常都沒那么走運,亂碼問題依舊存在。也許,這時你向數據庫添加 了一條記錄以測試系統(tǒng),可是此時顯示出的還是亂碼。那么可以肯定是Request參數傳遞出錯!那么先寫個測試語句:<%= request.getParameter(“Para”) %>,OK,果然是亂。那么,現在有兩種解決方法。
*解決方案:
1.加上這條語句: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

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

32

33

34

35

36

37

38

39



40

41

42

43

44



45

46



47

48



49

50



51

52

53

54

55

56



57

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

OK,現在可以編譯你的SetCharacterEncodingFilter.java文件啦!
至此,亂碼將與你格格不入!