Cet article partage avec vous les méthodes pertinentes d'utilisation des cookies pour afficher des livres récemment consultés pour votre référence. Le contenu spécifique est le suivant
1. Package Ben
import java.io.serializable; Le livre de classe publique implémente Serializable {private String id; nom de chaîne privé; Prix de chaîne privée; String privé auth; Private String Publish; Description de la chaîne privée; Public Book () {} Public Book (String id, nom de chaîne, prix de chaîne, chaîne auth, chaîne publie, description de la chaîne) {super (); this.id = id; this.name = name; this.price = prix; this.auth = auth; this.publish = publier; 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 () {Nom de retour; } public void setName (string name) {this.name = name; } public String getPrice () {prix de retour; } public void setPrice (String Price) {this.price = prix; } 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. Package DAO
import java.util.linkedhashmap; import java.util.map; Importer Cn.huiyu.ben.book; classe publique bookdao {carte statique privée <chaîne, livre> bookmap = new LinkedHashmap <String, book> (); BookDao privé () {} statique {bookmap.put ("1", nouveau livre ("1", "1111", "11.0", "zqwang", "111 pression", "11111111")); bookmap.put ("2", nouveau livre ("2", "2222", "22.0", "zqwang", "2222222222")); bookmap.put ("3", nouveau livre ("3", "3333", "33.0", "Zqwang", "333333333")); } Public Static Map <String, Book> GetBooks () {return BookMap; } public static livre getBook (String id) {return bookmap.get (id); }} 3.Servlet
public void doGet (HttpServLetRequest Request, HttpServletResponse Response) lève ServletException, ioException {réponse.setContentType ("text / html; charset = utf-8"); // 1. Interrogez tous les livres de la base de données pour afficher la carte <chaîne, livre> map = bookdao.getBooks (); for (map.entry <string, book> entrée: map.entryset ()) {book book = entry.getValue (); Response.getWriter (). Write ("<a href = '" + request.getContextPath () + "/ servlet / bookInfoServlet? id =" + book.getID () + "'>" + book.getName () + "</a> <br>"); } réponse.getWriter (). Write ("<hr>"); // 2. Montrez les livres que j'ai lus avant le cookie [] cs = request.getcookies (); Cookie findc = null; if (cs! = null) {for (cookie c: cs) {if ("last" .equals (c.getName ())) {findc = c; }}} if (findc == null) {réponse.getWriter (). Write ("Je n'ai lu aucun livre!"); } else {réponse.getWriter (). Write ("Les livres que vous avez parcourus: <br>"); String [] ids = findc.GetValue (). Split (","); for (String id: ids) {book book = bookdao.getbook (id); réponse.getWriter (). Write (book.getName () + "<br>"); }}} 4.servlet
public void doGet (HttpServLetRequest Request, HttpServletResponse Response) lève ServletException, ioException {réponse.setContentType ("text / html; charset = utf-8"); // 1. Obtenez l'ID du livre à lire, interrogez la base de données pour trouver le livre et émettez les informations détaillées du livre Id = request.getParamètre ("ID"); Livre de livres = bookdao.getbook (id); if (book == null) {réponse.getWriter (). Write ("Ce livre ne peut pas être trouvé!"); retour; } else {réponse.getWriter (). Write ("<h1> title:" + book.getName () + "</h1>"); réponse.getWriter (). Write ("<h3> auteur:" + book.getAuth () + "</h3>"); Response.getWriter (). Write ("<h3> Prix de vente:" + book.getprice () + "</h3>"); réponse.getWriter (). WRITE ("<h3> éditeur:" + book.getPublish () + "</h3>"); réponse.getWriter (). Write ("<h3> Description:" + book.getDescription () + "</h3>"); } // 2. Envoyer des cookies pour enregistrer le dernier livre que vous lisez // --- 1 -> 1 // 1 --2,1 -> 2,1 // 2,1--3,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) {// Explication des enregistrements qui n'ont pas lu le livre avant ids + = book.getId (); } else {// Explication Les enregistrements des livres qui ont été lus dans l'histoire auparavant, et un nouvel enregistrement doit être calculé sur la base de la chaîne d'histoire [] olds = findc.GetValue (). Split (","); StringBuffer Buffer = new StringBuffer (); Buffer.APPEND (book.getId () + ","); for (int i = 0; i <olds.length && tamper.toString (). Split (","). Longueur <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 ("dernier", ids); lastc.SetMaxage (3600 * 24 * 30); lastc.SetPath (request.getContextPath ()); réponse.addcookie (lastc); }Ce qui précède concerne cet article. J'espère qu'il sera utile pour tout le monde d'apprendre à utiliser des cookies pour afficher des livres récemment consultés.