1 package com.yxfyg.servlet;
2
3 import java.io.IOException;
4 import java.util.LinkedHashMap;
5 import java.util.Map;
6
7 import javax.servlet.ServletException;
8 import javax.servlet.http.Cookie;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import javax.servlet.http.HttpSession;
13
14 import com.yxfyg.util.CookieUtil;
15
16 public class ShoppingServlet extends HttpServlet {
17
18 @Override
19 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
20
21 String id = req.getParameter("id");
22 Cookie[] cookies = req.getCookies();
23 Cookie cookie = CookieUtil.findCookie(cookies, "history");
24 if (cookie != null) {
25 String ids = cookie.getValue();
26 ids = id + "#" + ids;
27 cookie.setValue(ids);
28 cookie.setMaxAge(60 * 60 * 24 * 7);
29 cookie.setPath("/SimpleShop");
30 resp.addCookie(cookie);
31 } else {
32 Cookie history = new Cookie("history", id);
33 history.setMaxAge(60 * 60 * 24 * 7);
34 history.setPath("/SimpleShop");
35 resp.addCookie(history);
36 }
37
38 HttpSession session = req.getSession();
39 Map<Integer, Integer> map = (Map<Integer, Integer>) session.getAttribute("cart");
40
41 int index = Integer.parseInt(id);
42
43 if (map == null) {
44 map = new LinkedHashMap<Integer, Integer>();
45 map.put(index, 1);
46 } else {
47 if (map.containsKey(index)) {
48 map.put(index, map.get(index) + 1);
49 } else {
50 map.put(index, 1);
51 }
52 }
53 session.setAttribute("cart", map);
54
55 resp.sendRedirect("product_list.jsp");
56 }
57
58 @Override
59 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
60 doGet(req, resp);
61 }
62 }
1 package com.yxfyg.servlet;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.http.Cookie;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import com.yxfyg.util.CookieUtil;
12
13 public class ClearHistory extends HttpServlet{
14
15 @Override
16 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
17 Cookie history = new Cookie("history", "");
18 history.setMaxAge(0);
19 resp.addCookie(history);
20 resp.sendRedirect("product_list.jsp");
21
22 }
23
24 @Override
25 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
26 doGet(req, resp);
27 }
28 }
1 package com.yxfyg.servlet;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.http.HttpServlet;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9 import javax.servlet.http.HttpSession;
10
11 public class ClearCart extends HttpServlet{
12
13 @Override
14 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
15 HttpSession session = req.getSession();
16 session.invalidate();
17 resp.sendRedirect("product_list.jsp");
18 }
19
20 @Override
21 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
22 doGet(req, resp);
23 }
24
25 }
package com.yxfyg.util;
import javax.servlet.http.Cookie;
public class CookieUtil {
public static Cookie findCookie(Cookie[] cookies,String name) {
if(cookies != null) {
for(Cookie cookie : cookies) {
if(name.equals(cookie.getName())) {
return cookie;
}
}
}
return null;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="com.yxfyg.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品列表</title>
</head>
<body>
<div>
<a href="ShoppingServlet?id=0">苹果</a><br/>
<a href="ShoppingServlet?id=1">芒果</a><br/>
<a href="ShoppingServlet?id=2">柠檬</a><br/>
<a href="ShoppingServlet?id=3">橙子</a><br/>
<a href="ShoppingServlet?id=4">草莓</a><br/>
<a href="ShoppingServlet?id=5">西瓜</a><br/>
</div>
<div>
<h3>浏览记录(<a href="ClearHistory">清除浏览记录</a>)</h3>
<%
Cookie[] cookies = request.getCookies();
Cookie history = CookieUtil.findCookie(cookies,"history");
if(history == null){
%>
<h3>还没有浏览任何商品</h3>
<%
}else{
String[] ids = history.getValue().split("#");
String[] names = {"苹果","芒果","柠檬","橙子","草莓","西瓜"};
int[] list = new int[ids.length];
boolean flag = true;
for(int i=0;i<ids.length;i++){
list[i] = Integer.parseInt(ids[i]);
}
for(int i=0;i<ids.length;i++){
flag = true;
for(int j=0;j<i;j++){
if(list[i] == list[j]){
flag = false;
break;
}
}
if(flag){
%>
<a href="ShoppingServlet?id=<%=list[i] %>"><%=names[list[i]] %></a>
<%
}
}
}
%>
</div>
<div>
<h3><a href="cart.jsp">我的购物车</a></h3>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.Map"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
</head>
<body>
<div>
<h3>购物车(<a href="ClearCart">清空购物车</a>)</h3>
<%
String[] names = {"苹果","芒果","柠檬","橙子","草莓","西瓜"};
Map<Integer,Integer> map = (Map<Integer,Integer>)session.getAttribute("cart");
if(map != null){
for(int index : map.keySet()){
%>
<a href="ShoppingServlet?id=<%=index %>"><%=names[index] %></a> 数量:<%=map.get(index) %><br/>
<%
}
}else{
%>
<h3>空空如也</h3>
<%
}
%>
</div>
</body>
</html>