本文實例為大家分享了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