一、显示上次浏览商品的实现过程图
二、要实现的功能如下:
三、实现代码
book.jsp
<%@ 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>书籍列表</title>
</head>
<body>
<table border="1px">
<caption>四大名著</caption>
<tr>
<td>水浒传</td>
<td><a href="<%=request.getContextPath() %>/bookServlet?id=1">浏览</a></td>
</tr>
<tr>
<td>红楼梦</td>
<td><a href="<%=request.getContextPath() %>/bookServlet?id=2">浏览</a></td>
</tr>
<tr>
<td>三国演义</td>
<td><a href="<%=request.getContextPath() %>/bookServlet?id=3">浏览</a></td>
</tr>
<tr>
<td>西游记</td>
<td><a href="<%=request.getContextPath() %>/bookServlet?id=4">浏览</a></td>
</tr>
</table>
<hr>
<a href="show.jsp">浏览记录</a>
</body>
</html>
show.jsp
<%@page import="com.it.utils.CookieUtil"%>
<%@ 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>
<%
String[] books = {"水浒传", "红楼梦", "三国演义", "西游记"};
Cookie cookie = CookieUtil.getCookie("bookHistory", request.getCookies());
if(cookie == null){
%> 记录为空 <%
} else {
String[] ids = cookie.getValue().split("-");
for(String id : ids){
%> 浏览的图书为:<%=books[Integer.parseInt(id)-1]%><br><%
}
}
%>
</body>
</html>
com.it.utils.CookieUtil
package com.it.utils;
import javax.servlet.http.Cookie;
public class CookieUtil {
public static Cookie getCookie(String name, Cookie[] cookies) {
if(null == cookies || cookies.length == 0) {
return null;
} else {
for(Cookie cookie : cookies) {
if(name.equals(cookie.getName())) {
return cookie;
}
}
}
return null;
}
}
com.it.servlet.cookie.BookServlet
package com.it.servlet.cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
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.it.utils.CookieUtil;
public class BookServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//得到浏览书籍的id
String id = request.getParameter("id").trim();
//得到Cookie
Cookie cookie = CookieUtil.getCookie("bookHistory", request.getCookies());
if(cookie == null) {
cookie = new Cookie("bookHistory", id);
}else {
String value = cookie.getValue();//1-3-4
List<String> ids = Arrays.asList(value.split("-"));
if(!ids.contains(id)) {
value += "-" + id;
}
cookie = new Cookie("bookHistory", value);
}
response.addCookie(cookie);
response.sendRedirect(request.getContextPath() + "/book.jsp");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}