亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用session實現(xiàn)簡易購物車功能

 更新時間:2022年02月09日 16:04:45   作者:來份代碼  
這篇文章主要為大家詳細介紹了使用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("&nbsp&nbsp "+"已經添加"+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中

    本篇介紹了Spring Boot 使用Java代碼創(chuàng)建Bean并注冊到Spring中,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • Python學習之書寫格式及變量命名

    Python學習之書寫格式及變量命名

    這篇文章我們給大家總結了關于Python書寫格式及變量命名,小編覺得這篇文章寫的還不錯,有興趣的朋友跟著參考學習下,希望能夠給你帶來幫助
    2021-10-10
  • java object 之clone方法全面解析

    java object 之clone方法全面解析

    下面小編就為大家?guī)硪黄猨ava object 之clone方法全面解析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • Java Spring事務使用及驗證過程詳解

    Java Spring事務使用及驗證過程詳解

    這篇文章主要介紹了Java Spring事務使用及驗證過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-12-12
  • 從Android源碼剖析Intent查詢匹配的實現(xiàn)

    從Android源碼剖析Intent查詢匹配的實現(xiàn)

    這篇文章主要介紹了從Android源碼剖析Intent查詢匹配的實現(xiàn),Intent部分的源碼為Java代碼,需要的朋友可以參考下
    2015-07-07
  • Spring Boot 整合 TKMybatis 二次簡化持久層代碼的實現(xiàn)

    Spring Boot 整合 TKMybatis 二次簡化持久層代碼的實現(xiàn)

    這篇文章主要介紹了Spring Boot 整合 TKMybatis 二次簡化持久層代碼的實現(xiàn),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Java 獲取properties的幾種方式

    Java 獲取properties的幾種方式

    這篇文章主要介紹了Java 獲取properties的幾種方式,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下
    2021-04-04
  • 詳解SpringCloud微服務架構之Hystrix斷路器

    詳解SpringCloud微服務架構之Hystrix斷路器

    本篇文章主要介紹了詳解SpringCloud微服務架構之Hystrix斷路器,Hystrix是一個庫,通過添加延遲容差和容錯邏輯來幫助您控制這些分布式服務之間的交互,有興趣的可以了解一下
    2018-01-01
  • springboot swagger不顯示接口的問題及解決

    springboot swagger不顯示接口的問題及解決

    這篇文章主要介紹了springboot swagger不顯示接口的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Spring注入Bean的一些方式總結

    Spring注入Bean的一些方式總結

    這篇文章主要給大家總結介紹了關于Spring注入Bean的一些方式,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-04-04

最新評論