add_cast.jsp
<%@ page import="java.util.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name = request.getParameter("name");
String price = request.getParameter("price");
String type = request.getParameter("type");
if(name!=null && price!=null) {
Map<String,Integer> cart= (Map<String,Integer>)session.getAttribute("cart");
if(cart ==null){
cart= new HashMap<>();
session.setAttribute("cart",cart);
}else {
cart.put(name,Integer.parseInt(price));
}
}
if("history".equals(type)){
response.sendRedirect("history_book.jsp");
}else {
response.sendRedirect("computer_book.jsp");
}
%>
</body>
</html>
computer_book.jsp
<%@ page import="java.util.*" %>
<%@ page import="java.util.HashMap" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>计算机图书</title>
</head>
<body>
<%
Map<String,Integer> computerBooks =new HashMap<>();
computerBooks.put("JAVA程序设计",86);
computerBooks.put("Java高级编程",46);
computerBooks.put("数据库系统与实现",96);
computerBooks.put("设计模式",76);
computerBooks.put("计算机网络",66);
%>
<h2>计算机图书列表</h2>
<table border="1">
<thead>
<tr>
<td>名称<td>
<td>价格</td>
<td>功能</td>
</tr>
</thead>
<tbody>
<%
Set<String> keySet = computerBooks.keySet();
Iterator<String> it = keySet.iterator();
String key;
Integer value;
while(it.hasNext()){
key = it.next();
value = computerBooks.get(key);
%>
<tr>
<td><%=key%><td>
<td><%=value%></td>
<td><a href="add_cart.jsp?name=<%=key%>&price=<%=value%>&type=computer">购买</a></td>
</tr>
<%
}
%>
</tbody>
</table>
<div><a href="show_cart.jsp">查看购物车</a></div>
<div><a href="history_book.jsp">历史图书列表</a></div>
</body>
</html>
delete_cart.jsp
<%@ page import="java.util.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>删除购物车</title>
</head>
<body>
<%
Map<String,Integer> cart= (Map<String,Integer>) session.getAttribute("cart");
String name= request.getParameter("name");
if(name!=null){
cart.remove(name);
}
response.sendRedirect("show_cart.jsp");
%>
</body>
</html>
histrot_book.jsp
<%@ page import="java.util.*" %>
<%@ page import="java.util.HashMap" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>历史图书</title>
</head>
<body>
<%
Map<String,Integer> historyBooks =new HashMap<>();
historyBooks.put("史记",86);
historyBooks.put("人类简史",46);
historyBooks.put("西游记",96);
historyBooks.put("红楼梦",76);
historyBooks.put("水浒传",66);
%>
<h2>历史图书列表</h2>
<table border="1">
<thead>
<tr>
<td>名称<td>
<td>价格</td>
<td>功能</td>
</tr>
</thead>
<tbody>
<%
Set<String> keySet = historyBooks.keySet();
Iterator<String> it = keySet.iterator();
String key;
Integer value;
while(it.hasNext()){
key = it.next();
value = historyBooks.get(key);
%>
<tr>
<td><%=key%><td>
<td><%=value%></td>
<td><a href="add_cart.jsp?name=<%=key%>&price=<%=value%>&type=history">购买</a></td>
</tr>
<%
}
%>
</tbody>
</table>
<div><a href="show_cart.jsp">查看购物车</a></div>
<div><a href="computer_book.jsp">计算机图书列表</a></div>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>购物商城</title>
</head>
<body>
<a href="history_book.jsp"> 历史图书</a>
<a href="computer_book.jsp"> 计算机图书</a>
</body>
</html>
show_cart.jsp
<%@ page import="java.util.*" %>
<%@ page import="java.util.HashMap" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>查看购物车</title>
</head>
<body>
<h2>查看购物车</h2>
<%
Map<String,Integer> cart= (Map<String,Integer>) session.getAttribute("cart");
if(cart!=null && cart.size()>0){
%>
<table border="1">
<thead>
<tr>
<td>名称<td>
<td>价格</td>
<td>功能</td>
</tr>
</thead>
<tbody>
<%
Set<String> keySet = cart.keySet();
Iterator<String> it = keySet.iterator();
String name;
Integer price;
while(it.hasNext()){
name = it.next();
price = cart.get(name);
%>
<tr>
<td><%=name%><td>
<td><%=price%></td>
<td><a href="delete_cart.jsp?name=<%=name%>">删除</a></td>
</tr>
<%
}
%>
</tbody>
</table>
<%
}else{
out.print("购物车空空如也");
}
%>
<div><a href="history_book.jsp">历史图书列表</a></div>
<div><a href="computer_book.jsp">计算机图书列表</a></div>
</body>
</html>