最近学习jsp,有一个小demo,使用jsp中的cookie技术保存购物网站最近浏览商品的功能。
总体设计分:1.DBHelper类(连接数据库的工具类)2,商品实体类3,获取商品方法的实现(所有商品的展示和详细商品的展示)4,使用Cookie保存浏览商品记录
//DBHelper类
public class DBHelper{
private static final String driver="com.mysql.jdbc.Driver";
private static final String url="jdbc:mysql://localhost:3306/candy?useUnicode=true&characterEncoding=UTF-8";
private static final String username="root";
private static final String password="";
//单例模式
private static Connection conn=null;
//反射
static
{
try{
Class.forName("driver");
}catch(Exception e){
e.printStackTrace();
}
}
public static Connection getConnection() throws Exception{
if(conn=null){
conn=DriverManager.getConnection(url,username,password);
return conn;
}
return conn;
}
}
//商品类
public class Items{
private int id;
private String name;
private String city;
private int price;
private int number;
private String picture;
}
Items类还有get和set的方法
//ItemDao 获取方法的实现(数据访问对象)
public class ItemDao{
public ArrayList<Items> getAllItems(){
Connection conn=null;
PreparedStatment stmt=null;
ResultSet rs=null;
ArrayList<Items> list=new ArrayList<Items>();
try{
conn=DBHelper.getConnection();
String sql="select * from items";
stmt=conn.prepareStatement(sql);
rs=stmt.executeQuery();
//把数据库中的数据添加到实体类中
while(rs.next()){
}
}catch (Exception ex) {
ex.printStackTrace();
return null;
} finally {
if (rs != null) {
try {
rs.close();
rs = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
stmt = null;
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
//通过id获取到商品的详细信息
public Items getItemsById(int id){
//跟获取所有商品的信息差不多
//改变sql语句 String sql="select * from items where id=?";
//预编译PreparedStatement中设置参数
stmt.setInt(1,id);
}
//通过保存在Cookie中的有关id的字符串信息作为参数,返回浏览过商品的集合
public ArrayList<Items> getViewList(String list){
ArrayList<Items> itemList=new ArrayList<Items>();
if(liist!=null&&list.length>0){
String[] arr =list.split(",");
if(arr.length>=5)
{
for(int i=arr.length-1;i>=arr.length-5t;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;
}
}
}
所有商品的body部分:
<body>
<h1>商品展示</h1>
<hr>
<center>
<table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<!-- 商品循环开始 -->
<%
ItemsDAO itemsDao = new ItemsDAO();
ArrayList<Items> list = itemsDao.getAllItems();
if(list!=null&&list.size()>0)
{
for(int i=0;i<list.size();i++)
{
Items item = list.get(i);
%>
<div>
<dl>
<dt>
<a href="details.jsp?id=<%=item.getId()%>"><img src="images/<%=item.getPicture()%>" width="120" height="90" border="1"/></a>
</dt>
<dd class="dd_name"><%=item.getName() %></dd>
<dd class="dd_city">产地:<%=item.getCity() %> 价格:¥ <%=item.getPrice() %></dd>
</dl>
</div>
<!-- 商品循环结束 -->
<%
}
}
%>
</td>
</tr>
</table>
</center>
</body>
//商品详细界面的jsp
<body>
<h1>商品详情</h1>
<hr>
<center>
<table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
<tr>
<!-- 商品详情 -->
<%
ItemsDAO itemDao = new ItemsDAO();
Items item = itemDao.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="200" height="160"/></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 ="";
//从客户端获得Cookies集合
Cookie[] cookies = request.getCookies();
//遍历这个Cookies集合
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(",");
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 = itemDao.getViewList(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() %> 价格:<%=i.getPrice() %> ¥ </dd>
</dl>
</div>
<%
}
}
%>
<!-- 循环结束 -->
</td>
</tr>
</table>
</center>
</body>
//导入相关的类和连接数据库的jar包和一些素材。