session应用之购物车添加商品
来源:互联网 发布:golang gui 编辑:程序博客网 时间:2024/04/30 21:22
package com.neuedu.servlet;import com.neuedu.domain.product;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.List;/** * Created by Administrator on 2016/11/28 0028. */@WebServlet(name = "productlistServlet",urlPatterns = "/list")public class productlistServlet extends HttpServlet{ //1.写一个servlet,处理显示全部商品(/lst_product)请求//2.写一个对应的product_lst.jsp,上面的那个servlet准备好数据后,将请求转发给该jsp处理//3.再写一个servlet,处理加入购物车请求(CarServlet),// 将商品加入到购物车中后,将购物车变量放入session,调用重定向,让客户端,重定向到show_cart.jsp//4.写show_cart.jsp,将第三步中的购物车变量里的数据显示出来。 List<product> lst=new ArrayList<product>(); @Override public void init() throws javax.servlet.ServletException { produce(10); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } private void produce(int count){ for(int i=0;i<count;i++){ product p=new product(); p.setId(i); p.setName("商品"+i); lst.add(p); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); HttpSession hs=request.getSession(); hs.setAttribute("product",lst); request.getRequestDispatcher("/productlist.jsp").forward(request,response); }}
package com.neuedu.servlet;import com.neuedu.domain.product;import com.neuedu.domain.shopcart;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import java.io.IOException;import java.util.List;import java.util.Objects;/** * Created by Administrator on 2016/11/28 0028. */@WebServlet(name = "cartServlet",urlPatterns = "/cart")public class cartServlet extends HttpServlet{ //shopcart sc=new shopcart(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //从List里找到某个ID对应的商品对象 private product getproduct(List<product> lst,int id){ for(product p:lst){ if(p.getId()==id){ return p; } } return null; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String strid=request.getParameter("id"); HttpSession hs=request.getSession(); Object o=hs.getAttribute("ct"); shopcart sc=null; if(o!=null){ sc=(shopcart)o; } else{ sc=new shopcart(); } List<product> lst=(List<product>)hs.getAttribute("product"); //找到要加入到购物车里的商品 product p1=getproduct(lst,Integer.parseInt(strid)); //将该商品加入到购物车里 sc.addtocart(p1); hs.setAttribute("ct",sc); response.sendRedirect("show.jsp"); }}<%-- Created by IntelliJ IDEA. User: Administrator Date: 2016/11/28 0028 Time: 20:12 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html><head> <title>Title</title></head><body><table> <thead> <td>ID</td> <td>商品名称</td> <td>数量</td> </thead> <tbody> <c:forEach var="p1" items="${ct.map}"> <tr><td>${p1.key} </td> <td>${p1.value.nproduct.name}</td> <td>${p1.value.ncount}</td> </tr> </c:forEach> </tbody> </table></body></html><%-- Created by IntelliJ IDEA. User: Administrator Date: 2016/11/28 0028 Time: 19:26 To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><html><head> <title>Title</title></head><body><table> <thead> <td>id</td> <td>商品名称</td> <td>操作</td> </thead> <tbody> <c:forEach var="p" items="${product}"> <tr> <td>${p.id}</td> <td>${p.name}</td> <td><a href="/cart?id=${p.id}">加入购物车</a> </td> </tr> </c:forEach> </tbody></table></body></html>package com.neuedu.domain;/** * Created by Administrator on 2016/11/28 0028. */public class product{ int id; String name; 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; }}package com.neuedu.domain;import java.util.HashMap;import java.util.Map;/** * Created by Administrator on 2016/11/28 0028. */public class shopcart{ //键保存的是商品的id,值保存的是带数量的商品信息 private Map<Integer,numproduct> map=new HashMap<Integer,numproduct>(); public Map<Integer, numproduct> getMap() { return map; } public void setMap(Map<Integer, numproduct> map) { this.map = map; } public void addtocart(product p){ int pid=p.getId(); //看购物车里是否包含该商品 if(map.containsKey(pid)){ //取出该商品id对应的对象 numproduct np=map.get(pid); //得到该商品目前的数量 int num=np.getNcount(); num++; np.setNcount(num); } else { numproduct np=new numproduct(); np.setNcount(1); np.setNproduct(p); map.put(pid,np); } }}package com.neuedu.domain;/** * Created by Administrator on 2016/11/28 0028. */public class numproduct{ private int ncount; private product nproduct; public int getNcount() { return ncount; } public void setNcount(int ncount) { this.ncount = ncount; } public product getNproduct() { return nproduct; } public void setNproduct(product nproduct) { this.nproduct = nproduct; }}
0 0
- session应用之购物车添加商品
- 将商品添加到购物车(session、map)
- Ajax_典型应用_购物车添加商品
- ecshop批量添加商品到购物车----之三步走
- 用jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。
- 商品添加购物车实现动画效果
- 添加商品到购物车动画解析
- 商品详情页+添加购物车
- 购物车架构 一:添加商品到购物车
- Android自定义控件之高仿京东购物车添加或者减少商品数量功能
- session购物车怎么提交相同的ID商品
- Magento如何通过url参数给购物车添加商品?
- Asp.NET网上商城向购物车添加新商品
- 把商品添加到购物车的方法
- 一个购物车添加商品程序(java+ajax+servlet)
- Magento商品列表点击添加到购物车
- php中ajax实战购物车添加商品
- Android中使用动画动态添加商品进购物车
- 从特征描述符到深度学习:计算机视觉发展20年
- BitMap算法
- 琴语
- 绝对路径以及相对路径中的斜杠和反斜杠
- hpu 1194 Sequence
- session应用之购物车添加商品
- 链表的一级指针和二级指针
- centos 查看内存占用
- Unity入门 Application 类
- 记忆
- weblogic去项目名访问
- 最简逆波兰模板
- centos6换用163yum 源
- 高精度乘法