模拟显示已浏览商品
2016-01-07 16:44
246 查看
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; //显示商品 已浏览商品 public class IndexServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter write = response.getWriter(); //输出网站商品 write.print("网站书籍如下<br/>"); for(Map.Entry<String, Book> entry : Db.getAll().entrySet()){ Book book = entry.getValue(); write.print("<a href='/Servlet/ShowBook?id="+book.getId()+"' target='_blank'>"+book.getName()+"</a></br>"); } //显示用户曾看过的商品 write.print("<br/><br/><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 id : ids){ write.print(Db.getAll().get(id).getName()+"<br/>"); } } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } //模仿数据库 class Db{ private static Map<String,Book> map = new LinkedHashMap<>(); static { map.put("1", new Book("1","Head First Java","author1","nice")); map.put("2", new Book("2","Head First jsp&servlet","author2","nice")); map.put("3", new Book("3","think in java","author3","nice")); map.put("4", new Book("4","javaweb","author4","nice")); map.put("5", new Book("5","android","author5","nice")); } public static Map<String,Book> getAll(){ return map; } } //book实体类 class Book{ private String id; private String name; private String author; private String description; public Book() { super(); } 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; } }
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 ShowBook extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); PrintWriter write = response.getWriter(); // 显示商品 String id = request.getParameter("id"); Book book = new Book(); book = Db.getAll().get(id); write.print(book.getId() + "<br/>"); write.print(book.getName() + "<br/>"); write.print(book.getAuthor() + "<br/>"); write.print(book.getDescription() + "<br/>"); // 回写cookie String cookieValue = bulidCookie(request, id); Cookie cookie = new Cookie("bookHistory", cookieValue); cookie.setMaxAge(1 * 30 * 24 * 60 * 60); cookie.setPath("/Servlet"); response.addCookie(cookie); } // 创建cookie private String bulidCookie(HttpServletRequest request, String id) { String bookHistory = null; Cookie cookies[] = request.getCookies(); 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<String> list = new LinkedList<>(Arrays.asList(bookHistory.split("\\,"))); /*if (list.contains(id)) { list.remove(id); list.addFirst(id); } else { if (list.size() >= 3) { list.removeLast(); list.addFirst(id); } else { list.addFirst(id); } } */ //以下代码实现追加cookie逻辑 if (list.contains(id)) { list.remove(id); } else if (list.size() >= 3) { list.removeLast(); } list.addFirst(id); StringBuffer sb = new StringBuffer(); for (String bookId : list) { sb.append(bookId + ","); } return sb.deleteCharAt(sb.length() - 1).toString(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
相关文章推荐
- 进制转换
- Leetcode220: First Missing Positive
- AndroidStudio 开发遇到的问题
- 修改xcode生成app的名字
- linux下安装oracle11g,netca和dbca命令不能使用,报错command not found!
- 关于arraylist 与泛型的概念
- Core Data 多线程时多个context使用
- Baxter机器人---Hello_baster(二)
- 图解SQL的各种连接(Inner join,outer join,left join,right join)
- 弹性盒子布局flex, box-sizing: border-box
- 在Imac上安装nginx
- cstring转const char*
- <div>标签嵌套多层如何找准位置
- 一次简单的分析手机APK病毒木马
- SQL Server 2012 创建操作员
- mysql group by 用法解析(详细)
- 2016-1-7-Thingking in Java 读书笔记(三)--操作符
- 每天一个Linux之head命令
- qt tableview代理1
- 点击键盘的 next 按钮,在不同的 textField 之间换行