博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cookie02--回显浏览过的商品
阅读量:5083 次
发布时间:2019-06-13

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

1.

package cn.itcast.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;//代表首页的servletpublic class CookieDemo3 extends HttpServlet {		@Override	protected void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		//1. 输出网站所有的商品		request.setCharacterEncoding("utf-8");		response.setContentType("text/html;charset=utf-8");		PrintWriter writer = response.getWriter();		writer.write("本网站有如下商品:
"); // 浏览器换行用br,java用/r/n //1.2 从DB中获取所有的商品信息 Map
map = DB.getAll(); for(Map.Entry
entry : map.entrySet()){ Book book = entry.getValue(); writer.print("
"+ book.getName() +"
"); } //2.显示用户曾经看过的商品 writer.print("
您曾经浏览过的商品:
"); 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 id : ids) { // 通过id去db中查找对应的书 Book book = DB.getAll().get(id); writer.print(book.getName() + "
"); } } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException ,IOException { this.doGet(request, response); }}class DB{ //想存的数据的顺序与显示的顺序一致,用LinkedHashMap private static Map
map = new LinkedHashMap
(); static{ //静态代码块确保只加载一次 map.put("1", new Book("1", "javaweb开发", "老张", "高薪书")); map.put("2", new Book("2", "andorid开发", "老张", "高薪书")); map.put("3", new Book("3", "ios开发", "老张", "高薪书")); map.put("4", new Book("4", "javaEE开发", "老张", "高薪书")); map.put("5", new Book("5", "疯狂的java开发", "老张", "高薪书")); } public static Map
getAll(){ return map; }}class Book{ private String id; private String name; private String author; private String description; public Book(){} 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; } }

  2.

package cn.itcast.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;public class CookieDemo4 extends HttpServlet {		@Override	protected void doGet(HttpServletRequest request, HttpServletResponse response)			throws ServletException, IOException {		request.setCharacterEncoding("utf-8");		response.setContentType("text/html;charset=utf-8");		PrintWriter out = response.getWriter();				//根据用户传过来的id,显示相应商品的详细信息		String id = request.getParameter("id");		Book book = DB.getAll().get(id);		out.write(book.getId() + "
"); out.write(book.getName() + "
"); out.write(book.getAuthor() + "
"); out.write(book.getDescription() + "
"); //构建cookie, 回写给浏览器 String cookieValue = buildCookie(id, request); Cookie cookie = new Cookie("bookHistory", cookieValue); cookie.setMaxAge(3600*24*30); cookie.setPath(request.getContextPath()); response.addCookie(cookie); } private String buildCookie(String id, HttpServletRequest request) { //bookHistory = null 1 1 //bookHistory = 2,5,1 1 1,2,5 //bookHistory = 2,5,4 1 1,2,5 //bookHistory = 2,5 1 1,2,5 Cookie[] cookies = request.getCookies(); String bookHistory = null; for(int i = 0; cookies != null && i < cookies.length; i++) { if(cookies[i].getName().equals("bookHistory")){ bookHistory = cookies[i].getValue(); } } if(bookHistory == null){ //第一次 return id; } LinkedList
list = new LinkedList
(Arrays.asList(bookHistory.split("\\,"))); /*if(list.contains(id)){ //当前浏览的商品在cookie中存在 list.remove(id); //把存在的删除 list.addFirst(id);//把当前的放在第一个 } else{ //不存在; if(list.size() >= 3){// 如何list满了把当前的放在第一个并删除最后一个 list.removeLast(); list.addFirst(id); } else{ //直接添加到list头部 list.addFirst(id); } }*/ if(list.contains(id)){ list.remove(id); } else{ if(list.size() >= 3){ list.removeLast(); } } list.addFirst(id); StringBuilder sb = new StringBuilder(); for(String bookId : list) { sb.append(bookId).append(","); } sb.deleteCharAt(sb.length() - 1); bookHistory = sb.toString(); return bookHistory; }}

  

转载于:https://www.cnblogs.com/bravolove/p/6376299.html

你可能感兴趣的文章
map基本用法
查看>>
poj-1163 动态规划
查看>>
Golang之interface(多态,类型断言)
查看>>
Redis快速入门
查看>>
BootStrap---2.表格和按钮
查看>>
Linear Algebra lecture 2 note
查看>>
CRC计算模型
查看>>
Ajax之404,200等查询
查看>>
Aizu - 1378 Secret of Chocolate Poles (DP)
查看>>
csv HTTP简单表服务器
查看>>
OO设计的接口分隔原则
查看>>
数据库连接字符串大全 (转载)
查看>>
java类加载和对象初始化
查看>>
对于负载均衡的理解
查看>>
django简介
查看>>
window.event在IE和Firefox的异同
查看>>
常见的js算法面试题收集,es6实现
查看>>
IO流写出到本地 D盘demoIO.txt 文本中
查看>>
Windows10 下Apache服务器搭建
查看>>
HDU 5458 Stability
查看>>