一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - javaweb購物車案列學習開發

javaweb購物車案列學習開發

2021-05-04 14:01牛穿瘋 Java教程

這篇文章主要為大家詳細介紹了javaweb購物車案列學習開發的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了javaweb購物車案列的具體代碼,供大家參考,具體內容如下

一、項目目錄結構  

 javaweb購物車案列學習開發

二、源代碼

dao包——dao層:bookdao.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.dao;
 
import java.util.map;
import com.db.db;
import com.domain.book;
 
 
public class bookdao {
 
 public map getall(){
 return db.getall();
 }
 
 public book find(string id){
 return (book) db.getall().get(id);
 }
}

db包:db.java——模擬數據庫

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.db;
import java.util.linkedhashmap;
import java.util.map;
import com.domain.book;
import com.sun.org.apache.bcel.internal.generic.new;
//代表數據庫
//代表數據庫
public class db {
 
 private static map map = new linkedhashmap();
 static{
 map.put("1", new book("1","javaweb開發","老張",38,"一本好書"));
 map.put("2", new book("2","jdbc開發","老黎",18,"一本好書"));
 map.put("3", new book("3","ajax開發","老佟",328,"一本好書"));
 map.put("4", new book("4","jbpm開發","老畢",58,"一本好書"));
 map.put("5", new book("5","struts開發","老方",28,"一本好書"));
 map.put("6", new book("6","spring開發","老方",98,"一本好書"));
 }
 
 
 public static map getall(){
 return map;
 }
 
}

domain包:

book.java:書的實體類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.domain;
//書的實體類
public class book {
 private string id;
 private string name;
 private string author;
 private double price;
 private string description;
 
 public book() {
 super();
 // todo auto-generated constructor stub
 }
 public book(string id, string name, string author, double price,
  string description) {
 super();
 this.id = id;
 this.name = name;
 this.author = author;
 this.price = price;
 this.description = description;
 }
 public string getid() {
 return id;
 }
 public void setid(string id) {
 this.id = id;
 }
 public string getname() {
 return name;
 }
 public void setname(string name) {
 this.name = name;
 }
 public string getauthor() {
 return author;
 }
 public void setauthor(string author) {
 this.author = author;
 }
 public double getprice() {
 return price;
 }
 public void setprice(double price) {
 this.price = price;
 }
 public string getdescription() {
 return description;
 }
 public void setdescription(string description) {
 this.description = description;
 }
 
 
}

cart.java:購物車類    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.domain;
 
import java.util.linkedhashmap;
import java.util.map;
 
//代表用戶的購物車
 
//代表用戶的購物車
public class cart {
 
 private map<string,cartitem> map = new linkedhashmap();
 private double price; //記住購物車所有商品多少錢
 
 public void add(book book){
 //看購物車中有沒有,要添加的書對應的購物項
 cartitem item = map.get(book.getid());
 if(item==null){
  item = new cartitem();
  item.setbook(book);
  item.setquantity(1);
  map.put(book.getid(), item);
 }else{
  item.setquantity(item.getquantity()+1);
 }
 }
 
 public map<string, cartitem> getmap() {
 return map;
 }
 public void setmap(map<string, cartitem> map) {
 this.map = map;
 }
 public double getprice() {
 double totalprice = 0;
 for(map.entry<string, cartitem> entry : map.entryset()){
  cartitem item = entry.getvalue();
  totalprice += item.getprice();
 }
 this.price = totalprice;
 return price;
 }
 public void setprice(double price) {
 this.price = price;
 }
}

cartitem.java:購物項   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.domain;
//用于代表某個商品,以及商品出現的次數(購物項)
public class cartitem {
 
 private book book;
 private int quantity;
 private double price;
 
 
 public book getbook() {
 return book;
 }
 public void setbook(book book) {
 this.book = book;
 }
 public int getquantity() {
 return quantity;
 }
 public void setquantity(int quantity) {
 this.quantity = quantity;
 this.price = this.book.getprice() * this.quantity;
 }
 public double getprice() {
 return price;
 }
 public void setprice(double price) {
 this.price = price;
 }
 
}

service包:service層

businessservice.java:      

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.service;
 
import java.util.map;
 
import com.dao.bookdao;
import com.domain.book;
import com.domain.cart;
import com.domain.cartitem;
//業務類,統一對web層提供所有服務
public class businessservice {
 
 private bookdao dao = new bookdao();
 
 public map getallbook(){
 return dao.getall();
 }
 
 public book findbook(string id){
 return dao.find(id);
 }
 
 //刪除購物車中的購物項
 public void deletecartitem(string id, cart cart) {
 cart.getmap().remove(id);
 }
 
 //清空購物車
 public void clearcart(cart cart) {
 cart.getmap().clear();
 }
 
