使用session實現(xiàn)簡易購物車功能
本文實例為大家分享了用session實現(xiàn)簡易購物車功能的具體代碼,供大家參考,具體內容如下
整體思路:先寫一個JSP用于實現(xiàn)商品圖片的讀?。ㄔ俅沃耙獙懞眠B接數(shù)據(jù)庫),當點加入購物車市,根據(jù)商品唯一的標識來添加進去(我這里是商品的ID號),點擊查看購物車可以看到剛添加進去的東西,和總價錢,點擊刪除商品可以刪除商品。點擊返回就到商品商城
前置工作:
我在WebContent下創(chuàng)建了一個imges的文件夾放我的圖片
然后創(chuàng)建了一個product.java的實體類用來封裝,如下:
package com.huangxu.Dao; import java.sql.Date; public class product { ?? ?private int pid; ?? ?private int ptype; ?? ?private String pname; ?? ?private float pprice; ?? ?private int pquantity; ?? ?private String pimage; ?? ?private String pdesc; ?? ?private Date ptime; ?? ?public int getPid() { ?? ??? ?return pid; ?? ?} ?? ?public void setPid(int pid) { ?? ??? ?this.pid = pid; ?? ?} ?? ?public int getPtype() { ?? ??? ?return ptype; ?? ?} ?? ?public void setPtype(int ptype) { ?? ??? ?this.ptype = ptype; ?? ?} ?? ?public String getPname() { ?? ??? ?return pname; ?? ?} ?? ?public void setPname(String pname) { ?? ??? ?this.pname = pname; ?? ?} ?? ?public float getPprice() { ?? ??? ?return pprice; ?? ?} ?? ?public void setPprice(float pprice) { ?? ??? ?this.pprice = pprice; ?? ?} ?? ?public int getPquantity() { ?? ??? ?return pquantity; ?? ?} ?? ?public void setPquantity(int pquantity) { ?? ??? ?this.pquantity = pquantity; ?? ?} ?? ?public String getPimage() { ?? ??? ?return pimage; ?? ?} ?? ?public void setPimage(String pimage) { ?? ??? ?this.pimage = pimage; ?? ?} ?? ?public String getPdesc() { ?? ??? ?return pdesc; ?? ?} ?? ?public void setPdesc(String pdesc) { ?? ??? ?this.pdesc = pdesc; ?? ?} ?? ?public Date getPtime() { ?? ??? ?return ptime; ?? ?} ?? ?public void setPtime(Date ptime) { ?? ??? ?this.ptime = ptime; ?? ?} }
接著寫了一個ProductDAO.java的類用來連接數(shù)據(jù)庫,如下:
package com.huangxu; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.huangxu.Dao.product; public class ProductDAO extends jdbcDao { ?? ?/* ?? ? * 得到產品的信息 ?? ? */ ?? ?public List<product> getAllproducts() { ?? ??? ?String sql = "select * from product"; ?? ??? ?List<product> plist = new ArrayList<product>(); ?? ??? ?try { ?? ??? ??? ?conn = getConnection(); ?? ??? ??? ?pst = conn.prepareStatement(sql); ?? ??? ??? ?rs=pst.executeQuery(); ?? ??? ??? ?while(rs.next()){ ?? ??? ??? ??? ?product p=new product(); ?? ??? ??? ??? ?p.setPid(rs.getInt(1)); ?? ??? ??? ??? ?p.setPtype(rs.getInt(2)); ?? ??? ??? ??? ?p.setPname(rs.getString(3)); ?? ??? ??? ??? ?p.setPprice(rs.getFloat(4)); ?? ??? ??? ??? ?p.setPquantity(rs.getInt(5)); ?? ??? ??? ??? ?p.setPimage(rs.getString(6)); ?? ??? ??? ??? ?p.setPdesc(rs.getString(7)); ?? ??? ??? ??? ?p.setPtime(rs.getDate(8)); ?? ??? ??? ??? ?plist.add(p); ?? ??? ??? ?} ?? ??? ? ?? ??? ?} catch (SQLException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ??? ?return plist; ?? ?} ?? ?public product findProductByID(int pid) ?? ?{ ?? ??? ?product p=null; ?? ??? ?String sql="SELECT * from product where pid=?"; ?? ??? ?try { ?? ??? ??? ?pst=getConnection().prepareStatement(sql); ?? ??? ??? ?pst.setInt(1, pid); ?? ??? ??? ?rs=pst.executeQuery(); ?? ??? ??? ?if(rs.next()) ?? ??? ??? ?{ ?? ??? ??? ??? ?p=new product(); ?? ??? ??? ??? ?p.setPid(rs.getInt(1)); ?? ??? ??? ??? ?p.setPtype(rs.getInt(2)); ?? ??? ??? ??? ?p.setPname(rs.getString(3)); ?? ??? ??? ??? ?p.setPprice(rs.getFloat(4)); ?? ??? ??? ??? ?p.setPquantity(rs.getInt(5)); ?? ??? ??? ??? ?p.setPimage(rs.getString(6)); ?? ??? ??? ??? ?p.setPdesc(rs.getString(7)); ?? ??? ??? ??? ?p.setPtime(rs.getDate(8)); ?? ??? ??? ??? ?} ?? ??? ?} catch (SQLException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?e.printStackTrace(); ?? ??? ?}finally ?? ??? ?{ ?? ??? ??? ?close(); ?? ??? ?} ?? ??? ? ?? ??? ?return p; ?? ?} }
前置工作完畢,接著開始設計購物車
1.首先寫一個jsp頁面,我這里叫做imgs.jsp
<%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@page language="java" contentType="text/html; charset=UTF-8" ?? ?pageEncoding="UTF-8"%> <%@page import="com.huangxu.ProductDAO"%> <%@ page import="com.huangxu.Dao.product"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style type="text/css"> .mybox { ?? ?widows: 200pt; ?? ?height: auto; ?? ?border: 1px solid red; ?? ?float: left; ?? ?margin: 10px 10px; ?? ?padding: 5px 10px; } .mybox:HOVER { ?? ?background-color: #FAB; } </style> </head> <body> ?? ?<% ?? ??? ?List list=(List)session.getAttribute("shopcar"); ?? ?if(list==null){ ?? ??? ?list=new ArrayList(); ?? ??? ?session.setAttribute("shopcar", list); ?? ?} ?? ?out.println("   "+"已經添加"+list.size()+"件商品"); ?? ?%> ?? ?<div width="860px"> ?? ??? ?<% ?? ??? ??? ?ProductDAO pdao=new ProductDAO(); ?? ??? ??? ??? ?for(product p:pdao.getAllproducts()){ ?? ??? ?%> ?? ??? ?<div class="mybox"> ?? ??? ??? ?<span><img src="<%=p.getPimage()%>"></span><br> 名字:<span><%=p.getPname()%></span><br> ?? ??? ??? ?庫存:<span><%=p.getPquantity()%></span>個<br> 單價:<span>¥<%=p.getPprice()%></span>元<br> ?? ??? ??? ?<span><a href="shop.jsp?pid=<%=p.getPid()%>">加入購物車</a></span> ?? ??? ??? ? ? ?? ??? ?</div> ?? ??? ?<% ?? ??? ??? ?} ?? ??? ?%> ?? ?</div> ?? ?</div> ?? ?<br> ?? ?<div> ?? ??? ?<hr> ?? ? ?? ?<a href="showshop.jsp">查看購物車</a> ?? ? ?? ?</div> </body> </html>
2 接著寫一個jsp頁面(shop.jsp)為點擊加入購物車的實際操作進行處理:
<%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" ? ? pageEncoding="UTF-8"%> <%@page import="com.huangxu.ProductDAO"%> <%@ page import="com.huangxu.Dao.product"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% int pid=Integer.parseInt(request.getParameter("pid")); ProductDAO pdao=new ProductDAO(); product p=pdao.findProductByID(pid); List shopList=(List)session.getAttribute("shopcar"); if(shopList!=null){shopList.add(p);} response.sendRedirect("imgs.jsp"); %> </body> </html>
3.在寫一個jSP頁面(showshop.jsp)用來處理查看購物車的實際操作:
<%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" ?? ?pageEncoding="UTF-8"%> <%@page import="com.huangxu.ProductDAO"%> <%@ page import="com.huangxu.Dao.product"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> ?? ?<table width="400" border="0" cellpadding="0" cellspacing="1" ? bgcolor="#00FF66"> ? <tr bgcolor="#FFFFFF"> ? ? <th>序號</td> ? ? <th>商品名</td> ? ? <th>價格</td> ? ? <th>刪除</td> ? </tr>?? ? ?? ?<% ?? ?List<product>list=(List)session.getAttribute("shopcar");? ?? ?float sum=0; ?? ?if(list!=null) ?? ?{ ?? ??? ?for(product p:list) ?? ??? ?{sum =sum+ p.getPprice(); ?? ?%>?? ??? ? ? <tr bgcolor="#FFFFFF"> ? ? <td><%=list.indexOf(p)+1 %></td> ? ? <td><%=p.getPname() %></td> ? ? <td><%=p.getPprice() %></td> ? ? <td><a href="spsc.jsp?xl=<%=list.indexOf(p)%>">刪除商品</a></td> ? ? ? </tr>?? ??? ??? ??? ? ?? ?<%?? ??? ? ?? ??? ?} ?? ?} ?? ?%> ?? ?<tr bgcolor="#FFFFFF"> ? ? <td colspan="2">合計</td> ? ?? ? ? <td colspan="2"><%=sum %>元</td> ?? ? ? ? </tr> ? ? ? <tr><td colspan="3"><a href="imgs.jsp">返回商城</a></td></tr>?? ? ?? ?</table>?? ? </body> </html>
4、最后是刪除寫一個JSP頁面(spsc.jsp)用來處理刪除的實際操作:
<%@page import="com.sun.corba.se.spi.orbutil.fsm.FSM"%> <%@page import="com.huangxu.Dao.product"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" ? ? pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% float sum=0; int xl=Integer.parseInt(request.getParameter("xl")); List<product>list=(List)session.getAttribute("shopcar");? if(list.remove(xl)!=null){%> ?? ? ?? ? ?? ?<table width="400" border="0" cellpadding="0" cellspacing="1" ? bgcolor="#00FF66"> ?? ? ?<tr bgcolor="#FFFFFF"> ?? ? ? ?<th>序號</td> ?? ? ? ?<th>商品名</td> ?? ? ? ?<th>價格</td> ?? ? ? ?<th>刪除</td> ?? ? ?</tr>?? ??? ?<% ?? ?for(product p:list){ ?? ??? ?sum =sum+ p.getPprice(); ?? ?%>?? ??? ? ?? ? ?<tr bgcolor="#FFFFFF"> ?? ? ? ?<td><%=list.indexOf(p)+1 %></td> ?? ? ? ?<td><%=p.getPname() %></td> ?? ? ? ?<td><%=p.getPprice() %></td> ?? ? ? ?<td><a href="spsc.jsp?xl=<%=list.indexOf(p)%>">刪除商品</a></td> ?? ? ?? ?? ? ?</tr>?? ??? ??? ??? ? ?? ??? ?<%?? ??? ? ?? ??? ??? ?} ?? ??? ?}else{ ?? ??? ? ?? ??? ?response.sendRedirect("imgs.jsp");} ?? ??? ?%> ?? ??? ?<tr bgcolor="#FFFFFF"> ?? ? ? ?<td colspan="2">合計</td> ?? ? ? ? ?? ? ? ?<td colspan="2"><%=sum %>元</td> ?? ? ? ?? ?? ?? ? ?</tr>?? ? ?? ? ? ?<tr><td colspan="3"><a href="imgs.jsp">返回商城</a></td></tr> ?? ??? ?</table>?? ? ?? ? </body> </html>
這樣就全部寫完了用session做的一個簡易購物車!
下面附上SQL的表:(在test庫中)創(chuàng)建一個叫product的表
創(chuàng)建語句如下:
CREATE TABLE `product` ( ? `pid` int(11) NOT NULL AUTO_INCREMENT, ? `ptype` int(11) DEFAULT NULL, ? `pname` varchar(50) DEFAULT NULL, ? `pprice` float DEFAULT NULL, ? `pquantity` int(11) DEFAULT NULL, ? `pimage` varchar(100) DEFAULT NULL, ? `pdesc` varchar(300) DEFAULT NULL, ? `ptime` time DEFAULT NULL, ? PRIMARY KEY (`pid`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
下列是表中的數(shù)據(jù)如圖:
這些就是整個購物車的全部。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解Spring Boot 使用Java代碼創(chuàng)建Bean并注冊到Spring中
本篇介紹了Spring Boot 使用Java代碼創(chuàng)建Bean并注冊到Spring中,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02從Android源碼剖析Intent查詢匹配的實現(xiàn)
這篇文章主要介紹了從Android源碼剖析Intent查詢匹配的實現(xiàn),Intent部分的源碼為Java代碼,需要的朋友可以參考下2015-07-07Spring Boot 整合 TKMybatis 二次簡化持久層代碼的實現(xiàn)
這篇文章主要介紹了Spring Boot 整合 TKMybatis 二次簡化持久層代碼的實現(xiàn),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01