栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

本文实例为大家分享了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 bookMap = new linkedHashMap();
 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 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 map = BookDao.getBooks();
  for(Map.Entry entry : map.entrySet()){
   Book book = entry.getValue();
   response.getWriter().write(""+book.getName()+"
"); } response.getWriter().write("
"); //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("您曾经浏览过的书:
"); String[] ids = findC.getValue().split(","); for(String id : ids){ Book book = BookDao.getBook(id); response.getWriter().write(book.getName()+"
"); } } }

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("书名:"+book.getName()+"");
   response.getWriter().write("作者:"+book.getAuth()+"");
   response.getWriter().write("售价:"+book.getPrice()+"");
   response.getWriter().write("出版社:"+book.getPublish()+"");
   response.getWriter().write("描述信息:"+book.getDescription()+"");
  }
   
  //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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/150280.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号