JSP 頁面間透過 URL 傳遞中文參數,需雙重編碼/解碼確保不亂碼。
a.href = "./showCont.jsp?tcontent=" + encodeURI(encodeURI(tcontent));
雙重 encodeURI 防止瀏覽器自動解碼導致伺服器端接收錯誤。
String value = java.net.URLDecoder.decode(request.getParameter("tcontent"), "UTF-8");
<%@ page contentType="text/html; charset=GB2312" %>
<%
String str_test = "華工";
%>
<form method="post" action="b.jsp?test=<%=java.net.URLEncoder.encode(str_test, "GB2312") %>">
<input type="submit" value="提交">
</form>
<%@ page contentType="text/html; charset=GB2312" %>
<%
String str = new String(request.getParameter("test").getBytes("ISO8859_1"), "GB2312");
%>
<%= str %>
getBytes("ISO8859_1") 取得原始字節陣列,再用 new String(bytes, "GB2312") 正確重組字串。GB2312 替換為 UTF-8 並配合 Filter 設定 request.setCharacterEncoding("UTF-8") 即可簡化為單層編碼/解碼。