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
原创粉丝点击