关于cookie的问题

yoxibaga 2013-01-18 10:19:52
	public void doGet(HttpServletRequest req, HttpServletResponse res){

//req用于获取客户端(浏览器)的信息
//res用于向客户端(浏览器)返回信息

res.setContentType("text/html; charset=gbk");

Connection ct = null;
PreparedStatement sm = null;
ResultSet rs = null;

String userName = "";
String passwd = "";

try {

HttpSession hs = req.getSession(true);

String name = (String)hs.getAttribute("pass");
PrintWriter pw = res.getWriter();

if(name == null){

//如果session中不存在此用户,在查看cookie中是否存在此记录

//获取客户端的cookies
Cookie[] cookies = req.getCookies();
pw.println(cookies.length);
//遍历cookies,查找此记录
if(cookies != null){
for(int i = 0; i < cookies.length; i++){
Cookie temp = cookies[i];
pw.println(temp.getName());//结果为JSESSIONID
pw.println(temp.getValue());//结果为145F3F272C677BE92BA0A87B1743249F

if(temp.getName().equals("user")){
userName = temp.getValue();
pw.println("userName="+userName);
}
else if(temp.getName().equals("password")){
passwd = temp.getValue();
pw.println("passwd="+passwd);
}
}//end for
pw.println(userName + " " +passwd + "hello");
if(!userName.equals("") && !passwd.equals("")){

//为了防止无效cookie,跳转到loginCl,到数据库验证
res.sendRedirect("logincl?username="+userName +"&"+"passwd="+passwd);
return ;
}
}
else{
res.sendRedirect("login");
return ;
}
}
}

这段程序是为了获取客户端的cookie。在C:\Users\LW\AppData\Roaming\Microsoft\Windows\Cookies文件夹里有我
保存的一个cookies.但是,不知为何读取的时候,cookie的长度为1,而且其name=JSESSIONID,value=145F3F272C677BE92BA0A87B1743249F;
这打印出来的好像是session的id。不知为何??求大神看看。
...全文
102 回复 打赏 收藏 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,620

社区成员

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

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