显示商品的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<a href="/Servlet/cookieDemo?bookId=1">JavaWeb从入门到精通</a><br/><br/>
<a href="/Servlet/cookieDemo?bookId=2">21天学会JavaWeb项目开发</a><br/><br/>
<a href="/Servlet/cookieDemo?bookId=3">轻量级JavaEE与企业应用</a><br/><br/>
<a href="/Servlet/cookieDemo?bookId=4">搞定JavaEE</a><br/><br/>
<h3>你浏览过的商品:</h3>
<c:forEach items="${viewHistory }" var="book">
${book}<br/><br/>
</c:forEach>
</body>
</html>
后台处理servlet文件(CookieSevlet.java)
package com.servlet.cookie;
import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CookieServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取浏览历史的cookie值
Cookie[] cookies = request.getCookies();
String history = null;
for(int i = 0; cookies != null && i < cookies.length; i++){
if(cookies[i].getName().equals("viewHistory")){
history = cookies[i].getValue();
break;
}
}
String item = request.getParameter("bookId");
String cookievalue = buildCookie(history, item);
//给客户端回写新的cookie值
Cookie cookie = new Cookie("viewHistory", cookievalue);
cookie.setMaxAge(60*60*10);
response.addCookie(cookie);
//页面跳转
response.setCharacterEncoding("UTF-8");
List<String> historyList = buildBookList(cookievalue);
request.setAttribute("viewHistory", historyList);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
public String buildCookie(String history, String item){
/*
* 新建cookie:
* 1、判断商品是否已存在
* 2、如果已存在,将旧的删去,将新的添加到第一个位置
* 3、如果不存在,判断如果将商品添加进去是否超过最大值
* 4、如果没有超过,直接将商品添加到第一个位置
* 4、如果超过最大值,删去最后的一个商品,然后将新商品添加到第一个位置
*/
if(history == null){
return item;
}
LinkedList<String> list = new LinkedList<String>(Arrays.asList(history.split(",")));
if(list.contains(item)){
list.remove(item);
list.addFirst(item);
}else{
if(list.size() >= 3){
list.removeLast();
list.addFirst(item);
}else{
list.addFirst(item);
}
}
StringBuffer sb = new StringBuffer();
for(String id: list){
sb.append(id + ",");
}
return sb.deleteCharAt(sb.length()-1).toString();
}
//根据id构造浏览历史的商品名称
public List<String> buildBookList(String bookStr){
Map<String, String> books = new HashMap<String, String>();
books.put("1", "JavaWeb从入门到精通");
books.put("2", "21天学会JavaWeb项目开发");
books.put("3", "轻量级JavaEE与企业应用");
books.put("4", "搞定JavaEE");
List<String> list = new ArrayList<String>(Arrays.asList(bookStr.split(",")));
List<String> bookList = new ArrayList<String>();
for(int i = 0; list != null&& i < list.size(); i++){
bookList.add(books.get(list.get(i)));
}
return bookList;
}
}
页面效果: