• 使用cookies查询商品浏览记录


    经历了俩个星期,易买网项目如期完工,现在总结一下如何使用cookies实现浏览商品的历史记录。

    第一步:创建商品实体类。

    第二步:连接oracle数据库。

    第三步:创建商品三层架构。

    效果图:

    在要显示 的地方加入核心代码:

    <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("product-view")){
                            
                            //调用查询商品的方法
                            List<easybuy_product> list4 = productbiz.product(cookies[j].getValue());
                            request.setAttribute("list4",list4);
                            
                            %>
                <c:forEach var="hao" items="${requestScope.list4}" >    
                    <dl class="clearfix">
                    <dt><img src="${hao.ep_file_name}" width="50px"/></dt>
                    <dd><a href="addcookie?id=${hao.ep_id}" target="_blank">${hao.ep_name}</a></dd>
            
                    
                </dl>
                </c:forEach>
                <%}}}%>
            </div>

    点击跳转查看商品详细页代码如下:

    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    //获取商品id
    request.setCharacterEncoding("utf-8");
    String book = request.getParameter("id");
    Cookie[] cookies = request.getCookies();
    List<Cookie> cookieList = new ArrayList<Cookie>();
    Cookie tempCookie =null;
    
    //筛选cookie
    if(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);
    
    %>

    这样就实现了商品的浏览记录。

  • 相关阅读:
    js 时间相关函数
    javascript面向对象:继承、多态
    面向对象相关
    reset.css css重置公共样式
    开通博客园第一天。
    vue 和 react 路由跳转和传参
    前端密码加密方式
    react组件回顶部
    移动端使用rem方法
    用rekit创建react项目
  • 原文地址:https://www.cnblogs.com/wlx520/p/4578722.html
Copyright © 2020-2023  润新知