JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Servlet/JSP 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Cookie 的中文問題 [精華]
enriqueliu





發文: 5
積分: 1
於 2003-06-09 21:41 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
小弟目前正在努力學jsp,但是在做cookie溝通時,程式如下
會有錯誤產生,似乎是compiler就沒過了,不是錯在中文的處理上
但百思不解,錯在哪裡,請問各位版友,有沒有解法,感激不盡~~~~~~
Cookie user1 = new Cookie("id1","中文");
Cookie user2 = new Cookie("id2","中文");
response.addCookie(user1);
response.addCookie(user2);

...

Cookie[] allcookie = request.getCookies(); //取得所有的 Cookie
for(int i=0;i<allcookie.length;i++)
{
String idx = allcookie[i].getName(); //取得 Cookie 的索引值
String value = allcookie[i].getValue(); //取得 Cookie 的內容
out.print("<tr><td bgcolor=yellow>");
out.print("<tr><td bgcolor=yellow>");
out.print(idx+"</td><td>");
out.print(value+"</td></tr>");
}
...


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
browser

戀香

版主

發文: 3570
積分: 1
於 2003-06-10 09:27 user profilesend a private message to usersend email to browserreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
enriqueliu wrote:
小弟目前正在努力學jsp,但是在做cookie溝通時,程式如下
會有錯誤產生,似乎是compiler就沒過了,不是錯在中文的處理上
但百思不解,錯在哪裡,請問各位版友,有沒有解法,感激不盡~~~~~~
Cookie user1 = new Cookie("id1","中文");
Cookie user2 = new Cookie("id2","中文");
response.addCookie(user1);
response.addCookie(user2);

...

Cookie[] allcookie = request.getCookies(); //取得所有的 Cookie
for(int i=0;i<allcookie.length;i++)
{
String idx = allcookie[i].getName(); //取得 Cookie 的索引值
String value = allcookie[i].getValue(); //取得 Cookie 的內容
out.print("<tr><td bgcolor=yellow>");
out.print("<tr><td bgcolor=yellow>");
out.print(idx+"</td><td>");
out.print(value+"</td></tr>");
}
...


沒有 error message ..
實在很難看出 .. 那裡有問題 ..


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
enriqueliu





發文: 5
積分: 1
於 2003-06-10 16:49 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
error message .. 如下

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: ?B?p?q
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:254)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:295)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
....... 略
 
root cause

java.lang.IllegalArgumentException: ?B?p?q
  at org.apache.tomcat.util.http.ServerCookie.maybeQuote(ServerCookie.java:315)
  at org.apache.tomcat.util.http.ServerCookie.appendCookieValue(ServerCookie.java:248)
....... 略


browser edited on 2003-07-20 13:19
reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
browser

戀香

版主

發文: 3570
積分: 1
於 2003-06-10 16:58 user profilesend a private message to usersend email to browserreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
看樣子是cookie設為中文的問題
你先試試把中文改為設英文
如果沒有 .. 就先暫時用英文
如果一定要用中文
就再另尋解法 ....


reply to postreply to post
作者 試試看用urlEncode方法 [Re:enriqueliu]
vip88





發文: 77
積分: 1
於 2003-06-10 23:04 user profilesend a private message to usersend email to vip88reply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
試試看urlEncode與urlDecode的方法.

reply to postreply to post
歡迎光臨神奇力量網
http://www.688.idv.tw

歡迎加入神奇力量論壇
http://www.688.idv.tw/forum
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
enriqueliu





發文: 5
積分: 1
於 2003-06-11 16:12 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
可否詳細說明urlEncode與urlDecode的方法
我是初學者............


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
ㄚ孝





發文: 81
積分: 0
於 2003-06-11 16:39 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我看得一頭霧水@@"

小弟一般都習慣使用session,不知道session會不會有一樣的問題 ...


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
enriqueliu





