Use cookies to dynamically display the items that users have browsed.

Source: Internet
Author: User

Use cookies to dynamically display the items that users have browsed.

1. Mainly do two things in the DisPlay servlet

1.1 items displayed on sale

1.2 display the items that the customer browsed

The DisPlay code is as follows:

Package com. baowei. cookie; import java. io. IOException; import java. io. printWriter; import java. util. iterator; import java. util. map; import java. util. map. entry; import java. util. set; import javax. servlet. servletException; import javax. servlet. http. cookie; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; public class Display exten Ds HttpServlet {@ Override protected void doGet (HttpServletRequest req, javasresp) throws handle, IOException {doPost (req, resp);} @ Override protected void doPost (HttpServletRequest req, comment resp) throws ServletException, IOException {resp. setCharacterEncoding ("UTF-8"); resp. setContentType ("text/html; charset = UTF-8"); // display all books PrintWriter out = resp. getW Riter (); out. print ("books sold in this bookstore are as follows: <br/>"); Map <String, Book> books = DB. getDBMap (); Set <Entry <String, Book> set = books. entrySet (); Iterator <Entry <String, Book> it = set. iterator (); while (it. hasNext () {Entry <String, Book> entry = it. next (); Book = entry. getValue (); out. print ("<a href = '/HuiHuaTest/BookDetail? Id = "+ book. getId () + "'target = '_ blank'>" + book. getName () + "</a> <br/>");} // The book you have recently read out. print ("the books you recently browsed are as follows: <br/>"); Cookie cookie = null; Cookie [] cookies = req. getCookies (); for (int I = 0; cookies! = Null & I <cookies. length; I ++) {if (cookies [I]. getName (). equals ("bookHistory") {cookie = cookies [I] ;}} if (cookie! = Null) {// found the cookie bookHistory String bookHistory = cookie. getValue (); // 4_6_1 String ids [] = bookHistory. split ("\ _"); // This is better divided for (String id: ids) {Book book = (Book) DB. getDBMap (). get (id); out. print (book. getName () + "<br/> ");}}}}

 

2. The BookDetail servlet, which provides a detailed description of books, also provides two functions:

2.1 introduce product details

2.2 Add the browsed item information to the Cookie and write the Cookie back to the browser

The implementation code of BookDetail is as follows:

1 package com. baowei. cookie; 2 3 import java. io. IOException; 4 import java. io. printWriter; 5 import java. util. arrays; 6 import java. util. export list; 7 8 import javax. servlet. servletException; 9 import javax. servlet. http. cookie; 10 import javax. servlet. http. httpServlet; 11 import javax. servlet. http. httpServletRequest; 12 import javax. servlet. http. httpServletResponse; 13 14 public class BookDetail extend S HttpServlet {15 16 public void doGet (HttpServletRequest request, HttpServletResponse response) 17 throws ServletException, IOException {18 19 doPost (request, response ); 20 21} 22 23 public void doPost (HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException {25 26 response. setContentType ("text/html; charset = UTF-8"); 27 response. setCharacterEncoding ("UTF-8"); 28 PrintWriter out = response. getWriter (); 29 30 // 1. based on the id value brought by the user, 31 out of the corresponding item is displayed. print ("the detailed information of the book you want to read is: <br/>"); 32 String id = request. getParameter ("id"); 33 Book book = (Book) DB. getDBMap (). get (id); 34 out. print (book. getId () + "<br/>"); 35 out. print (book. getName () + "<br/>"); 36 out. print (book. getAuthor () + "<br/>"); 37 38 // 2. return the ID number of the item in the form of a cookie to the browser 39 String bookHistory = makeCookie (book. getId (), request); 40 Cookie cookie = new Cookie ("bookHistory", bookHistory); 41 // cookie. setMaxAge (10000); // Save the Cookie in the hard disk 42 response. addCookie (cookie); 43 44} 45 46 // create a new cookie value 47 private String makeCookie (String id, httpServletRequest request) {48 49 // bookHistory = null 3 bookHistory = 350 // bookHistory = 2_history 5 3 bookHistory = 3_2_151 // bookHistory = 2 3 3 bookHistory = 3_252 // bookHistory = 2_3 3 B OokHistory = 3_253 54 // 1. obtain the book 55 String bookHistory = null; 56 Cookie cookies [] = request. getCookies (); 57 for (int I = 0; cookies! = Null & I <cookies. length; I ++) {58 if (cookies [I]. getName (). equals ("bookHistory") {59 bookHistory = cookies [I]. getValue (); 60} 61} 62 63 if (bookHistory = null) {64 bookHistory = id; 65 return bookHistory; 66} 67 68 // bookHistory = 1_2_5 indicates that the user has read some books, and the program wants to get the 69 String ids [] = bookHistory. split ("_"); 70 // to check whether the array contains the current id, we should convert the data into a set, 71 Items List <String> idList = new items list (Arrays. asList (ids); 72 73 if (idList. contains (id) {74 idList. remove (id); 75} else {76 if (idList. size ()> = 5) {77 idList. removeLast (); 78} 79} 80 idList. addFirst (id); 81 82 StringBuffer sb = new StringBuffer (); 83 for (String lid: idList) {// 1_2_3_84 sb. append (lid + "_"); 85} 86 87 return sb. deleteCharAt (sb. length ()-1 ). toString (); 88} 89 90}

 

The Book class and DB class used in the above two programs are as follows:

Book class code:

 1 package com.baowei.cookie; 2  3 public class Book { 4      5     private String id; 6     private String name; 7     private String author; 8      9     public Book() {10 11     }12     13     public Book(String id, String name, String author) {14         this.id = id;15         this.name = name;16         this.author = author;17     }18     public String getId() {19         return id;20     }21     public void setId(String id) {22         this.id = id;23     }24     public String getName() {25         return name;26     }27     public void setName(String name) {28         this.name = name;29     }30     public String getAuthor() {31         return author;32     }33     public void setAuthor(String author) {34         this.author = author;35     }36     37     38 39 }

The database class code is as follows:

Package com. baowei. cookie; import java. util. hashMap; import java. util. map; public class DB {static Map <String, Book> map = new HashMap <String, Book> (); static {map. put ("1", new Book ("1", "JavaWeb Development", "Zhang "); map. put ("2", new Book ("2", "Operating System", "Zhang "); map. put ("3", new Book ("3", "Spring Development", "Zhang "); map. put ("4", new Book ("4", "Struts2 Development", "Zhang "); map. put ("5", new Book ("5", "Java Basic Development", "Zhang "); map. put ("6", new Book ("6", "Data Structure", "Zhang "); map. put ("7", new Book ("7", "ACM", "Zhang "); map. put ("8", new Book ("8", "Computer Network", "Zhang "); map. put ("9", new Book ("9", "Software Engineering", "Zhang "); map. put ("10", new Book ("10", "Android Development", "Zhang ");} public static Map <String, Book> getDBMap () {return map ;}}

 

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.