一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java使用cookie顯示最近查看過的書

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

2020-04-18 12:12zqwang121 JAVA教程

這篇文章主要為大家詳細介紹了Java使用cookie顯示最近查看過的書,感興趣的小伙伴們可以參考一下

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

1.ben包    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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包    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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顯示最近查看過的書的方法有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本妇人成熟免费观看18 | 视频国产精品 | 微福利92合集| 肥胖女性大bbbbbb视频女厕 | 操乳| 成人女人天堂午夜视频 | 天堂网www在线中文天堂 | 99在线资源 | 猛h辣h高h文湿校园1v1 | 男插女的下面免费视频夜色 | 99一区二区三区 | 久久免费国产 | 青青艹视频在线 | 精品综合久久久久久8888 | 精品视频手机在线观看免费 | 99r视频在线观看 | 夫妻性生活一级黄色片 | 东北美女野外bbwbbw免费 | 亚洲香蕉伊在人在线观婷婷 | 天堂伊人 | 四虎最新免费观看网址 | 欧美人在线一区二区三区 | 动漫美女被吸乳羞羞小说 | 2015小明台湾永久区域免费 | 亚洲精品中文字幕第一区 | 美女张开腿黄网站免费精品动漫 | 亚洲爱视频 | 亚洲大爷操 | 免费国产午夜高清在线视频 | 爱操综合网 | 青苹果乐园影院免费观看完整版 | 97色资源 | 91碰碰| 日本无遮挡亲吻膜下面免费 | 极限淫生小说 | 国产精品久久久久久久牛牛 | 500av导航大全精品 | 日本特级大片 | 亚洲毛片网 | 全黄一级裸片视频免费 | 91夜夜操 |