經歷了倆個星期,易買網項目如期完工,現在總結一下如何使用cookies實現瀏覽商品的歷史記錄。
第一步:創建商品實體類。
第三步:創建商品三層架構。
效果圖:
在要顯示 的地方加入核心代碼:
<h2>最近瀏覽</h2> <div style="height:200px;display:block;overflow:hidden"> <% //獲取所有的cookie Cookie[] cookies = request.getCookies(); //篩選cookie if(cookies != null && cookies.length>0){ for(int j = cookies.length-1; j>=0; j--){ String cookieName = cookies[j].getName(); if(cookieName.startsWith("點擊跳轉查看商品詳細頁代碼如下:
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";//獲取商品idrequest.setCharacterEncoding("utf-8");String book = request.getParameter("id");Cookie[] cookies = request.getCookies();List<Cookie> cookieList = new ArrayList<Cookie>();Cookie tempCookie =null;//篩選cookieif(cookies != null && cookies.length>0){ for(Cookie c:cookies){ String cookieName = c.getName(); if(cookieName.startsWith("product-view")){ cookieList.add(c); } if(c.getValue().equals(book)){ tempCookie = c; } }} if(cookieList.size()>=10 && tempCookie==null){ tempCookie = cookieList.get(0);}Cookie cookie = new Cookie("product-view"+book,book);response.addCookie(cookie);%>這樣就實現了商品的瀏覽記錄。
新聞熱點
疑難解答