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>

Cookie 简单使用记录浏览记录的更多相关文章

  1. cookie记录浏览记录

    cookie记录浏览记录 HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在.在HashMap中,key-value总是会当做 ...

  2. 简单的Cookie记录浏览记录案例

    books.jsp 界面 代码 <%@ page contentType="text/html;charset=UTF-8" language="java" ...

  3. Cookie中图片的浏览记录与cookie读取servle时路径的设置(文字描述)

    public class ShowServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpSer ...

  4. js操作Cookie,实现历史浏览记录

    /** * history_teacher.jsp中的js,最近浏览名师 * @version: 1.0 * @author: mingming */ $(function(){ getHistory ...

  5. 用JS中的cookie实现商品的浏览记录

    最近在做一个购物车效果,为了实现商品的浏览记录效果可是让我百般周折,避免以后忘记特写此随笔与大家共享,希望博友们看后有所收获. 第一步:在一个公用的js文件下getCookie(“liulan”),c ...

  6. 使用cookie实现打印浏览记录的功能

    可以用cookie知识来实现打印浏览记录.这里面用到的思路是将浏览记录以字符串的方式保存到cookie中,当浏览记录增加时,再将其转化为数组. $uri=$_SERVER['REQUEST_URI'] ...

  7. destoon系统开发-最新利用浏览器的cookie 做历史浏览记录

      注意: 代码 放在要显示的为 (一般放在详情页),注意本教程不入库,直接利用浏览器的 cookie 缓存判断    <!--历史浏览记录 S--> <div class=&quo ...

  8. 使用Cookie实现用户商品历史浏览记录

    该功能分为四个模块: 1. 获取所有商品并以链接的形式显示 out.write("网站商品: <br/>"); Map<String, Book> book ...

  9. Java遇见HTML——JSP篇之商品浏览记录的实现

    一.项目总体介绍 使用Cookie实现商品浏览记录. 要实现这个程序采取的是Model1(Jsp+JavaBean)架构实现,具体步骤: 首先要有个数据库,商品表,操作数据库的一个类DBHelper类 ...

随机推荐

  1. kvm的live-snapshot

    目前项目中已经存在的快照是针对卷的快照,并且需要关机.所以目前的需求有两个:1.不关机快照:2.针对虚拟机的快照,而不是针对券的快照. 由需求所以针对libvirt做了一些实验,纪录如下: 环境:物理 ...

  2. JavaScript 小技巧汇总

    判断一个变量是否申明 if (typeof v === "undefined") { // ... } 判断一个变量是否是函数 function f() {} typeof f / ...

  3. Review Board的使用

    代码审核工具.先在命令行界面,进入到工程的Main目录下,然后使用命令 svn diff>yus.diff  这样就将Main里面的所有内容生成了,然后在浏览器里进入到自己的Review Boa ...

  4. logstash安装配置

    vim /usr/local/logstash/etc/hello_search.conf 输入下面: input { stdin { type => "human" }} ...

  5. android 手势识别学习

    引自http://www.cnblogs.com/android100/p/android-hand.html    http://blog.csdn.net/jiangshide/article/d ...

  6. 【转载】彻底弄懂css中单位px和em,rem的区别

    原文链接:http://www.cnblogs.com/leejersey/p/3662612.html 国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什 ...

  7. 解决cookie 跨iframe

    document.cookie = "name=caoyc;path=/"document.cookie = "age=13;path=/"//时间可以不要,但 ...

  8. WebDriver(Selenium2) 判断页面是否刷新的方法

    http://uniquepig.iteye.com/blog/1568208 public static boolean waitPageRefresh(WebElement trigger) { ...

  9. 用Java开源项目JOONE实现人工智能编程

    http://www.robotsky.com/ZhiN/MoS/2011-08-25/13142461416649.html 用Java开源项目JOONE实现人工智能编程 https://sourc ...

  10. ecstore中kvstore之mongodb

    mongodb安装 详细见 http://blog.csdn.net/motian06/article/details/17560067 mongodb扩展安装 详细见 http://blog.csd ...