在jsp中使用<%@include file="in.html" %>導入html頁面時,如果html頁面裏有中文,就會產生亂碼。檢查jsp文件和html文件的編碼,編碼一致,都是統一使用的utf-8,檢查生成的Servlet類文件,發現裏面直接就亂碼了。
jsp頁面內容:
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>測試JSP的include指令</title>
- </head>
- <body>
- <%@include file="in.html" %><br/>
- <%@include file="in1.jsp"%><br/>
- <%@include file="in2.html" %>
- </body>
- </html>
in.html文件內容:
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title></title>
- </head>
- <body>
- 我是in.html文件的內容
- </body>
- </html>
- out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
- out.write("<html>\r\n");
- out.write("<head>\r\n");
- out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
- out.write("<title></title>\r\n");
- out.write("</head>\r\n");
- out.write("<body>\r\n");
- out.write("ææ¯in.htmlæ件çå 容\r\n");
- out.write("</body>\r\n");
- out.write("</html>\r\n");
通過上面生成的Servlet內容可以看出,在將jsp文件編譯成Java類這一過程就出現了亂碼,問題肯定是編碼一致的,而設置編碼的有兩個:pageEncoding和contentType,這兩個屬性的區別如下:
pageEncoding是jsp文件本身的編碼,是指定web容器將jsp編譯成java文件時採用什麼編碼讀取jsp文件。
contentType的charset設置的編碼是指服務器發送給客戶端時的內容編碼。
而客戶端訪問一個jsp文件要經過如下三個階段:
1、(第一次訪問時)web容器將jsp編譯成java文件,這個階段編譯器會根據pageEncoding設置的編碼讀取jsp文件,翻譯成統一的utf-8的Servlet類,如果pageEncoding設置錯誤或未設置,編譯出來的java文件就會出現中文亂碼。
2、由javac將java源碼編譯成class字節碼,javac用utf-8編碼讀取java源碼,編譯成utf-8編碼的二進制文件。
3、web容器載入class字節碼文件,將內容輸出結果到客戶端,這一過程內容的編碼爲contentType設置的編碼。
由此可見,是由於pageEncoding設置問題導致翻譯jsp時亂碼。有兩種方式處理:
方法一:在每個引入的html文件設置pageEncoding編碼,即在html添加<%@page pageEncoding="UTF-8"%>,儘管html不能識別該指令,但通過include指令引入時該指令就能起作用了,如下:
- <%@page pageEncoding="UTF-8"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title></title>
- </head>
- <body>
- 我是in.html文件的內容
- </body>
- </html>
方法二:在web.xml裏統一配置pageEncoding的編碼,在web-app標籤裏添加如下配置:
- <jsp-config>
- <jsp-property-group>
- <description>html encoding</description>
- <display-name>JSPConfiguration</display-name>
- <url-pattern>*.html</url-pattern>
- <el-ignored>true</el-ignored>
- <page-encoding>UTF-8</page-encoding>
- <scripting-invalid>false</scripting-invalid>
- <include-prelude></include-prelude>
- <include-coda></include-coda>
- </jsp-property-group>
- </jsp-config>
方法一和方法二原理是一樣的,都是通過設置pageEncoding編碼來指定jsp將html文件include時使用的編碼。方法一和方法二任選一種即可,如果同時使用需要注意兩個地方設置的pageEncoding編碼必須一致,否則將會報如下編碼不一致的錯誤:
- org.apache.jasper.JasperException: /in.html (line: 1, column: 2) Page-encoding specified in jsp-property-group (UTF-8) is different from that specified in page directive (GBK)