發文: 5
積分: 1
於 2003-06-11 20:58 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
將...
Cookie user1 = new Cookie("id1","中文");
Cookie user2 = new Cookie("id2","中文");
response.addCookie(user1);
response.addCookie(user2);
改為下列的話,不會有問題
Cookie user1 = new Cookie("id1","AAA");
Cookie user2 = new Cookie("id2","BBB");
response.addCookie(user1);
response.addCookie(user2);
如browser 兄所言,應該是cookie設為中文的問題
但要如何使cookie也能設中文,希望高手指點......thx


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
alanting

Alan Ting



發文: 200
積分: 3
於 2003-06-12 17:59 user profilesend a private message to usersend email to alantingreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你試試編碼吧,把你的中文字編號iso碼...
String x = "中文";
x = new String(x.getBytes(),"iso-8859-1");
//iso-8859-1是中文的iso碼


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
enriqueliu





發文: 5
積分: 1
於 2003-06-16 13:44 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我似乎找到答案了~~~
以下是轉貼自JAVA API Documentation 說法.........
Cookie
public Cookie(java.lang.String name,
java.lang.String value)Constructs a cookie with a specified name and value.
The name must conform to RFC 2109. That means it can contain only ASCII alphanumeric characters and cannot contain commas, semicolons, or white space or begin with a $ character. The cookie's name cannot be changed after creation.

The value can be anything the server chooses to send. Its value is probably of interest only to the server. The cookie's value can be changed after creation with the setValue method.

By default, cookies are created according to the Netscape cookie specification. The version can be changed with the setVersion method.

....
原來Cookie物件根據RFC 2109規定只能包含ASCII code,其他的不行
有興趣請到sun網站去查...
共勉之~~~


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
browser

戀香

版主

發文: 3570
積分: 1
於 2003-06-16 13:51 user profilesend a private message to usersend email to browserreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
enriqueliu wrote:
我似乎找到答案了~~~
以下是轉貼自JAVA API Documentation 說法.........
Cookie
public Cookie(java.lang.String name,
java.lang.String value)Constructs a cookie with a specified name and value.
The name must conform to RFC 2109. That means it can contain only ASCII alphanumeric characters and cannot contain commas, semicolons, or white space or begin with a $ character. The cookie's name cannot be changed after creation.

The value can be anything the server chooses to send. Its value is probably of interest only to the server. The cookie's value can be changed after creation with the setValue method.

By default, cookies are created according to the Netscape cookie specification. The version can be changed with the setVersion method.

....
原來Cookie物件根據RFC 2109規定只能包含ASCII code,其他的不行
有興趣請到sun網站去查...
共勉之~~~


GOOD .... 加入精華區 .... 並且加一分 ....


reply to postreply to post
作者 踢館:Cookie 的中文問題 [Re:browser]
vip88





發文: 77
積分: 1
於 2003-06-16 15:45 user profilesend a private message to usersend email to vip88reply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
哈哈,看樣子不把我的看家本領拿出來不可了。規格上不可以,難道就沒有
變通的方式嗎?當然有的,別忘了URLEncoder與URLDecoder是作啥用的。

廢話少說,以下面實例證明:

先建立一個cookie_tw.jsp,內容如下:

<%@ page import="java.net.*" contentType="text/html;charset=BIG5"%>
<%
Cookie cookie = new Cookie(URLEncoder.encode("中文"),
URLEncoder.encode("許攻績蓋"));

response.addCookie(cookie);
%>

再來建立讀取cookie的檔案叫做rcookie_tw.jsp:

<%@ page import="java.net.*" contentType="text/html;charset=BIG5"%>
<%
Cookie rcookie[] = request.getCookies();
if (rcookie == null)
out.print("讀不到cookie");
else
{
for (int i = 0; i < rcookie.length; i++)
{
if (URLDecoder.decode(rcookie[i].getName()).equals("中文"))
{
out.print(URLDecoder.decode(rcookie[i].getName()) +
URLDecoder.decode(rcookie[i].getValue()));
}
}
}
%>

