博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
TF-IDF原理
查看>>
用JS制作博客页面背景随滚动渐变的效果
查看>>
JavaScript的迭代函数与迭代函数的实现
查看>>
一步步教你学会browserify
查看>>
Jmeter入门实例
查看>>
亲近用户—回归本质
查看>>
中文脏话识别的解决方案
查看>>
CSS之不常用但重要的样式总结
查看>>
Python编译错误总结
查看>>
URL编码与解码
查看>>
日常开发时遇到的一些坑(三)
查看>>
Eclipse 安装SVN插件
查看>>
深度学习
查看>>
TCP粘包问题及解决方案
查看>>
构建之法阅读笔记02
查看>>
添加按钮
查看>>
移动端页面开发适配 rem布局原理
查看>>
Ajax中文乱码问题解决方法(服务器端用servlet)
查看>>
会计电算化常考题目一
查看>>
阿里云服务器CentOS6.9安装Mysql
查看>>