package qingxia.tang.entity;public class Book { private String id; private String bookName; private double bookPrice; private String author; public Book(String id, String bookName, double bookPrice, String author) { super(); this.id = id; this.bookName = bookName; this.bookPrice = bookPrice; this.author = author; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public double getBookPrice() { return bookPrice; } public void setBookPrice(double bookPrice) { this.bookPrice = bookPrice; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } @Override public String toString() { return "Book [id=" + id + ", bookName=" + bookName + ", bookPrice=" + bookPrice + ", author=" + author + "]"; } }
package qingxia.tang.util;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import qingxia.tang.entity.Book;public class DbUtils { private static Map books =new HashMap (); static { books.put("1", new Book("1","西游记",20,"吴承恩")); books.put("2", new Book("2","红楼梦",30,"曹雪婷")); books.put("3", new Book("3","水浒传",25,"施耐庵")); books.put("4", new Book("4","三国演义",10,"罗贯中")); } public static Map getAllBook(){ return books; } public static Book getBookById(String id){ return books.get(id); } }
package tang;import java.io.IOException;import java.io.PrintWriter;import java.util.Map;import java.util.Map.Entry;import java.util.Set;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import qingxia.tang.entity.Book;import qingxia.tang.util.DbUtils;/** * Servlet implementation class ShowBookDetail */@WebServlet("/ShowBookDetail")public class ShowBookDetail extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ShowBookDetail() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //显示所有书籍 response.setContentType("text/html;charset=UTF-8"); PrintWriter pw=response.getWriter(); pw.write("图书馆有如下书籍:"); Map allBook = DbUtils.getAllBook(); for(Entry book:allBook.entrySet()){ pw.write(" "+book.getValue().getBookName()+" "); } pw.write("已浏览如下书籍: "); Cookie[] cookies = request.getCookies(); for (int i = 0;cookies!=null && i < cookies.length; i++) { if(cookies[i].getName().equals("bookDetailHistory")){ //获取以-拼接的字符串 String value = cookies[i].getValue(); //以-分裂字符串,得到字符串数组 String[] split = value.split("-"); for (int j = 0; j < split.length; j++) { pw.write(DbUtils.getBookById(split[j]).getBookName()+" "); } } } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}
-
书详情的显示
-
package tang;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.annotation.WebServlet;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import qingxia.tang.entity.Book;import qingxia.tang.util.DbUtils;/** * Servlet implementation class BookDetail */@WebServlet("/BookDetail")public class BookDetail extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public BookDetail() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter pw=response.getWriter(); String id=request.getParameter("id"); Book bookById = DbUtils.getBookById(id); pw.write(bookById.toString()); Cookie cookie = new Cookie("bookDetailHistory",oganizeId(id,request)); //设置cookie的路径 cookie.setPath("/"); //设置cookie存活时间 cookie.setMaxAge(Integer.MAX_VALUE); //将cookie提交到浏览器客户端 response.addCookie(cookie); } /** * 将书的key值用-隔开存入到cookie当中 * @param id * @param request * @return */ private String oganizeId(String id, HttpServletRequest request) { Cookie[] cookies = request.getCookies(); if(cookies==null){ //说明第一次访问,无cookies值 return id; } //浏览器中有cookie值 Cookie historyBook=null; for (int i = 0; i < cookies.length; i++) { //判断cookie中是否保存了bookDetailHistory //如果cookie值中包含bookDetailHistory if("bookDetailHistory".equals(cookies[i].getName())){ //将key值是bookDetailHistory的value值取到 historyBook=cookies[i]; } } //如果cookie中无bookDetailHistory值 if(historyBook==null){ return id; } //cookie值中有bookDetailHistory值 //获取cookie值是bookDetailHistory的value值 String value = historyBook.getValue(); System.out.println("value"+value); //将bookDetailHistory的value值以-分隔开 String[] split = value.split("-"); //将取到的字符串数组转化成list LinkedList list=new LinkedList (Arrays.asList(split)); System.out.println("list.size()"+list.size()+" id:"+id); //如果list中的元素小于3 if(list.size()<3){ //list中的元素包含某一本书,将包含的书移除掉 if(list.contains(id)){ list.remove(id); } //如果list中的元素等于大于3 }else{ //list中的元素包含某一本书,将包含的书移除掉 if(list.contains(id)){ list.remove(id); //list中的元素中无与id相同的书,则将最后一本书删除,即只能显示三本书 }else{ list.removeLast(); } } //将书添加到首栏中 list.addFirst(id); StringBuffer sb=new StringBuffer(); for (int i = 0;i 0){ sb.append("-"); } //先拼接再添加值,保证最后一位不会出现- sb.append(list.get(i)); } return sb.toString(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); }}
显示效果:
-
![](https://img2018.cnblogs.com/blog/1136550/201810/1136550-20181017103440221-1659710051.png)
![](https://img2018.cnblogs.com/blog/1136550/201810/1136550-20181017103512062-799764336.png)
-
-