顯示具有 Tomcat 標籤的文章。 顯示所有文章
顯示具有 Tomcat 標籤的文章。 顯示所有文章

2009-03-17

Tomcat Post / Get 中文編碼處理方法 (中文亂碼問題)


最近在把原本佈署在 Resin 的 Java Web Application移植到 Tomcat 時,有許多原本正常的中文get / post 功能,都變的異常,只要是透過 get / post 得到的資料,就會變亂碼

剛好也驗證了 Resin 在Character Encoding的部分做的較好
而看看網路上大家的問題,也可以知道 Tomcat 在編碼處理的部分 真是讓很多人頭痛

主要的原因是,TOMCAT 預設都是用 ISO-8859-1 的編碼方式來傳遞資訊

這個問題,可以解決的方式整理如下:

1. JSP 頁面的編碼宣告需與實際儲存檔案時用的編碼一致

這是個很好玩的問題~ 有些人 在頁面宣告用 big5 or UTF-8 or... 字集
但是,檔案實際儲存的方式與該編碼不同,則 頁面當然會出現亂碼的問題

2. 自行轉碼
form method=POST or GET 時 因為Tomcat預設編碼是ISO-8859-1的關係
直接用 <%=request.getParameter("firstname")%> 取值,中文字會變亂碼
(你傳的明明是UTF-8, 但是讀取時 確用 ISO-8859-1來解譯.. 當然有問題囉)
要使用 <%=new String(request.getParameter("firstname").getBytes( "ISO-8859-1"), "UTF-8")%>這種方式 來轉換編碼才會正常 (假設,頁面用的編碼為UTF-8時)

3. 透過設定 server.xml URIEncoding的方式 來處理 GET 亂碼問題
在 tomcat 的設定檔內 ./conf/server.xml 修改下列設定的話,則透過GET的方式傳遞參數
的話,Tomcat會用你指定的編碼方式來對待用get方法傳遞的參數,而不是使用預設的ISO-8859-1 (get 的方式是透過 url parameter傳遞參數)
但是,因為只處理 URIEncoding, 所以若使用 Post 傳遞,還是會有問題


URIEncoding: This specifies the character encoding used to decode the URI bytes, after %xx decoding the URL. If not specified, ISO-8859-1 will be used.

P.S. 若使用 GET Method, 且設定了URIEncoding 的話就不用再做上述二的自行轉碼動作,如果多加該處理,一樣會有亂碼的問題

4. 使用 request.setCharacterEncoding 來處理 GET / POST 的亂碼問題
在處理的頁面,加上下列的處理即可,明確的告訴Tomcat request的編碼為何,不要一廂情願的使用ISO-8859-1來解讀~

<%request.setCharacterEncoding("UTF-8");%>

javax.servlet.servletRequest.setCharacterEncoding(string env)
Overrides the name of the character encoding used in the body of this request. This method must be called prior to reading request parameters or reading input using getReader(). Otherwise, it has no effect.

5. 使用 filter 來處理 GET / POST 的亂碼問題
這個方法是最方便的,只要在 web.xml 裡面 透過 filter 的設定,讓每個網頁都能
透過 filter 的處理一體適用所有編碼問題的解決
方法如下:

a. 目前 Tomcat 提供的範例裡面,就有現成的 Encoding Filter, 位置如下:
.\Tomcat 6.0\webapps\examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.class

b. 所以,只要把該 class 檔,copy 到你的 web application 的
.\WEB-INF\classes\filter 下,再搭配 web.xml 的filter 設定,
就可以讓所有符合 url pattern 的網頁,都能自動apply所想要的編碼設定(透過 encoding這個 parameter來設定),是不是很方便呢 ?
(這個功能 就像是自動把上述第四點的 setCharacterEncoding 語句 自動apply到各頁面囉)



------------
以上~ 就從 Resin Porting 到 Tomcat 的經驗,當然是使用 filter 的方式 一次解決~ 不然的話 就得每個網頁每個網頁去改囉~

延伸資料:
FAQ/CharacterEncoding
http://jim.blogsome.com/2005/05/27/jsp-chinese-character-solution/
http://www.javaworld.com.tw/confluence/pages/viewpage.action?pageId=752


閱讀全文...

2009-02-18

IIS6 + Tomcat6 設定方法


1. 先有IIS

2. 安裝JDK
- download JDK and install
- set JAVA_HOME 環境變數至 jdk 安裝目錄

3. 安裝 Tomcat
- download Tomcat and install
- set CATALINA_HOME 環境變數至 Tomcat 安裝目錄

a. 首先先依上述連結下載取得 isapi_redirect-1.2.27.dll (目前最新版本)
b. 將該dll置於一特定目錄 (如: c:\TomcatConnector)
c. 在該目錄下 建立三個config file 分別為
isapi_redirect-1.2.27.properties (主檔名與 isapi_redirectxxxx.dll 的主檔名同)
workers.properties
uriworkermap.properties
d. 下述是我針對三個檔案的設定 (可在tomcat網站看到how-to說明及這些檔案的定義)
基本上,除 uriworkermap.properties 外的兩個檔案 都大概變動不大
uriworkermap.properties看名字跟內容就知,是用來設定哪些URL Pattern
是IIS要透過此connector交由tomcat處理的

isapi_redirect-1.2.27.properties
# Configuration file for the Jakarta ISAPI Redirector
# The path to the ISAPI Redirector Extension, relative to the website
# This must be in a virtual directory with execute privileges
extension_uri=/jakarta/isapi_redirect-1.2.27.dll

# Full path to the log file for the ISAPI Redirector
log_file=c:\TomcatConnector\isapi_redirect.log
# Log level (debug, info, warn, error or trace)
log_level=debug
# Full path to the workers.properties file
worker_file=c:\TomcatConnector\workers.properties
# Full path to the uriworkermap.properties file
worker_mount_file=c:\TomcatConnector\uriworkermap.properties

workers.properties
worker.list=wlb
worker.wlb.type=ajp13
worker.wlb.host=localhost
worker.wlb.port=8009

uriworkermap.properties
/examples/*=wlb

5. 設定IIS
a. 設定網頁服務延伸


b. 在站台上加入ISAPI filter

c. 在IIS站台上新增一虛擬目錄,命名為 jakarta 指向 Tomcat connector存放的位置
(本例為: c:\TomcatConnector),這個動作 對應到
isapi_redirect-1.2.27.properties 此設定檔內的下列設定:
extension_uri=/jakarta/isapi_redirect-1.2.27.dll

d. 注意,check download 下來的 isapi_redirect-1.2.27.dll 有無被 windows 封鎖


6. restart tomcat & iis

7. 連到 http://localhost:8080/examples 與 http://localhost/examples
check IIS 是否真的把符合 /exmaples/* 的 URL request 都餵給tomcat處理

8. 恭喜~ 大功告成

9. 後記: Tomcat網站上 說明有點遺漏
a. dll 可能被block的問題 官網上沒說明到
b. 除了 IIS 網頁服務延伸設定外,在 IIS 站台上 也要設定對應所使用的ISAPI位置
c. 官網上提供修改 windows registery的做法來記錄
如workers.properties、uriworkermap.properties這些重要設定檔的位置
另外也提到可以在該 ISAPI dll 所在位置建立一isapi_redirect-1.2.27.properties 來
存放設定檔位置
當然~ 用第二種方法 感覺比較容易 :)


閱讀全文...