利用cookie实现历史记录浏览:

由于是简单演示,所以直接用javabean 取代数据库了

数据存储类:

package com.dzq.dao;

import java.util.*;

import com.dzq.domain.BookBean;

public class BookDao {
private static Map<String, BookBean> bookMap=new LinkedHashMap<String, BookBean>();
private BookDao(){ }
static { bookMap.put("1", new BookBean("1","三国演义","99.0","肚肚","河马出版","那人的故事"));
bookMap.put("2", new BookBean("2","西御街","99.0","肚吱吱","河马出版","那人的故事"));
bookMap.put("3", new BookBean("3","疏忽转","99.0","肚吱子","河马出版","那人的故事"));
bookMap.put("4", new BookBean("4","啪啪啪","99.0","蔺泽春","河马出版","那人的故事")); }
public static Map<String,BookBean> getBooks(){
return bookMap;
}
public static BookBean getBook(String id){
return bookMap.get(id); }
}

  javaBean 类:

package com.dzq.domain;

import java.io.Serializable;

public class BookBean  implements Serializable{
private String id;
private String name;
private String price;
private String auth;
private String publish;
private String discribe; public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAuth() {
return auth;
}
public void setAuth(String auth) {
this.auth = auth;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
} public String getDiscribe() {
return discribe;
}
public void setDiscribe(String discribe) {
this.discribe = discribe;
}
public BookBean(){ }
public BookBean(String id, String name, String price, String auth,
String publish, String discribe) {
this.id = id;
this.name = name;
this.price = price;
this.auth = auth;
this.publish = publish;
this.discribe = discribe;
} }

  显示历史图书信息和图书概览的servlet

import java.io.IOException;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.dzq.dao.BookDao;
import com.dzq.domain.BookBean; @WebServlet("/BookListServlet")
public class BookListServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//查询所有书并显示,
Map<String,BookBean> map=BookDao.getBooks();
for(Map.Entry<String, BookBean> entry:map.entrySet()){
BookBean book=entry.getValue();
response.getWriter().write("<a href='BookInfoServlet?id="+book.getId()+"'>"+book.getName()+"</a><br/>");
}
response.getWriter().write("<hr>");
//显示之前浏览的书
Cookie [] cs=request.getCookies();
Cookie findc=null;
if(cs!=null){
for(Cookie c:cs){
if("last".equals(c.getName())){
findc=c;
}
}
}
if(findc==null){
response.getWriter().write("没有看过任何书");
}else{
response.getWriter().write("你最后看过的书:<br>");
String[] ids=findc.getValue().split(","); for(String id:ids){
BookBean book=BookDao.getBook(id);
response.getWriter().write(book.getName()+"<br/>");
}
} } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }

  显示详细图书信息的servlet

package com.dzq.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.dzq.dao.BookDao;
import com.dzq.domain.BookBean; @WebServlet("/BookInfoServlet")
public class BookInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String id=request.getParameter("id");
BookBean book=BookDao.getBook(id);
if(book==null){
response.getWriter().write("找不到该书");
}else{ response.getWriter().write("<h1>书名:"+book.getName()+"<h1/>");
response.getWriter().write("<h3>作者:"+book.getAuth()+"<h3/>");
response.getWriter().write("<h3>价格:"+book.getPrice()+"<h3/>");
response.getWriter().write("<h3>出版社:"+book.getPublish()+"<h3/>");
response.getWriter().write("<h3>描述:"+book.getDiscribe()+"<h3/>");
}
//显示之前的书
String ids="";
Cookie [] cs=request.getCookies();
Cookie findc=null;
if(cs!=null){
for(Cookie c:cs){
if("last".equals(c.getName())){
findc=c;
}
}
}
if(findc==null){
//说明之前没看过书
ids+=book.getId();
}else{ //说明之前看过书
String[] olds=findc.getValue().split(",");
StringBuffer buffer=new StringBuffer();
buffer.append(book.getId()+",");
for(int i=0;i<olds.length&&buffer.toString().split(",").length<3;i++){
String old=olds[i];
if(!old.equals(book.getId())){
buffer.append(old+",");
}
}
ids=buffer.substring(0, buffer.length()-1);
} Cookie lastC=new Cookie("last", ids);
lastC.setMaxAge(3600*24*30);
lastC.setPath(request.getContextPath());
response.addCookie(lastC);
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
} }

  功能:显示历史浏览的三本书信息,并按照浏览顺序排序,最新浏览的排在最前面

