博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cookie实现:您曾经浏览过的商品记录
阅读量:5298 次
发布时间:2019-06-14

本文共 6241 字,大约阅读时间需要 20 分钟。

1、CookieHistoryDemo1.java代码实现如下:

import java.io.IOException;      import java.io.PrintWriter;      import java.util.Map;      import java.util.Set;            import javax.servlet.ServletException;      import javax.servlet.http.Cookie;      import javax.servlet.http.HttpServlet;      import javax.servlet.http.HttpServletRequest;      import javax.servlet.http.HttpServletResponse;      public class CookieHistoryDemo1 extends HttpServlet {          public void doGet(HttpServletRequest request, HttpServletResponse response)                  throws ServletException, IOException {              response.setContentType("text/html;charset=UTF-8");              PrintWriter out = response.getWriter();              // 1.显示网站所有商品              out.write("本网站有如下书籍:
"); Set
> set=DB.getAll().entrySet(); for(Map.Entry
me : set){ Book book= me.getValue(); out.write("
"+book.getName()+""); out.write("
"); } //2.显示用户曾经浏览过的商品 out.write("
您曾经浏览过的商品:
" ); Cookie cookies[] = request.getCookies(); for(int i=0;cookies!=null && i
"); } } } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

2,CookieHistoryDemo2.java

import java.io.IOException;      import java.io.PrintWriter;      import java.util.Arrays;      import java.util.LinkedList;      import java.util.List;      import javax.servlet.ServletException;      import javax.servlet.http.Cookie;      import javax.servlet.http.HttpServlet;      import javax.servlet.http.HttpServletRequest;      import javax.servlet.http.HttpServletResponse;                        public class CookieHistoryDemo2 extends HttpServlet {          public void doGet(HttpServletRequest request, HttpServletResponse response)                  throws ServletException, IOException {              response.setContentType("text/html;charset=UTF-8");              PrintWriter out = response.getWriter();              //1.根据用户带过来的id号,显示商品的详细信息              String id = request.getParameter("id");              Book book = (Book) DB.getAll().get(id);                            out.write("您要查看的书的详细信息如下:
"); out.write(book.getId() + "
"); out.write(book.getName() + "
"); out.write(book.getAuthor() + "
"); out.write(book.getDescription() + "
"); //2.给用户回送包含当前商品id的cookie String bookHistory = makeHistory(request,id); // 3_2 Cookie cookie = new Cookie("bookHistory",bookHistory); cookie.setMaxAge(1*30*24*3600); response.addCookie(cookie); } private String makeHistory(HttpServletRequest request, String id) { String bookHistory = null; Cookie cookies[] = request.getCookies(); for(int i=0;cookies!=null&&i
list = new LinkedList(); list.addAll(l); if(list.contains(id)){ // bookHistory=3_1_5 1 bookHistory=1_3_5 list.remove(id); list.addFirst(id); }else{ if(list.size()>=3){ // bookHistory=3_2_5 1 bookHistory=1_3_2 list.removeLast(); list.addFirst(id); }else{ // bookHistory=3_2 1 bookHistory=1_3_2 list.addFirst(id); } } StringBuffer sb = new StringBuffer(); //2_3_4 for(String lid: list){ sb.append(lid + "_"); } return sb.deleteCharAt(sb.length()-1).toString(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }

3,DB.java

import java.util.LinkedHashMap;      import java.util.Map;                  public class DB {          private static Map
map = new LinkedHashMap(); static{ map.put("1", new Book("1","javaweb","Job","New book")); map.put("2", new Book("2","spring","Hitpop","Good book")); map.put("3", new Book("3","hibernate","Brucec","Nice book")); map.put("4", new Book("4","struts","Smith","Pass book")); map.put("5", new Book("5","ajax","Zhangli","Hand book")); } public static Map getAll(){ return map; } }

4,Book.java

public class Book {          private String id;          private String name;          private String author;          private String description;          public Book() {              super();              // TODO Auto-generated constructor stub          }          public Book(String id, String name, String author, String description) {              super();              this.id = id;              this.name = name;              this.author = author;              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 getAuthor() {              return author;          }          public void setAuthor(String author) {              this.author = author;          }          public String getDescription() {              return description;          }          public void setDescription(String description) {              this.description = description;          }            }

转载于:https://www.cnblogs.com/lichone2010/archive/2013/06/09/3128113.html

你可能感兴趣的文章
代码为什么需要重构
查看>>
TC SRM 593 DIV1 250
查看>>
SRM 628 DIV2
查看>>
2018-2019-2 20165314『网络对抗技术』Exp5:MSF基础应用
查看>>
Python-S9-Day127-Scrapy爬虫框架2
查看>>
使用Chrome(PC)调试移动设备上的网页
查看>>
使用gitbash来链接mysql
查看>>
SecureCRT的使用方法和技巧(详细使用教程)
查看>>
右侧导航栏(动态添加数据到list)
查看>>
81、iOS本地推送与远程推送详解
查看>>
虚拟DOM
查看>>
uva 11468 Substring
查看>>
自建数据源(RSO2)、及数据源增强
查看>>
BootStrap2学习日记2--将固定布局换成响应式布局
查看>>
关于View控件中的Context选择
查看>>
2018icpc徐州OnlineA Hard to prepare
查看>>
Spark的启动进程详解
查看>>
使用命令创建数据库和表
查看>>
数据库的高级查询
查看>>
机器视觉:SSD Single Shot MultiBox Detector
查看>>