日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java web 购物车_java web开发之实现购物车功能

發布時間:2025/4/5 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java web 购物车_java web开发之实现购物车功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為了方便自己以后復習,所以寫的比較仔細,記錄下自己的成長。

既然是做購物車,那么前提條件是首先需要一系列商品,也就是要建一個實體,這里建了一個商品表、

通過查詢在瀏覽器上顯示

基本顯示已經做好了,現在進入我們的重頭戲,servlet

點擊放入購物車時,將訪問servlet

購物車代碼

package com.servlet;

import java.io.ioexception;

import java.io.printwriter;

import java.util.hashmap;

import java.util.map;

import javax.servlet.servletexception;

import javax.servlet.http.httpservlet;

import javax.servlet.http.httpservletrequest;

import javax.servlet.http.httpservletresponse;

import com.dao.goodsdao;

import com.entity.goods;

import com.entity.goodsitem;

public class putcarservlet extends httpservlet {

public void doget(httpservletrequest request, httpservletresponse response)

throws servletexception, ioexception {

response.setcontenttype("text/html");

printwriter out = response.getwriter();

dopost(request, response);

}

public void dopost(httpservletrequest request, httpservletresponse response)

throws servletexception, ioexception {

response.setcontenttype("text/html");

printwriter out = response.getwriter();

//得到編號

string id = request.getparameter("goodsid");

//通過編號得到商品對象的所有信息

goodsdao dao = new goodsdao();

goods g = dao.getgoodsbyid(id);

//將商品放入購物車

//map集合 就是購物車

// map 商品編號作為鍵 商品項作為值

//1.判斷是否存在購物車

//購物車是放在session中的

//從session去取購物車

map gwc = (map)request.getsession().getattribute("gwc");

//判斷是否存在

if(gwc==null){

//創建購物車

gwc = new hashmap();

}

//將商品項放入購物車

//put(商品編號,商品項) 向gwc集合中添加數據

//你要想 購物車中是否已存在該商品

// 說白了 就是在gwc集合中去匹配是否存在這樣一個商品項 ==》去集合中匹配是否存在這樣一個商品編號的key

//判斷是否存在商品編號的鍵

if(gwc.containskey(id)){

//存在

//設置數量+1

//通過鍵 獲得值

//鍵為商品編號 值為商品項 商品項里面包含商品對象信息 和數量信息

goodsitem spx = gwc.get(id);

//得到原來的數量

int yldsl = spx.getcount();

//在原來的數量上+1

gwc.get(id).setcount(yldsl+1);

// gwc.get(id).setcount(gwc.get(id).getcount()+1) ;

}else{

//不存在

//創建一個新的商品項 數量為1

goodsitem gi = new goodsitem(g, 1);

//將此商品項放入gwc

gwc.put(id, gi);

}

//將購物車放入session

request.getsession().setattribute("gwc", gwc);

//繼續購物

response.sendredirect("index.jsp");

}

}

執行結果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持萬仟網。

如您對本文有疑問或者有任何想說的,請點擊進行留言回復,萬千網友為您解惑!

總結

以上是生活随笔為你收集整理的java web 购物车_java web开发之实现购物车功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。