目录
实现步骤
1. 创建 Book 封装图书信息
2. 创建 BookDB ,模拟数据库,里面保存图书
3. 提供图书的购买页面:listBookServlet
4. 添加购物车:purcharseServlet
5. 回显购物车信息:cartServlet
导航栏
Book
package chapter05.session;
public class Book {
public String id;
public String name;
//alt + shift + s
//构造有参 Generate constructors using fields...
public Book(String id, String name) {
this.id = id;
this.name = name;
}
//添加get、set方法 generate getters and setters
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;
}
}
BookDB
模拟数据库
package chapter05.session;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
public class BookDB {
//模拟数据库
private static Map<String,Book> map =new LinkedHashMap<String,Book>();
static {
map.put("1", new Book("1","第一战场指挥官"));
//选中需要复制的,alt+ctrl+pgDn
map.put("2", new Book("2","开端"));
map.put("3", new Book("3","海岛基建日常"));
map.put("4", new Book("4","吾友皆大佬"));
map.put("5", new Book("5","荣誉老王"));
map.put("6", new Book("6","赚钱真的好难哦!"));
}
//1.提供一个得到图书的方法
public static Collection<Book> getAll(){
return map.values();
}
//2.根据map的key,也就是图书id,得到图书
public static Book getBook(String id) {
return map.get(id);
}
}
listBookServlet
package chapter05.session;
import java.io.IOEx