cookie中文问题! 真郁闷!!
<%
String myvalue= "彩虹 ";
URLEncoder.encode(myvalue, "GBK ");
Cookie mycookie=new Cookie( "user ",myvalue);
mycookie.setMaxAge(30*60);
response.addCookie(mycookie);
out.println( "Cookie 已经添加完毕 ");
%>
源码这样子.jsp, 读取出来的是这样子:
name user,
value iy
name JSESSIONID,
value 955608A832D2BAC71FEC8255F36717FC
请问这是为什么呢?
我读取的时候有进行解码.
<%
Cookie cookies[]=request.getCookies();
Cookie scookie=null;
String svalue=null;
String sname=null;
for(int i=0;i <cookies.length;i++)
{
scookie=cookies[i];
sname=scookie.getName();
svalue=URLDecoder.decode(scookie.getValue());
out.println( "name "+sname+ ", <br> "+ "value "+svalue+ " <br> ");
}
%>
------解决方案--------------------关注
------解决方案--------------------同上
------解决方案--------------------使用
String value = java.net.URLEncoder.encode(中文);
Cookie cookie = new Cookie( "key ",value);
response.addCookie(cookie);
然后在jsp页面中使用:
<%
for(int i = 0 ;i < cookies.length ; i ++)
{
temp = cookies[i];
if(temp.getName().equals( "key "))
out.print(java.net.URLDecoder.decode(temp.getValue()));
}
%>
关键在于使用 java.net.URLEncoder.encode(中文);的字符传递,使用: java.net.URLDecoder.decode(转换后的字符);
--------------------
J2EE技术交流QQ群:6890959
欢迎大家加入..
一起切磋交流..
共同进步..
群空间好东西很多..
------解决方案--------------------我以前遇到类似的问题,当时好像就是加了一个import 语句,好像就是类似于java.net.URLEncoder.encode(中文);这样的你可以试试看
------解决方案-------------------- <%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.net.* "%>
<html>
<head> </head>
<body>
<%
String myvalue = "彩虹 ";
Cookie mycookie = new Cookie( "user ", URLEncoder.encode(myvalue, "UTF-8 "));
mycookie.setMaxAge(30 * 60);
response.addCookie(mycookie);
out.println( "Cookie 已经添加完毕 ");
%>
<%
Cookie cookies[] = request.getCookies();
Cookie scookie = null;
String svalue = null;
String sname = null;
if(cookies!=null){
for (int i = 0; i < cookies.length; i++) {
scookie = cookies[i];
sname = scookie.getName();
svalue = scookie.getValue();
out.println( "name " + sname + ", <br/> " + "value " + URLDecoder.decode(svalue, "UTF-8 ")
+ " <br/> ");
}
}
%>
</body>
</html>
------解决方案--------------------up