Cookie案例-显示商品浏览历史纪录

时间:2023-03-09 03:54:45
Cookie案例-显示商品浏览历史纪录
 package cn.itcast.cookie;

 import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; 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 cn.itcast.Book;
import cn.itcast.Db; public class CookieDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter(); // 1.输出网站的商品
out.write("本网站有如下商品:<br/>");
Db db = new Db();
Map<String, Book> map = db.getAll(); for (Map.Entry<String, Book> entry : map.entrySet()) {
Book book = entry.getValue();
out.print("<a href='/ServletDemo/servlet/CookieDemo2?id="
+ book.getId() + "'target='_blank'>" + book.getName()
+ "</a><br/>");
} // 2.显示用户曾经看过的商品
out.print("<br/>您曾经看过如下商品:<br/>");
Cookie cookies[] = request.getCookies();
for (int i = 0; cookies != null && i < cookies.length; i++) {
if (cookies[i].getName().equals("bookHistory")) {
String ids[] = cookies[i].getValue().split("\\,");
for(String id:ids){
Book book = (Book) Db.getAll().get(id);
out.print(book.getName()+"<br/>");
}
}
} } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } }
 package cn.itcast.cookie;

 import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List; 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 cn.itcast.Book;
import cn.itcast.Db; public class CookieDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter(); // 1.给据用户带来的id,显示相应商品的详细信息。
String id = request.getParameter("id");
Book book = (Book) Db.getAll().get(id);
out.write(book.getId() + "<br/>");
out.write(book.getName() + "<br/>");
out.write(book.getAuthor() + "<br/>");
out.write(book.getDescription() + "<br/>"); // 2.构建cookie,会写给浏览器
String cookieValue = buildCookie(id, request);
Cookie cookie = new Cookie("bookHistory", cookieValue);
cookie.setMaxAge(1 * 30 * 24 * 60 * 60);
cookie.setPath("/ServletDemo");
response.addCookie(cookie);
} private String buildCookie(String id, HttpServletRequest request) { String bookHistory = null;
Cookie cookies[] = request.getCookies();
for (int i = 0; cookies != null && i < cookies.length; i++) {
if (cookies[i].getName().equals("bookHistory")) {
bookHistory = cookies[i].getValue();
} } if (bookHistory == null) {
return id;
} LinkedList<String> list = new LinkedList(Arrays
.asList(bookHistory.split("\\,"))); if (list.contains(id)) {
list.remove(id);
} else {
if (list.size() >= 3) {
list.removeLast();
}
} list.addFirst(id); StringBuffer sb = new StringBuffer();
for (String bid : list) {
sb.append(bid+",");
} return sb.deleteCharAt(sb.length()-1).toString();
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } }
 package cn.itcast;

 import java.util.LinkedHashMap;
import java.util.Map; public class Db {
private static Map<String, Book> map = new LinkedHashMap(); static{
map.put("1", new Book("1","javaweb开发","Zero","一本好书!!"));
map.put("2", new Book("2","jdbc开发","one","一本好书!!"));
map.put("3", new Book("3","spring开发","two","一本好书!!"));
map.put("4", new Book("4","struks开发","three","一本好书!!"));
map.put("5", new Book("5","hibernate开发","four","一本好书!!"));
} public static Map getAll(){
return map;
}
}
 package cn.itcast;

 public class Book {
private String id;
private String name;
private String author;
private String description; public Book(String id, String name, String author, String description) {
super();
this.id = id;
this.name = name;
this.author = author;
this.description = description;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAuthor() {
return author;
} public void setAuthor(String author) {
this.author = author;
} public String getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
}
}