当前位置: 移动技术网 > IT编程>开发语言>Java > Java使用cookie显示最近查看过的书

Java使用cookie显示最近查看过的书

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了java使用cookie显示最近查看过的书的相关方法,供大家参考,具体内容如下

1.ben包    

import java.io.serializable;
 
public class book implements serializable {
 private string id;
 private string name;
 private string price;
 private string auth;
 private string publish;
 private string description;
  
 public book() {
 }
  
 public book(string id, string name, string price, string auth,
   string publish, string description) {
  super();
  this.id = id;
  this.name = name;
  this.price = price;
  this.auth = auth;
  this.publish = publish;
  this.description = description;
 }
 
 public string getdescription() {
  return description;
 }
 
 public void setdescription(string description) {
  this.description = description;
 }
 
 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;
 }
 
}

2.dao包    

import java.util.linkedhashmap;
import java.util.map;
 
import cn.huiyu.ben.book;
 
 
 
public class bookdao {
 private static map<string,book> bookmap = new linkedhashmap<string, book>();
 private bookdao() {
 }
 static{
  bookmap.put("1", new book("1","1111","11.0","zqwang","111出版社","111111111"));
  bookmap.put("2", new book("2","2222","22.0","zqwang","222出版社","222222222"));
  bookmap.put("3", new book("3","3333","33.0","zqwang","333出版社","333333333"));
 }
  
 public static map<string,book> getbooks(){
  return bookmap;
 }
  
 public static book getbook(string id){
  return bookmap.get(id);
 }
}

3.servlet    

public void doget(httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  response.setcontenttype("text/html;charset=utf-8");
  //1.查询数据库中所有的书展示
  map<string,book> map = bookdao.getbooks();
  for(map.entry<string , book> entry : map.entryset()){
   book book = entry.getvalue();
   response.getwriter().write("<a href='"+request.getcontextpath()+"/servlet/bookinfoservlet?id="+book.getid()+"'>"+book.getname()+"</a><br>");
  }
  response.getwriter().write("<hr>");
   
  //2.显示之前看过的书
  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){
    book book = bookdao.getbook(id);
    response.getwriter().write(book.getname()+"<br>");
   }
  }
 }

4.servlet

public void doget(httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  response.setcontenttype("text/html;charset=utf-8");
  //1.获取要看的书的id,查询数据库找出书,输出书的详细信息
  string id = request.getparameter("id");
  book book = bookdao.getbook(id);
  if(book==null){
   response.getwriter().write("找不到这本书!");
   return;
  }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.getdescription()+"</h3>");
  }
   
  //2.发送cookie保存最后看过的书
  // --- 1 --> 1
  // 1 --2,1 --> 2,1
  // 2,1--3,2,1 --> 3,2,1
  // 3,2,1 -- 4,3,2 --> 4,3,2
  // 4,3,2 --3,4,2 --> 3,4,2
  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);
 }

以上就是本文的全部内容,希望对大家学习java使用cookie显示最近查看过的书的方法有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网