一、文件结构图
二、代码实现
com.it.domain.Book类
package com.it.domain;
import java.io.Serializable;
public class Book implements Serializable{
private String id;
private String name;
private String price;
private String author;
public Book() {
super();
}
public Book(String id, String name, String price, String author) {
super();
this.id = id;
this.name = name;
this.price = price;
this.author = author;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
com.it.global.BookGlobal类
package com.it.global;
import java.util.ArrayList;
import java.util.List;
import com.it.domain.Book;
public class BookGlobal {
static List<Book> bookList;
static {
bookList = new ArrayList<Book>();
bookList.add(new Book("1","水浒传","9","施耐庵"));
bookList.add(new Book("2","红楼梦","19","曹雪芹"));
bookList.add(new Book("3","三国演义","29","罗贯中"));
bookList.add(new Book("4","西游记","39","吴承恩"));
}
public static List<Book> getBooks() {
return bookList;
}
public static Book getBookById(String id) {
Book book = new Book();
book.setId(id);
int index = bookList.indexOf(book);
return bookList.get(index);
}
}
book.jsp
<%@page import="com.it.global.BookGlobal"%>
<%@page import="com.it.domain.Book"%>
<%@page import="java.util.List"%>
<%@ 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>
<table border="1px">
<caption>书籍列表</caption>
<%
List<Book> books = new BookGlobal().getBooks();
for (Book book : books) {
%>
<tr>
<td><%=book.getName()%></td>
<td><%=book.getPrice()%></td>
<td><%=book.getAuthor()%></td>
<td><a
href="<%=request.getContextPath()%>/books?id=<%=book.getId()%>">添加商品到购物车</a></td>
</tr>
<%
}
%>
</table>
<hr />
<a href="<%=request.getContextPath()%>/show.jsp">查询购物车商品</a>
</body>
</html>
com.it.servlet.session.BooksServlet类
package com.it.servlet.session;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.it.domain.Book;
import com.it.global.BookGlobal;
/**
* 用于将商品信息存储到session中
*/
public class BooksServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
//1. 得到商品id
String id = request.getParameter("id");
//2. 将商品放大购物车中,将购物车做成一个map
Map<Book, Integer> cart = (Map<Book, Integer>) request.getSession().getAttribute("cart");
if(cart == null) {
cart = new HashMap<Book, Integer>();
}
Book book = BookGlobal.getBookById(id);
Integer count = cart.get(book);//获得指定id的书籍对象
if(count == null) {
count = 1;
}else {
count += 1;
}
cart.put(book,count);
request.getSession().setAttribute("cart", cart);
response.getWriter().write("<a href='/day9_2/book.jsp'>继续购物</a> <a href='/day9_2/show.jsp'>查看购物车</a>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
show.jsp
<%@page import="java.util.Set"%>
<%@page import="java.util.Collection"%>
<%@page import="com.it.domain.Book"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@ 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>
<table border="1px">
<caption>购物车信息</caption>
<tr>
<td>书名</td>
<td>价格</td>
<td>作者</td>
<td>数量</td>
</tr>
<%
Map<Book, Integer> cart = (Map<Book, Integer>) request.getSession().getAttribute("cart");
if (cart != null && cart.size() != 0) {
Set<Book> books = cart.keySet();
for (Book book : books) {
%>
<tr>
<td><%=book.getName()%></td>
<td><%=book.getPrice()%></td>
<td><%=book.getAuthor()%></td>
<td><%=cart.get(book)%></td>
</tr>
<%
}
}
%>
</table>
</body>
</html>