最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2022年02月09日 16:04:45   作者:來份代碼  
這篇文章主要為大家詳細介紹了使用session實現(xiàn)簡易購物車功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了用session實現(xiàn)簡易購物車功能的具體代碼,供大家參考,具體內(nèi)容如下

整體思路:先寫一個JSP用于實現(xiàn)商品圖片的讀取(再次之前要寫好連接數(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 {
?? ?/*
?? ? * 得到產(chǎn)品的信息
?? ? */
?? ?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 "+"已經(jīng)添加"+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ù)如圖:

這些就是整個購物車的全部。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 實現(xiàn)RSA非對稱加密算法

    Java 實現(xiàn)RSA非對稱加密算法

    RSA解決了對稱加密的一個不足,比如AES算法加密和解密時使用的是同一個秘鑰,因此這個秘鑰不能公開,因此對于需要公開秘鑰的場合,我們需要在加密和解密過程中使用不同的秘鑰,加密使用的公鑰可以公開,解密使用的私鑰要保密,這就是非對稱加密的好處?!?/div> 2021-06-06
  • Java httpClient連接池支持多線程高并發(fā)的實現(xiàn)

    Java httpClient連接池支持多線程高并發(fā)的實現(xiàn)

    本文主要介紹了Java httpClient連接池支持多線程高并發(fā)的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • spring的構(gòu)造函數(shù)注入屬性@ConstructorBinding用法

    spring的構(gòu)造函數(shù)注入屬性@ConstructorBinding用法

    這篇文章主要介紹了關(guān)于spring的構(gòu)造函數(shù)注入屬性@ConstructorBinding用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java中Map的computeIfAbsent方法詳解

    Java中Map的computeIfAbsent方法詳解

    這篇文章主要介紹了Java的Map中computeIfAbsent方法詳解,在jdk1.8中Map接口新增了一個computeIfAbsent方法,這是Map接口中的默認實現(xiàn)該方法是首先判斷緩存Map中是否存在指定的key的值,如果不存在,會調(diào)用mappingFunction(key)計算key的value,需要的朋友可以參考下
    2023-11-11
  • JVM常量池的深入講解

    JVM常量池的深入講解

    這篇文章主要給大家介紹了關(guān)于JVM常量池的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • MyBatis深入分析數(shù)據(jù)庫交互與關(guān)系映射

    MyBatis深入分析數(shù)據(jù)庫交互與關(guān)系映射

    這篇文章主要介紹了MyBatis中的數(shù)據(jù)庫交互與關(guān)系映射,MyBatis是一款優(yōu)秀的持久層框架,它支持定制化SQL、存儲過程以及高級映射,MyBatis避免了幾乎所有的JDBC代碼和手動設置參數(shù)以及獲取結(jié)果集,需要的朋友可以參考下
    2024-05-05
  • 關(guān)于postman傳參的幾種格式 list,map 等

    關(guān)于postman傳參的幾種格式 list,map 等

    這篇文章主要介紹了postman傳參的幾種格式 list,map等,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 詳解springboot+mybatis多數(shù)據(jù)源最簡解決方案

    詳解springboot+mybatis多數(shù)據(jù)源最簡解決方案

    本篇文章主要介紹了詳解springboot+mybatis多數(shù)據(jù)源最簡解決方案,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Springboot重寫addInterceptors()方法配置攔截器實例

    Springboot重寫addInterceptors()方法配置攔截器實例

    這篇文章主要介紹了Springboot重寫addInterceptors()方法配置攔截器實例,spring?boot拋棄了復雜的xml配置,我們可以自定義配置類(標注@Configuration注解的類)來實現(xiàn)WebMvcConfigurer接口,并重寫addInterceptors()方法來配置攔截器,需要的朋友可以參考下
    2023-09-09
  • Java之理解Redis回收算法LRU案例講解

    Java之理解Redis回收算法LRU案例講解

    這篇文章主要介紹了Java之理解Redis回收算法LRU案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08

最新評論

崇义县| 浦北县| 阿坝县| 开封县| 铜鼓县| 呼图壁县| 襄汾县| 靖西县| 金华市| 门源| 临洮县| 饶平县| 南安市| 松潘县| 吉隆县| 易门县| 大渡口区| 城市| 马鞍山市| 慈溪市| 淳安县| 石渠县| 浦北县| 中方县| 娱乐| 洪雅县| 绥芬河市| 盐源县| 海南省| 遵化市| 五台县| 南通市| 玉环县| 永川市| 通化县| 昭苏县| 河北省| 泰顺县| 修武县| 永嘉县| 德州市|