1.思维导图

2.代码部分
Cookie基本使用
Cookie[] cookies = request.getCookies();
Cookie cookie = null;
for(Cookie sonCookie : cookies){
if("msg".equals(sonCookie.getName())){
cookie = sonCookie;
}
}
if(null != cookie){
System.out.println("name : "+msgCookie.getName() + " , value : "+ msgCookie.getValue());
}
Cookie案例:记录上一次访问时间
//判断是否是一次请求
Cookie[] cookies = request.getCookies();
Cookie cookie = null;//记录上一次的访问时间
if (cookies != null && cookies.length != 0 ) {
for (Cookie sonCookie : cookies) {
if ("lastTime".equals(sonCookie.getName())) {
cookie = sonCookie;
}
}
}
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
if (null == cookie) {
//第一次访问 ,打印当前时间,并将创建Cookie对象,存储当前时间
Date currentDate = new Date();
System.out.println("第一次访问,时间为" + format.format(currentDate));
cookie = new Cookie("lastTime",currentDate.getTime()+"");
} else {
//不是第一次访问,从cookie取出上一次的访问时间,并打印。获取当前时间,并存储cookie对象中
long lastDateMills = Long.parseLong(cookie.getValue());
Date lastDate = new Date(lastDateMills);
//获取到了上一次的访问时间
String lastTimeStr = format.format(lastDate);
System.out.println("上一次访问,时间为" + lastTimeStr);
//获取当前时间,并存储cookie对象中
Date currentDate = new Date();
cookie = new Cookie("lastTime",currentDate.getTime()+"");
}
response.addCookie(cookie);
Cookie案例:商品浏览记录
<a href="/day56/history?id=0">西游记</a><br>
<a href="/day56/history?id=1">红楼梦</a><br>
<a href="/day56/history?id=2">水浒传</a><br>
<a href="/day56/history?id=3">三国志</a><br>
String id = request.getParameter("id");
Cookie cookie = null;
Cookie[] cookies = request.getCookies();
if (null != cookies && 0 != cookies.length){
for (Cookie sonCookie : cookies) {
if ("history".equals(sonCookie.getName())) {
cookie = sonCookie;
}
}
}
if (null == cookie) {
//之前没有任何浏览记录 ,创建Cookie对象 ,并存储浏览记录(id)
cookie = new Cookie("history",id);
} else {
//之前有浏览记录
String historyStr = cookie.getValue();
if (!historyStr.contains(id)) {
//有记录,但是不包含当前浏览的商品;
//将浏览商品拼接到已有浏览记录中
//120
//1-2-0
historyStr += "-"+id;
cookie.setValue(historyStr);
} else {
//有记录,包含当前浏览的商品 ,不做任何处理
}
}
response.addCookie(cookie);
//显示商品浏览记录
response.sendRedirect(request.getContextPath()+ File.separator+"showHistory");
Cookie cookie = null;
Cookie[] cookies = request.getCookies();
if (null != cookies && 0 != cookies.length) {
for (Cookie sonCookie : cookies) {
if ("history".equals(sonCookie.getName())) {
cookie = sonCookie;
}
}
}
StringBuffer responseContent = new StringBuffer();//记录响应正文
if (null == cookie) {
//没有浏览记录
responseContent.append("<font color='red'>没有浏览记录</font>,");
responseContent.append("<a href='books.html'>浏览商品</a>");
} else {
//有浏览记录
//获取浏览记录 0-1-2-3
String[] bookNames = {"西游记","红楼梦","水浒传","三国志"};
String historyStr = cookie.getValue();
String[] historys = historyStr.split("-");
responseContent.append("您的浏览记录如下:<br>");
for (String history : historys) {
//history : 0 / 1 / 2 /3
String bookName = bookNames[Integer.parseInt(history)];
responseContent.append(bookName+"<br>");
}
}
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(responseContent.toString());
CookieUtils工具类
public static Cookie getCookie(Cookie[] cookies ,String cookieName){
if (null != cookies && 0 != cookies.length) {
for (Cookie sonCookie : cookies) {
if (cookieName.equals(sonCookie.getName())) {
return sonCookie;
}
}
}
return null;
}