商品瀏覽記錄的處理
阿新 • • 發佈:2019-01-14
獲取記錄:
servlet程式碼:
import java.io.IOException; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.huida.utils.CookUtils; /* * 記錄商品瀏覽記錄 * */ public class GetProductByIdServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //設定編碼 //獲取當前訪問的商品 String id=request.getParameter("id"); String ids=""; //獲取指定的cookies ids 1-2-3 Cookie c=CookUtils.getCookieByName("ids", request.getCookies()); //判斷cookie是否為空 if(c==null){ //若為空 需要將當前商品的id放入ids中 ids=id; }else{ //若不為空,繼續判斷id是否已在ids中 //獲取值 ids=c.getValue(); String[] arr=ids.split("-"); //陣列長度固定 陣列與集合間的轉換 List<String> asList=Arrays.asList(arr); LinkedList<String> list=new LinkedList<String>(asList); if(list.contains(id)){ //若ids中包含id 將id移除 放到最前面 list.remove(id); list.addFirst(id); }else{ //若ids中不包含id 繼續判斷長度是否大於2 if(list.size()>2){ //移除最後一個 將當前的放入最前面 list.removeLast(); list.addFirst(id); }else{ //長度<3 將當前放入最前面 list.addFirst(id); } } ids=""; //將list轉換成字串 for(String s:list){ ids+=(s+"-"); } ids=ids.substring(0, ids.length()-1); } //將ids寫回去 c=new Cookie("ids",ids); //設定訪問路徑 c.setPath(request.getContextPath()+"/"); //設定存存活時間 c.setMaxAge(3600); //寫回瀏覽器 response.addCookie(c); //跳轉到指定的商品頁面上 response.sendRedirect(request.getContextPath()+"/product_info"+id+".htm"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
jsp瀏覽記錄部分程式碼:
<% //獲取指定名稱的cookie Cookie c = CookUtils.getCookieByName("ids", request.getCookies()); //判斷ids是否為空 if(c==null){ %> <h2>暫無瀏覽記錄</h2> <% }else{ String[] arr = c.getValue().split("-"); for(String id:arr){ %> <li style="width: 150px;height: 216;float: left;margin: 0 8px 0 0;padding: 0 18px 15px;text-align: center;"><img src="products/1/cs1000<%=id %>.jpg" width="130px" height="130px" /></li> <% } } %>
清除記錄:
import java.io.IOException; 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 ClearHistoryServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //建立一個cookie Cookie c=new Cookie("ids",""); c.setPath(request.getContextPath()+"/"); c.setMaxAge(0); response.addCookie(c); response.sendRedirect(request.getContextPath()+"/product_list.jsp"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }