package de.bvb.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* 显示商品列表和已经浏览过的商品
*
* @author joker
*
*/
public class CookieDemo11 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
// 显示商品列表
out.write("商品列表: <br/>");
for (Entry<String, Book> b : Db.getBooks().entrySet()) {
out.write("<a target='_blank' href='/web/CookieDemo12?id="
+ b.getKey() + "'>" + b.getValue().getName() + "</a><br/>");
} // 显示浏览过的商品
out.write(" <br/> <br/>浏览过的商品: <br/>");
Cookie[] cookies = request.getCookies();
for (int i = 0; cookies != null && i < cookies.length; i++) {
if (cookies[i].getName().equals("history")) {
String[] ids = cookies[i].getValue().split("\\,");
for (String s : ids) {
out.write(Db.getBooks().get(s).getName() + "<br/>");
}
}
}
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
}
} class Db {
private static LinkedHashMap<String, Book> books = new LinkedHashMap<String, Book>();
static {
books.put("1", new Book("1", "javaWeb", "javaweb"));
books.put("2", new Book("2", "android", "android"));
books.put("3", new Book("3", "spring", "spring"));
books.put("4", new Book("4", "strus", "strus"));
books.put("5", new Book("5", "ios", "ios"));
} public static Map<String, Book> getBooks() {
return books;
}
} class Book {
private String id;
private String name;
private String description; public Book(String id, String name, String description) {
super();
this.id = id;
this.name = name;
this.description = description;
} public Book() {
super();
} 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 getDescription() {
return description;
} public void setDescription(String description) {
this.description = description;
} }
package de.bvb.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.Map.Entry; import javax.enterprise.inject.ResolutionException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.DateFormatter; /**
* 显示商品详情
*
* @author joker
*
*/
public class CookieDemo12 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter(); // 显示商品详情
out.write("商品详情: <br/>");
String id = request.getParameter("id");
for (Entry<String, Book> b : Db.getBooks().entrySet()) {
if (id.equals(b.getKey())) {
out.write(b.getValue().getId() + "<br/>");
out.write(b.getValue().getName() + "<br/>");
out.write(b.getValue().getDescription() + "<br/>");
}
} // 回写cookie,保存最后浏览的商品id
String history = buildIds(id, request);
Cookie cookie = new Cookie("history", history);
cookie.setMaxAge(1 * 30 * 24 * 60 * 60);
cookie.setPath("/web");
response.addCookie(cookie);
} private String buildIds(String id, HttpServletRequest request) {
String history = null;
Cookie[] cookies = request.getCookies();
for (int i = 0; cookies != null && i < cookies.length; i++) {
if (cookies[i].getName().equals("history")) {
history = cookies[i].getValue();
}
}
if (history == null) {
return id;
}
LinkedList<String> ids = new LinkedList<String>(Arrays.asList(history
.split("\\,")));
if (ids.contains(id)) {
ids.remove(id);
} else {
if (ids.size() >= 3) { // 最多显示3条浏览历史
ids.removeLast();
}
}
ids.addFirst(id);
StringBuffer sb = new StringBuffer();
for (String s : ids) {
sb.append(s).append(",");
}
return sb.deleteCharAt(sb.length() - 1).toString();
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doGet(request, response);
} }

最新文章

  1. Java技术体系图
  2. 【BZOJ-2476】战场的数目 矩阵乘法 + 递推
  3. 图像开发的p2s模式:halcon+opencv的联动
  4. [Android疑难杂症]动态改变Background后Padding无效的问题
  5. C++ notes for beginners(2)
  6. JS匿名执行函数
  7. JS脚本加载与执行对性能的影响
  8. IOS中TableView的使用(1) -创建一个简单的tableView
  9. Windows搭建以太坊的私有链环境
  10. webstorm安装与本地激活
  11. 自制简易Linux系统
  12. python 接口测试1 --如何创建和打印日志文件
  13. 网络编程基础+UDP的实现
  14. Selenium2Lib库之操作浏览器相关的关键字实战
  15. Linux grep 命令详解
  16. 使用IDEA创建SSM框架
  17. C++ is_same
  18. Spark_RDD之基本RDD操作
  19. Maven使用deploy上传jar包到远程库
  20. 封装 vue 组件的过程

热门文章

  1. java中String.valueOf()和toString()方法的区别
  2. Which Python memory profiler is recommended
  3. python中新式类和经典类的区别
  4. mina学习
  5. Python模块(radom)
  6. linux内核编译相关
  7. windows下使用pthreads
  8. Java基础之读文件——从文件中读取文本(ReadAString)
  9. Java 集合类型
  10. Leetcode: Arithmetic Slices