购物车的代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>showCart</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
  <table>
  <tr>
  <th>书籍名称</th>
  <th>价格</th>
  <th>数量</th>
  <th>小计</th>
  </tr>
<c:forEach items="${cart.cart}" var="item">
 <tr>
 <td>${item.value.bookInfo.bookname}</td>
 <td>${item.value.bookInfo.price}</td>
 <td>${item.value.count}</td>
 <td>${item.value.tmoney}</td>
 </tr>
</c:forEach>
 <tr>
  <td>&nbsp;&nbsp;</td><td>&nbsp;&nbsp;</td><td>&nbsp;&nbsp;</td> <td>总计:${cart.total}</td>
 </tr>
 <tr><td>&nbsp;&nbsp;</td><td><a href="${pageContext.request.contextPath}/index.jsp">继续购买</a></td></tr>
  </table>
  </body>
</html>

 

---servlet代码

package org.xiaoai.web.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.xiaoai.entity.BookInfo;
import org.xiaoai.entity.CartBean;
import org.xiaoai.servers.BookInfoServices;
import org.xiaoai.servers.impl.BookInfoServicesImpl;

public class CartServlet extends HttpServlet {

 
 BookInfoServices se=new BookInfoServicesImpl();
 /**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

   this.doPost(request, response);
 }

 /**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
          
  String way=request.getParameter("method");
  if("addCart".equals(way)){
   
   this.addCart(request, response);
  }
  
 }
 
 //添加到购物车
 public void addCart(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
  
  
    String id=request.getParameter("id");
    BookInfo bookInfo=se.abookInfo(new Integer(id));
    HttpSession session=request.getSession();
   CartBean cart=(CartBean)session.getAttribute("cart");
   if(cart==null){
    cart=new CartBean();
    session.setAttribute("cart", cart);
   }
    cart.addBookInfo(bookInfo);
    response.sendRedirect(request.getContextPath()+"/"+"showCart.jsp");
 }

}

 

 

--相关的实体

--cartBean

package org.xiaoai.entity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 *购物车
 */
public class CartBean {

 //定义集合存储购买的物品
  private Map<Integer,CartItem> cart=new HashMap<Integer,CartItem>(); 
 //总价
  private double total;
public Map<Integer, CartItem> getCart() {
 return cart;
}
public void setCart(Map<Integer, CartItem> cart) {
 this.cart = cart;
}
public double getTotal() {
 
 double total = 0.0d;
 Set<Integer> keys = cart.keySet();
 Iterator<Integer> it = keys.iterator();
 while(it.hasNext()){
  Integer key = it.next();
  // 获得对应Item
  CartItem item = cart.get(key);
  total += item.getTmoney();
 }
 
 return total;
}
public void setTotal(double total) {
 this.total = total;
}
 

 //添加到购物车的物品进行判断

public void addBookInfo(BookInfo bookInfo){
 
 if(cart!=null){
  
 
   //判断该物品是否存在
  if(cart.containsKey(bookInfo.getBookid())){
   //表示此书已经存在
   CartItem item=cart.get(bookInfo.getBookid());
     item.setCount(item.getCount()+1);
  }else{
  //书籍不存在
    CartItem item=new CartItem();
    item.setCount(1);
    item.setBookInfo(bookInfo);
    cart.put(bookInfo.getBookid(), item);
  
 }
 }
}
}

 

--cartItem代码

package org.xiaoai.entity;

import java.text.DecimalFormat;

/**
 *封装购物车信息
 */
public class CartItem {

 
    //书籍信息
   private BookInfo bookInfo=new BookInfo();
 //书籍购买的数量
   private Integer count;
   //小计
   private double tmoney;
public BookInfo getBookInfo() {
 return bookInfo;
}
public void setBookInfo(BookInfo bookInfo) {
 this.bookInfo = bookInfo;
}
public Integer getCount() {
 return count;
}
public void setCount(Integer count) {
 this.count = count;
}
public double getTmoney() {

 DecimalFormat format=new DecimalFormat("0.00");
  String dou=format.format(count*bookInfo.getPrice());
 return new Double(dou);
}
public void setTmoney(double tmoney) {
 this.tmoney = tmoney;
}
 
}

 


 

你可能感兴趣的:(String,server,Integer,equals,Class,stylesheet)