package com.heima.demo1;import java.io.IOException;import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import javax.servlet.ServletContext;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 com.heima.beans.Book;public class ShowBookList extends HttpServlet{/*** Destruction of the servlet. <br>*/public void destroy(){super.destroy(); // Just puts "destroy" string in log// Put your code here}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{// 准备图书信息ServletContext context = this.getServletContext();Map<String, Book> map = (Map<String, Book>) context.getAttribute("map");request.setAttribute("map", map);// 取出历史纪录List<Book> history = new ArrayList<Book>();Cookie[] cookies = request.getCookies();// 判断cookies是否为空,很重要,否者会出现无法获取数据,// 和空指针异常的错误if (cookies != null){//遍历cookie数组for (Cookie c : cookies){//找到带有历史信息的cookieif ("ids".equals(c.getName())){//获取其中的数据String ids = c.getValue();//把原有的数据,切割成数组,取出每个书的编号信息String[] split = ids.split("-");for (String s : split){//通过编号信息,取出对应的书Book b = map.get(s);// 把所有的书添加到一个list集合中,方便数据的传递history.add(b);}//把数据设置到request域中request.setAttribute("history", history);}}}// 把准备好的数据发送到页面进行显示request.getRequestDispatcher("/showBookList.jsp").forward(request,response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{doGet(request, response);}public void init() throws ServletException{//进行图书页面的显示图书的数据准备Map<String, Book> map = new LinkedHashMap<String, Book>();for (int i = 0; i < 10; i++){Book b = new Book();b.setId(i + "");b.setName("java" + i);b.setNum(i);b.setPrice((i + 20) + "");// System.out.println(i);map.put(b.getId(), b);}//把拥有图书数据的map集合放到ServletContext域中,给所有程序共享this.getServletContext().setAttribute("map", map);}}
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><c:forEach items="${requestScope.map }" var="entry"><h2><a href="/Test5/showBookInfo?id=${entry.key}">《${entry.value.name }》</a></h2></c:forEach><h3>您浏览过的图书如下</h3><c:forEach items="${requestScope.history}" var="book"><h2>《${book.name}》</h2></c:forEach></body></html>
package com.heima.demo1;import java.io.IOException;import java.util.Arrays;import java.util.LinkedList;import java.util.List;import java.util.Map;import javax.servlet.ServletContext;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 com.heima.beans.Book;public class ShowBookInfo extends HttpServlet{/*** Destruction of the servlet. <br>*/public void destroy(){super.destroy(); // Just puts "destroy" string in log// Put your code here}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{// 获取ServletContext域中的map集合ServletContext context = this.getServletContext();Map<String, Book> map = (Map<String, Book>) context.getAttribute("map");// 获取图书编号String key = request.getParameter("id");Book b = map.get(key);// 创建浏览记录Cookie[] cookies = request.getCookies();// 用于判断是否是第一次访问boolean flag = true;// 判断cookies是否为空,很重要,否者会出现无法获取数据,// 和空指针异常的错误if (cookies != null){//遍历数组for (Cookie cook : cookies){//取出我们需要的历史纪录的数据if ("ids".equals(cook.getName())){//如果有数据说明不是第一访问,把开关置为falseflag = false;//获取cookie保存的数据String ids = cook.getValue();//按格式切割出我们原来的数据,这里是图书的编号String[] split = ids.split("-");//把数组转换为集合方便操作List<String> asList = Arrays.asList(split);LinkedList<String> list = new LinkedList(asList);//如果集合中包含当前访问的页面if (list.contains(key)){//把集合中的历史纪录删除list.remove(key);}//重新添加当前访问页面的纪录,添加在首歌list.addFirst(key);//把集合中的数据重新拼接成字符串数据,给Cookie保存StringBuffer sb = new StringBuffer();//只需要最大4次的访问记录for (int i = 0; i < list.size() && i < 4; i++){sb.append(list.get(i));sb.append("-");}//把字符串末尾的“-”去除ids = sb.substring(0, sb.length() - 1);// System.out.println(ids);//重新创建一个名为ids的CookiesCookie c = new Cookie("ids", ids);//向response域添加Cookie,覆盖原有Cookieresponse.addCookie(c);}}}//判断开关,如果为真则是第一次访问if (flag){//新建名为ids的Cookie,保存当前访问的数据Cookie c = new Cookie("ids", key);//添加Cookieresponse.addCookie(c);}// 获取准备图书信息request.setAttribute("book", b);//把准备好的数据发送到显示页面request.getRequestDispatcher("/showBookInfo.jsp").forward(request,response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{doGet(request, response);}/*** Initialization of the servlet. <br>** @throws ServletException* if an error occurs*/public void init() throws ServletException{}}
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><h1>图书的详细信息</h1><h3>图书名称:《${requestScope.book.name }》</h3><h3>图书价格:${requestScope.book.price }</h3><h3>图书描述:${requestScope.book.description }</h3><h2><a href="/Test5/showBookList">返回图书列表</a></h2></body></html>
原文:http://www.cnblogs.com/didixyy/p/bd979620be4670537d9fa0a42551cdb2.html