啟動相關的Appication Server,先執行 cookie_tw.jsp,然後再執行
rcookie_tw.jsp。大家一定可以看到中文字正確顯示在瀏覽器上。


reply to postreply to post
歡迎光臨神奇力量網
http://www.688.idv.tw

歡迎加入神奇力量論壇
http://www.688.idv.tw/forum
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
mousetail





發文: 142
積分: 1
於 2003-07-03 15:57 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問 cookie 要存中文是不是只能用這個方法呢?
因為我在 tomact4 (localhost)這個方法可行
可是在 iPlanet6 (server) 這個方法就無效了
因為在iPlanet encode 的結果變成
=%3F%3F%3F%3F%3F%3F%3F%3F%2C%3F%3F%3F%3F%3F%3F%2C%3F%3F%3F%3F%7C
decode回來就是 ????????,??????,????
兩個環境用的是同一個字串
要在請教各位前輩了

謝謝


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:mousetail]
JiaYun



版主

發文: 761
積分: 9
於 2003-07-04 08:07 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
mousetail wrote:
請問 cookie 要存中文是不是只能用這個方法呢?
因為我在 tomact4 (localhost)這個方法可行
可是在 iPlanet6 (server) 這個方法就無效了
因為在iPlanet encode 的結果變成
=%3F%3F%3F%3F%3F%3F%3F%3F%2C%3F%3F%3F%3F%3F%3F%2C%3F%3F%3F%3F%7C
decode回來就是 ????????,??????,????
兩個環境用的是同一個字串
要在請教各位前輩了


你在 iPlanet6 上的 JSP 也有設定 contentType="text/html;charset=BIG5" 嗎

不設連 tomcat 也會有你說的問題


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
mousetail





發文: 142
積分: 1
於 2003-07-04 13:07 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
您是說 jsp的最前頭嗎 有啊 有設
其他的字都是中文,就是cookie是亂碼
後來我發現,這台iPlanet不用去encode就能存中文
甚至連()等怪符號都能存!!
這似乎和sun的文件不相符....

謝謝


reply to postreply to post
作者 Re:踢館:Cookie 的中文問題 [Re:vip88]
rainman





發文: 67
積分: 1
於 2003-07-04 14:45 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
用Base64编码也是一个好办法。存进Cookie前编码,取出后解码。URLEncoding也不错。

reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:mousetail]
jini

SoftLeader Taiwan

版主

發文: 1266
積分: 23
於 2003-07-04 17:29 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
mousetail wrote:
您是說 jsp的最前頭嗎 有啊 有設
其他的字都是中文,就是cookie是亂碼
後來我發現,這台iPlanet不用去encode就能存中文
甚至連()等怪符號都能存!!
這似乎和sun的文件不相符....

謝謝


可否詳細說是 iPlanet 的哪一個版本
如果是 iPlanet 6 應該是可以的
因為它支援了 中文網址
Netscape 4 應該就是不行不論是網址或者中文cookie

相對的 處理中文應該有一定的編碼原則
雖然我不太清楚他的處理模式
不過應該差不多是這原則.....


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
mousetail





發文: 142
積分: 1
於 2003-07-07 08:07 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
沒錯 就是iPlanet6的版本
後來索性不編碼 讓他直接存中文就可以了
謝謝


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
chctony





發文: 64
積分: 0
於 2004-12-03 15:40 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
為何上面request.getCookies();第一次沒有Cookies,但在未寫入Cookies的情況下,重新整理,Cookies的length變成1?

reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:chctony]
frank



版主

發文: 498
積分: 13
於 2004-12-04 19:32 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
chctony wrote:
在未寫入Cookies的情況下,重新整理,Cookies的length變成1?

先透過 Cookie 的 getName() 看看是什麼東西。

有可能是 server 對 client 寫入一些作為 session tracking 的 cookie


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
chctony





