現象: new Cookie("name", "中文值") 拋出 IllegalArgumentException: Control character in cookie value。
原因: Cookie 標準僅支援 ASCII,Tomcat 7 預設嚴格檢查。
解決: 寫入時 URLEncoder.encode(value, "UTF-8"),讀取時 URLDecoder.decode(cookie.getValue(), "UTF-8")。
window.parent.location.href = "parent.jsp";
<a disabled="disabled"> 不生效。替代方案:
<a href="link.html" class="not-active">Link</a>
.not-active { pointer-events: none; cursor: default; }
或 jQuery:$('a.current-page').click(() => false);
使用 readonly 替代 disabled,值會正常提交且不可編輯。
明確指定 <button type="button"> 避免誤觸發表單提交。
NOT NULL 約束:可直接設為 NULL。NOT NULL 約束:設 NULL 會寫入該型別預設值(如 0、空字串)。錯誤:<script src="..." />。正確:<script src="..."></script>。
亂碼本質:編碼與解碼使用的字符集不一致。
參數在 URL 中,瀏覽器通常以 UTF-8 編碼。Tomcat 需在 server.xml 的 <Connector> 加入 URIEncoding="UTF-8" 並重啟。
不建議 在 GET 請求傳遞中文參數。
在讀取參數前呼叫:request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8"); // 必須在 getWriter() 前
編寫編碼過濾器,對所有請求/響應統一設定 UTF-8,但僅對 POST 生效;GET 仍需配合 Tomcat URIEncoding。
String value = new String(request.getParameter("param").getBytes("ISO-8859-1"), "UTF-8");
結論: 優先使用 POST 提交中文;不同伺服器/作業系統預設編碼可能不同,統一配置 UTF-8 為最佳實踐。