Using Cookie to realize product browsing record https://www.imooc.com/video/4720
1. Display the details of the goods
<body> <h1>Commodity details</h1> <hr> <center> <table width="750" height="60" cellpadding="0" cellspacing="0" border="0"> <tr> <!-- Commodity details --> <% ItemsDAO itemDao = new ItemsDAO(); Items item = itemDao.getItemsById(Integer.parseInt(request.getParameter("id"))); if(item!=null) { %> <td width="70%" valign="top"> <table> <tr> <td rowspan="4"><img src="images/<%=item.getPicture()%>" width="200" height="160"/></td> </tr> <tr> <td><B><%=item.getName() %></B></td> </tr> <tr> <td>Place of Origin:<%=item.getCity()%></td> </tr> <tr> <td>Price:<%=item.getPrice() %>¥</td> </tr> </table> </td> <% } %>
2. Use cookie s to store browsed products
<% String list =""; //Get Cookies collection from client Cookie[] cookies = request.getCookies(); //Traverse the Cookies collection if(cookies!=null&&cookies.length>0) { for(Cookie c:cookies) { if(c.getName().equals("ListViewCookie")) { list = c.getValue(); } } } list+=request.getParameter("id")+","; //If there are more than 1000 browsing records, clear them //Here, arr only plays a role of judgment and clearing String[] arr = list.split(","); if(arr!=null&&arr.length>0) { if(arr.length>=1000) { list=""; } } //The listview cookie defined here is stored in the list, and the cookie is stored in the list. //This is where the previous judgment about c.getName().equals("ListViewCookie") came from. //It can be seen as the question of chicken or egg Cookie cookie = new Cookie("ListViewCookie",list); response.addCookie(cookie); %>
3. Read out the contents of the cookie
<!-- Products viewed --> <td width="30%" bgcolor="#EEE" align="center"> <br> <b>Products you have visited</b><br> <!-- Loop start --> <% ArrayList<Items> itemlist = itemDao.getViewList(list); if(itemlist!=null&&itemlist.size()>0 ) { System.out.println("itemlist.size="+itemlist.size()); for(Items i:itemlist) { %> <div> <dl> <dt> <a href="details.jsp?id=<%=i.getId()%>"><img src="images/<%=i.getPicture() %>" width="120" height="90" border="1"/></a> </dt> <dd class="dd_name"><%=i.getName() %></dd> <dd class="dd_city">Place of Origin:<%=i.getCity() %> Price:<%=i.getPrice() %> ¥ </dd> </dl> </div> <% } } %> <!-- Loop end --> </td> </tr> </table> </center> </body>
General idea:
1. If there is no cookie named ListViewCookie in the previous cookie list, then the list is empty, then the id number of the product will be obtained and added to the list. If the list is not empty, then what is stored in the list is the previous browsing record. Then this statement is to add a new commodity id on the basis of the original browsing record, so as to ensure that all browsing records in the list will always be. The next code is to clear after more than 1000.
2. Get the string and transfer it to the logical business.
3. In the logical business operation, define a method to accept the string. After obtaining the string, use str.sqlit(",") to divide the string into string array [1,3,5,1 ]In this way, the browsing record of the commodity id is obtained.
4. After that, you can traverse and add the array, and finally return the array of browsing records.