通过面向对象的分层和设计:
一、实体层
import java.io.Serializable;
public class Product implements Serializable {
private int id; //商品的id
private String name; //商品的名称
private int num = 1; //商品的数量
private double price; //商品的价格
private String desc; //商品的描述
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Product(int id, String name, double price, String desc) {
super();
this.id = id;
this.name = name;
this.price = price;
this.desc = desc;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
}
二、业务层
import java.util.ArrayList;
import java.util.List;
public class ProductDao {
private static List<Product> list = new ArrayList<Product>();
static {
list.add(new Product(1,"三国演义",100,"基友日记"));
list.add(new Product(2,"西游记",100,"人畜日记"));
list.add(new Product(3,"水浒传",100,"兄弟日记"));
list.add(new Product(4,"红楼梦",100,"娘炮日记"));
}
public List<Product> findAll() {
// TODO Auto-generated method stub
return list;
}
public Product findById(int id){
for(Product p : list){
if(p.getId()==id)
return p;
}
return null;
}
}
三、服务层和视图层
import java.io.IOException;
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 javax.servlet.http.HttpSession;
public class AddcarServlet extends HttpServlet {
private ProductDao dao = new ProductDao();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//根据id得到所选择的标签
String id = request.getParameter("id");
Product product = dao.findById(Integer.parseInt(id));
//保存在session中
HttpSession session = request.getSession();
Map<Integer,Product> car = (Map<Integer,Product>) session.getAttribute("car");
//第一次添加的时候要创建
if(car ==null){
car = new HashMap<Integer,Product>();
}
//计算添加到购物车的商品的数量
if(car != null && car.containsKey(Integer.parseInt(id))){
Product p = car.get(Integer.parseInt(id));
p.setNum(p.getNum()+1);
}else{
//没买过的时候,需要新加入购物车
car.put(product.getId(), product);
}
session.setAttribute("car", car);
//4.回显
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(product.getName()+"已经成功加入购物车!<a href='"+request.getContextPath()+"/ShowCarServlet'>查看购物车</a>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ShowCarServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
Map<Integer,Product> map = (Map<Integer, Product>) session.getAttribute("car");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String html = "";
html +="<table border='1' width='500px'>";
html +="<caption>当前购物车的商品:</caption>";
html += "<tr><th>编号</th><th>商品名称</th><th>数量</th><th>价格</th><th>描述</th><th>小计</th></tr>";
double totalPrice = 0;
for( Entry<Integer, Product> set : map.entrySet()){
Product p = set.getValue();
html += "<tr><td>"+p.getId()+"</td><td>"+p.getName()+"</td><td>"+p.getNum()+"</td><td>"+p.getPrice()+"元</td><td>"+p.getDesc()+"</td><td>"+p.getNum()*p.getPrice()+"元</td></tr>";
totalPrice+=p.getNum()*p.getPrice();
}
html+= "<tr><td colspan='6' align='right'>合计:"+totalPrice+"元</td></tr>";
html +="</table>";
out.write(html);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowServlet extends HttpServlet {
private ProductDao dao = new ProductDao();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//先得到列表中的数据
List<Product> list = dao.findAll();
//显示数据
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String html = "";
html +="<table border='1' width='500px'>";
html += "<tr><th>编号</th><th>商品名称</th><th>价格</th><th>描述</th><th>操作</th></tr>";
for (Product p : list) {
html += "<tr><td>"+p.getId()+"</td><td>"+p.getName()+"</td><td>"+p.getPrice()+"</td><td>"+p.getDesc()+"</td><td><a href='"+request.getContextPath()+"/AddcarServlet?id="+p.getId()+"'>购买</a></td></tr>";
}
html +="</table>";
out.write(html);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
通过对session的购物车练习,明白了对session会话管理的基础应用