JSP 靜態包含 <%@ include file="..." %> 會將目標檔案內容直接合併到主檔案編譯。若主檔與被包含檔的 pageEncoding 或 contentType 中 charset 值大小寫不一致(如 UTF-8 vs utf-8),編譯器會報錯:pageEncoding attribute cannot be different from the pageEncoding of the included file。
index.jsp: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ include file="./include.jsp" %> include.jsp: <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> Hello World!
訪問 index.jsp 直接拋出 Jasper 編譯異常。
JSP 規範要求靜態包含時,**主檔與被包含檔的 pageEncoding 必須完全一致(含大小寫)**。編譯器進行字串精確比對,UTF-8 ≠ utf-8。
UTF-8(大寫),修改 include.jsp 為 charset=UTF-8、pageEncoding="UTF-8"。<jsp:include page="include.jsp" /> 獨立編譯,不受主檔 encoding 限制(但需注意請求屬性傳遞)。web.xml 或過濾器統一設定 request/response encoding = UTF-8。<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>。