Chinaunix首页 | 论坛 | 博客
  • 博客访问: 390938
  • 博文数量: 152
  • 博客积分: 6020
  • 博客等级: 准将
  • 技术积分: 850
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-11 19:20
文章分类

全部博文(152)

文章存档

2017年(1)

2010年(1)

2007年(3)

2006年(147)

我的朋友

分类: Java

2006-03-28 23:24:57

實際運用 Tomcat 5.0.19,我們了解在不修改 Tomcat 原始碼的狀況下,使用者透過 Form submit 的資料將一律以 ISO8859-1 處理,程式設計師必須自行將字串將轉換為 Big5(繁體中文) or GB2312/GBK(簡體中文),我們在應用程式中,對所有的 request.getParameter("xx"); 作了 toBig5String() 的處理,理論上,所有的中文問題應該不會出現才對,結果,還是發現某些狀況下,中文還是變成亂碼!

經過分析整理,我們發現問題出在 QueryString 的解析,以前在 Tomcat 4.x 時代,無論 SUBMIT 時採用 GET or POST,Tomcat server 對 parameters 的處理都採用相同的編碼,但在 Tomcat 5.x 版,不知何故,卻將 QueryString 的解析獨立出來,目前確認,Form 的 Method 採用 GET 及直接將參數寫在 URL 上的中文,上傳到 Tomcat 時,無論如何轉碼,都會變成亂碼,那怕你事先作過 URLEncode 也一樣。

網站上,有人針對這個問題,建議將所有中文改採用 base64 編碼,到了 server 上,程式將自行土 base64 decode 回來,確保中文不會發生問題。這樣作法當然可以解決這個問題,但是所有網頁變成限定要採用 POST,且程式設計師要隨時分清楚,那個參數是採用 GET 上傳,那個參數是採用 POST 上傳,然後再針對不同的方式採用不同的解析,這樣的程式一點兒移植性都沒有,更別提跨平台、跨國際語言了。

研究 Tomcat 的文件及原始碼,我們找到了問題所在及解決的方法,只有按著以下的作法,才能使 Form submit 的資料完全按著 ISO8859-1 的編碼,當然,若是全照著 Tomcat 的文件說明去作,肯定還是不行,你還是得加上這個參數到 server.xml 中才行。

解決方案

請先研究 $TOMCAT_HOME/webapps/tomcat-docs/config/http.html 這個說明檔,擷錄重點如下:
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.

useBodyEncodingForURI:This specifies if the encoding specified in contentType should be used for URI query parameters, instead of using the URIEncoding. This setting is present for compatibility with Tomcat 4.1.x, where the encoding specified in the contentType, or explicitely set using Request.setCharacterEncoding method was also used for the parameters from the URL. The default value is false.

上述二個 Tomcat 參數,是設定在 server.xml 中的 http 區塊,要解決 QueryString 中文變成亂碼的問題,你必須至少設定這二個參數其中之一。
URIEncoding 請設定為 URIEncoding="ISO-8859-1" 指定為 "ISO-8859-1" 編碼,讓 QueryString 的字元編碼與 post body 相同。
useBodyEncodingForURI 這是用來相容 Tomcat 4.x 版的,設定的值是 "true" or "false",意思是指 "要不要讓 QueryString 與 POST BODY 採用相同的字元編碼 ?",若是設成 true,那也可達到 "ISO-8859-1" 編碼的需求。
建議,採用 URIEncoding 的設定,畢竟 useBodyEncodingForURI 的作法是為了相容 Tomcat 4.X。不過若照原文的說明,理論上這二個參數都不設,Tomcat 也該採用 "ISO-8859-1" 的編碼,那為什麼還是會有問題呢 ? 我們由 Tomcat Source Code 來看就清楚了。
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
// 這一段碼是 Tomcat 用來解 QueryString 的程式,
// 在 org.apache.tomcat.util.http.Parameters 這個 class 裡。
private String urlDecode(ByteChunk bc, String enc)
  throws IOException {
  if( urlDec==null ) {
     urlDec=new UDecoder(); 
  }
  urlDec.convert(bc);
  String result = null;
  if (enc != null) {
    bc.setEncoding(enc);
    result = bc.toString();
  } 
  else {
    CharChunk cc = tmpNameC;
    cc.allocate(bc.getLength(), -1);
    // Default encoding: fast conversion
    byte[] bbuf = bc.getBuffer();
    char[] cbuf = cc.getBuffer();
    int start = bc.getStart();
    for (int i = 0; i < bc.getLength(); i++) {
      cbuf[i] = (char) (bbuf[i + start] & 0xff);
    }
    cc.setChars(cbuf, 0, bc.getLength());
    result = cc.toString();
    cc.recycle();
  }
  return result;
}

