Java學習-25天
// TODO Auto-generated method stub
//1.獲取請求引數:id,price
String bookName = request.getParameter("id");
int price = Integer.parseInt(request.getParameter("price"));
//2.獲取購物車物件
HttpSession session = request.getSession();
ShoppingCart sc = (ShoppingCart)session.getAttribute("sc");
if (sc == null) {
sc = new ShoppingCart();
session.setAttribute("sc", sc);
}
//3.把點選的選項加入到購物車中
sc.addToCart(bookName, price);
//4.準備響應的JSON物件
StringBuilder result = new StringBuilder();
result.append("{")
.append("'bookName':'" + bookName + "'")
.append(",")
.append("'totalBookName':" + sc.getTotalBookNumber() )
.append(",")
.append("'bookName':" + sc.getTotalMoney() )
.append("}")
;
//5.響應JSON物件
response.setContentType("text/javascript");
response.getWriter().print(result.toString());
}