บทความนี้แบ่งปันวิธีการที่เกี่ยวข้องในการใช้คุกกี้เพื่อแสดงหนังสือที่ดูเมื่อเร็ว ๆ นี้สำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
1. แพ็คเกจ
นำเข้า java.io.serializable; หนังสือชั้นเรียนสาธารณะใช้ serializable {รหัสส่วนตัวส่วนตัว; ชื่อสตริงส่วนตัว; ราคาสตริงส่วนตัว Auth String ส่วนตัว; สตริงส่วนตัวเผยแพร่; คำอธิบายสตริงส่วนตัว; หนังสือสาธารณะ () {} หนังสือสาธารณะ (รหัสสตริง, ชื่อสตริง, ราคาสตริง, สตริงรับรองความถูกต้อง, สตริงเผยแพร่, คำอธิบายสตริง) {super (); this.id = id; this.name = ชื่อ; this.price = ราคา; this.auth = auth; this.publish = เผยแพร่; this.description = คำอธิบาย; } สตริงสาธารณะ getDescription () {return คำอธิบาย; } โมฆะสาธารณะ setDescription (คำอธิบายสตริง) {this.description = คำอธิบาย; } สตริงสาธารณะ getId () {return id; } โมฆะสาธารณะ setId (รหัสสตริง) {this.id = id; } สตริงสาธารณะ getName () {ชื่อคืน; } โมฆะสาธารณะ setName (ชื่อสตริง) {this.name = name; } สตริงสาธารณะ getPrice () {ราคาคืน; } โมฆะสาธารณะ setPrice (ราคาสตริง) {this.price = ราคา; } สตริงสาธารณะ getAuth () {return auth; } โมฆะสาธารณะ setauth (สตริงรับรองความถูกต้อง) {this.auth = auth; } สตริงสาธารณะ getPublish () {return publish; } โมฆะสาธารณะ setPublish (สตริงเผยแพร่) {this.publish = เผยแพร่; - 2. แพ็คเกจ Dao
นำเข้า java.util.linkedhashmap; นำเข้า java.util.map; นำเข้า cn.huiyu.ben.book; ชั้นเรียนสาธารณะ Bookdao {แผนที่คงที่ส่วนตัว <String, Book> BookMap = ใหม่ LinkedHashMap <String, Book> (); Private Bookdao () {} Static {BookMap.put ("1", หนังสือเล่มใหม่ ("1", "1111", "11.0", "Zqwang", "111 กด", "11111111111")); bookmap.put ("2", หนังสือเล่มใหม่ ("2", "2222", "22.0", "zqwang", "2222222222222")); bookmap.put ("3", หนังสือเล่มใหม่ ("3", "3333", "33.0", "zqwang", "33333333333")); } แผนที่คงที่สาธารณะ <สตริงหนังสือ> getBooks () {return bookmap; } หนังสือสาธารณะคงที่ getBook (รหัสสตริง) {return bookmap.get (id); - 3. Servlet
โมฆะสาธารณะ DOGET (คำขอ httpservletRequest, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception {response.setContentType ("text/html; charset = utf-8"); // 1. แบบสอบถามหนังสือทั้งหมดในฐานข้อมูลเพื่อแสดงแผนที่ <สตริงหนังสือ> แผนที่ = bookdao.getbooks (); สำหรับ (map.entry <string, book> รายการ: map.entryset ()) {book book = entry.getValue (); Response.getWriter (). เขียน ("<a href = '"+request.getContextPath ()+"/servlet/bookinfoservlet? id ="+book.getId ()+"'>"+book.getName ()+"</a> <br>"); } response.getWriter (). เขียน ("<hr>"); // 2. แสดงหนังสือที่ฉันได้อ่านก่อนคุกกี้ [] cs = request.getCookies (); คุกกี้ findc = null; if (cs! = null) {สำหรับ (คุกกี้ c: cs) {ถ้า ("สุดท้าย" .equals (c.getName ())) {findc = c; }}} ถ้า (findc == null) {response.getWriter () เขียน ("ฉันยังไม่ได้อ่านหนังสือใด ๆ !"); } else {response.getWriter () เขียน ("หนังสือที่คุณเรียกดู: <br>"); String [] ids = findc.getValue (). แยก (","); สำหรับ (String id: ids) {book book = bookdao.getbook (id); Response.getWriter (). เขียน (book.getName ()+"<br>"); - 4. บริการ
โมฆะสาธารณะ DOGET (คำขอ httpservletRequest, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception {response.setContentType ("text/html; charset = utf-8"); // 1. รับ ID ของหนังสือเล่มนี้ที่จะอ่านสอบถามฐานข้อมูลเพื่อค้นหาหนังสือและส่งออกข้อมูลรายละเอียดของหนังสือสตริงหนังสือ = request.getParameter ("id"); หนังสือหนังสือ = bookdao.getbook (id); if (book == null) {response.getWriter () เขียน ("หนังสือเล่มนี้ไม่สามารถพบได้!"); กลับ; } else {response.getWriter (). เขียน ("<h1> ชื่อเรื่อง:"+book.getName ()+"</h1>"); Response.getWriter (). เขียน ("<h3> ผู้แต่ง:"+book.getauth ()+"</h3>"); Response.getWriter (). เขียน ("<h3> ราคาขาย:"+book.getPrice ()+"</h3>"); Response.getWriter (). เขียน ("<h3> สำนักพิมพ์:"+book.getPublish ()+"</h3>"); Response.getWriter (). เขียน ("<H3> คำอธิบาย:"+book.getDescription ()+"</h3>"); } // 2 ส่งคุกกี้เพื่อบันทึกหนังสือเล่มสุดท้ายที่คุณอ่าน // --- 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,2-3,4,2- คุกกี้ [] cs = request.getCookies (); คุกกี้ findc = null; if (cs! = null) {สำหรับ (คุกกี้ c: cs) {ถ้า ("สุดท้าย" .equals (c.getName ())) {findc = c; }}} if (findc == null) {// บันทึกคำอธิบายที่ไม่ได้อ่านหนังสือก่อน ids += book.getId (); } else {// บันทึกคำอธิบายของหนังสือที่อ่านในประวัติศาสตร์มาก่อนและบันทึกใหม่จะต้องคำนวณตามสตริงประวัติ [] olds = findc.getValue (). แยก (","); StringBuffer buffer = new StringBuffer (); buffer.append (book.getId ()+","); สำหรับ (int i = 0; i <olds.length && buffer.toString (). แยก (","). ความยาว <3; i ++) {string old = olds [i]; if (! old.equals (book.getId ())) {buffer.append (old+","); }} ids = buffer.substring (0, buffer.length ()-1); } คุกกี้ lastc = คุกกี้ใหม่ ("สุดท้าย", ids); lastc.setMaxage (3600*24*30); LastC.SetPath (request.getContextPath ()); Response.addcookie (LastC); -ข้างต้นเป็นเรื่องเกี่ยวกับบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์สำหรับทุกคนในการเรียนรู้วิธีใช้คุกกี้เพื่อแสดงหนังสือที่ดูเมื่อเร็ว ๆ นี้