假设有一个数据库和书本对象
//数据库
class Db{
private static Map<String, Book> map = new LinkedHashMap();
static{
map.put("1", new Book("1","web开发","老张"));
map.put("2", new Book("2","spring","老毕"));
map.put("3", new Book("3","struts","老张"));
map.put("4", new Book("4","jdbc","老黎"));
}
//拿到书
public static Map getAll(){
return map;
}
}
//书对象
class Book{
private String id;
private String name;
private String author;
public Book() {
super();
}
public Book(String id, String name, String author) {
super();
this.id = id;
this.name = name;
this.author = author;
}
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;
}
}
第一个servlet显示商品列表和历史记录
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置防止乱码
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//显示商品信息
PrintWriter out = response.getWriter();
out.write("有以下商品信息:<br>");
Map<String, Book> map = Db.getAll();
for(Map.Entry<String, Book> entry : map.entrySet()){
Book book = entry.getValue();
out.print("<a href='/day07/CookieDemo3?id="+book.getId()+"'target='_blank'> "+book.getName()+"</a><br>");
}
//显示用户曾经看过什么商品
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("\\,"); //2,2,3
for(String id : ids){
Book book = (Book) Db.getAll().get(id);
out.print(book.getName()+"<br>");
}
}
}
}
第二个servlet显示商品具体信息,存储cookie
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置防止乱码
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//显示商品信息
PrintWriter out = response.getWriter();
//根据用户带来的id显示商品信息
String id = request.getParameter("id");
Book book = (Book) Db.getAll().get(id);
out.print(book.getId()+"<br>");
out.print(book.getName()+"<br>");
out.print(book.getAuthor()+"<br>");
//构建Cookie回写给浏览器
String cookieValue = buildCookie(id,request);
Cookie cookie = new Cookie("bookHistory", cookieValue);
cookie.setPath("/day07");
cookie.setMaxAge(3600*24);
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();
}
}
/*分情况谈论
* old id now
* null 1 1
* 2,3 1 1,2,3
* 1 1 1
* ...
*/
if(bookHistory==null){
return id;
}
//LinkedList便于增删改查
LinkedList list = new LinkedList(Arrays.asList(bookHistory.split("\\,")));
if(list.contains(id)){
list.remove(id);
list.addFirst(id);
}else{
if(list.size()>=3){
list.removeLast();
list.addFirst(id);
}else{
list.addFirst(id);
}
}
StringBuilder sb = new StringBuilder();
for(Object ob : list){
sb.append(ob+",");
}
return sb.deleteCharAt(sb.length()-1).toString();
}