用cookie实现浏览记录

首先连接数据库

——————————————————————————

实力类

package com.items.entity;

//商品表
public class Items {

    private int id;// 商品编号
    private String name;// 商品名称
    private String city;// 商品产地
    private int price;// 商品价格
    private int number;// 商品库存
    private String picture;// 商品图片

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

}

——————————————————

dao层的方法

package com.items.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.items.entity.Items;
import com.items.util.DBHelper;

//商品的业务逻辑层
public class ItemsDao {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "";
    boolean flag = false;

    // 获得所有的商品信息
    public ArrayList<Items> queryAll() {
        ArrayList<Items> list = new ArrayList<Items>();// 商品集合
        try {
            conn = DBHelper.getConnection();
            sql = "select * from items;";// 要执行的sql语句
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Items item = new Items();
                item.setId(rs.getInt(1));
                item.setName(rs.getString(2));
                item.setCity(rs.getString(3));
                item.setNumber(rs.getInt(4));
                item.setPrice(rs.getInt(5));
                item.setPicture(rs.getString("picture"));
                list.add(item);// 把表中对应的一行加入集合
            }
            return list;// 返回集合
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {

            // 释放数据集对象
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            // 释放语句对象
            if (ps != null) {
                try {
                    ps.close();
                    ps = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    // 根据橡皮那编号获得商品的详细信息
    public Items getItemsById(int id) {
        try {
            conn = DBHelper.getConnection();
            sql = "select * from items where id=?;";// 要执行的sql语句
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                Items item = new Items();
                item.setId(rs.getInt(1));
                item.setName(rs.getString(2));
                item.setCity(rs.getString(3));
                item.setNumber(rs.getInt(4));
                item.setPrice(rs.getInt(5));
                item.setPicture(rs.getString("picture"));
                return item;
            } else {
                return null;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {

            // 释放数据集对象
            if (rs != null) {
                try {
                    rs.close();
                    rs = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            // 释放语句对象
            if (ps != null) {
                try {
                    ps.close();
                    ps = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }
    //获取最近浏览的前五条商品信息
    public ArrayList<Items>  getViewsList(String list){
        ArrayList<Items> itemlist = new ArrayList<Items>();
        int Icount=5;//每次返回前五条记录
        if(list!=null&&list.length()>0){
            String[] arr=list.split(",");
            //如果商品记录大于等于五条
            if(arr.length>=5){
                for(int i=arr.length-1; i>=arr.length-Icount;i--){
                    itemlist.add(getItemsById(Integer.parseInt(arr[i])));
                }
            }else{
                    for(int i=arr.length-1;i>=0;i--)
                    {
                        itemlist.add(getItemsById(Integer.parseInt(arr[i])));
                    }
                }
                return itemlist;
            }
            
        else{
            return null;
        }
    }

}

————————————————————————————

所有商品的展示

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="com.items.dao.ItemsDao" %>
<%@ page import="com.items.entity.Items" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<style type="text/css">
  div{
    float:left;
    margin:10px;
  }
  div dd{
    margin:0px;
    font-size:10px;
  }
  div dd.dd_name{
    color:blue;
  }
  div dd.dd_city{
    color:#000;
  }
</style>
</head>
<body>
    <h1>商品展示</h1>
    <hr>
    
    <center>
       <table width="750px" height="60px" cellpadding="0" cellspacing="0" border="0">
           <tr>
              <td>
              
                  <!-- 商品循环开始 -->
                  <%
                 
                  ItemsDao itemsDao=new ItemsDao();
                  ArrayList<Items> list=itemsDao.queryAll();
                  if(list!=null&&list.size()>0){
                  for(int i=0;i<list.size();i++){
                      Items item=list.get(i);
                  %>
                  <div>
                     <dl>
                        <dt>
                           <a href="detail.jsp?id=<%=item.getId() %>"><img  src="images/<%=item.getPicture() %>" width="120px" heigh="90px" border="1px"></a>
                        </dt>
                        <dd class="dd_name"><%=item.getName() %></dd>
                        <dd class=dd_city>产地:<%=item.getCity() %>&nbsp;&nbsp;价格:¥<%=item.getPrice() %></dd>
                     </dl>
                  </div>
                  <!-- 商品循环结束 -->
                  <%
                  }
                  }
                  %>
              </td>
           </tr>
       </table>
    </center>
</body>
</html>

——————————————————

根据id得到商品的详细信息并且显示浏览的记录

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%@ page import="com.items.dao.ItemsDao" %>
<%@ page import="com.items.entity.Items" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<style type="text/css">
  div{
    float:left;
    margin:10px;
  }
  div dd{
    margin:0px;
    font-size:10px;
  }
  div dd.dd_name{
    color:blue;
  }
  div dd.dd_city{
    color:#000;
  }
</style>
</head>
<body>
    <h1>商品展示</h1>
    <hr>
    
    <center>
       <table width="750px" height="60px" cellpadding="0" cellspacing="0" border="0">
           <tr>
             <!-- 商品详情 -->
             <%
             ItemsDao itemsDao=new ItemsDao();
             Items item=itemsDao.getItemsById(Integer.parseInt(request.getParameter("id")));
             if(item!=null)
             {
             %>
             <td width="70%" valign="top">
                 <table>
                     <tr>  
                        <td rowspan="4"><img  src="images/<%=item.getPicture() %>" width="200px" height="160px"></td>
                     </tr>
                       <tr>
                 <td><B><%=item.getName() %></B></td>
               </tr>
               <tr>
                 <td>产地:<%=item.getCity()%></td>
               </tr>
               <tr>
                 <td>价格:<%=item.getPrice() %>¥</td>
               </tr>
                 </table>               
             </td>
             
             <%  } %>
           <%
             String list="";
             //从客户端获得客户Cookie集合
             Cookie[] cookies=request.getCookies();
             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(",");
             //遍历Cookie集合
             if(arr!=null&&arr.length>0){
                 if(arr.length>=1000){
                     list="";
                 }
             }
             Cookie   cookie=new Cookie("ListViewCookie",list);
             response.addCookie(cookie);
           %>
           <!-- 浏览过的商品 -->
           <td width="30%" bgcolor="#eee" align="center">
              <br>
              <b>您浏览过的商品</b><br>
              <!-- 循环开始 -->
                 <%
                ArrayList<Items> itemlist = itemsDao.getViewsList(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() %>&nbsp;&nbsp;价格:<%=i.getPrice() %> ¥ </dd>
             </dl>
             </div>
             <%
                   }
                }
             %>
             <!-- 循环结束 -->              
           </td>
              
                  
           </tr>
       </table>
    </center>
</body>
</html>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值