程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java應用cookie顯示比來檢查過的書

Java應用cookie顯示比來檢查過的書

編輯:關於JAVA

Java應用cookie顯示比來檢查過的書。本站提示廣大學習愛好者:(Java應用cookie顯示比來檢查過的書)文章只能為提供參考,不一定能成為您想要的結果。以下是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<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顯示比來檢查過的書的辦法有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved