java中cookie存中文的问题!

cjdxhc 2009-06-01 05:28:09
登录页面
1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
<head>
<title>
登陆系统
</title>
</head>
<body>
<center>
<b>欢迎登录</b>
<form name="form1" action="Dso.jsp" method="post">
姓名:<input name="un" type="text" size="15"><br>
班级:<input name="uc" type="text" size="15"><br>
<input type="submit" value="登录">
<input type="reset" value="全部重写">
</form>
</center>
</body>
</html>


Dso.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

<html>
<body>
<%
request.setCharacterEncoding("gb2312");
String username;String userclass;
int maxtime=30*600;
username=request.getParameter("un");
userclass=request.getParameter("uc");

Cookie co=new Cookie("aa", username);
co.setMaxAge(maxtime);
response.addCookie(co);
Cookie coo=new Cookie("bb",userclass);
coo.setMaxAge(maxtime);
response.addCookie(coo);
request.getRequestDispatcher("2.jsp").forward(request,response);
%>
</body>
</html>


2.jsp

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<html>
<head>
<title>
成功的登录系统
</title>
</head>
<body>
<%
Cookie cookies[]=request.getCookies();
Cookie sCookie=null;
String stname="";
String stnvalue="";

if(cookies==null) out.print("none any cookie.");
if(cookies.length==0)
out.println("do not support");
else{
for(int i=0;i<cookies.length;i++){
sCookie=cookies[i];
stname=sCookie.getName();
if(stname.equals("aa") || stname.equals("bb"))
{stnvalue=sCookie.getValue();
out.println(stname+"->"+stnvalue+"<br>");}

}
}
%>
</body>
</html>


跳到Dso.jsp报这个错误如何解决
org.apache.jasper.JasperException: An exception occurred processing JSP page /Dso.jsp at line 15

13: Cookie co=new Cookie("aa", username);
14: co.setMaxAge(maxtime);
15: response.addCookie(co);

java.lang.IllegalArgumentException: Control character in cookie value, consider BASE64 encoding your value

就算跳到2.jsp
显示出来的也是乱码
如何显示正常?
网上说URLDecoder.decode()用这个!但是我试下还是不行?


...全文
795 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuguanqun 2009-06-02
  • 打赏
  • 举报
回复
存 java.net.URLEncoder.encode()

取 java.net.URLDecoder.decode()

少龙123 2009-06-02
  • 打赏
  • 举报
回复
帮顶
cjdxhc 2009-06-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 qiheia 的回复:]
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
[/Quote]

这样做无效!
ldg_2 2009-06-01
  • 打赏
  • 举报
回复
cookie中存中文需要转码
qiheia 2009-06-01
  • 打赏
  • 举报
回复
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
cjdxhc 2009-06-01
  • 打赏
  • 举报
回复
请高手帮忙解答下啊!

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