URL 標準 (RFC 3986) 僅允許 ASCII 字元。中文參數在 URL 傳遞時,瀏覽器會自動進行 **Percent-Encoding (URL Encoding)**,但不同瀏覽器、伺服器、框架的預設編碼可能不一致 (UTF-8 vs GBK),導致解碼錯誤產生亂碼。
// 單次 encodeURI 足夠 (編碼保留字元),但雙重編碼可防止伺服器雙重解碼
var url = './showCont.jsp?tcontent=' + encodeURI(encodeURI(tcontent));
// 或使用 encodeURIComponent (編碼所有非保留字元,適合參數值)
var url = './showCont.jsp?tcontent=' + encodeURIComponent(tcontent);
location.href = url;
差異: encodeURI 不編碼 :/?#[]@ 等保留字元;encodeURIComponent 全編碼,**參數值建議用 encodeURIComponent**。
<%@ page pageEncoding="UTF-8" %>
<%
// 方式一:標準 API (需 try-catch)
String tcontent = request.getParameter("tcontent");
if (tcontent != null) {
tcontent = java.net.URLDecoder.decode(tcontent, "UTF-8");
}
// 方式二:Tomcat 8+ 配置 URIEncoding="UTF-8" 後,getParameter 直接返回正確字串,無需手動解碼
%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>發送中文參數</title>
</head>
<body>
<%
String strTest = "華工"; // 中文內容
%>
<!-- 方式一:表單 GET 提交 (瀏覽器自動編碼) -->
<form action="b.jsp" method="get">
<input type="hidden" name="test" value="<%=strTest%>">
<input type="submit" value="提交 GET">
</form>
<!-- 方式二:JavaScript 動態構建 URL -->
<button onclick="sendByJs()">JS 跳轉</button>
<script>
function sendByJs() {
var val = "<%=strTest%>"; // JSP 輸出到 JS 字串
// 使用 encodeURIComponent 確保參數值完整編碼
location.href = 'b.jsp?test=' + encodeURIComponent(val);
}
</script>
</body></html>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.net.URLDecoder" %>
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>接收結果</title></head>
<body>
<%
// 情況 1:Tomcat 已配置 URIEncoding="UTF-8" (推薦)
String str1 = request.getParameter("test");
// 情況 2:未配置或不確定,手動解碼 (舊專案相容)
String raw = request.getParameter("test");
String str2 = null;
if (raw != null) {
try {
// 取得 ISO-8859-1 位元組再以 UTF-8 解碼
str2 = new String(raw.getBytes("ISO-8859-1"), "UTF-8");
// 或使用 URLDecoder (效果相同)
// str2 = URLDecoder.decode(raw, "UTF-8");
} catch (Exception e) {
str2 = "解碼失敗";
}
}
%>
<h3>自動解碼結果: <%=str1%></h3>
<h3>手動解碼結果: <%=str2%></h3>
</body></html>
修改 conf/server.xml:
<Connector port="8080" protocol="HTTP/1.1"
URIEncoding="UTF-8"
useBodyEncodingForURI="true" />
配置後,**所有 GET 請求參數自動以 UTF-8 解碼**,無需在 JSP 中手動 URLDecoder.decode 或 getBytes("ISO-8859-1")。
POST 請求體編碼由 Content-Type: application/x-www-form-urlencoded; charset=... 決定。
<%-- JSP 頁面頂部 --%>
<% request.setCharacterEncoding("UTF-8"); %>
<!-- 或配置 Filter 統一處理 -->
配合 <form accept-charset="UTF-8"> 強制瀏覽器以 UTF-8 編碼表單資料。
| 錯誤做法 | 後果 | 正確做法 |
|---|---|---|
只用 encodeURI 不雙重編碼 | 參數含 & = 等保留字元被截斷 | 參數值用 encodeURIComponent |
後端用 new String(bytes, "GBK") | 前端 UTF-8 編碼,後端 GBK 解碼 → 亂碼 | 前後端統一 UTF-8 |
手動 getBytes("ISO-8859-1") 但 Tomcat 已配置 UTF-8 | 雙重解碼錯誤 | 配置 Tomcat UTF-8 後直接用 getParameter() |
| JS 中直接拼接中文到 URL | 瀏覽器編碼行為不一致 | 必須 encodeURIComponent |
@GetMapping("/receive")
public String receive(@RequestParam String test) {
// Spring 自動處理 UTF-8 解碼 (需配置 CharacterEncodingFilter)
System.out.println(test); // 直接獲得正確中文
return "result";
}
<!-- web.xml -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param>
<init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param>
</filter>
JSP 頁面間傳中文參數核心原則:**前端 encodeURIComponent(UTF-8) → 後端 Tomcat 配置 URIEncoding=UTF-8 → 直接 getParameter()**。舊專案無法修改 Tomcat 配置時,使用 new String(param.getBytes("ISO-8859-1"), "UTF-8") 手動還原。全站統一 UTF-8、配置編碼 Filter、避免手動編解碼,是徹底解決之道。