-網頁文件 存檔編碼 必須為 UTF-8

使用 記事本(Notepad) 或 UltraEdit 把 網頁文件(*.htm,*.jsp)轉存為 UTF-8 編碼


-網頁文件 編寫 設定使用 UTF-8

*.htm

<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head>


*.jsp

<%@page contentType="text/html; charset=utf-8" %>


-設定 server.xml 使用 UTF-8 編碼

# vi server.xml
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8">


-使用 filters UTF-8 編碼

下載 Tomcat 6.x

將 webapps/examples/WEB-INF/classes/filters/SetCharacterEncodingFilter.class

複製到 你的webapp資料夾中

(你的webapp名稱)/WEB-INF/classes/filters/SetCharacterEncodingFilter.class


編輯 (你的webapp名稱)/WEB-INF/web.xml

# vi web.xml

加入以下的內容

<filter>
<filter-name>SetCharacterEncoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>SetCharacterEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


在Tomcat 7.x之後的版本,已將SetCharacterEncodingFilter 加入Tomcat核心中,

可以不需要複製class到自己的webapp資料夾中.

編輯 (你的webapp名稱)/WEB-INF/web.xml


# vi web.xml

加入以下的內容

<filter>
<filter-name>Set_Character_Encoding</filter-name>
<filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>


-jsp:include htm 使用 UTF8 編碼

編輯 (你的webapp名稱)/WEB-INF/web.xml

修改 web.xml用2.4版

# vi web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:web="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd " version="2.4">


加入以下的內容

<jsp-config>
<jsp-property-group>
<url-pattern>*.htm</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>


-資料庫 建立使用 UTF8 編碼

(Mysql database)

CREATE DATABASE mytestdb CHARACTER SET utf8;


-資料庫 JDBC 使用 UTF8 編碼

(Mysql database)

useUnicode=true&characterEncoding=utf-8

Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://[hostname]/[database]?user=&password=&useUnicode=true&characterEncoding=utf-8");