 //改變購物項的數量
 public void changeitemquantity(string id, string quantity, cart cart) {
 cartitem item = cart.getmap().get(id);
 item.setquantity(integer.parseint(quantity));
 }
 
}

web層:

listbookservlet.java:顯示所有書籍      

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.web.controller;
 
import java.io.ioexception;
import java.io.printwriter;
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.service.businessservice;
//獲取所有書籍
//獲取所有書
public class listbookservlet extends httpservlet {
 
 public void doget(httpservletrequest request, httpservletresponse response)
  throws servletexception, ioexception {
 
 businessservice service = new businessservice();
 map map = service.getallbook();
 request.setattribute("map", map);
 
 request.getrequestdispatcher("/web-inf/jsp/listbook.jsp").forward(request, response);
 }
 
 public void dopost(httpservletrequest request, httpservletresponse response)
  throws servletexception, ioexception {
 doget(request, response);
 }
 
}

 buyservlet.java:處理購買請求      

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.web.controller;
 
import java.io.ioexception;
 
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
import com.domain.book;
import com.domain.cart;
import com.service.businessservice;
 
//完成書籍購買
//完成書籍購買
public class buyservlet extends httpservlet {
 
 public void doget(httpservletrequest request, httpservletresponse response)
  throws servletexception, ioexception {
 
 string id = request.getparameter("id");
 businessservice service = new businessservice();
 book book = service.findbook(id);
 
 //得到用戶的購物車
 cart cart = (cart) request.getsession().getattribute("cart");
 if(cart==null){
  cart = new cart();
  request.getsession().setattribute("cart", cart);
 }
 
 //把書加到用戶購物車中,完成購買
 cart.add(book);
 
 //response.sendredirect("/web-inf/jsp/listcart.jsp");
 request.getrequestdispatcher("/web-inf/jsp/listcart.jsp").forward(request, response);
 
 }
 
 public void dopost(httpservletrequest request, httpservletresponse response)
  throws servletexception, ioexception {
 doget(request, response);
 }
 
}

deleteitemservlet.java:刪除某一種商品

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.web.controller;
 
import java.io.ioexception;
 
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
import com.domain.cart;
import com.service.businessservice;
//刪除指定的購物項
public class deleteitemservlet extends httpservlet {
 
 public void doget(httpservletrequest request, httpservletresponse response)
  throws servletexception, ioexception {
 
 string id = request.getparameter("id");
 cart cart = (cart) request.getsession().getattribute("cart");
 
 
 businessservice service = new businessservice();
 service.deletecartitem(id,cart);
 
 //刪除成功
 request.getrequestdispatcher("/web-inf/jsp/listcart.jsp").forward(request, response);
 
 }
 
 public void dopost(httpservletrequest request, httpservletresponse response)
  throws servletexception, ioexception {
 doget(request, response);
 }
 
}

clearcartservlet.java:清空購物車

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.web.controller;
 
import java.io.ioexception;
import java.io.printwriter;
 
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
import com.domain.cart;
import com.service.businessservice;
//清空購物車
public class clearcartservlet extends httpservlet {
 
 public void doget(httpservletrequest request, httpservletresponse response)
  throws servletexception, ioexception {
 
 cart cart = (cart) request.getsession().getattribute("cart");
 
 businessservice service = new businessservice();
 service.clearcart(cart);
 
 request.getrequestdispatcher("/web-inf/jsp/listcart.jsp").forward(request, response);
 
 }
 
 public void dopost(httpservletrequest request, httpservletresponse response)
  throws servletexception, ioexception {
 doget(request, response);
 }
 
}

changequantityservlet.java:修改購物車中指定商品的數量

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.web.controller;
 
import java.io.ioexception;
import java.io.printwriter;
 
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
 
import com.domain.cart;
import com.service.businessservice;
 
//把購物車中的書修改為指定數量
public class changequantityservlet extends httpservlet {
 
 public void doget(httpservletrequest request, httpservletresponse response)
  throws servletexception, ioexception {
 
 string id = request.getparameter("id");
 string quantity = request.getparameter("quantity");
 
 cart cart = (cart) request.getsession().getattribute("cart");
 
 businessservice service = new businessservice();
 service.changeitemquantity(id,quantity,cart);
 
 
 request.getrequestdispatcher("/web-inf/jsp/listcart.jsp").forward(request, response);
 }
 
 public void dopost(httpservletrequest request, httpservletresponse response)
  throws servletexception, ioexception {
 doget(request, response);
 }
 
}

 jsp頁面: 