20160328 javaweb Cookie 小练习的更多相关文章

  1. Cookie小案例-----记住浏览过的商品记录

    Cookie小案例------记住浏览过的商品记录 我们知道,这个功能在电商项目中非经常见.这里处理请求和页面显示都是由servlet实现,主要是为了体现cookie的作用, 实现功能例如以下: 1, ...

  2. JavaWeb Cookie详解

    代码地址如下:http://www.demodashi.com/demo/12713.html Cookie的由来 首先我们需要介绍一下,在Web开发过程中为什么会引入Cookie.我们知道Http协 ...

  3. Javaweb Cookie机制

    Javaweb Cookie机制 一.前言 HTTP协议是一种无状态的协议,WEB服务器本身不能识别出哪些请求是同一个浏览器发出的 ,浏览器的每一次请求都是完全孤立的,即使 HTTP1.1 支持持续连 ...

  4. JavaWeb:Cookie处理和Session跟踪

    JavaWeb:Cookie处理和Session跟踪 Cookie处理 什么是Cookie Cookie 是存储在客户端计算机上的文本文件,保留了各种跟踪信息.因为HTTP协议是无状态的,即服务器不知 ...

  5. 入门:JavaWeb Cookie

    总结: JavaWeb 利用Cookie 存储在本地用户名和密码,设置Cookie的生存时间. 两个页面,一个登陆页面,一个登陆后的页面,在登陆页面选择是否保存Cookie(保存Cookie,下次自动 ...

  6. cookie小栗子-实现简单的身份验证

    关于Cookie Cookie是一种能够让网站Web服务器把少量数据储存到客户端的硬盘或内存里,或是从客户端的硬盘里读取数据的一种技术. 用来保存客户浏览器请求服务器页面的请求信息,可以在HTTP返回 ...

  7. JavaWeb Cookie

    1. Cookie 1.1. Cookie概述 Cookie译为小型文本文件或小甜饼,Web应用程序利用Cookie在客户端缓存服务器端文件.Cookie是以键值对形式存储在客户端主机硬盘中,由服务器 ...

  8. JavaWeb -- Cookie应用实例 -- 购物历史记录

    1. 页面一:主页面                                         页面二: 详细显示页面   Demo2 负责页面一, 显示商品清单和历史记录 Demo3负责页面二 ...

  9. JavaWeb——Cookie,Session学习汇总

    什么是Cookie Cookie的作用 安全性能 Cookie的语法 Cookie注意细节 Cookie实例练习 什么是会话Session Session语法 Session与浏览器窗口的关系 ses ...

随机推荐

  1. 生产环境下JAVA进程高CPU占用故障排查

    问题描述:生产环境下的某台tomcat7服务器,在刚发布时的时候一切都很正常,在运行一段时间后就出现CPU占用很高的问题,基本上是负载一天比一天高. 问题分析:1,程序属于CPU密集型,和开发沟通过, ...

  2. [POJ2084]Game of Connections

      Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7888   Accepted: 3965 Description Thi ...

  3. 【CSS】Intermediate2:Grouping and Nesting

    1.Grouping 2.Nesting If the CSS is structured well, there shouldn’t be a need to use many class or I ...

  4. nyoj VF函数

    大意就是: 在1到在10的9次方中,找到各个位数和为固定值s的数的个数, 首先我们确定最高位的个数,为1到9: 以后的各位为0,到9: 运用递归的思想,n位数有n-1位数生成 f(n)(s) +=f( ...

  5. Linux下安装mysql5.6.11(找点有用的信息太费劲)(转)

    Linux下安装mysql5.6.11(找点有用的信息太费劲) (2013-04-25 10:25:09)     1.申请阿里云Linux服务器 昨天在阿里云申请了一个免费试用5天的Linux云服务 ...

  6. Hadoop学习记录(4)|MapReduce原理|API操作使用

    MapReduce概念 MapReduce是一种分布式计算模型,由谷歌提出,主要用于搜索领域,解决海量数据计算问题. MR由两个阶段组成:Map和Reduce,用户只需要实现map()和reduce( ...

  7. Codeforces335B - Palindrome(区间DP)

    题目大意 给定一个长度不超过5*10^4的只包含小写字母的字符串,要求你求它的回文子序列,如果存在长度为100的回文子序列,那么只要输出长度为一百的回文子序列即可,否则输出它的最长回文子序列 题解 这 ...

  8. 两个栈实现一个队列,C语言实现,队列可伸缩,容纳任意数目的元素。

    一.思路:1.创建两个空栈A和B:2.A栈作为队列的入口,B栈作为队列的出口:3.入队列操作:即是入栈A:4.出队列操作:若栈B为空,则将A栈内容出栈并压人B栈,再出 B栈:不为空就直接出栈: 二.代 ...

  9. mysql常见优化,更多mysql,Redis,memcached等文章

    mysql常见优化 http://www.cnblogs.com/ggjucheng/archive/2012/11/07/2758058.html 更多mysql,Redis,memcached等文 ...

  10. Yii Active Record 查询结果转化成数组

    使用Yii 的Active Record 来获取查询结果的时候,返回的结果集是一个对象类型的,有时候为了数据处理的方便希望能够转成数组返回.比如下面的方法: // 查找满足指定条件的结果中的第一行 $ ...