最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java使用cookie顯示最近查看過的書

 更新時(shí)間:2016年04月19日 16:17:40   作者:zqwang121  
這篇文章主要為大家詳細(xì)介紹了Java使用cookie顯示最近查看過的書,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Java使用cookie顯示最近查看過的書的相關(guān)方法,供大家參考,具體內(nèi)容如下

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.查詢數(shù)據(jù)庫中所有的書展示
  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("您曾經(jīng)瀏覽過的書:<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,查詢數(shù)據(jù)庫找出書,輸出書的詳細(xì)信息
  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>售價(jià):"+book.getPrice()+"</h3>");
   response.getWriter().write("<h3>出版社:"+book.getPublish()+"</h3>");
   response.getWriter().write("<h3>描述信息:"+book.getDescription()+"</h3>");
  }
   
  //2.發(fā)送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{
   //說明之前有歷史看過的書的記錄,需要根據(jù)歷史記錄算一個(gè)新的記錄出來
   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);
 }

以上就是本文的全部內(nèi)容,希望對大家學(xué)習(xí)Java使用cookie顯示最近查看過的書的方法有所幫助。

相關(guān)文章

  • springboot 使用上下文獲取bean

    springboot 使用上下文獲取bean

    這篇文章主要介紹了springboot 使用上下文獲取bean,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Netty分布式Server啟動(dòng)流程服務(wù)端初始化源碼分析

    Netty分布式Server啟動(dòng)流程服務(wù)端初始化源碼分析

    本章主要講解server啟動(dòng)的關(guān)鍵步驟,?讀者只需要了解server啟動(dòng)的大概邏輯,?知道關(guān)鍵的步驟在哪個(gè)類執(zhí)行即可,?并不需要了解每一步的運(yùn)作機(jī)制,?之后會對每個(gè)模塊進(jìn)行深度分析
    2022-03-03
  • SpringBoot API增加version版本號方式

    SpringBoot API增加version版本號方式

    這篇文章主要介紹了SpringBoot API增加version版本號方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java中實(shí)現(xiàn)Comparable接口實(shí)現(xiàn)自定義排序的示例

    java中實(shí)現(xiàn)Comparable接口實(shí)現(xiàn)自定義排序的示例

    下面小編就為大家?guī)硪黄猨ava中實(shí)現(xiàn)Comparable接口實(shí)現(xiàn)自定義排序的示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • Java定時(shí)任務(wù)的三種實(shí)現(xiàn)方式

    Java定時(shí)任務(wù)的三種實(shí)現(xiàn)方式

    這篇文章主要給大家介紹了關(guān)于Java定時(shí)任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-01-01
  • Mybatis逆向工程筆記小結(jié)

    Mybatis逆向工程筆記小結(jié)

    MyBatis官方為我們提供了一個(gè)逆向工程,通過這個(gè)逆向工程,只需要建立好數(shù)據(jù)表,MyBatis就會根據(jù)這個(gè)表自動(dòng)生成pojo類、mapper接口、sql映射文件,本文主要介紹了Mybatis逆向工程筆記小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-05-05
  • springboot中JSONObject遍歷并替換部分json值

    springboot中JSONObject遍歷并替換部分json值

    這篇文章主要介紹了springboot中JSONObject遍歷并替換部分json值,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • FastJson時(shí)間格式化問題避坑經(jīng)驗(yàn)分享

    FastJson時(shí)間格式化問題避坑經(jīng)驗(yàn)分享

    這篇文章主要為大家介紹了FastJson時(shí)間格式化問題避坑經(jīng)驗(yàn)分享,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • java中年月日的加減法使用示例

    java中年月日的加減法使用示例

    這篇文章主要介紹了java中年月日的加減法使用示例的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • java利用遞歸調(diào)用實(shí)現(xiàn)樹形菜單的樣式

    java利用遞歸調(diào)用實(shí)現(xiàn)樹形菜單的樣式

    這篇文章主要給大家介紹了關(guān)于java利用遞歸調(diào)用實(shí)現(xiàn)樹形菜單樣式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09

最新評論

永康市| 资源县| 高州市| 兴宁市| 木兰县| 黄平县| 建平县| 阆中市| 枣强县| 莒南县| 达日县| 海安县| 湘潭县| 鹤庆县| 江永县| 定西市| 深圳市| 策勒县| 北京市| 南靖县| 右玉县| 鹿邑县| 广汉市| 宁南县| 成都市| 兴宁市| 宁乡县| 聊城市| 桂东县| 从江县| 依兰县| 扎兰屯市| 潮州市| 涪陵区| 益阳市| 漯河市| 江川县| 克拉玛依市| 兴安县| 丰城市| 津南区|