Posted on 2006-10-09 23:20
hays(海納百川) 閱讀(641)
評(píng)論(1) 編輯 收藏
解決 <%@ include file="/global/topright.html" %> 的中文亂碼問題 (引用)
要解決這個(gè)問題,當(dāng)然最簡(jiǎn)單的就是在每個(gè)被 include 的檔案第一行,加上 <%@ page contentType="text/html;charset=big5" %> 這樣一定可以確保中文 jsp 檔不會(huì)出現(xiàn)亂碼,只不過,一旦程式修改成這樣的模式,你的程式就無法在舊的 jsp/servlet container 上執(zhí)行了,因?yàn)榕f的規(guī)格是不允被 include 檔案中再出現(xiàn) <%@ page ... %> 這樣的定義的。
況且,就算你願(yuàn)意為了 Tomcat 5.0.x 特別維護(hù)一套不同版本的 Source Code,你會(huì)遇到重大的挫折,因?yàn)?Tomcat 5.0.x 版在 charset 的設(shè)定上,會(huì)特別檢查 include 別人的程式與被人 include 的程式,這二個(gè)程式中所定義的 charset 是不是一樣,如果不一樣,在編譯時(shí)就會(huì)產(chǎn)生錯(cuò)誤。更恐怖的是,竟然還分大小寫,比如說:"big5" "Big5" 這樣的定義,在 Tomcat 的認(rèn)定上是不同的哦。
假如有一個(gè) Head.jsp,所有程式都會(huì)去 include 的檔案,試著在 Head.jsp 中,加上 <%@ page contentType="text/html;charset=big5" %> ,然後再執(zhí)行,天啊,有人習(xí)慣用 "big5", 有人習(xí)慣用 "Big5",更有人習(xí)慣用 "BIG5",怎麼辦 ? 難道要一支一支程式去改嗎 ? 還是用 UltraEdit 的 Replace in Files,一次全部更正過來 ? 那日後,是不是得規(guī)定所有程式設(shè)計(jì)師統(tǒng)一採(cǎi)用 "Big5" 呢 ?
想想,不太可行,也太愚蠢了。
正確解決方案
在 Tomcat 5.0.x 中,Tomcat 支援了 JSP 2.0 的規(guī)格,同時(shí)也支援了部分 J2EE 1.4 的規(guī)格,在 J2EE 1.4 的規(guī)格中,有關(guān) JSP 的部份,有一個(gè) <jsp-config> 的 XML Tag,這個(gè) XML 區(qū)塊用來定義與 JSP 相關(guān)的特殊屬性,包含採(cǎi)用的 taglib 與 以下說明的 <jsp-property-group> ,而解決 include 檔中文問題的方法就定義在 <jsp-property-group> 中。
首先,請(qǐng)至你開發(fā)的 webapps 的目錄下,找到 WEB-INF\web.xml 檔案,比如說:eip 專案的目錄,就是 $TOMCAT_HOME\webapps\eip\WEB-INF\web.xml。
用文字編輯器開啟 web.xml,將以下這段 xml head 的定義 copy & replace 到 web.xml:
1 2 3 4 5
|
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
|
特別注意,version="2.4",Tomcat 5.0.19 會(huì)去偵測(cè)這個(gè)版本資訊,目前只有 2.4 的才會(huì)處理 jsp-config 的參數(shù)。
在 web.xml 中,加上以下這個(gè)區(qū)塊:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
<jsp-config>
<jsp-property-group>
<description>
Special property group for JSP Configuration JSP example.
</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>Big5</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude></include-prelude>
<include-coda></include-coda>
?
<description>
Special property group for JSP Configuration JSP example.
</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.html</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>Big5</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude></include-prelude>
<include-coda></include-coda>
</jsp-property-group>
</jsp-config>
|
這二個(gè)區(qū)塊的定義,就是告知 Tomcat,在 eip 專案目錄下,所有的 .jsp, .html 檔案,若是沒有定義 contentType="text/html;charset=big5" 時(shí),請(qǐng)採(cǎi)用預(yù)設(shè)的 "Big5" 字元集去處理,如此一來,你就不須要在每個(gè) include 的檔案第一行加上 contentType="text/html;charset=big5" 了。
想更進(jìn)一步了解 <jsp-config> 的說明,請(qǐng)參考
http://java.sun.com/xml/ns/j2ee/#oldresources
web.xml 範(fàn)例n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
<?xml version="1.0" encoding="ISO-8859-1"?>
?
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>EIP 2E PROJECT</display-name>
<description>EIP 2E PROJECT</description>
?
<jsp-config>
<jsp-property-group>
<description>
Special property group for JSP Configuration JSP example.
</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>Big5</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude></include-prelude>
<include-coda></include-coda>
?
<description>
Special property group for JSP Configuration JSP example.
</description>
<display-name>JSPConfiguration</display-name>
<url-pattern>*.html</url-pattern>
<el-ignored>true</el-ignored>
<page-encoding>Big5</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude></include-prelude>
<include-coda></include-coda>
</jsp-property-group>
</jsp-config>
?
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>
|