1,Jsp頁面
<% @page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
2,JSP頁面頭部
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
3,瀏覽器調用servlet,頁面顯示亂碼
servlet使用response對象輸出前,先執行
1 response.setContentType("text/html;charset=utf-8"); 3 response.setCharacterEncoding("utf-8");
4,jsp或servlet中表單參數提交給對應的jsp或servlet,接收的jsp或servlet中接收到的參數中文顯示亂碼
在接收post提交的參數前,使用
request.setCharacterEncoding("utf-8");
設定接收參數的內容格式為utf-8編碼。
5,數據庫連接
設置characterencoding為UTF-8 如:
String url = "jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF8";
6,使用Hibernate,所有的配置文件頭部的編碼格式改成UTF-8
7, URL方式傳遞參數(Tomcat)亂碼
為了保證get/post數據都采用相同的UTF8編碼,在tomecat中server.xml設置:
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />
8,數據庫(mysql)
輸入查詢語句SHOW VARIABLES LIKE 'character_set_%';查看所有的編碼是否是UTF-8。如不是,使用Server Instance Config 把默認的字符集設置為utf-8或者修改/MySQL/MySQL Server 5.0/my.ini中的default-character-set=gbk
character-set-server=gbk;
重新啟動mysql的服務