session應用之購物車新增商品
阿新 • • 發佈:2019-02-16
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; importjava.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; } }