Artikel ini berbagi dengan Anda metode yang relevan untuk menggunakan cookie untuk ditampilkan buku yang baru -baru ini dilihat untuk referensi Anda. Konten spesifiknya adalah sebagai berikut
1. Paket Besar
impor java.io.serializable; Buku kelas publik mengimplementasikan serial {private string id; nama string pribadi; harga string pribadi; Private String Auth; Private String Publish; deskripsi string pribadi; buku publik () {} buku publik (ID string, nama string, harga string, string auth, string publish, string description) {super (); this.id = id; this.name = name; this.price = harga; this.auth = auth; this.publish = publish; this.description = description; } public String getDescription () {return description; } public void setDescription (deskripsi string) {this.description = description; } public String getId () {return id; } public void setId (string id) {this.id = id; } public string getName () {return name; } public void setName (name string) {this.name = name; } public String getPrice () {harga kembali; } public void setPrice (string price) {this.price = harga; } 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. Paket DAO
impor java.util.linkedhashmap; impor java.util.map; impor cn.huiyu.ben.book; Public Class bookdao {private static Map <string, book> bookmap = new LinkedHashMap <String, book> (); private bookdao () {} static {bookmap.put ("1", buku baru ("1", "1111", "11.0", "zqwang", "111 press", "111111111")); bookmap.put ("2", buku baru ("2", "2222", "22.0", "zqwang", "2222222222")); bookmap.put ("3", buku baru ("3", "3333", "33.0", "zqwang", "33333333333")); } peta statis public <string, book> getBooks () {return bookmap; } public static book getBook (string id) {return bookmap.get (id); }} 3.Servlet
public void doGet (permintaan httpservletRequest, respons httpservletResponse) melempar servletException, ioException {response.setContentType ("Teks/html; charset = utf-8"); // 1. Permintaan semua buku dalam database untuk menampilkan peta <string, book> peta = bookdao.getBooks (); untuk (MAP.ENTRY <String, Book> Entry: MAP.ENTRYSET ()) {BUKU BUKU = ENTER.GETVALUE (); response.getWriter (). write ("<a href = '"+request.getContextPath ()+"/servlet/bookInfoservlet? id ="+book.getid ()+"'>"+book.getName ()+"</a> <br>"); } response.getWriter (). write ("<hr>"); // 2. Tunjukkan buku yang telah saya baca sebelum 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 (). tulis ("Saya belum membaca buku apa pun!"); } else {response.getWriter (). write ("Buku yang telah Anda browsed: <br>"); String [] ids = findC.getValue (). Split (","); untuk (ID string: id) {buku buku = bookdao.getBook (id); response.getWriter (). write (book.getName ()+"<br>"); }}} 4.Servlet
public void doGet (permintaan httpservletRequest, respons httpservletResponse) melempar servletException, ioException {response.setContentType ("Teks/html; charset = utf-8"); // 1. Dapatkan ID buku yang akan dibaca, meminta database untuk menemukan buku, dan mengeluarkan informasi terperinci dari buku string id = request.getParameter ("id"); Buku buku = bookdao.getBook (id); if (book == null) {response.getWriter (). tulis ("Buku ini tidak dapat ditemukan!"); kembali; } else {response.getWriter (). write ("<h1> judul:"+book.getName ()+"</h1>"); response.getWriter (). write ("<h3> penulis:"+book.getAuth ()+"</h3>"); response.getWriter (). Tulis ("<h3> Harga jual:"+book.getPrice ()+"</h3>"); response.getWriter (). write ("<h3> Penerbit:"+book.getPublish ()+"</h3>"); response.getWriter (). tulis ("<h3> Deskripsi:"+book.getDescription ()+"</h3>"); } // 2. Kirim cookie untuk menyimpan buku terakhir yang Anda baca // --- 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,4,4,4,4,2-ID "" ID "2 // 4,3"; Cookie [] cs = request.getCookies (); Cookie findc = null; if (cs! = null) {for (cookie c: cs) {if ("last" .equals (c.getName ())) {findc = c; }}} if (findc == null) {// catatan penjelasan yang belum membaca buku sebelum id += book.getId (); } else {// Catatan penjelasan buku yang telah dibaca dalam sejarah sebelumnya, dan catatan baru perlu dihitung berdasarkan string sejarah [] olds = findc.getValue (). split (","); StringBuffer buffer = stringBuffer baru (); buffer.append (book.getId ()+","); untuk (int i = 0; i <olds.length && buffer.toString (). split (","). Length <3; i ++) {String Old = Olds [i]; if (! old.equals (book.getId ())) {buffer.append (old+","); }} id = buffer.substring (0, buffer.length ()-1); } Cookie lastc = cookie baru ("last", ids); lastc.setmaxage (3600*24*30); lastc.setPath (request.getContextPath ()); response.addcookie (lastc); }Di atas adalah semua tentang artikel ini. Saya harap akan sangat membantu bagi semua orang untuk belajar cara menggunakan cookie untuk menampilkan buku yang baru -baru ini dilihat.