标签:
一、实现原理:
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)
{
//找到带有历史信息的cookie
if ("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()))
{
//如果有数据说明不是第一访问,把开关置为false
flag = 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的Cookies
Cookie c = new Cookie("ids", ids);
//向response域添加Cookie,覆盖原有Cookie
response.addCookie(c);
}
}
}
//判断开关,如果为真则是第一次访问
if (flag)
{
//新建名为ids的Cookie,保存当前访问的数据
Cookie c = new Cookie("ids", key);
//添加Cookie
response.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