-
@CartServlet.java——(下) ... if(request.getParameter("action")!=null) { this.action = request.getParameter("action"); if(action.equals("add")) //如果是添加商品进购物车 { if(addToCart(request,response)) { request.getRequestDispatcher("/success.jsp").forward(request, response); } else { request.getRequestDispatcher("/failure.jsp").forward(request, response); } } if(action.equals("show"))//如果是显示购物车 { request.getRequestDispatcher("/cart.jsp").forward(request, response); } if(action.equals("delete")) //如果是执行删除购物车中的商品 { if(deleteFromCart(request,response)) { request.getRequestDispatcher("/cart.jsp").forward(request, response); } else { request.getRequestDispatcher("/cart.jsp").forward(request, response); } } } } } 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
@CartServlet.java——(上) public class CartServlet extends HttpServlet { private String action ; //表示购物车的动作 ,add,show,delete //商品业务逻辑类的对象 private ItemsDAO idao = new ItemsDAO(); public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); ... 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
@Cart.java——(下) public static void main(String[] args) { //先创建两个商品对象 Items i1 = new Items(1,"沃特篮球鞋","温州",200,500,"001.jpg"); Cart c = new Cart(); c.addGoodsInCart(i1, 1); //再次购买沃特篮球鞋,购买3双 c.addGoodsInCart(i1, 3); //遍历购物商品的集合 Set<Map.Entry<Items, Integer>> items= c.getGoods().entrySet(); for(Map.Entry<Items, Integer> obj:items) { System.out.println(obj); } System.out.println("购物车的总金额:"+c.getTotalPrice()); } } 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
@CartServlet.java //从购物车中删除商品 private boolean deleteFromCart(HttpServletRequest request, HttpServletResponse response) { String id = request.getParameter("id"); Cart cart = (Cart)request.getSession().getAttribute("cart"); Items item = idao.getItemsById(Integer.parseInt(id)); if(cart.removeGoodsFromCart(item)) { return true; } else { return false; } } 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
@Cart.java——(上) //购物车类 public class Cart { //购买商品的集合 private HashMap<Items,Integer> goods; //购物车的总金额 private double totalPrice; //构造方法 public Cart() { goods = new HashMap<Items,Integer>(); totalPrice = 0.0; } //setter/getter方法... //添加商品进购物车的方法 public boolean addGoodsInCart(Items item ,int number) { if(goods.containsKey(item)) { goods.put(item, goods.get(item)+number); } else { goods.put(item, number); } calTotalPrice(); //重新计算购物车的总金额 return true; } //删除商品的方法 public boolean removeGoodsFromCart(Items item) { goods.remove(item); calTotalPrice(); //重新计算购物车的总金额 return true; } //统计购物车的总金额 public double calTotalPrice() { double sum = 0.0; Set<Items> keys = goods.keySet(); //获得键的集合 Iterator<Items> it = keys.iterator(); //获得迭代器对象 while(it.hasNext()) { Items i = it.next(); sum += i.getPrice()* goods.get(i); } this.setTotalPrice(sum); //设置购物车的总金额 return this.getTotalPrice(); } ...查看全部
-
@cart.jsp <% //首先判断session中是否有购物车对象 if(request.getSession().getAttribute("cart")!=null) { Cart cart = (Cart)request.getSession().getAttribute("cart"); HashMap<Items,Integer> goods = cart.getGoods(); Set<Items> items = goods.keySet(); Iterator<Items> it = items.iterator(); while(it.hasNext()) { Items i = it.next(); <%=i.getPrice()*goods.get(i) %> } <%=cart.getTotalPrice() %> } %> @details.jsp <script type="text/javascript"> //按“+”按钮+1 function add() { var num = parseInt(document.getElementById("number").value); if(num<100) { document.getElementById("number").value = ++num; } } //按“-”按钮-1 function sub() { var num = parseInt(document.getElementById("number").value); if(num>1) { document.getElementById("number").value = --num; } } </script>查看全部
-
@CartServlet.java //添加商品进购物车的方法 private boolean addToCart(HttpServletRequest request, HttpServletResponse response) { String id = request.getParameter("id"); String number = request.getParameter("num"); Items item = idao.getItemsById(Integer.parseInt(id)); //是否是第一次给购物车添加商品,需要给session中创建一个新的购物车对象 if(request.getSession().getAttribute("cart")==null) { Cart cart = new Cart(); request.getSession().setAttribute("cart",cart); } Cart cart = (Cart)request.getSession().getAttribute("cart"); if(cart.addGoodsInCart(item, Integer.parseInt(number))) { return true; } else { return false; } } 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
@Items.java//解决编号没有合并问题 @Override public int hashCode() { //若getId和getName相同,则hashCode一定相同 return this.getId()+this.getName().hashCode(); } @Override public boolean equals(Object obj) { // TODO Auto-generated method stub if(this==obj) { return true; } if(obj instanceof Items) { Items i = (Items)obj; if(this.getId()==i.getId()&&this.getName().equals(i.getName())) { return true; } else { return false; } } else { return false; } } @Cart.java //添加商品进购物车的方法 public boolean addGoodsInCart(Items item ,int number) { if(goods.containsKey(item))//重复添加,解决数量没有相加问题 { goods.put(item, goods.get(item)+number); } else//第一次添加 { goods.put(item, number); } calTotalPrice(); //重新计算购物车的总金额 return true; } 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
@Items.java //重写toString() public String toString() { return "商品编号:"+this.getId()+",商品名称:"+this.getName(); } @Cart.java //遍历购物商品的集合 Set<Map.Entry<Items, Integer>> items= c.getGoods().entrySet(); for(Map.Entry<Items, Integer> obj:items) { System.out.println(obj); } 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
1.Set集合不能用for循环遍历 2.迭代器Iterator可以加泛型,利用迭代器遍历取值 //购物车类 public class Cart { //购买商品的集合 private HashMap<Items,Integer> goods; //购物车的总金额 private double totalPrice; //构造方法 public Cart() { goods = new HashMap<Items,Integer>(); totalPrice = 0.0; } //统计购物车的总金额 public double calTotalPrice() { double sum = 0.0; Set<Items> keys = goods.keySet(); //获得键的集合 Iterator<Items> it = keys.iterator(); //获得迭代器对象 while(it.hasNext()) { Items i = it.next(); sum += i.getPrice()* goods.get(i); } this.setTotalPrice(sum); //设置购物车的总金额 return this.getTotalPrice(); } } 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
EIS:企业信息系统 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
tomcat容器等级:四个,查看全部
-
在web.xml中配置Servlet时,可以配置一些初始化参数。而在Servlet中可以通过ServletConfig接口提供的方法来取得这些参数。 @index.jsp <a href="servlet/GetInitParameterServlet">获取初始化参数Servlet</a> @web.xml <servlet> <init-param> <param-name>username</param-name> <param-value>admin</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>.</param-value> </init-param> </servlet> 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
-
@GetInitParameterServlet.java public class GetInitParameterServlet extends HttpServlet { private String username;//用户名 private String password;//密码 //setter()和getter()... public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=utf-8"); PrintWriter out=response.getWriter(); out.println("用户名:"+this.getUsername()); out.println("密码:"+this.getPassword()); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void init() throws ServletException { this.setUsername(this.getInitParameter("username")); this.setPassword(this.getInitParameter("password")); } } 【温馨提示:JavaSE/EE、SSH/SSM、Hybrid APP、JQ/JS/CSS3/H5等编程爱好者关注我,加我慕课好友,互相学习,共同进步!】查看全部
举报
0/150
提交
取消