發文: 64
積分: 0
於 2004-12-05 14:38 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
getName() :JSESSIONID
getValue() :BFB43D019EA3002D5A131D7AE15BDB17
但問題又來了,我是寫會員登入網頁的功能,在log.jsp登入後,加入cookie,並redirect到其B網頁,在B網頁request.getCookies()後,欲抓不出來,但在log.jsp當場寫當場抓就可以?


reply to postreply to post
作者 Re:踢館:Cookie 的中文問題 [Re:vip88]
chctony





發文: 64
積分: 0
於 2004-12-05 14:45 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
找到了第一個問題:http://www.javaworld.com.tw/jute/post/view?bid=6&id=52449&tpg=1&ppg=1&sty=1&age=0#52449
但第二個還沒找到?


reply to postreply to post
作者 Re:踢館:Cookie 的中文問題 [Re:chctony]
jini

SoftLeader Taiwan

版主

發文: 1266
積分: 23
於 2004-12-05 15:28 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
真不明白你的標題 " Re:踢館:Cookie 的中文問題 "

是想要證明什麼

加上問題, 更是搞不懂你想表達什麼


reply to postreply to post
作者 Re:踢館:Cookie 的中文問題 [Re:vip88]
ETERNAL





發文: 1
積分: 0
於 2005-03-09 10:26 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
vip88 wrote:
哈哈,看樣子不把我的看家本領拿出來不可了。規格上不可以,難道就沒有
變通的方式嗎?當然有的,別忘了URLEncoder與URLDecoder是作啥用的。

廢話少說,以下面實例證明:

先建立一個cookie_tw.jsp,內容如下:

<%@ page import="java.net.*" contentType="text/html;charset=BIG5"%>
<%
Cookie cookie = new Cookie(URLEncoder.encode("中文"),
URLEncoder.encode("許攻績蓋"));

response.addCookie(cookie);
%>

再來建立讀取cookie的檔案叫做rcookie_tw.jsp:

<%@ page import="java.net.*" contentType="text/html;charset=BIG5"%>
<%
Cookie rcookie[] = request.getCookies();
if (rcookie == null)
out.print("讀不到cookie");
else
{
for (int i = 0; i < rcookie.length; i++)
{
if (URLDecoder.decode(rcookie[i].getName()).equals("中文"))
{
out.print(URLDecoder.decode(rcookie[i].getName()) +
URLDecoder.decode(rcookie[i].getValue()));
}
}
}
%>

啟動相關的Appication Server,先執行 cookie_tw.jsp,然後再執行
rcookie_tw.jsp。大家一定可以看到中文字正確顯示在瀏覽器上。


經過測試後
你提供的URLEncoder與URLDecoder方法,只有在(NT+ressin)的平台上有效
但是在(linux+ressin)一樣會變成??????符號 Dead


reply to postreply to post
作者 Re:Cookie 的中文問題 [Re:enriqueliu]
ray_linn

什么都不懂的小白

版主

發文: 540
積分: 10
於 2005-03-09 11:26 user profilesend a private message to usersend email to ray_linnreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Base64的例子:好处是不单中文,包括图片也可以被encode和decode然后保存在XML中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%
Cookie cookies[] = request.getCookies();
if(cookies!=null)
{
   for(int i=0;i<cookies.length;i++)
  {
     if((cookies[i].getName()).equals("id"))
      {
      String decoded=(String) Base64.decodeToObject(cookies[i].getValue());
      out.println(decoded);
      }
   }
}
else
{
    out.println("No Cookies");
}
%>


1
2
3
4
5
6
<%
   String original="中文";
   String encoded=Base64.encodeObject(original);
   Cookie cookie = new Cookie("id",encoded);
   response.addCookie(cookie);
%>


用到的Base64 API到这里下载:
http://iharder.sourceforge.net/base64/


reply to postreply to post
飞翔的候鸟
go to first page go to previous page  1   2  go to next page go to last page
» JWorld@TW »  Servlet/JSP 討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8