Cookie 简单使用记录浏览记录
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() %> 价格:<%=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 简单使用记录浏览记录的更多相关文章
- cookie记录浏览记录
cookie记录浏览记录 HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以key-value的形式存在.在HashMap中,key-value总是会当做 ...
- 简单的Cookie记录浏览记录案例
books.jsp 界面 代码 <%@ page contentType="text/html;charset=UTF-8" language="java" ...
- Cookie中图片的浏览记录与cookie读取servle时路径的设置(文字描述)
public class ShowServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpSer ...
- js操作Cookie,实现历史浏览记录
/** * history_teacher.jsp中的js,最近浏览名师 * @version: 1.0 * @author: mingming */ $(function(){ getHistory ...
- 用JS中的cookie实现商品的浏览记录
最近在做一个购物车效果,为了实现商品的浏览记录效果可是让我百般周折,避免以后忘记特写此随笔与大家共享,希望博友们看后有所收获. 第一步:在一个公用的js文件下getCookie(“liulan”),c ...
- 使用cookie实现打印浏览记录的功能
可以用cookie知识来实现打印浏览记录.这里面用到的思路是将浏览记录以字符串的方式保存到cookie中,当浏览记录增加时,再将其转化为数组. $uri=$_SERVER['REQUEST_URI'] ...
- destoon系统开发-最新利用浏览器的cookie 做历史浏览记录
注意: 代码 放在要显示的为 (一般放在详情页),注意本教程不入库,直接利用浏览器的 cookie 缓存判断 <!--历史浏览记录 S--> <div class=&quo ...
- 使用Cookie实现用户商品历史浏览记录
该功能分为四个模块: 1. 获取所有商品并以链接的形式显示 out.write("网站商品: <br/>"); Map<String, Book> book ...
- Java遇见HTML——JSP篇之商品浏览记录的实现
一.项目总体介绍 使用Cookie实现商品浏览记录. 要实现这个程序采取的是Model1(Jsp+JavaBean)架构实现,具体步骤: 首先要有个数据库,商品表,操作数据库的一个类DBHelper类 ...
随机推荐
- Oracle新建用户赋只读某几张表的权限
create user JSETI_WZQ identified by abcdef; -- 假设abcdef是密码 grant connect,resource to JSETI_WZQ; gran ...
- SGU_390_Tickets(另类数位DP)
Tickets Time Limit : 1000/500ms (Java/Other) Memory Limit : 524288/262144K (Java/Other) Total Subm ...
- AnimationSet的使用
Animations的使用(3) 1 AnimationSet的使用方法 什么是AnimationSet 1 AnimationSet是Animation的子类 2 一个AnimationSet包含了 ...
- Server对象
Server是服务器对象,定义了一个与Web服务器相关的类,用于访问服务器上的资源. 属性 MachineName 获取服务器的计算机名. 返回本地计算机的名称 ScriptTimeout ...
- ajax 跨域请求资源问题
其实相当的简单:只需要在服务端设置一下响应头: header("Access-Control-Allow-Origin: *");就可以了!! nice,有木有? 下面两句也可以带 ...
- js 中ajax请求时设置 http请求头中的x-requestd-with= ajax
今天发现 AngularJS 框架的$http服务提供的$http.get() /$http.post()的ajax请求中没有带 x-requested-with字段. 这样的话,后端的php 就无法 ...
- 页面新宠图片格式WebP
WebP格式,谷歌(google)开发的一种旨在加快图片加载速度的图片格式.图片压缩体积大约只有JPEG的2/3,并能节省大量的服务器带宽资源和数据空间.Facebook Ebay等知名网站已经开始测 ...
- 总结自己的Git常用命令
总结自己的Git常用命令 使用git也有一段时间了,把自己常用的命令用自己的描述记录起来,方便自己备忘也方便其他人参考. 目录: 最基本的命令: git clone 拷贝并跟踪远程的master分支. ...
- [转] Linux下移动virtualbox虚拟硬盘丢失eth0
1.遇到什么的问题(What) 在新的virtualbox虚拟机上挂上曾使用过的虚拟硬盘,在启动的时候,发现找不到网卡eth0, 在输入ifconfig –a的时候,也没有任何Ethnet的 ...
- 使用python将mysql数据库的数据转换为json数据
由于产品运营部需要采用第三方个推平台,来推送消息.如果手动一个个键入字段和字段值,容易出错,且非常繁琐,需要将mysql的数据转换为json数据,直接复制即可. 本文将涉及到如何使用Python访问M ...