JSP 採用 GBK 編碼時,Tomcat 預設 URIEncoding=ISO-8859-1 導致中文亂碼。兩處設定雙管齊下徹底解決。
<Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true"
URIEncoding="GBK" />
重啟 Tomcat 即生效。若使用 UTF-8 專案,請設為 URIEncoding="UTF-8"。
response.setContentType("text/html; charset=GBK");
或在 JSP 頁面頂部:
<%@ page contentType="text/html; charset=GBK" %>
server.xml → Connector → URIEncodingresponse.setContentType 或 request.setCharacterEncodingpageEncoding 與 contentTypecharacterEncoding=UTF8 (JDBC URL)<meta charset="GBK">建議全專案統一使用 UTF-8,避免編碼地獄。