导航

Cookie实现商品浏览记录--方式一:Java实现

Posted on 2016-10-24 10:53  好一棵杨林  阅读(758)  评论(0编辑  收藏  举报

方式一:Java代码方式实现:此种方式实现思路较为顺畅。
难点在于,如何实现将最近浏览的产品显示在最前面:实现方式是借助LinkedList提供的remove()方法,先将此id从列表中移除,然后再借助addFirst()方法将此id插入到最前面。具体实现如下:
(1). 在JSP页面中,显示所有的商品列表。当我们选择某一款产品时,通过超链接跳转到Servlet中,并将此产品的ID一并传过去。
```
<dt><a href="<%=path %>/servlet/do_home_control?param=recode&ep_id=${ep.ep_id}" target="_self"><img src="images/product/1.jpg" /></a></dt>
```
(2). 在Servlet中,接收产品id。
```
public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String param = request.getParameter("param");
        String path = request.getContextPath();
        PrintWriter out = response.getWriter();
                //......
        if("recode".equals(param)) {
            String ep_id = request.getParameter("ep_id");
            // 如果是首页显示,则ep_id为null,则只显示cookie中的值即可。
            // 根据cookie的name,重设对应的value
            String cvalue = Tool.BuildCookie("productRecode", ep_id, request);
            // 将新的cookie写入
            Cookie cookie = new Cookie("productRecode", cvalue);
            cookie.setMaxAge(3600 * 1000);
            cookie.setPath("/");
            response.addCookie(cookie);
            // 根据id获得对应的产品集合,最终将产品列表显示到jsp页面中
            List<Easybuy_Product> eps = new ArrayList<Easybuy_Product>();
            for (String c : cvalue.split(",")) {
                eps.add(productService.getProductByEp_Id(Integer.parseInt(c)));
            }

            request.getSession().setAttribute("productRecode", eps);
            response.sendRedirect(request.getContextPath()
                    + "/product-view.jsp?ep_id=" + ep_id);
        }
}
```
(3). 上面涉及到一个方法,将此方法写到工具类中了。

```
//Tool.java工具类中写了下面的两个方法
    /**
     * 将id添加到cookie中,并返回最终的Cookie字符串
     * @param cookieName
     * @param ep_id
     * @param request
     * @return String
     */
    public static String BuildCookie(String cookieName, String ep_id,
            HttpServletRequest request) {

        StringBuilder sb = new StringBuilder();
        String recodeValue = getCookieValue(cookieName, request);
        // 判断cookie的value值中是否包含此id
        if (recodeValue == null) {
            // 判断ep_id是否为空。如果为空,则为首页显示
            if (ep_id == null || "".equals(ep_id)) {
                return null;
            }
            return ep_id;
        } else {
            //转换成LinkedList很有必要。普通的ArrayList想要实现最近浏览的产品移动到最前面比较困难,不如LinkedList提供的方法来的直接。
            LinkedList<String> s = new LinkedList<String>(
                    Arrays.asList(recodeValue.split(",")));
            // 判断ep_id是否为空。如果为空,则为首页显示
              //如果ep_id不为空,则判断LinkedList中是否包含此ID。如果包含,则移除掉。然后将此ID放置到列表中的第一个位置。
            if (ep_id != null) {
                if (s.contains(ep_id)) {
                    s.remove(ep_id);
                }
                //商品显示3条
                if (s.size() > 3) {
                    s.removeLast();
                }
                s.addFirst(ep_id);
            }
            for (String bid : s) {
                sb.append(bid + ",");
            }
            return sb.deleteCharAt(sb.length() - 1).toString();
        }
    }

    /**
     * 根据cookieName获取cookie对应的value值
     *
     * @param cookieName
     * @return
     */
    public static String getCookieValue(String cookieName,
            HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String recodeValue = null;
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookieName.equals(cookie.getName())) {
                    recodeValue = cookie.getValue();
                    break;
                }
            }
        }
        return recodeValue;
    }
```
这个功能就这样实现了!如果你有想法,咱们一起探讨!