請特別注意紅色區塊,當 Tomcat 發現 QueryString 並沒有設定 encode 時,並非像文件中所說預設採用 ISO-8859-1 的編碼,而是用一段 fast conversion 來處理,才會造成中文問題,所以,還是必須在 Server.xml 中,加上 URLEncoding 的參數設定才行哦。

Connector 的設定範例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"0"
acceptCount="100"
connectionTimeout="20000"
disableUploadTimeout="true"
port="80"
redirectPort="8443"
enableLookups="false"
minSpareThreads="25"
maxSpareThreads="75"
maxThreads="150"
maxPostSize="0"
URIEncoding="ISO-8859-1"
>



browser edited on 2004-04-14 02:22

作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
browser

戀香

小版主

發文: 3379
積分: 1
於 2004-04-14 02:40 user profilesend a private message to usersend email to browserreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
太好了 .... Thumbs up
十分感謝 精靈 兄整理的這份資料 ...
這份文件是 Tomcat 5.0.19 中文參數傳遞的正解 ...
原本當初也想在特別提出來介紹的,但是當時己經進入排版階段 .. Disapproved

在這邊我做幾項補充 ...
一般說來,我們在使用 Tomcat 4 透過 GET or POST 的方式傳參數時,通常都是使用 Filter 的方式來解決中文傳參數的問題。
但是到了 Tomcat 5.0.19 之後,解決中文傳遞參數時,就必須考慮是使用 GET or POST,兩種解決的方式不一樣。

如果是使用 GET 的方式傳遞時,就如同 精靈 兄 的文章所述,或者使用

1
String name = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"Big5");


;若是使用 POST 的方式時,就延用傳統一般解決中文的方式

1
request.setCharacterEncoding("Big5");


不過當初我最後的做法是使用 Filter 的方式
Filter 的做法就是:先判斷是使用那種傳遞方式( GET or POST),假若是用 GET 的方式就採用第一種 code;若使用POST 方式,就採用第二種 code。


browser edited on 2005-03-16 15:15


作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
發條盒子

問問題的麻煩傢伙



發文: 120
積分: 0
於 2004-04-16 01:05 user profilesend a private message to usersend email to 發條盒子reply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
request.setCharacterEncoding("Big5");
我用這一行可以解決get和post的問題,這是為什麼呢?
反而我去設定了URIEncoding="ISO-8859-1"卻沒有什麼用也
環境tomcat5.0.16+j2sdk1.4.2_03+windows xp


browser edited on 2004-04-16 01:07

如果我能學好jsp就好了
我是新手,希望大家體諒我問的問題太白痴^^
買書買到沒有錢錢
作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
jan





發文: 47
積分: 0
於 2004-05-07 16:04 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我在server.xml中依照精靈前輩所說的這個設定修改了Connector Tag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"0"
acceptCount="100"
connectionTimeout="20000"
disableUploadTimeout="true"
port="80"
redirectPort="8443"
enableLookups="false"
minSpareThreads="25"
maxSpareThreads="75"
maxThreads="150"
maxPostSize="0"
URIEncoding="ISO-8859-1">


SUBMIT中文時採用GET的方式依然結取到亂碼耶~~
請問還有要注意什麼嗎?

我的平台:
OS : Windows2003 Server
SDK : Sun SDK 1.4.2_03-b02
Tomcat 5.0.19


jan edited on 2004-05-07 16:20

作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:jan]
browser

戀香

小版主

發文: 3379
積分: 1
於 2004-05-07 16:25 user profilesend a private message to usersend email to browserreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
jan wrote:
我在server.xml中依照精靈前輩所說的這個設定修改了Connector Tag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"0"
acceptCount="100"
connectionTimeout="20000"
disableUploadTimeout="true"
port="80"
redirectPort="8443"
enableLookups="false"
minSpareThreads="25"
maxSpareThreads="75"
maxThreads="150"
maxPostSize="0"
URIEncoding="ISO-8859-1">


SUBMIT中文時採用GET的方式依然結取到亂碼耶~~
請問還有要注意什麼嗎?

我的平台:
OS : Windows2003 Server
SDK : Sun SDK 1.4.2_03-b02
Tomcat 5.0.19


我是使用 URIEncoding="Big5" Smile




作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
plutotw

井底蛙



發文: 337
積分: 1
於 2004-05-09 17:43 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我們老師提的解決方案,用
1
URIEncoding="MS950"

測試結果在 windows 上是正常的



作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
senshaw





發文: 1
積分: 0
於 2004-06-12 18:55 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝~我發現tomcat 5中文參數問題後,遲遲不敢升級,現在見到曙光了Big Smile


作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:browser]
sindylee





發文: 1
積分: 0
於 2004-07-14 17:21 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問若是在filter判斷是否為GET 或 POST 的話, 那麼
若是 POST method, 則可直接用 request.setCharacterEncoding 做掉,
若是 GET method, 該如何將處理完的結果存回 request , 然後傳送到 JSP
中呢? 因為不想在每支JSP 中去判斷及處理



作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
hcl





發文: 6
積分: 0
於 2004-07-16 13:09 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
想請教各位先進,小弟我依照上述方式改了設定,但是圖檔連結的檔名
是中文,那在TOMCAT5.0.19上面無法顯示,想請問一下該如何處理呢?
1
"中文檔名" />

環境:
WINDOWS 2000 SP4 + TOMCAT5.0.19 + IIS 5.0


hcl edited on 2004-07-16 15:47

作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
prf





發文: 1
積分: 0
於 2004-08-16 11:05 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
好贴。


作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:hcl]
Jill_Yeh

眾裡尋他千百度



發文: 86
積分: 0
於 2004-09-02 13:45 user profilesend a private message to usersend email to Jill_Yehreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
hcl wrote:
想請教各位先進,小弟我依照上述方式改了設定,但是圖檔連結的檔名
是中文,那在TOMCAT5.0.19上面無法顯示,想請問一下該如何處理呢?
1
"中文檔名" />

環境:
WINDOWS 2000 SP4 + TOMCAT5.0.19 + IIS 5.0


若是搭配 IIS 的話, 不是 JAVA 的皆是由 IIS 來處理
所以你這個 基本上是透過 IIS 在傳遞
接下來就是中文的問題, IIS 理論上沒這個問題, 問題應該出在瀏覽器這端
若是你使用 IE, 則去選項設定那邊查看是否有開啟「永遠將 URL 傳送成 UTF8....」

這是我之前的經驗, 有錯誤請指正 Smile



作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
shirley_wang





發文: 3
積分: 0
於 2004-09-21 15:39 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
如果是普通的form,用ISO-8859-1讀取沒問題。可是當form的enctype是multipart/form-data的時候,再用ISO-8859-1讀取字符串就出現問題。

那位大蝦能幫忙解決一下?萬分感謝!

BR/Shirley



作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
rawhead





發文: 21
積分: 0
於 2005-01-15 21:24 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
debug="0"
acceptCount="100"
connectionTimeout="20000"
disableUploadTimeout="true"
port="80"
redirectPort="8443"
enableLookups="false"
minSpareThreads="25"
maxSpareThreads="75"
maxThreads="150"
maxPostSize="0"
URIEncoding="ISO-8859-1">


我的Tomcat預設的port是8080
那我是要在
port是8080的
直接加入URIEncoding="ISO-8859-1"就好
還是要把整段都寫進去?



作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
Sovina





發文: 3
積分: 0
於 2005-02-06 01:26 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問如果用URL來傳遞中文參數,應如何處理?

例如:
URL -->
因URL 不能傳遞中文, 所以我用了
1
java.net.URLEncoder.encode("正常","Big5")

來把中文ENCODE.
轉換后的URL -->


當我在另外一頁JSP 用
1
2
keyword = request.getParameter("keyword");
out.println(keyword);


所顯示的是亂碼. 如何解決?

平台:
winXP, Tomcat5.0.27



作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
EVAzero





