JSP+javabean實現購物車功能
簡單的小程式,java後臺 + Web前端,可以實現購物車的新增,刪除等功能,並沒有用到資料庫。而是用到的session存取功能。
Product.java
package shopping.cart; import java.io.Serializable; @SuppressWarnings("serial") public class Product implements Serializable{//序列化 private String id;//產品標識 private String name;//產品名稱 private String description;//產品描述 private double price;//產品價格 public Product(){} public Product(String id,String name,String description,double price){ this.id = id; this.name = name; this.description = description; this.price = price; } public void setId(String id){ this.id = id; } public void setName(String name){ this.name = name; } public void setDescription(String description){ this.description = description; } public void setPrice(double price){ this.price = price; } public String getId(){ return this.id; } public String getName(){ return this.name; } public String getDescription(){ return this.description; } public double getPrice(){ return this.price; } }
CartItem.java
package shopping.cart;
public class CartItem {
private Product product;
private int count;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
Cart.javaShowProducts.jsppackage shopping.cart; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Cart { List<CartItem> items = new ArrayList<CartItem>(); public List<CartItem> getItems() { return items; } public void setItems(List<CartItem> items) { this.items = items; } public void add(CartItem ci) { for (Iterator<CartItem> iter = items.iterator(); iter.hasNext();) { CartItem item = iter.next(); if(item.getProduct().getId() == ci.getProduct().getId()) { item.setCount(item.getCount() + 1); return; } } items.add(ci); } public double getTotalPrice() { double d = 0.0; for(Iterator<CartItem> it = items.iterator(); it.hasNext(); ) { CartItem current = it.next(); d += current.getProduct().getPrice() * current.getCount(); } return d; } public void deleteItemById(String productId) { for (Iterator<CartItem> iter = items.iterator(); iter.hasNext();) { CartItem item = iter.next(); if(item.getProduct().getId().equals(productId)) { iter.remove(); return; } } } }
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="shopping.cart.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'ShowProductsJSP.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body bgcolor="#ffffff">
<%
Map products = new HashMap();
products.put("1612001", new Product("1612001", "mp3播放器","效果很不錯的mp3播放器,儲存空間達1GB", 100.00));
products.put("1612002", new Product("1612002", "數碼相機", "畫素500萬,10倍光學變焦",500.00));
products.put("1612003", new Product("1612003", "數碼攝像機","120永珍素,支援夜景拍攝,20倍光學變焦", 200.00));
products.put("1612004", new Product("1612004", "迷你mp4","市面所能見到的最好的mp4播放器,國產", 300.00));
products.put("1612005", new Product("1612005", "多功能手機","集mp3播放、100永珍素數碼相機,手機功能於一體", 400.00));
products.put("1612006", new Product("1612006", "蘋果4S","高貴的品牌手機,智慧手機,公雞中的戰鬥機",2399.00));
session.setAttribute("products", products);
%>
<H1>產品顯示</H1>
<form name="productForm" action="" method="POST">
<input type="hidden" name="action" value="purchase">
<table border="1" cellspacing="0">
<tr bgcolor="#CCCCCC">
<tr bgcolor="#CCCCCC">
<td>序號</td>
<td>產品名稱</td>
<td>產品描述</td>
<td>產品單價(¥)</td>
<td>新增到購物車 </td>
</tr>
<%
Set productIdSet = products.keySet();
Iterator it = productIdSet.iterator();
int number = 1;
while (it.hasNext()) {
String id = (String) it.next();
Product product = (Product) products.get(id);
%><tr>
<td><%=number++%></td>
<td><%=product.getName()%></td>
<td><%=product.getDescription()%></td>
<td><%=product.getPrice()%></td>
<td><a href="Buy.jsp?id=<%=product.getId()%>&action=add" target="cart">我要購買</a></td>
</tr>
<%
}
%>
</table>
<p></p>
</form>
</body>
</html>
Buy.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ page import="shopping.cart.*" %>
<%
Cart c = (Cart)session.getAttribute("cart");
if(c == null) {
c = new Cart();
session.setAttribute("cart", c);
}
double totalPrice = c.getTotalPrice();
request.setCharacterEncoding("GBK");
String action = request.getParameter("action");
Map products = (HashMap)session.getAttribute("products");
if(action != null && action.trim().equals("add")) {
String id = request.getParameter("id");
Product p = (Product)products.get(id);
CartItem ci = new CartItem();
ci.setProduct(p);
ci.setCount(1);
c.add(ci);
}
if(action != null && action.trim().equals("delete")) {
String id = request.getParameter("id");
c.deleteItemById(id);
}
if(action != null && action.trim().equals("update")) {
for(int i=0; i<c.getItems().size(); i++) {
CartItem ci = c.getItems().get(i);
int count = Integer.parseInt(request.getParameter("p" + ci.getProduct().getId()));
ci.setCount(count);
}
}
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
List<CartItem> items = c.getItems();
%>
<!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=GB18030">
<title>購物車</title>
</head>
<body>
<!-- c的值是:<%=(c == null) %> items的值是:<%=(items == null) %> -->
<form action="Buy.jsp" method="get">
<input type="hidden" name="action" value="update"/>
<table align="center" border="1">
<tr>
<td>產品ID</td>
<td>產品名稱</td>
<td>購買數量</td>
<td>單價</td>
<td>總價</td>
<td>處理</td>
</tr>
<%
for(Iterator<CartItem> it = items.iterator(); it.hasNext(); ) {
CartItem ci = it.next();
%>
<tr>
<td><%=ci.getProduct().getId() %></td>
<td><%=ci.getProduct().getName() %></td>
<td>
<input type="text" size=3 name="<%="p" + ci.getProduct().getId() %>" value="<%=ci.getCount() %>"
onkeypress="if (event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;"
onchange="document.forms[0].submit()">
</td>
<td><%=ci.getProduct().getPrice() %></td>
<td><%=ci.getProduct().getPrice()*ci.getCount() %></td>
<td>
<a href="Buy.jsp?action=delete&id=<%=ci.getProduct().getId() %>">刪除</a>
</td>
</tr>
<%
}
%>
<tr>
<td colspan=3 align="right">所有商品總價格為:</td>
<td colspan=3><%=c.getTotalPrice() %></td>
</tr>
<tr>
<!-- <td colspan=3>
<a href="javascript:document.forms[0].submit()">修改</a>
</td> -->
<td colspan=6 align="right"><a href="">下單</a></td>
</tr>
</table>
</form>
</body>
</html>
下面是採用同樣session方法,但淺顯易懂的一種方式:
相關推薦
JSP+javabean實現購物車功能
簡單的小程式,java後臺 + Web前端,可以實現購物車的新增,刪除等功能,並沒有用到資料庫。而是用到的session存取功能。
javaEE之jsp+JavaBean實現登入功能
javaEE之jsp+JavaBean實現登入功能(不加資料庫) 實現效果 JavaBean檔案: #UserBean.java package nmx; public class UserBean { private String username; p
jsp+javabean實現購物車
採用Model1(jsp+javabean) 實現DBHelper類 建立實體類 建立業務邏輯類(dao) DBHelper類的設計 package util; import j
javaEE之jsp+JavaBean實現登入+註冊+留言功能(外掛資料庫)
javaEE之jsp+JavaBean實現登入+註冊+留言功能(外掛資料庫) 實現效果 #UserBean.java package nmx; public class UserBean { private String username; private Stri
用jsp+javabean實現使用者註冊和檢視的功能
本文主要參考了,JSP+javabean循序漸進開發平臺:winxp+tomcat4+mysql+javabean在實現在了用servlet註冊還有登入以後,打算做一個註冊頁面首先建立一個表:username VARCHAR2(20) 使用者名稱 password VARCH
電商---實現購物車功能
decode typeof led 不存在 htm 問題 cap ssi des 購物車實現3種方式 1、利用cookie 優點:不占用服務器資源,可以永遠保存,不用考慮失效的問題 缺點: 對購買商品的數量是有限制的,存放數據的大小 不可以超過2k,用戶如果禁用cook
VUE.JS實現購物車功能
add http 功能 hang 總數 tps conf methods htm 購物車是電商必備的功能,可以讓用戶一次性購買多個商品,常見的購物車實現方式有如下幾種: 1. 用戶更新購物車裏的商品後,頁面自動刷新。 2. 使用局部刷新功能,服務器端返回整個購物
實現購物車功能 --- 文件操作版
sent not 判斷 保存 進入 ali 修改 能夠 ret 1.用戶接口 >>>判斷用戶工資是否有記錄 >>>能夠從文件中讀取商品列表 >>>能夠選擇想要的商品,並扣除工資 >>>打印並保存訂單信息
1.實現購物車功能
alex for div 直接 watch car code pen dex # 1輸入工資,打印商品列表 # 2根據id選擇商品 # 3選擇商品檢查余額是否不足,直接扣款,提醒 # 4隨時退出,打印購物車 product_list = [ (‘Iphone‘,
cookie來實現購物車功能
pub boolean lis 商品 tao this token ont variable 一、大概思路 1、從cookie中取商品列表 2、判斷要添加的商品是否存在cookie中。 3、如果已添加過,則把對應的商品取出來,把要添加的商品的數量加上去。 4
用python實現購物車功能
功能 鼠標 購物車 %d () 顯示 ood 自己 根據 """功能要求:1.要求用戶輸入自己擁有的總資產,例如:20002.顯示商品列表的序號,商品名稱,商品價格,讓用戶根據序號選擇商品,然後加入購物車 例如: 1 電腦 1999 2 鼠
JSP+JavaBean實現任意兩個整數和
【問題】設計 Web 程式,計算任意兩個整數的和,並在網頁上顯示結果。要求在 JavaBean 中實現資料的求和功能。 【分析】需要兩個頁面 input.jsp 和 show.jsp ,以及Add.java 【實現】 (1)首先設計實現資料求和的 JavaBean 類 Add.java,
JavaEE初學之jsp+JavaBean實現頁面簡單計算器
JavaEE初學之jsp+JavaBean實現頁面簡單計算器 這個學期剛剛學了JavaEE,簡單記錄一下,希望以後會有幫助。 實現效果 首先新建一個web project:Calculator,然後新建一個calculate.jsp檔案和CalculatorBean.jav
vue商品分類,實現購物車功能
new Vue({ el: "#app", data: { cIndex: 0, lists: [ { title: "推薦商品", goods: [
會話跟蹤:實現購物車功能
小白成長記,不
用OC和UI實現購物車功能 在iOS平臺上
購物車專案 作用:可以通過按鈕把物品簡單的新增到購物車中 思路: 一,設定兩個加減按鈕,有普通,高亮和enable狀態,在interface中有屬性宣告 二,設定購物車的imageview,在interface中有屬性宣告 三,設定陣列裝載字典物件,字典裡包含物品圖片
vuex實現購物車功能
購物車功能描述: 1. 點選+,-按鈕,“已選:xx件”隨之增減 2. checkbox選中時,當前項的已選數量增加到頭部“已選擇xx件”中,未選中時數量不計入 程式碼: 服務端 node+koa+koa-router server.js 1 const koa = require('
簡單的實現購物車功能,還有不到位的地方,加油!!!
要求: 1 import time 2 goods = [{"huawei":1000},{"apple":10},{"banana":23},{"pen":140},{"wanju":78}, 3 {"book":200},{"taiden
在做商城專案實現購物車功能的時候除了個小bug...
@Autowired private ItemService itemService; @Value("${COOKIE_CART_EXPIRE}") private int COOKIE_CART_EXPIRE; @Autowired private Cart
h5本地快取實現購物車功能(全功能)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>購物車特效</title>