Cookie 简单使用记录浏览记录

时间:2023-05-31 17:32:56

ItemsDAO.java

package dao;
import java.util.* ;
import java.sql.* ; import util.DBHelper; import entity.Items;
//商品的业务逻辑类
public class ItemsDAO {
static public ArrayList<Items> getAllItems()
{
Connection conn = null ;
PreparedStatement stmt = null ;
ResultSet rs = null ; //数据集
ArrayList<Items> list = new ArrayList<>() ;
try {
conn = DBHelper.getConnection();
String sql = "select * from items;" ;
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while(rs.next()){
Items item = new Items() ;
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setCity(rs.getString("city"));
item.setPricce(rs.getInt("pricce"));
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 (Exception ex){
ex.printStackTrace() ;
}
}
//释放语句对象 if(stmt != null)
{
try{
stmt.close() ;
stmt = null ;
} catch (Exception ex){
ex.printStackTrace() ;
}
} } } //根据商品编号获得商品资料
public Items getItemsById(int id){
Connection conn = null ;
PreparedStatement stmt = null ;
ResultSet rs = null ; //数据集
try {
conn = DBHelper.getConnection();
String sql = "select * from items where id = ?;" ;
stmt = conn.prepareStatement(sql);
stmt.setInt(,id);
rs = stmt.executeQuery();
if(rs.next()){
Items item = new Items() ;
item.setId(rs.getInt("id"));
item.setName(rs.getString("name"));
item.setCity(rs.getString("city"));
item.setPricce(rs.getInt("pricce"));
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 (Exception ex){
ex.printStackTrace() ;
}
}
//释放语句对象 if(stmt != null)
{
try{
stmt.close() ;
stmt = null ;
} catch (Exception ex){
ex.printStackTrace() ;
}
}
} }
//获取最近浏览的前五条
public ArrayList<Items> getViewList(String list){
ArrayList<Items> itemlist = new ArrayList<Items> () ;
int iCount = ;
if(list != null && list.length()>){
String [] arr = list.split(","); if(arr.length >= ){
for(int i = arr.length- ; i >= arr.length-iCount- ; i--){
itemlist.add(getItemsById(Integer.parseInt(arr[i])));
}
} else {
for(int i = arr.length- ; i>= ; i --){
itemlist.add(getItemsById(Integer.parseInt(arr[i])));
}
} return itemlist ;
} else {
return null ;
}
}
}

Items.java

package entity;

public class Items {
private int id;
private String name;
private String city;
private int pricce;
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 getPricce() {
return pricce;
} public void setPricce(int pricce) {
this.pricce = pricce;
} public String getPicture() {
return picture;
} public void setPicture(String picture) {
this.picture = picture;
} }

DBHElper.java

package util;
import java.sql.Connection;
import java.sql.DriverManager; public class DBHelper { private static final String driver = "com.mysql.jdbc.Diver";
private static final String url = "jdbc:mysql://localhost:3306/zhuopengDB?useUnicode=true&characterEncoding=GB2312";
private static final String username = "zhuopeng" ;
private static final String password = "zhuopeng" ;
private static Connection conn = null ;
//静态代码块加载驱动
static
{
try
{
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e)
{
e.printStackTrace();
System.out.println("驱动");
} }
//单例模式
public static Connection getConnection() throws Exception
{
if(conn == null)
{
conn = DriverManager.getConnection(url,username,password);
}
return conn ;
} }

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "dao.ItemsDAO" %>
<%@ page import = "java.util.*" %>
<%@ page import = "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>商品</title>
</head>
<body>
<h1>商品展示</h1>
<hr>
<center>
<table width="" height = "60 "cellpadding = "" celpacing = "" border = "">
<tr>
<td>
<%
ItemsDAO itemsDao = new ItemsDAO() ;
ArrayList<Items> list = itemsDao.getAllItems() ;
if(list != null && list.size() > ){
for(int i = ; i < list.size() ; i ++){
Items item = list.get(i); %>
<!-- 循环部分 -->
<div>
<dl>
<dt>
<a href = "details.jsp?id=<%=item.getId()%>"> <img src ="E:\javawebTest\MyfirstWebapp\WebContent\WEB-INF\images\<%=item.getPicture() %>" width = "" heigh=""/></a>
</dt>
<dd class = "dd_name" > <%=item.getName() %> </dd>
<dd class = "dd_city" > <%=item.getCity() %> &nbsp; &nbsp; 价格:<%=item.getPricce() %></dd> </dl>
</div>
<!-- 循环结束哦 -->
<%
} }
%>
</td>
</tr>
</table>
</center>
</body>
</html>

details.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "dao.ItemsDAO" %>
<%@ page import = "java.util.*" %>
<%@ page import = "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>
</head>
<body>
<%
ItemsDAO itemDao = new ItemsDAO () ;
Items item = itemDao.getItemsById(Integer.parseInt(request.getParameter("id")));
if(item != null)
{
%>
<img src ="E:\javawebTest\MyfirstWebapp\WebContent\WEB-INF\images\<%=item.getPicture() %>" width = "" heigh=""/><br>
产地:<%=item.getCity() %><br>
价格:<%=item.getPricce() %><br> <%
}
%> <%
String list = "" ;
Cookie [] cookies = request.getCookies() ;
if(cookies !=null && cookies.length>)
for(Cookie c :cookies){
if(c.getName() . equals("ListViewCookie"))
{
list = c.getValue() ;
}
} list += request.getParameter("id")+"," ;
String [] arr = list.split(",");
if(arr!=null && arr.length> ){
if(arr.length>=)
{
list = "" ;
}
}
Cookie cookie = new Cookie("ListViewCookie" ,list) ; //创建Cookie
cookie.setMaxAge();
response.addCookie(cookie); //添加Cookie
%>
<hr>
浏览过的商品:<br>
<%
ArrayList<Items> itemslist = itemDao.getViewList(list);
if(itemslist!= null && itemslist.size()>){
for(Items i : itemslist){ %>
<img src ="E:\javawebTest\MyfirstWebapp\WebContent\WEB-INF\images\<%=i.getPicture() %>" width = "" heigh=""/><br>
产地:<%=i.getCity() %><br>
价格:<%=i.getPricce() %><br>
<%
}
}
%>
</body>
</html>