Java uses cookies to display recently viewed books _java

Source: Internet
Author: User
Tags auth send cookies stringbuffer

The example of this article for you to share the Java use of cookies to display recently viewed books related methods for your reference, the specific contents 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 Descriptio
  N) {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 publishers", "111111111 "));
  Bookmap.put ("2", New book ("2", "2222", "22.0", "Zqwang", "222 publishing house", "222222222"));
  Bookmap.put ("3", New book ("3", "3333", "33.0", "Zqwang", "333 publishing house", "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 {R
  Esponse.setcontenttype ("Text/html;charset=utf-8");
  1. Query the database all the books show 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 (" 

4.servlet

public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {R
  Esponse.setcontenttype ("Text/html;charset=utf-8");
  1. Get the ID of the book to read, 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 ("Can't find this book!");
  Return
   }else{response.getwriter (). Write (" 

This is all in this article and I want to help you learn how Java uses cookies to display recently viewed books.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.