Cookie案例-显示商品浏览历史纪录

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

               

在购物网站中,最常见的一个功能就是当年浏览完几个商品之后,再次访问这个网站,网站会提醒你,你上次看了什么什么商品,或者当你选择好几个商品放入购物车时候最终的结账,都有这个东西,大概是使用Cookie实现的,最终还是Servlet的载入,下面就来实现这个案例

1.首先是首页执行显示的Servlet,他的功能是输出网站的所有商品和显示最近浏览过什么商品

package com.bird.cookie;import java.io.IOException;import java.io.PrintWriter;import java.util.LinkedHashMap;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @category 网站的首页,用来显示所有的信息 * @author Bird * */public class CookieDemo2 extends HttpServlet private static final long serialVersionUID = 1Lpublic void doGet(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {  //解决乱码问题  response.setCharacterEncoding("UTF-8");//控制服务器编码方式  response.setContentType("text/html;charset=UTF-8");  PrintWriter out = response.getWriter();        //1.输出网站的所有商品  out.write("本网站有如下商品<br/>");  Map<String,Book> map = DB.getAll();  for(Map.Entry<String, Book> entry : map.entrySet()){   Book book = entry.getValue();   out.print("<a href='/WebDemo/servlet/CookieDemo3?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a><br/>");  }    //2.显示用户看过的商品  out.write("<br/>您曾经看过如下商品<br/>");  Cookie cookies[] = request.getCookies();  for(int i = 0; cookies!=null && i < cookies.length; i++){   if(cookies[i].getName().equals("bookHistory")){    String []ids = cookies[i].getValue().split("\\,");    for(String s : ids){     Book book = DB.getAll().get(s);     out.write(book.getName()+"<br/>");    }   }  } }    public void doPost(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException { }}class DB//必须使用链表Map进行有序存储 private static Map<String,Book> map = new LinkedHashMap<String,Book>(); static{  map.put("1", new Book("1","javaweb开发详解","老张","一本好书!!!"));  map.put("2", new Book("2","jdbc开发详解","老张","一本好书!!!"));  map.put("3", new Book("3","sping开发详解","老黎","一本好书!!!"));  map.put("4", new Book("4","struts开发详解","老宇","一本好书!!!"));  map.put("5", new Book("5","heibrate开发详解","老毕","一本好书!!!")); }  public static Map<String,Book> getAll(){  return map; }}class Bookprivate String id; private String name; private String author; private String description;  public Book(){}  public Book(String id, String name, String author, String description) {  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; }  }

2.第二个Servlet是用来显示详细的商品信息的,还有一个功能就是用来发送cookie到 客户机,更新客户机的cookie,但是制作cookie的步骤比较麻烦。得考虑显示最近访问商品数的大小和链表的长度还有最近访问的商品一定得在上边,等等。

package com.bird.cookie;import java.io.IOException;import java.io.PrintWriter;import java.util.Arrays;import java.util.LinkedList;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @category 显示商品的详细信息 * @author Bird * */public class CookieDemo3 extends HttpServlet private static final long serialVersionUID = 1Lpublic void doGet(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {  response.setCharacterEncoding("UTF-8");  response.setContentType("text/html;charset=UTF-8");  PrintWriter out = response.getWriter();    //1.根据用户提交的id显示对应的商品的详细的信息    String id = request.getParameter("id");  Book book = DB.getAll().get(id);  out.write("id为"+book.getId()+"<br/>");  out.write("书名为"+book.getName()+"<br/>");  out.write("作者为"+book.getAuthor()+"<br/>");  out.write("描述为"+book.getDescription()+"<br/>");    //2.构建Cookie,回写给浏览器  String cookieValue = buildCookie(id,request);  Cookie cookie = new Cookie("bookHistory",cookieValue);  cookie.setMaxAge(30*24*60*60);  cookie.setPath("/WebDemo");  response.addCookie(cookie); } private String buildCookie(String id, HttpServletRequest request) {  String bookHisttory = null;  Cookie [] cookies = request.getCookies();  for(int i = 0; cookies!=null && i < cookies.length; i++){   bookHisttory = cookies[i].getValue();  }    if(bookHisttory==null)   return id;    LinkedList<String> list = new LinkedList<String>(Arrays.asList(bookHisttory.split("\\,")));  if(list.contains(id)){   list.remove(id);  }else{   if(list.size()>=3){    list.removeLast();   }  }  list.addFirst(id);    StringBuffer sb = new StringBuffer();  for(String bid: list){   sb.append(bid + ",");  }  return sb.deleteCharAt(sb.length()-1).toString();//删除最后多余 的一个逗号  } public void doPost(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException { }}

这个案例是cookie的一个比较完备的案例,应该说有很好的

           

分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/qq_43679366/article/details/87906211