jsp通過include指令引入html亂碼的解決方法

在jsp中使用<%@include file="in.html" %>導入html頁面時,如果html頁面裏有中文,就會產生亂碼。檢查jsp文件和html文件的編碼,編碼一致,都是統一使用的utf-8,檢查生成的Servlet類文件,發現裏面直接就亂碼了。

jsp頁面內容:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6. <title>測試JSP的include指令</title>  
  7. </head>  
  8. <body>  
  9. <%@include file="in.html" %><br/>  
  10. <%@include file="in1.jsp"%><br/>  
  11. <%@include file="in2.html" %>  
  12. </body>  
  13. </html>  

in.html文件內容:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title></title>  
  6. </head>  
  7. <body>  
  8. 我是in.html文件的內容  
  9. </body>  
  10. </html>  
生成的Servlet內容:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");  
  2. out.write("<html>\r\n");  
  3. out.write("<head>\r\n");  
  4. out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");  
  5. out.write("<title></title>\r\n");  
  6. out.write("</head>\r\n");  
  7. out.write("<body>\r\n");  
  8. out.write("我是in.html文件的内容\r\n");  
  9. out.write("</body>\r\n");  
  10. 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指令引入時該指令就能起作用了,如下:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <%@page pageEncoding="UTF-8"%>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  3. <html>  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6. <title></title>  
  7. </head>  
  8. <body>  
  9. 我是in.html文件的內容  
  10. </body>  
  11. </html>  

方法二:在web.xml裏統一配置pageEncoding的編碼,在web-app標籤裏添加如下配置:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <jsp-config>  
  2.     <jsp-property-group>  
  3.         <description>html encoding</description>  
  4.         <display-name>JSPConfiguration</display-name>  
  5.         <url-pattern>*.html</url-pattern>  
  6.         <el-ignored>true</el-ignored>  
  7.         <page-encoding>UTF-8</page-encoding>  
  8.         <scripting-invalid>false</scripting-invalid>  
  9.         <include-prelude></include-prelude>  
  10.         <include-coda></include-coda>  
  11.     </jsp-property-group>  
  12. </jsp-config>  

方法一和方法二原理是一樣的,都是通過設置pageEncoding編碼來指定jsp將html文件include時使用的編碼。方法一和方法二任選一種即可,如果同時使用需要注意兩個地方設置的pageEncoding編碼必須一致,否則將會報如下編碼不一致的錯誤:

[plain] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. 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)  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章