这里是我往购物车(cookies)中添加商品
public void addCartCookie() throws Exception {
System.out.println("添加到购物车");
Cookie[] cookies = request.getCookies();
if (cookies == null) {
Cookie cookie = new Cookie(id, "1");
response.addCookie(cookie);
} else {
int index = -1;
for (int j = 0; j < cookies.length; j++) {
if (cookies[j].getName().equals(id)) {
index = j;
System.out.println("index=" + index);
break;
}
}
if (index < 0) {
Cookie cookie = new Cookie(id, "1");
response.addCookie(cookie);
} else {
int count = Integer.parseInt(cookies[index].getValue()) + 1;
Cookie cookie = new Cookie(cookies[index].getName(), count + "");
response.addCookie(cookie);
}
}
}
但是我用ServletActionContext.getRequest().getCookies();获取不到上面自己创建的cookies,用获取出来只是JSESSIONID和D6C1CC448D1F91056B18136BCF1FE91B