<% String list =""; //从客户端获得Cookies集合 Cookie[] cookies = request.getCookies(); //遍历这个Cookies集合 if(cookies!=null&&cookies.length>0) { for(Cookie c:cookies) { if(c.getName().equals("ListViewCookie")) { list = c.getValue(); } } } list+=request.getParameter("id")+","; //如果浏览记录超过1000条,清零. String[] arr = list.split(","); if(arr!=null&&arr.length>0) { if(arr.length>=1000) { list=""; } } Cookie cookie = new Cookie("ListViewCookie",list); %> <!-- 浏览过的商品 --> <td width="30%" bgcolor="#EEE" align="center"> <br> <b>您浏览过的商品</b><br> <!-- 循环开始 --> <% 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">产地:<%=i.getCity() %> 价格:<%=i.getPrice() %> ¥ </dd> </dl> </div> <% } } %> <!-- 循环结束 -->
Cookie cookie = new Cookie("ListViewCookie",list); //把cookie加入 response.addCookie(cookie);