Java USES cookie to display the most recently viewed books

  • 2020-05-09 18:32:53
  • OfStack

For your reference, this article provides an example of how Java USES cookie to display recently reviewed books. The details are as follows

1.ben package        


import java.io.Serializable;
 
public class Book implements Serializable {
 private String id;
 private String name;
 private String price;
 private String auth;
 private String publish;
 private String description;
  
 public Book() {
 }
  
 public Book(String id, String name, String price, String auth,
   String publish, String description) {
  super();
  this.id = id;
  this.name = name;
  this.price = price;
  this.auth = auth;
  this.publish = publish;
  this.description = description;
 }
 
 public String getDescription() {
  return description;
 }
 
 public void setDescription(String description) {
  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 getPrice() {
  return price;
 }
 public void setPrice(String price) {
  this.price = price;
 }
 public String getAuth() {
  return auth;
 }
 public void setAuth(String auth) {
  this.auth = auth;
 }
 public String getPublish() {
  return publish;
 }
 public void setPublish(String publish) {
  this.publish = publish;
 }
 
}

2.Dao package        


import java.util.LinkedHashMap;
import java.util.Map;
 
import cn.huiyu.ben.Book;
 
 
 
public class BookDao {
 private static Map<String,Book> bookMap = new LinkedHashMap<String, Book>();
 private BookDao() {
 }
 static{
  bookMap.put("1", new Book("1","1111","11.0","zqwang","111 Press. ","111111111"));
  bookMap.put("2", new Book("2","2222","22.0","zqwang","222 Press. ","222222222"));
  bookMap.put("3", new Book("3","3333","33.0","zqwang","333 Press. ","333333333"));
 }
  
 public static Map<String,Book> getBooks(){
  return bookMap;
 }
  
 public static Book getBook(String id){
  return bookMap.get(id);
 }
}

3.servlet        


public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html;charset=utf-8");
  //1. Query the database for all book displays 
  Map<String,Book> map = BookDao.getBooks();
  for(Map.Entry<String , Book> entry : map.entrySet()){
   Book book = entry.getValue();
   response.getWriter().write("<a href='"+request.getContextPath()+"/servlet/BookInfoServlet?id="+book.getId()+"'>"+book.getName()+"</a><br>");
  }
  response.getWriter().write("<hr>");
   
  //2. Show the books you've read before 
  Cookie [] cs = request.getCookies();
  Cookie findC = null;
  if(cs!=null){
   for(Cookie c : cs){
    if("last".equals(c.getName())){
     findC = c;
    }
   }
  }
  if(findC == null){
   response.getWriter().write(" I haven't read any books !");
  }else{
   response.getWriter().write(" The books you've browsed :<br>");
   String[] ids = findC.getValue().split(",");
   for(String id : ids){
    Book book = BookDao.getBook(id);
    response.getWriter().write(book.getName()+"<br>");
   }
  }
 }

4.servlet


public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  response.setContentType("text/html;charset=utf-8");
  //1. Get the book you want to read id, Query the database to find the book , Output the details of the book 
  String id = request.getParameter("id");
  Book book = BookDao.getBook(id);
  if(book==null){
   response.getWriter().write(" I can't find the book !");
   return;
  }else{
   response.getWriter().write("<h1> Title: :"+book.getName()+"</h1>");
   response.getWriter().write("<h3> The author :"+book.getAuth()+"</h3>");
   response.getWriter().write("<h3> price :"+book.getPrice()+"</h3>");
   response.getWriter().write("<h3> Press. :"+book.getPublish()+"</h3>");
   response.getWriter().write("<h3> Description information :"+book.getDescription()+"</h3>");
  }
   
  //2. send cookie Save the last book you read 
  // --- 1 --> 1
  // 1 --2,1 --> 2,1
  // 2,1--3,2,1 --> 3,2,1
  // 3,2,1 -- 4,3,2 --> 4,3,2
  // 4,3,2 --3,4,2 --> 3,4,2
  String ids = "";
   
  Cookie [] cs = request.getCookies();
  Cookie findC = null;
  if(cs!=null){
   for(Cookie c : cs){
    if("last".equals(c.getName())){
     findC = c;
    }
   }
  }
   
  if(findC == null){
   // There is no record of having read the book before 
   ids += book.getId();
  }else{
   // A record of books previously read in history , You have to do it historically 1 Two new records come out 
   String [] olds = findC.getValue().split(","); 
   StringBuffer buffer = new StringBuffer();
   buffer.append(book.getId()+",");
   for(int i = 0;i<olds.length && buffer.toString().split(",").length<3 ;i++){
    String old = olds[i];
    if(!old.equals(book.getId())){
     buffer.append(old+",");
    }
   }
   ids = buffer.substring(0, buffer.length()-1);
  }
   
   
   
  Cookie lastC = new Cookie("last",ids);
  lastC.setMaxAge(3600*24*30);
  lastC.setPath(request.getContextPath());
  response.addCookie(lastC);
 }

That's all for this article, which I hope will help you learn how Java USES cookie to display recently reviewed books.


Related articles: