日期:2014-05-19  浏览次数:20947 次

跪求高手解决,为啥不能创建cookies,不胜感激
为啥不能创建cookies啊,浏览器没问题,代码也没问题啊,而且代码执行两次
以下是运行结果:
nullcookies
sunshiwu66Fri Apr 06 11:37:09 CST 2012
1234Fri Apr 06 11:37:09 CST 2012
[Ljavax.servlet.http.Cookie;@1a6b16f创建Fri Apr 06 11:37:09 CST 2012
sunshiwu66Fri Apr 06 11:37:09 CST 2012
1234Fri Apr 06 11:37:09 CST 2012
[Ljavax.servlet.http.Cookie;@114a947创建Fri Apr 06 11:37:09 CST 2012
以下是代码:
package com.supershopping.sunshiwu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;


import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.supershopping.sunshiwu.daoImpl.UserOperateImpl;
import com.supershopping.sunshiwu.po.Userinfo;

public class AutoLogin extends HttpServlet {


/**

*/
private static final long serialVersionUID = 1L;


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);

}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
Userinfo ui=new Userinfo();
UserOperateImpl io=new UserOperateImpl();
String username=request.getParameter("username");
String userpass=request.getParameter("userpass");
System.out.println(username+new Date());
System.out.println(userpass+new Date());
if(!username.equals(null)&&!"".equals(username)){
if(!userpass.equals(null)&&!"".equals(userpass)){
ui.setUsername(username);
ui.setUserpass(userpass);
if(io.queryconfirm(ui)){
int seconds=24*60*60;
  Cookie cookie = new Cookie("user", username+"=="+userpass);
  cookie.setMaxAge(seconds);  
  response.addCookie(cookie);
  Cookie[] cookies = request.getCookies();
  System.out.println(cookies+"创建"+new Date());
  request.getSession().setAttribute("user", username);
  request.getRequestDispatcher("Toindex").forward(request, response);
}
}
}
out.flush();
out.close();
}

}


------解决方案--------------------
你是怎么判断Cookie没添加成功的?

另外Cookie的值,不要有“=”符号,因为Cookie在传输时的格式就是:
Cookie: name=value; name=value
------解决方案--------------------
如果你确定以上建不了,就当我帮顶了!!!(我估计是你没找到,还是不知道Cookie怎么判断添加成功)