URL 標準僅支援 ASCII,中文參數經瀏覽器 URL Encoding 後傳輸,伺服器解碼時若編碼不一致 (UTF-8 vs GBK) 即產生亂碼。
// 推薦:encodeURIComponent (參數值完整編碼)
var url = 'b.jsp?param=' + encodeURIComponent(chineseStr);
// 表單提交:瀏覽器自動按頁面編碼/accept-charset 編碼
//
<%@ page pageEncoding="UTF-8" %>
<%
// 方案 1:Tomcat 配置 URIEncoding="UTF-8" 後,直接取得正確字串 (推薦)
String value = request.getParameter("param");
// 方案 2:未配置 Tomcat 時,手動還原 (舊專案相容)
String raw = request.getParameter("param");
String decoded = new String(raw.getBytes("ISO-8859-1"), "UTF-8");
// 或
String decoded = java.net.URLDecoder.decode(raw, "UTF-8");
%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html><head><meta charset="UTF-8"><title>發送頁</title></head>
<body>
<%
String chinese = "華工"; // 測試中文
%>
<!-- 方式一:表單 GET -->
<form action="b.jsp" method="get" accept-charset="UTF-8">
<input type="hidden" name="test" value="<%=chinese%>">
<input type="submit" value="GET 提交">
</form>
<!-- 方式二:表單 POST -->
<form action="b.jsp" method="post" accept-charset="UTF-8">
<input type="hidden" name="test" value="<%=chinese%>">
<input type="submit" value="POST 提交">
</form>
<!-- 方式三:JS 動態跳轉 -->
<button onclick="goJs()">JS 跳轉</button>
<script>
function goJs() {
var val = "<%=chinese%>";
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>
<%
// 獲取參數
String raw = request.getParameter("test");
String result;
if (raw == null) {
result = "無參數";
} else {
// 如果 Tomcat 已設 URIEncoding=UTF-8,raw 已是正確中文
// 否則需手動轉碼
try {
result = new String(raw.getBytes("ISO-8859-1"), "UTF-8");
// result = URLDecoder.decode(raw, "UTF-8"); // 同效果
} catch (Exception e) {
result = "解碼錯誤: " + e.getMessage();
}
}
%>
<h3>接收結果: <%=result%></h3>
<p>請求方法: <%=request.getMethod()%></p>
<p>Query String: <%=request.getQueryString()%></p>
</body></html>
<!-- conf/server.xml -->
<Connector port="8080" protocol="HTTP/1.1"
URIEncoding="UTF-8"
useBodyEncodingForURI="true" />
設定後,**所有 GET/POST 參數自動以 UTF-8 解碼**,JSP 完全不需手動處理。
瀏覽器 (頁面 UTF-8)
│
▼
encodeURIComponent("華工") → "%E8%8F%AF%E5%B7%A5"
│
▼
HTTP 請求: GET /b.jsp?test=%E8%8F%AF%E5%B7%A5
│
▼
Tomcat (URIEncoding=UTF-8)
│
▼
request.getParameter("test") → "華工" ✓
| 錯誤代碼 | 現象 | 原因 | 修正 |
|---|---|---|---|
new String(param.getBytes("GBK")) | 亂碼 | 前端 UTF-8 編碼,後端 GBK 解碼 | 統一 UTF-8 |
雙重 encodeURI(encodeURI()) | 參數變雙重編碼 | 單次 encodeURIComponent 即可 | 移除雙重編碼 |
Tomcat 已配 UTF-8 仍手動 getBytes("ISO-8859-1") | 雙重解碼錯誤 | 直接用 getParameter() | 移除手動轉碼 |
POST 表單無 request.setCharacterEncoding("UTF-8") | 中文亂碼 | Filter 統一設定或頁面頂部設定 | 配置 CharacterEncodingFilter |
// 只需配置 CharacterEncodingFilter (web.xml 或 SpringBoot 自動配置)
@GetMapping("/receive")
public String receive(@RequestParam String test) {
// 直接獲得正確中文,無需任何手動解碼
return "result";
}
**最佳實踐**:全專案統一 UTF-8 → Tomcat URIEncoding="UTF-8" + CharacterEncodingFilter → 前端 encodeURIComponent / 表單 accept-charset="UTF-8" → 後端直接 getParameter()。舊專案無法改 Tomcat 設定時,使用 new String(raw.getBytes("ISO-8859-1"), "UTF-8") 手動還原。拒絕 GBK/Big5 混用,擁抱 UTF-8 全棧統一。