dvd shopping cart project tutorial
DESCRIPTION
how to shop dvd onlineTRANSCRIPT
-
5/24/2018 DVD Shopping Cart Project Tutorial
1/43
DVD Shopping Cart Project
DVD shopping cart project (Java and Oracle) using Struts2 Framewor!
Before starting this tutorial, we assume you have already installed Java, Oracle and
NetBeans IDE (version 5.5 and !truts" (#lug ins. $e will %uild our &ava a##lication
using NetBeans IDE.
Step "!
't the Net Bean !tartu#
!elect ile)*New +ro&ect
hen -hoose Java $e%* $e% '##lication then #ress Net
In the new dialog %o, name your #ro&ect and choose a location for it and +ress Net.
-
5/24/2018 DVD Shopping Cart Project Tutorial
2/43
In the net dialog %o select the server /lassfish 01 then #ress Net
-
5/24/2018 DVD Shopping Cart Project Tutorial
3/43
In the net dialog %o select the !truts" ramewor2 then #ress inish
3ou will see a default #age HelloWorld.jspand the directory structure li2e this4
-
5/24/2018 DVD Shopping Cart Project Tutorial
4/43
Step2!
In the Directory !tructure clic2 on !ource +ac2age and then ight clic2 on the eam#le
and select New, clic2 on the Java -lass from the dro# down menu.
Name your class name and choose the location to %e source #ac2age and clic2 inish.
-
5/24/2018 DVD Shopping Cart Project Tutorial
5/43
Edit the !ource -ode in the !how'ction.&ava
##Show$ction%java
im#ort &ava.s6l.!78Ece#tion9
im#ort &ava.servlet.htt#.:9
im#ort com.o#ensym#hony.wor2".'ction!u##ort9
im#ort &ava.util.'rray8ist9
im#ort org.a#ache.struts".interce#tor.!ervlete6uest'ware9
im#ort org.a#ache.struts".interce#tor.!ervletes#onse'ware9
#u%lic class !how'ction etends 'ction!u##ort im#lements
!ervlete6uest'ware,!ervletes#onse'ware
;
#rivate
-
5/24/2018 DVD Shopping Cart Project Tutorial
6/43
>
>
8i2e the a%ove !how'ction.&ava add the !ho##ing-art.&ava, emoveItem'ction.&ava,+roductDataBean.&ava, D0D.&ava, -hec2out'ction.&ava, 'ddo!ho##ing-art'ction.&ava
to the !ource +ac2age.
##ShoppingCart%java
im#ort &ava.util.:9
im#ort &ava.s6l.:9
#u%lic class !ho##ing-art im#lements &ava.io.!erialiAa%le ;
#rivate -onnection connection9
#rivate +re#ared!tatement addecord, getecords9
#rivate !tatement statement9
#rivate dou%le total+rice9
static int -'ID = 9
#rotected 0ector items9
#u%lic !ho##ing-art( ;
items = new 0ector(9
>
#u%lic 0ector getItems( ;
return (0ector items.clone(9
>
-
5/24/2018 DVD Shopping Cart Project Tutorial
7/43
#u%lic void addItem(D0D newItem ;
%oolean flag = false9
if (items.siAe( == C ;
items.addElement(newItem9
return9
>
for (int i = C9 i items.siAe(9 i ;
D0D dvd = (D0D items.element't(i9
if (dvd.getFovie(.e6uals(newItem.getFovie( ;
dvd.set7uantity(dvd.get7uantity(newItem.get7uantity(9
items.setElement't(dvd,i9
flag = true9
%rea29
>
>
if (newItem.get7uantity(*C GG (flag == false ;
items.addElement(newItem9
>
>
#u%lic void removeItem(int itemInde ;
items.removeElement't(itemInde9
>
#u%lic void com#leteOrder(
-
5/24/2018 DVD Shopping Cart Project Tutorial
8/43
throws Ece#tion ;
Enumeration e = items.elements(9
connection = +roductDataBean.get-onnection(9
statement = connection.create!tatement(9
while (e.hasForeElements( ;
D0D item = (D0D e.netElement(9
!tring item7uantity = ?? item.get7uantity(9
total+rice = total+rice item.get+rice( :
Integer.#arseInt(item7uantity9
!tring u#date!tring = ?IN!E INO !ho##ing-arts ?
? 0'8@E! (? -'ID ?, H?
item.getFovie( ?H, H?
item.getating( ?H, H?
item.get3ear( ?H, ?
item.get+rice( ?, ?
item.get7uantity( ??9
statement.eecute@#date(u#date!tring9
>
-'ID 9
>
#u%lic dou%le getotal+rice( ;
return this.total+rice9
>
-
5/24/2018 DVD Shopping Cart Project Tutorial
9/43
#u%lic void refreshoriginaldata(
throws Ece#tion ;
Enumeration e = items.elements(9
connection = +roductDataBean.get-onnection(9
statement = connection.create!tatement(9
while (e.hasForeElements( ;
D0D item = (D0D e.netElement(9
!tring movieName = item.getFovie(9
int 6t =item.get7uantity(9
esult!et srs = statement.eecute7uery(?select 6uantity from #roducts ?
?where movie = H?movieName?H?9
srs.net(9
srs.u#dateInt(?6uantity?, 6t9
srs.u#dateow(9
>
>
>
##&emove'tem$ction%java
im#ort &ava.servlet.htt#.:9
im#ort com.o#ensym#hony.wor2".'ction!u##ort9
im#ort org.a#ache.struts".interce#tor.!ervlete6uest'ware9
-
5/24/2018 DVD Shopping Cart Project Tutorial
10/43
im#ort org.a#ache.struts".interce#tor.!ervletes#onse'ware9
#u%lic class emoveItem'ction etends 'ction!u##ort im#lements
!ervlete6uest'ware,!ervletes#onse'ware
;
#rivate
-
5/24/2018 DVD Shopping Cart Project Tutorial
11/43
order more items
:: !tring url=?&s#!how+roduct-atalog.&s#?9
!ervlet-ontet sc = get!ervlet-ontet(9
e6uestDis#atcher rd = sc.gete6uestDis#atcher(url9
rd.forward(re6uest, res#onse9::
return !@--E!!9
>
>
##ProductDataean%java
im#ort &ava.io.:9
im#ort &ava.s6l.:9
im#ort &ava.util.:9
#u%lic class +roductDataBean im#lements !erialiAa%le ;
#rivate static -onnection connection9
#rivate +re#ared!tatement addecord, getecords9
#u%lic +roductDataBean( ;
try ;
!tring userName = ?sam#le?9
!tring #assword = ?sam#le?9
!tring url = ?&d%c4mys6l4localhosttest?9
-lass.forName (?oracle.&d%c.driver.OracleDriver?.newInstance(9
connection = DriverFanager.get-onnection(? &d%c4oracle4thin4localhost45"4KE?,
userName, #assword9
-
5/24/2018 DVD Shopping Cart Project Tutorial
12/43
!ystem.out.#rintln (?Data%ase connection esta%lished?9
> catch(Ece#tion e;e.#rint!tac2race(9>
>
#u%lic static -onnection get-onnection( ;
return connection9
>
#u%lic 'rray8ist get+roduct8ist( throws !78Ece#tion ;
'rray8ist #roduct8ist = new 'rray8ist(9
!tatement statement = connection.create!tatement(9
esult!et results = statement.eecute7uery(
?!E8E- : OF #roducts?9
while (results.net( ;
D0D movie = new D0D(9
movie.setFovie(results.get!tring(9
movie.setating(results.get!tring("9
movie.set3ear(results.get!tring(19
movie.set+rice(results.getDou%le(L9
#roduct8ist.add(movie9
>
return #roduct8ist9
>
>
##DVD%java
-
5/24/2018 DVD Shopping Cart Project Tutorial
13/43
im#ort &ava.io.:9
#u%lic class D0D im#lements !erialiAa%le ;
!tring mMmovie9
!tring mMrated9
!tring mMyear9
dou%le mM#rice9
int 6uantity9
#u%lic D0D( ;
mMmovie = ??9
mMrated = ??9
mMyear = ??9
mM#rice = C9
6uantity = C9
>
#u%lic D0D(!tring movieName, !tring movieate, !tring movie3ear,
dou%le movie+rice, int movie7uantity ;
mMmovie = movieName9
mMrated = movieate9
mMyear = movie3ear9
mM#rice = movie+rice9
6uantity = movie7uantity9
>
#u%lic void setFovie(!tring title ;
-
5/24/2018 DVD Shopping Cart Project Tutorial
14/43
mMmovie = title9
>
#u%lic !tring getFovie( ;
return mMmovie9
>
#u%lic void setating(!tring rating ;
mMrated = rating9
>
#u%lic !tring getating( ;
return mMrated9
>
#u%lic void set3ear(!tring year ;
mMyear = year9
>
#u%lic !tring get3ear( ;
return mMyear9
>
#u%lic void set+rice(dou%le # ;
mM#rice = #9
>
#u%lic dou%le get+rice( ;
return mM#rice9
>
#u%lic void set7uantity(int 6 ;
6uantity = 69
>
-
5/24/2018 DVD Shopping Cart Project Tutorial
15/43
#u%lic int get7uantity( ;
return 6uantity9
>
>
##Checout$ction%java
im#ort &ava.servlet.htt#.:9
im#ort com.o#ensym#hony.wor2".'ction!u##ort9
im#ort org.a#ache.struts".interce#tor.!ervlete6uest'ware9
im#ort org.a#ache.struts".interce#tor.!ervletes#onse'ware9
#u%lic class -hec2out'ction etends 'ction!u##ort im#lements!ervlete6uest'ware,!ervletes#onse'ware
;
#rivate
-
5/24/2018 DVD Shopping Cart Project Tutorial
16/43
/et the cart
!ho##ing-art cart = (!ho##ing-art session.get'ttri%ute(
?!ho##ing-art?9
try;
cart.com#leteOrder(9
cart.refreshoriginaldata(9
> catch(Ece#tion e;
e.#rint!tac2race(9>
res#onse.sendedirect(res#onse.encodeedirect@8(
?!how-onfirmation.&s#?9
return !@--E!!9
>
>
##$ddoShoppingCart$ction%java
im#ort &ava.servlet.htt#.:9
im#ort com.o#ensym#hony.wor2".'ction!u##ort9
im#ort org.a#ache.struts".interce#tor.!ervlete6uest'ware9
im#ort org.a#ache.struts".interce#tor.!ervletes#onse'ware9
#u%lic class 'ddo!ho##ing-art'ction etends 'ction!u##ort im#lements
!ervlete6uest'ware,!ervletes#onse'ware
;
#rivate
-
5/24/2018 DVD Shopping Cart Project Tutorial
17/43
this.re6uest=re6uest9
>
#u%lic void set!ervletes#onse(
#u%lic !tring eecute(
;
/et the D0D from the re6uest
!tring movieName = re6uest.get+arameter(?movieName?9
!tring movieate = re6uest.get+arameter(?movieate?9
!tring movie3ear = re6uest.get+arameter(?movie3ear?9
!tring #rice = re6uest.get+arameter(?movie+rice?9
int movie7uantity = Integer.#arseInt(
re6uest.get+arameter(?movie7uantity?9
dou%le movie+rice = Dou%le.#arseDou%le(#rice9
-reate this D0D and add to the cart
D0D D0DItem = new D0D(movieName, movieate, movie3ear,
movie+rice, movie7uantity9
-
5/24/2018 DVD Shopping Cart Project Tutorial
18/43
get'ttri%ute(?!ho##ing-art?9
cart.addItem(D0DItem9
:: !tring url=?!how+roduct-atalog.&s#?9
!ervlet-ontet sc = get!ervlet-ontet(9
e6uestDis#atcher rd = sc.gete6uestDis#atcher(url9
rd.forward(re6uest, res#onse9::
session.set'ttri%ute(?!ho##ingcart?,cart9
return !@--E!!9
>
>
Now In the Directory !tructure clic2 on $e% +ages and then ight clic2 on the eam#le
and select New, clic2 on the J!+ from the dro# down menu.
-
5/24/2018 DVD Shopping Cart Project Tutorial
19/43
Name your J!+ ile and choose the location to %e $e% +ages and the folder to %e
eam#le and clic2 inish.
-
5/24/2018 DVD Shopping Cart Project Tutorial
20/43
Now add the dis#laysho##ingcart.&s# in the res#ective &s# #age
-
5/24/2018 DVD Shopping Cart Project Tutorial
21/43
##displa*shoppingcart%jsp
)) Dis#lay!ho##ing-art.&s# ))*
#age contenty#e=?tethtml9charset=@)?language=?&ava?*
tagli% uri=?struts)tags? #refi=?s? *
#age im#ort=?eam#le.:,&ava.util.:,&ava.tet.:? *
!ho##ing-art cart = (!ho##ing-art
-
5/24/2018 DVD Shopping Cart Project Tutorial
22/43
session.get'ttri%ute(?!ho##ing-art?9
if (cart == null;
cart = new !ho##ing-art(9
session.set'ttri%ute(?!ho##ing-art?, cart9
>
0ector items = cart.getItems(9
if (items.siAe( P= C ;
*
)) Dis#lay the heading of the sho##ing-art ))*
h*!ho##ing -arth*
%r*
ta%le %order=L*
tr*th*D0D Namesth*ateth*3earth*+riceth*7uantity
th*emove
int numItems = items.siAe(9
Num%erormat currency = Num%erormat.get-urrencyInstance(9
for (int i=C9 i numItems9 i ;
D0D item = (D0D items.element't(i9
*
tr*
s4form action=?removeItem?
-
5/24/2018 DVD Shopping Cart Project Tutorial
23/43
method=?+O!?*
td*= item.getFovie( *td*
td*= item.getating( *td*
td*= item.get3ear( *td*
td*= item.get+rice( *td*
td*= item.get7uantity( *td*
td*
in#ut ty#e=?hidden? name= ?item? value=H= i *H*
in#ut ty#e=?su%mit? value=?emove?* td*
s4form*
tr*
>
*
ta%le*
s4form action=?chec2out?
method=?+O!?*
in#ut ty#e=?su%mit? name=?!u%mit? value=?-hec2 out?*
s4form*
>
*
8i2e the a%ove &s# #age add the showconfirmation.&s#, show#roductcatalog.&s#, and
welcome.&s#. the code is listed %elow
+,-- ShowCon.irmation%jsp --,/
-
5/24/2018 DVD Shopping Cart Project Tutorial
24/43
#age contenty#e=?tethtml9charset=@)?language=?&ava?*
tagli% uri=?struts)tags? #refi=?s? *
#age im#ort=?eam#le.:, &ava.tet.:? *
html*
%ody*
h1*3our Order is confirmedPh1*
Decimalormat twoDigits = new Decimalormat(?C.CC?9
!tring total+rice =
twoDigits.format(((!ho##ing-artsession.get'ttri%ute
(?!ho##ing-art?.getotal+rice(9
*
h1*he total ammount is Q=total+rice *h1*
session.invalidate(9 *
%ody*
html*
+,-- ShowProductCatalog%jsp --,/
#age contenty#e=?tethtml9charset=@)?language=?&ava?*
tagli% uri=?struts)tags? #refi=?s? *
#age im#ort=?eam#le.:,&ava.net.:,&ava.tet.:,&ava.util.:? *
&s#4useBean id = ?data? sco#e= ?re6uest?
class = ?eam#le.+roductDataBean? *
html*
%ody*
-
5/24/2018 DVD Shopping Cart Project Tutorial
25/43
)) -all get+roduct8ist( of the +roductDataBean to get the D0D #roduct
catalog and #ut it on the #roduct8ist
))*
8ist #roduct8ist = data.get+roduct8ist(9
Iterator #rod8istIterator = #roduct8ist.iterator(9
*
#*center*
h*D0D -atalogh*
ta%le %order4%lue %gcolor=?rg%("5,""5,"R?*
thread*
tr*
th*D0D Namesth*
th*atingth*
th*3earth*
th*+riceth*
th*7uantityth*
th*'dd-artth*
tr*
thread*
t%ody*
)) Dis#lay all D0D #roducts row %y row on the ta%le, add anadd-art
%utton at the end of each row to allow clients to select
))*
-
5/24/2018 DVD Shopping Cart Project Tutorial
26/43
while (#rod8istIterator.hasNet(;
D0D movie = (D0D#rod8istIterator.net(9
!tring movie7uantity = ?movie7uantity?9
*
tr %gcolor=?red?*
s4form name=?addtosho##ingcart?
action=?addtosho##ingcart?
method=?+O!?*
td *= movie.getFovie( *td*
td *= movie.getating( *td*
td *= movie.get3ear( *td*
td *= movie.get+rice( *td*
td *in#ut ty#e = tet name = ?movie7uantity?
siAe =?1? *td*
td*in#ut align=?right? ty#e=?su%mit? value=?'ddo-art?*
in#ut ty#e=?hidden? name= ?movieName?
value=H= movie.getFovie( *H*
in#ut ty#e=?hidden? name= ?movieate?
value=H= movie.getating( *H*
in#ut ty#e=?hidden? name= ?movie3ear?
value=H= movie.get3ear( *H*
in#ut ty#e=?hidden? name= ?movie+rice?
value=H= movie.get+rice( *H*td*
s4form*
tr*
>
-
5/24/2018 DVD Shopping Cart Project Tutorial
27/43
*
t%ody*
ta%le*
#*hr*
)) Dis#lay the current sho##ing -art %y including
Dis#lay!ho##ing-art.&s#
))*
&s#4include #age=?dis#laysho##ingcart.&s#? flush=?true? *
center*
%ody*
html*
##0elcome%jsp
#age contenty#e=?tethtml9charset=@)?language=?&ava?*
tagli% uri=?struts)tags? #refi=?s? *
html*
head*
title*$E8-OFE O D0D !
-
5/24/2018 DVD Shopping Cart Project Tutorial
28/43
s4form*
%ody*
html*
Step 2! Setting up the oracle driver
/o to the !ervices section which is %eside the #ro&ects and clic2 on that 4
-
5/24/2018 DVD Shopping Cart Project Tutorial
29/43
Inside the Data%ases ight -lic2 on the Drivers and !elect )*New Drivers
-
5/24/2018 DVD Shopping Cart Project Tutorial
30/43
' Dialog %o will o#en and in that add the eecuta%le .&ar file from your +- which arelocated in Oracle*+roduct*..C(version*d%M*&d%c*li%
-
5/24/2018 DVD Shopping Cart Project Tutorial
31/43
'fter adding the .&ar file clic2 OS.
-
5/24/2018 DVD Shopping Cart Project Tutorial
32/43
-
5/24/2018 DVD Shopping Cart Project Tutorial
33/43
Now in the Drivers you will see the New Driver which is Oracle hin. ight -lic2 on
the Oracle hin and #ress -onnect @sing T..
Now fill u# the
-
5/24/2018 DVD Shopping Cart Project Tutorial
34/43
Now in the same dialog %o clic2 on the 'dvanced a% which is %eside Basic !etting
and select the !chema which you created from the dro# down menu and #ress o2.
-
5/24/2018 DVD Shopping Cart Project Tutorial
35/43
hen e#and the &d%c4Oracle hin to create a ta%le.
-
5/24/2018 DVD Shopping Cart Project Tutorial
36/43
-reate a ta%le4
ight -lic2 on the a%le to create a ta%le
-
5/24/2018 DVD Shopping Cart Project Tutorial
37/43
-reate a +roducts a%le as shown %elow wit the res#ective values
-
5/24/2018 DVD Shopping Cart Project Tutorial
38/43
Insert the ecord into the a%le
-
5/24/2018 DVD Shopping Cart Project Tutorial
39/43
o 0iew the inserted data ight -lic2 on the +roducts a%le and -lic2 view data
-
5/24/2018 DVD Shopping Cart Project Tutorial
40/43
Now -reate another a%le which !ho##ing-art in the same way as the a%ove ta%le. he
field values are shown in the %elow figure
-
5/24/2018 DVD Shopping Cart Project Tutorial
41/43
-
5/24/2018 DVD Shopping Cart Project Tutorial
42/43
Output!
Now un the a%ove #ro&ect and you will get the out#ut to %e
'fter you clic2 on the %utton the %elow #age will dis#lay
-
5/24/2018 DVD Shopping Cart Project Tutorial
43/43
Now select the Fovie you li2e and enter the 6uantity in the res#ective field and clic2 on
'ddo-art and the cart will u#date li2e this.
-lic2 on the -hec2out %utton and the -onfirmation #age will come