webroot/web-inf/jsp/listbook.jsp:顯示書籍列表 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
 <title>書籍列表頁面</title>
 </head>
 
 <body style="text-align: center">
 
 <h1>書籍列表</h1>
 
 <table width="70%" border="1">
 <tr>
  <td>書名</td>
  <td>作者</td>
  <td>售價</td>
  <td>描述 </td>
  <td>操作</td>
 </tr>
 <c:foreach var="entry" items="${map}">
  <tr>
  <td>${entry.value.name }</td>
  <td>${entry.value.author }</td>
  <td>${entry.value.price }</td>
  <td>${entry.value.description } </td>
  <td>
   <a href="${pagecontext.request.contextpath }/servlet/buyservlet?id=${entry.value.id }" rel="external nofollow" target="_blank">購買</a>
  </td>
  </tr>
 </c:foreach>
 </table>
 
</body>

webroot/web-inf/jsp/listcart.jsp:顯示購物車列表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
 <head>
 <title>購物車列表</title>
 
 <script type="text/javascript">
 function deleteitem(id){
  var b = window.confirm("您確認刪除嗎??");
  if(b){
  window.location.href="${pagecontext.request.contextpath }/servlet/deleteitemservlet?id=" rel="external nofollow" +id;
  }
 }
 
 function clearcart(){
  var b = window.confirm("您確認清空嗎??");
  if(b){
  window.location.href="${pagecontext.request.contextpath}/servlet/clearcartservlet" rel="external nofollow" ;
  }
 }
 
 function changequantity(input,id,oldvalue){
  var quantity = input.value; //得到要修改的數量 sdfsfs
  
  /*
  //檢查用戶輸入的數量是不是一個數字
  if(isnan(quantity)){
  alert("請輸入數字!!");
  input.value = oldvalue;
  return;
  }
  */
  
  //檢查用戶輸入的數量是不是一個正整數
  if(quantity<0 || quantity!=parseint(quantity)){
  alert("請輸入正整數!!");
  input.value = oldvalue;
  return;
  
  
  var b = window.confirm("您確認把書的數量修改為:" + quantity);
  if(b){
  window.location.href="${pagecontext.request.contextpath}/servlet/changequantityservlet?id=" rel="external nofollow" + id + "&quantity=" + quantity;
  }
 }
 </script>
 
 </head>
 
 <body style="text-align: center">
 
 <h1>購物車列表</h1>
 
 <c:if test="${empty(cart.map)}">
 您沒有購買任何商品!!!
 </c:if>
 
 <c:if test="${!empty(cart.map)}">
 <table width="70%" border="1">
 <tr>
  <td>書名</td>
  <td>作者</td>
  <td>單價</td>
  <td>數量 </td>
  <td>小計</td>
  <td>操作</td>
 </tr>
 <c:foreach var="entry" items="${cart.map}">
  <tr>
  <td>${entry.value.book.name }</td>
  <td>${entry.value.book.author }</td>
  <td>${entry.value.book.price }</td>
  <td>
   <input type="text" name="quantity" value="${entry.value.quantity }" style="width:35px" onchange="changequantity(this,${entry.key},${entry.value.quantity})">
  </td>
  <td>${entry.value.price }</td>
  <td>
   <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" onclick="deleteitem(${entry.key })">刪除</a> <!-- 去掉超鏈接默認行為 -->
   
  </td>
  </tr>
 </c:foreach>
 
 <tr>
  <td colspan="3">總價</td>
  <td colspan="2">${cart.price }元</td>
  <td colspan="1">
  <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" onclick="clearcart()">清空購物車</a>
  </td>
 </tr>
 </table>
 </c:if>
 
 </body>
</html>

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

原文鏈接:http://www.cnblogs.com/niuchuangfeng/p/9077503.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 超碰av | 九九九国产在线 | 精品视频免费在线观看 | 海派甜心完整版在线观看 | 久久草香蕉频线观 | 欧美日本一道高清免费3区 欧美人做人爱a全程免费 | 亚洲va在线va天堂成人 | 女教师巨大乳孔中文字幕免费 | 青青青久热国产精品视频 | 7777奇米影视 | 好爽好深好猛好舒服视频上 | 久久精品人人做人人爽97 | 好大好深好舒服 | 亚洲欧美一区二区三区不卡 | 欧美一区二区三区高清不卡tv | 日韩中文字幕一区 | 日本免费观看95视频网站 | 青青青国产视频 | 精品国产一区二区三区久久久蜜臀 | 高清色黄毛片一级毛片 | 国产成人精品一区二区 | 国产欧美视频一区二区三区 | 青青成人 | 日韩视频在线观看中字 | av中文字幕在线 | 五月天导航 | crdy在线看亚洲 | 亚洲精品色综合久久 | 91在线精品国产丝袜超清 | 免费看黄色一级 | 成人aqq| 国产亚洲福利一区二区免费看 | 车上小婕子系列辣文小说 | 青青青视频蜜桃一区二区 | 免费看60分钟大片视频播放 | 狠狠躁夜夜躁人人爽天天miya | 久久免费特黄毛片 | 欧美不卡一区二区三区免 | 欧美日韩亚洲第一区在线 | 大片毛片女女女女女女女 | porono日本人xxx|