利用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);
}
}
功能:显示历史浏览的三本书信息,并按照浏览顺序排序,最新浏览的排在最前面