cookie小项目——历史记录简单实现

编程入门 行业动态 更新时间:2024-10-26 03:33:05

cookie小项目——<a href=https://www.elefans.com/category/jswz/34/1759988.html style=历史记录简单实现"/>

cookie小项目——历史记录简单实现

 实现效果如图所示:(浏览记录只保存最新的三条)

 

public class Book {private String id;private String name;private double price;private String author;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 double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}@Overridepublic String toString() {return "Book [id=" + id + ", name=" + name + ", price=" + price + ", author=" + author + "]";}public Book(String id, String name, double price, String author) {super();this.id = id;this.name = name;this.price = price;this.author = author;}
}
public class DBUtil {private static Map<String,Book> books = new HashMap<>();static {books.put("1", new Book("1", "金瓶梅", 20, "王瑞鑫"));books.put("2", new Book("2", "葵花宝典", 20, "杨成毅"));books.put("3", new Book("3", "九阴真经", 30, "陈光"));books.put("4", new Book("4", "玉女心经", 10, "陈志家"));}public static Map<String,Book> showAllBooks(){return books;}public static Book findBookById(String id) {return books.get(id);}
}

 

public class ShowAllBooksServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();out.write("网站有以下好书:<br/>");Map<String, Book> books = DBUtil.showAllBooks();for (Map.Entry<String, Book> book: books.entrySet()) {out.write("<a href='"+request.getContextPath()+"/showBookDetail?id="+book.getKey()+"' target='_blank'>"+book.getValue().getName()+"</a><br/>");}out.write("<hr/>您最近浏览过的书有:<br/>");Cookie[] cookies = request.getCookies();for(int i=0;cookies!=null && i<cookies.length;i++) {if("historyBookId".equals(cookies[i].getName())) {String value=cookies[i].getValue();String[] ids = value.split("-");for(int j=0;j<ids.length;j++) {Book book = DBUtil.findBookById(ids[j]);out.print(book.getName()+"<br/>");}}}}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}
public class ShowBookDetail extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();String id = request.getParameter("id");Book book = DBUtil.findBookById(id);out.print(book);String historyBookId = organized(id,request);Cookie ck = new Cookie("historyBookId",historyBookId);ck.setPath("/");ck.setMaxAge(Integer.MAX_VALUE);response.addCookie(ck);}/*** @param id* @param request* @return*/private String organized(String id, HttpServletRequest request) {Cookie[] cookies=request.getCookies();if(cookies==null) {return id;}Cookie historyBook = null;for(int i = 0;i<cookies.length;i++) {if("historyBookId".equals(cookies[i].getName())) {historyBook = cookies[i];}}if(historyBook==null) {return id;}String value = historyBook.getValue();String[] values = value.split("-");LinkedList<String> list = new LinkedList<String>(Arrays.asList(values));if(list.size()<3) {if(list.contains(id)) {list.remove(id);}}else {if(list.contains(id)) {list.remove(id);}else {list.removeLast();}}list.addFirst(id);StringBuffer sb=new StringBuffer();for (int i=0;i<list.size();i++) {if(i>0) {sb.append("-");}sb.append(list.get(i));}System.out.println(sb);return sb.toString();}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}

 

更多推荐

cookie小项目——历史记录简单实现

本文发布于:2024-02-12 03:28:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1685623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:历史记录   简单   项目   cookie

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!