發文: 46
積分: 0
於 2005-03-24 10:45 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
看了好幾篇文, 對中文參數傳遞時會變亂碼終於有點頭緒, 所以就來分享一下,
其實用是最簡單的解決方法, 建議初學者用這個先練習一下.
但就是要一個一個的改, 對於開發網站的不太好做, 所以分享一下我暫時的做法

os: Win2000 server + Tomcat 5.0.28 + JDK 1.4.2_07
*: 因為我都習慣用UTF-8, 所以檔案要是UTF-8格式(web.xml 和 server.xml 不用特意去改, 原本是怎樣就直接修改即可)
站台叫\mytest

參數傳遞可分為POST 和 GET

[POST]
用Filter, Tomcat 已經做了個例子, 直接拿來用,
在\jsp-examples\WEB-INF\classes\filters\SetCharacterEncodingFilter.class
copy 到自己的站台\mytest\WEB-INF\classes\filters\裡,(如果package有變更時需要修改.java檔再編譯為.class來使用)
修改mytest\WEB-INF\web.xml, 新增以下的code
1
2
3
4
5
6
7
8
9
10
11
12
    
        SetCharacterEncoding
        filters.SetCharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
      SetCharacterEncoding
      /*
    
Tips: 如果多個站台都想用這個方法, 可以把filters\SetCharacterEncodingFilter.class 複製到{tomcat}\shared\classes裡, 再修改{有需要用的站台}\WEB-INF\web.xml, 新增以上的code

[GET]
就是用本title第一篇精靈大大的方法, 修改{tomcat}\conf\server.xml,
找到
1
2
3
4
//看安裝tomcat時輸入那一個port
新增一句"URIEncoding="UTF-8"", 即
1
2
3
"UTF-8"
...略...照原來的, 不用動...
這樣, 用
的話, 直接接收就已經是中文了, 如果用link 時, 就要先把中文部分先編碼, 如
1
2
3
4
<%String str = "中文";
str = java.net.URLEncoder.encode(str,"UTF-8");
%>
"/mytest/test.jsp?str=<%=str%>"

直接接收即可

這樣, 基本上就解決了post和get的亂碼問題.

PS. 在修改web.xml 和 server.xml 後一定要記得重開Tomcat

[未解問題]
像shirley_wang 有提到, 用
1
"POST" enctype="multipart/form-data">
還是不行

[我有問題]
browser wrote:
不過當初我最後的做法是使用 Filter 的方式
Filter 的做法就是:先判斷是使用那種傳遞方式( GET or POST),假若是用 GET 的方式就採用第一種 code;若使用POST 方式,就採用第二種 code。

請問browser大大, 如何在Filter裡判斷GET 還是 POST 呢?

EVAzero edited on 2005-10-27 12:03

作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:EVAzero]
bmilk





發文: 32
積分: 1
於 2005-03-30 17:09 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
[我有問題]

請問browser大大, 如何在Filter裡判斷GET 還是 POST 呢?

request.getMethod()



作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:bmilk]
多多





發文: 11
積分: 0
於 2005-04-05 09:17 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
bmilk wrote:
[我有問題]

請問browser大大, 如何在Filter裡判斷GET 還是 POST 呢?

request.getMethod()


我沒弄錯的話…
getMethod()是HttpServletRequest這個類別的方法…

可是在Filter中的
doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

傳入的是ServletRequest這個類別耶…所以應該不行這樣子判斷才對…



作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
swpoker





發文: 63
積分: 0
於 2005-04-06 17:11 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這個我頁面是utf-8
1
String name = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"utf-8");


然後用form傳遞都可以

然後用url傳中文時
先用javascript轉成utf-8
然後都可以了



作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
valueyao





發文: 23
積分: 0
於 2005-04-13 10:04 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这位大哥,我照你的方法把快内添加了URIEncoding="ISO-8859-1,问题还是不能得到解决啊


作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
Nievor





發文: 3
積分: 0
於 2005-04-25 16:54 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我是個初學者,我很用心的把每個大大的回文都看了很幾遍,終於自己try出來了

使用post的方式的時候產生亂碼的問題,花了我很多時間 Sad

我把我的設定跟程式post出來給大家參考,希望遇到同樣狀況的版友可以快速解決

環境: WinXP SP1,Oracle 9i (9.0.1.1.1), Tomcat 5.0.16, JSP

設定步驟:
Step1: 將tomcat安裝目錄下的conf\server.xml 此檔案叫出來編輯
找出你的網頁server的port那裡 一般安裝好是8080 port
enableLookups="false" redirectPort="8443" acceptCount="100"
debug="0" connectionTimeout="20000" disableUploadTimeout="true"/>

URIEncoding="Big5" 是插進去的,編輯好後存檔即可

Step2:

在接收那一頁的jsp程式中加入
request.setCharacterEncoding("Big5");

然後在抓值的地方用UTF-8
statement.setAsciiStream(1, new ByteArrayInputStream(
      (request.getParameter("Name")).getBytes("UTF-8")),
      (request.getParameter("Name")).getBytes("UTF-8").length );

PS:我所有的頁面都是用Big5
PS:我問過公司很多IT人員,沒有一個會處理這個問題,我不是IT人員,但是因為自己的怨念,結果被我自己一個一個試,被我try出來
PS:為什麼這樣設定就可以顯示中文我實在是不解,我的程度還很淺,希望知道的大大指教

結語:
這樣的設定方式就可以使用Post的方式傳值寫入資料庫,而且都是顯示正確的中文,希望對遇到同樣問題的版眾有所幫助,這個網站的資料真是豐富,如果哪天我看的懂網站只要大約一半的內容,我想我就可以說我是java的高手了



作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
lin0_o





發文: 12
積分: 1
於 2005-05-06 16:05 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我解決的方法跟 EVAzero 描述的一樣
[POST 的情況]
加上 Filter 作處理
[GET 的情況]
1. 則在 {tomcat}\conf\server.xml 加上 URIEncoding="UTF-8"
2. 如果用 link 的話, 還要把中文部分先編碼
String str = java.net.URLEncoder.encode("中文","UTF-8");

這樣會變成還是要每次處理 GET 方式傳遞過來的數值, 所以我請教一下 browser 在這兩篇文章中提到的方式, 在 Filter 裡分 GET / POST 分別作處理, 請問一下是怎樣分的 ??
(這篇討論中 EVAzero 也提出了同樣問題, 我只知道答案不是 request.getMethod(); 後來就沒結果了)


[...略]
2. 一樣使用 Filter .. 不過此時必須要分為 GET / POST 做處理 .. 若為 Get 時,使用 getByte 方式轉碼;若為 Post 時,則使用 request.setCharacterEncoding("utf-8");
[略...]


[...略]
不過當初我最後的做法是使用 Filter 的方式
Filter 的做法就是:先判斷是使用那種傳遞方式( GET or POST),假若是用 GET 的方式就採用第一種 code;若使用POST 方式,就採用第二種 code。
[略...]

謝謝

========== 自言自語分隔線 =========


將上面這篇文章中的 doFilter 裡面 的 request 轉型就可以用了

1
2
3
4
5
6
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
if (httpServletRequest.getMethod().equals("POST")) {
        request.setCharacterEncoding("UTF-8");
} else if (httpServletRequest.getMethod().equals("GET")) {
        // 不過這邊要怎樣處理, 我還是不知道, 等我知道了我在補上吧.
}


lin0_o edited on 2005-05-08 11:17

作者 Re:解決 Tomcat 5.0.19 中文參數傳遞問題 [Re:精靈]
rueitsung





發文: 1
積分: 0
於 2005-06-20 15:10 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
之前我也遇上相同的問題,後來發現似乎linux的版本不同也有關係喔!
原本我使用的是Mandrake 10.0+jdk5.0+tomcat5.19+Mysql4.18
只要在每個jsp裡加上
1
request.setCharacterEncoding("UTF8");

就可正確顯示了。(我的輸入,輸出都統一使用UTF8)

可是後來使用CentOS4.0+jdk5.0+tomcat5.19+Mysql4.18
卻一直有中文亂碼的問題,後來才發現程式中字串一定要做轉碼的工作,才能夠解決中文亂碼的問題。這是我用Mandrake時,沒遇上的。
1
String name = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"utf-8");


似乎Mandrake 對中文支援相當不錯,或許有別的原因吧,小弟對linux學藝不精,就當小小心得,與大家分享

阅读(2273) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~