//
package cn.itcast.cookie;
import java.io.IOException;
import java.io.PrintWriter;
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;
public class cookiedemo3 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.println("本网站有如下商品:<br/>");
Map<String,Book>map=DB.getall();
for(Map.Entry<String, Book>it:map.entrySet()){
Book book=it.getValue();
out.print("<a href='/day07/servlet/cookiedemo4?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a><br>");
}
//获取曾经浏览过的信息
out.println("您曾经浏览过的商品是:<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("#");//注意此处是#号,我用myeclipse写的“,”会出现报错java.lang.IllegalArgumentException: An invalid character [44] was present in the Cookie value
for(String id:ids){
Book book=(Book) DB.getall().get(id);out.println(book.getName()+"<br/>");
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
class DB{
private static Map<String,Book> map=new LinkedHashMap<String,Book>();
static{
map.put("1",new Book("1","javaweb开发","老张","一本好书"));
map.put("2",new Book("2","jdbc开发","老张","一本好书"));
map.put("3",new Book("3","spring开发","老黎","一本好书"));
map.put("4",new Book("4","structs开发","老毕","一本好书"));
map.put("5",new Book("5","android开发","老黎","一本好书"));
}
public static Map getall(){
return map;
}
}
class Book{
private String id;
private String name;
private String author;
private String describe;
public Book() {
super();
}
public Book(String id, String name, String author, String describe) {
super();
this.id = id;
this.name = name;
this.author = author;
this.describe = describe;
}
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 getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
}
//只是cookiedemo4.java
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;
public class cookiedemo4 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();
// 根据用户带过来的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.getDescribe() + "<br/>");
// 构建cookie,回写给浏览器
String cookievalue = bildercookie(id, request);
Cookie cookie = new Cookie("bookhistory", cookievalue);
cookie.setMaxAge(1 * 30 * 24 * 3600);
cookie.setPath("/day07");
response.addCookie(cookie);
}
private String bildercookie(String id, HttpServletRequest request) {
// 一共会产生四种情况 新增 返回
// bookhistory=null 1 1
// bookhistory=2,5,1 1 1,2,5
// bookhistory=2,5,4 1 1,2,5(带过来的cookie超过最大值值,则去掉最后的cookie,添加新增的cookie)
// bookhistory=2,5 1 1,2,5
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();//1这一步不是很理解,这个bookhistory到底是值呢还是cookie的名称???
}
}
if (bookhistory == null) {
return id;
}
LinkedList<String> list = new LinkedList<String>(
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); } }
*/
if (list.contains(id)) {
list.remove(id);
} else {
if (list.size() >= 3) {
list.removeLast();
}
}
list.addFirst(id);
System.out.println(list);
// 最后将集合中的内容取出来
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 {
doGet(request, response);
}
}