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

使用session實(shí)現(xiàn)簡(jiǎn)易購(gòu)物車功能

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

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

整體思路:先寫一個(gè)JSP用于實(shí)現(xiàn)商品圖片的讀?。ㄔ俅沃耙獙懞眠B接數(shù)據(jù)庫),當(dāng)點(diǎn)加入購(gòu)物車市,根據(jù)商品唯一的標(biāo)識(shí)來添加進(jìn)去(我這里是商品的ID號(hào)),點(diǎn)擊查看購(gòu)物車可以看到剛添加進(jìn)去的東西,和總價(jià)錢,點(diǎn)擊刪除商品可以刪除商品。點(diǎn)擊返回就到商品商城

前置工作:

我在WebContent下創(chuàng)建了一個(gè)imges的文件夾放我的圖片

然后創(chuàng)建了一個(gè)product.java的實(shí)體類用來封裝,如下:

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;
?? ?}
}

接著寫了一個(gè)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;
?? ?}

}

前置工作完畢,接著開始設(shè)計(jì)購(gòu)物車

1.首先寫一個(gè)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>個(gè)<br> 單價(jià):<span>¥<%=p.getPprice()%></span>元<br>
?? ??? ??? ?<span><a href="shop.jsp?pid=<%=p.getPid()%>">加入購(gòu)物車</a></span>
?? ??? ??? ? ?
?? ??? ?</div>

?? ??? ?<%
?? ??? ??? ?}
?? ??? ?%>
?? ?</div>
?? ?</div>
?? ?<br>
?? ?<div>

?? ??? ?<hr>
?? ?
?? ?<a href="showshop.jsp">查看購(gòu)物車</a>
?? ?
?? ?</div>
</body>
</html>

2 接著寫一個(gè)jsp頁面(shop.jsp)為點(diǎn)擊加入購(gòu)物車的實(shí)際操作進(jìn)行處理:

<%@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.在寫一個(gè)jSP頁面(showshop.jsp)用來處理查看購(gòu)物車的實(shí)際操作:

<%@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>序號(hào)</td>
? ? <th>商品名</td>
? ? <th>價(jià)格</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">合計(jì)</td>
? ??
? ? <td colspan="2"><%=sum %>元</td>
??
? ?
? </tr>
? ? ? <tr><td colspan="3"><a href="imgs.jsp">返回商城</a></td></tr>?? ?
?? ?</table>?? ?
</body>
</html>

4、最后是刪除寫一個(gè)JSP頁面(spsc.jsp)用來處理刪除的實(shí)際操作:

<%@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>序號(hào)</td>
?? ? ? ?<th>商品名</td>
?? ? ? ?<th>價(jià)格</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">合計(jì)</td>
?? ? ? ?
?? ? ? ?<td colspan="2"><%=sum %>元</td>
?? ? ?
?? ??
?? ? ?</tr>?? ?
?? ? ? ?<tr><td colspan="3"><a href="imgs.jsp">返回商城</a></td></tr>
?? ??? ?</table>?? ?

?? ?
</body>
</html>

這樣就全部寫完了用session做的一個(gè)簡(jiǎn)易購(gòu)物車!
下面附上SQL的表:(在test庫中)創(chuàng)建一個(gè)叫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ù)如圖:

這些就是整個(gè)購(gòu)物車的全部。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

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

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

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

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

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

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

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

    Java中Map的computeIfAbsent方法詳解

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

    JVM常量池的深入講解

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

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

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

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

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

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

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

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

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

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

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

最新評(píng)論

湖口县| 阳曲县| 龙陵县| 台州市| 沁水县| 侯马市| 浦东新区| 丰镇市| 广平县| 上犹县| 丰城市| 城市| 边坝县| 社旗县| 荆州市| 新疆| 拜泉县| 东乌| 林口县| 宿松县| 固阳县| 海淀区| 葵青区| 丰顺县| 南丰县| 闻喜县| 磐石市| 通州区| 乐山市| 灌阳县| 沁源县| 彩票| 河源市| 巧家县| 穆棱市| 台北市| 田阳县| 白玉县| 丰城市| 申扎县| 南昌县|