利用Cookie记录商品浏览信息
2017-07-31 22:37
288 查看
利用Cookie记录商品浏览信息
Web客户端和Web服务端是基于Http协议进行通信的,Http协议具有无状态的特点,服务端只能被动的受理客户端的Http请求,并且不知道该Http是来自于那个客户端。
在实际开发环境中,为了能让服务器在一定程度上可以识别客户端,常利用会话技术来保存客户端和服务端之间的共同信息,通过这些信息,服务器可以获得客户端的一些状态。通过将信息保存在客户端和服务端,会话技术右细分为两类:Cookie和Session。Cookie和Session的一个重要的区别就是Cookie将信息保存在客户端,Session将信息保存在服务端。本案例涉及Cookie的基本用法,并且在Cookie的基础上完成商品浏览信息的记录。
1.基础声明
pid字段是数据库中product表记录的主键,是product的唯一标识,通过pid字段可以唯一的查询到商品信息。select*
from product where pid = ?
2.实现思路
图 2-1实现思路
在上述的实现思路中,主要涉及两个部分的内容:
2.1是如何创建一个Cookie可以保存浏览过的商品的Pid
在此种需求下,不难分析出,有很多个商品浏览记录,也就是需要保存多个pid的值,但是每一个Cookie只能保存一个键值对(Key-value),可以创建多个Cookie保存多个键值队,但是这样开销过大,为了降低开销我们采取拼串的方法利用一个Cookie保存键值对。下面贴出创建Cookie拼串的代码。
Web客户端和Web服务端是基于Http协议进行通信的,Http协议具有无状态的特点,服务端只能被动的受理客户端的Http请求,并且不知道该Http是来自于那个客户端。
在实际开发环境中,为了能让服务器在一定程度上可以识别客户端,常利用会话技术来保存客户端和服务端之间的共同信息,通过这些信息,服务器可以获得客户端的一些状态。通过将信息保存在客户端和服务端,会话技术右细分为两类:Cookie和Session。Cookie和Session的一个重要的区别就是Cookie将信息保存在客户端,Session将信息保存在服务端。本案例涉及Cookie的基本用法,并且在Cookie的基础上完成商品浏览信息的记录。
1.基础声明
pid字段是数据库中product表记录的主键,是product的唯一标识,通过pid字段可以唯一的查询到商品信息。select*
from product where pid = ?
2.实现思路
图 2-1实现思路
在上述的实现思路中,主要涉及两个部分的内容:
2.1是如何创建一个Cookie可以保存浏览过的商品的Pid
在此种需求下,不难分析出,有很多个商品浏览记录,也就是需要保存多个pid的值,但是每一个Cookie只能保存一个键值对(Key-value),可以创建多个Cookie保存多个键值队,但是这样开销过大,为了降低开销我们采取拼串的方法利用一个Cookie保存键值对。下面贴出创建Cookie拼串的代码。
Cookie[] cookieArray = request.getCookies(); String recorder=""; if(cookieArray!=null){ for(Cookie cookie:cookieArray){ //recorder是预先设计的Key值 if("recorder".equals(cookie.getName())){ recorder =cookie.getValue(); //这里预先设置每个pid之间利通“-”分割 String [] pids = recorder.split("-"); //保证List集合中没有重复pid //并且最新的pid永远放在链表的前面 //这里常进行插入操作,所以用链表 List<String> list =Arrays.asList(pids); LinkedList<String>linkedList =new LinkedList<String>(list); if(recorder.contains(id)){ linkedList.remove(id); } linkedList.addFirst(id); StringBuffer stringBuffer = new StringBuffer(); //从链表中恢复前num个字符串, for(int i=0;i<linkedList.size()&&i<7;i++){ stringBuffer.append(linkedList.get(i)+"-"); } recorder= stringBuffer.toString(); recorder=recorder.substring(0,recorder.length()-1); } } cookie ck= new Cookie("recorder",recorder); ck.setMaxAge(60*60*24); response.addCookie(ck);2.2如何从Cookie解析出商品的pid,借助解析的pid,查询数据库将结果返回。
2.2.1 Web层代码Cookie[] cookies = request.getCookies(); String result=null; if(cookies!=null){ for(Cookie cookie:cookies){ if(cookie.getName().equals("recorder")){ result = cookie.getValue(); String [] pids = result.split("-"); //其实将Service换成单例模式比较好 ProductService service =new ProductService(); try { List<Product>list = service.getProductListByPids(pids); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } }2.2.2 Service层代码 //这里考虑代码的清晰,还是避免频繁的堆栈调用可选用不同的方式实现 public List<Product> getProductListByPids(String[] pids) throws SQLException{ // TODO Auto-generated method stub List<Product>list =new ArrayList<>(); ProductDao dao =new ProductDao(); //这里考虑了解耦合的方式 for(String pid:pids){ list.add(dao.getProductBypid(pid)); } return list; } 2.2.3 Dao层代码 public Product getProductBypid(String pid) throws SQLException { // TODO Auto-generated method stub QueryRunner queryRunner = new QueryRunner(ConnectionUtils.getDataSource()); String sql = "select * from product where pid=?"; Product product = queryRunner.query(sql,new BeanHandler<Product>(Product.class),pid); return product; } |
相关文章推荐
- JavaWeb的Cookie技术显示历史商品( 信息 )浏览记录(学习方立勋老师的教程)
- 自己写的一个php利用cookie记录浏览(商品)历史记录的函数
- 利用cookie显示商品的浏览记录
- 【JSP开发】利用cookie实现商品浏览记录
- 【JSP开发】利用cookie实现商品浏览记录
- 利用Cookie实现用户浏览商品记录
- 利用Cookie显示商品浏览历史记录
- 【JSP开发】利用cookie实现商品浏览记录
- 利用Cookie记录用户浏览的图片
- cookie记录用户的浏览商品的路径
- servlet之利用Cookie显示最近浏览的商品
- java(16)--利用cookie显示商品浏览历史
- JavaScript 利用Cookie记录用户登录信息
- 【Cookie:默认存储在浏览器的内存中】显示上一次访问时间、商品浏览记录
- 使用Cookie保存商品浏览记录
- 记录用户的商品浏览记录(Linkedlist,删除cookie)
- 《JavaWeb---利用cookie记录用户的历史浏览》
- 利用COOKIE实现最近浏览商品
- 利用Cookie显示最近浏览的商品
- javaweb之Cookie显示商品的浏览记录和Cookie的常见应用有哪些