Professional Documents
Culture Documents
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 Ne.t
In the new dialog %o., name your #ro&ect and choose a location for it and +ress Ne.t.
In the ne.t dialog %o. select the server /lassfish 01 then #ress Ne.t
In the ne.t dialog %o. select the !truts" (ramewor2 then #ress (inish
3ou will see a default #age HelloWorld.jsp and the directory structure li2e this4
Step2!
In the Directory !tructure clic2 on !ource +ac2age and then 5ight clic2 on the e.am#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.
Edit the !ource -ode in the !how'ction.&ava
##Show$ction%java
im#ort &ava.s6l.!78E.ce#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.!ervlet5e6uest'ware9
im#ort org.a#ache.struts".interce#tor.!ervlet5es#onse'ware9
#u%lic class !how'ction e.tends 'ction!u##ort im#lements
!ervlet5e6uest'ware,!ervlet5es#onse'ware
;
#rivate <tt#!ervlet5e6uest re6uest9
#rivate <tt#!ervlet5es#onse res#onse9
#u%lic void set!ervlet5e6uest( <tt#!ervlet5e6uest re6uest
;
this.re6uest=re6uest9
>
#u%lic void set!ervlet5es#onse( <tt#!ervlet5es#onse res#onse
;
this.res#onse=res#onse9
>
#u%lic !tring e.ecute( throws !78E.ce#tion
;
+roductDataBean #d%=new +roductDataBean(9
'rray8ist al=#d%.get+roduct8ist(9
re6uest.set'ttri%ute(?#roductlist?,al9
return !@--E!!9
>
>
8i2e the a%ove !how'ction.&ava add the !ho##ing-art.&ava, 5emoveItem'ction.&ava,
+roductDataBean.&ava, D0D.&ava, -hec2out'ction.&ava, 'dd,o!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 add5ecord, get5ecords9
#rivate !tatement statement9
#rivate dou%le total+rice9
static int -'5,ID = B9
#rotected 0ector items9
#u%lic !ho##ing-art( ;
items = new 0ector(9
>
#u%lic 0ector getItems( ;
return (0ector items.clone(9
>
#u%lic void addItem(D0D newItem ;
%oolean flag = false9
if (items.siAe( == C ;
items.addElement(newItem9
return9
>
for (int i = C9 iD items.siAe(9 iEE ;
D0D dvd = (D0D items.element't(i9
if (dvd.getFovie(.e6uals(newItem.getFovie( ;
dvd.set7uantity(dvd.get7uantity(EnewItem.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(itemInde.9
>
#u%lic void com#leteOrder(
throws E.ce#tion ;
Enumeration e = items.elements(9
connection = +roductDataBean.get-onnection(9
statement = connection.create!tatement(9
while (e.hasForeElements( ;
D0D item = (D0D e.ne.tElement(9
!tring item7uantity = ?? E item.get7uantity(9
total+rice = total+rice E item.get+rice( :
Integer.#arseInt(item7uantity9
!tring u#date!tring = ?IN!E5, IN,O !ho##ing-arts ? E
? 0'8@E! (? E -'5,ID E ?, H? E
item.getFovie( E ?H, H? E
item.get5ating( E ?H, H? E
item.get3ear( E ?H, ? E
item.get+rice( E ?, ? E
item.get7uantity( E ??9
statement.e.ecute@#date(u#date!tring9
>
-'5,ID EE9
>
#u%lic dou%le get,otal+rice( ;
return this.total+rice9
>
#u%lic void refreshoriginaldata(
throws E.ce#tion ;
Enumeration e = items.elements(9
connection = +roductDataBean.get-onnection(9
statement = connection.create!tatement(9
while (e.hasForeElements( ;
D0D item = (D0D e.ne.tElement(9
!tring movieName = item.getFovie(9
int 6t =item.get7uantity(9
5esult!et srs = statement.e.ecute7uery(?select 6uantity from #roducts ? E
?where movie = H?EmovieNameE?H?9
srs.ne.t(9
srs.u#dateInt(?6uantity?, 6t9
srs.u#date5ow(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.!ervlet5e6uest'ware9
im#ort org.a#ache.struts".interce#tor.!ervlet5es#onse'ware9
#u%lic class 5emoveItem'ction e.tends 'ction!u##ort im#lements
!ervlet5e6uest'ware,!ervlet5es#onse'ware
;
#rivate <tt#!ervlet5e6uest re6uest9
#rivate <tt#!ervlet5es#onse res#onse9
#u%lic void set!ervlet5e6uest( <tt#!ervlet5e6uest re6uest
;
this.re6uest=re6uest9
>
#u%lic void set!ervlet5es#onse( <tt#!ervlet5es#onse res#onse
;
this.res#onse=res#onse9
>
#u%lic !tring e.ecute(
;
II /et the inde. of the item to remove
int itemInde. = Integer.#arseInt(re6uest.get+arameter(?item?9
<tt#!ession session = re6uest.get!ession(9
II /et the cart
!ho##ing-art cart = (!ho##ing-art session.get'ttri%ute(
?!ho##ing-art?9
cart.removeItem(itemInde.9
II Dis#lay the cart and allow user to chec2 out or
II order more items
I:: !tring url=?I&s#I!how+roduct-atalog.&s#?9
!ervlet-onte.t sc = get!ervlet-onte.t(9
5e6uestDis#atcher rd = sc.get5e6uestDis#atcher(url9
rd.forward(re6uest, res#onse9::I
return !@--E!!9
>
>
##ProductData(ean%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 add5ecord, get5ecords9
#u%lic +roductDataBean( ;
try ;
!tring userName = ?sam#le?9
!tring #assword = ?sam#le?9
II !tring url = ?&d%c4mys6l4IIlocalhostItest?9
-lass.forName (?oracle.&d%c.driver.OracleDriver?.newInstance(9
connection = DriverFanager.get-onnection(? &d%c4oracle4thin4Jlocalhost4B5"B4KE?,
userName, #assword9
!ystem.out.#rintln (?Data%ase connection esta%lished?9
> catch(E.ce#tion e;e.#rint!tac2,race(9>
>
#u%lic static -onnection get-onnection( ;
return connection9
>
#u%lic 'rray8ist get+roduct8ist( throws !78E.ce#tion ;
'rray8ist #roduct8ist = new 'rray8ist(9
!tatement statement = connection.create!tatement(9
5esult!et results = statement.e.ecute7uery(
?!E8E-, : (5OF #roducts?9
while (results.ne.t( ;
D0D movie = new D0D(9
movie.setFovie(results.get!tring(B9
movie.set5ating(results.get!tring("9
movie.set3ear(results.get!tring(19
movie.set+rice(results.getDou%le(L9
#roduct8ist.add(movie9
>
return #roduct8ist9
>
>
##DVD%java
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 movie5ate, !tring movie3ear,
dou%le movie+rice, int movie7uantity ;
mMmovie = movieName9
mMrated = movie5ate9
mMyear = movie3ear9
mM#rice = movie+rice9
6uantity = movie7uantity9
>
#u%lic void setFovie(!tring title ;
mMmovie = title9
>
#u%lic !tring getFovie( ;
return mMmovie9
>
#u%lic void set5ating(!tring rating ;
mMrated = rating9
>
#u%lic !tring get5ating( ;
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
>
#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.!ervlet5e6uest'ware9
im#ort org.a#ache.struts".interce#tor.!ervlet5es#onse'ware9
#u%lic class -hec2out'ction e.tends 'ction!u##ort im#lements
!ervlet5e6uest'ware,!ervlet5es#onse'ware
;
#rivate <tt#!ervlet5e6uest re6uest9
#rivate <tt#!ervlet5es#onse res#onse9
#u%lic void set!ervlet5e6uest( <tt#!ervlet5e6uest re6uest
;
this.re6uest=re6uest9
>
#u%lic void set!ervlet5es#onse( <tt#!ervlet5es#onse res#onse
;
this.res#onse=res#onse9
>
#u%lic !tring e.ecute(
;
II /et the cart
<tt#!ession session = re6uest.get!ession(9
II /et the cart
!ho##ing-art cart = (!ho##ing-art session.get'ttri%ute(
?!ho##ing-art?9
try;
cart.com#leteOrder(9
II cart.refreshoriginaldata(9
> catch(E.ce#tion e;
e.#rint!tac2,race(9>
II res#onse.send5edirect(res#onse.encode5edirect@58(
II ?!how-onfirmation.&s#?9
return !@--E!!9
>
>
##$dd)oShoppingCart$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.!ervlet5e6uest'ware9
im#ort org.a#ache.struts".interce#tor.!ervlet5es#onse'ware9
#u%lic class 'dd,o!ho##ing-art'ction e.tends 'ction!u##ort im#lements
!ervlet5e6uest'ware,!ervlet5es#onse'ware
;
#rivate <tt#!ervlet5e6uest re6uest9
#rivate <tt#!ervlet5es#onse res#onse9
#u%lic void set!ervlet5e6uest( <tt#!ervlet5e6uest re6uest
;
this.re6uest=re6uest9
>
#u%lic void set!ervlet5es#onse( <tt#!ervlet5es#onse res#onse
;
this.res#onse=res#onse9
>
#u%lic !tring e.ecute(
;
II /et the D0D from the re6uest
!tring movieName = re6uest.get+arameter(?movieName?9
!tring movie5ate = re6uest.get+arameter(?movie5ate?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
II -reate this D0D and add to the cart
D0D D0DItem = new D0D(movieName, movie5ate, movie3ear,
movie+rice, movie7uantity9
<tt#!ession session = re6uest.get!ession(9
II /et the cart
!ho##ing-art cart = (!ho##ing-art session.
get'ttri%ute(?!ho##ing-art?9
cart.addItem(D0DItem9
I:: !tring url=?!how+roduct-atalog.&s#?9
!ervlet-onte.t sc = get!ervlet-onte.t(9
5e6uestDis#atcher rd = sc.get5e6uestDis#atcher(url9
rd.forward(re6uest, res#onse9::I
session.set'ttri%ute(?!ho##ingcart?,cart9
return !@--E!!9
>
>
Now In the Directory !tructure clic2 on $e% +ages and then 5ight clic2 on the e.am#le
and select New, clic2 on the J!+ from the dro# down menu.
Name your J!+ (ile and choose the location to %e $e% +ages and the folder to %e
e.am#le and clic2 (inish.
Now add the dis#laysho##ingcart.&s# in the res#ective &s# #age
##displa*shoppingcart%jsp
DN)) Dis#lay!ho##ing-art.&s# ))N*
DNJ #age content,y#e=?te.tIhtml9charset=@,()O?language=?&ava?N*
DNJ tagli% uri=?Istruts)tags? #refi.=?s? N*
DNJ #age im#ort=?e.am#le.:,&ava.util.:,&ava.te.t.:? N*
DN !ho##ing-art cart = (!ho##ing-art
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 ;
N*
DN)) Dis#lay the heading of the sho##ing-art ))N*
DhB*!ho##ing -artDIhB*
D%r*
Dta%le %order=L*
Dtr*Dth*D0D NamesDth*5ateDth*3earDth*+riceDth*7uantity
Dth*5emove
DN int numItems = items.siAe(9
Num%er(ormat currency = Num%er(ormat.get-urrencyInstance(9
for (int i=C9 i D numItems9 iEE ;
D0D item = (D0D items.element't(i9
N*
Dtr*
Ds4form action=?removeItem?
method=?+O!,?*
Dtd*DN= item.getFovie( N*DItd*
Dtd*DN= item.get5ating( N*DItd*
Dtd*DN= item.get3ear( N*DItd*
Dtd*DN= item.get+rice( N*DItd*
Dtd*DN= item.get7uantity( N*DItd*
Dtd*
Din#ut ty#e=?hidden? name= ?item? value=HDN= i N*HI*
Din#ut ty#e=?su%mit? value=?5emove?I* DItd*
DIs4form*
DItr*
DN >
N*
DIta%le*
Ds4form action=?chec2out?
method=?+O!,?*
Din#ut ty#e=?su%mit? name=?!u%mit? value=?-hec2 out?*
DIs4form*
DN >
N*
8i2e the a%ove &s# #age add the showconfirmation.&s#, show#roductcatalog.&s#, and
welcome.&s#. the code is listed %elow
+,-- ShowCon.irmation%jsp --,/
DNJ #age content,y#e=?te.tIhtml9charset=@,()O?language=?&ava?N*
DNJ tagli% uri=?Istruts)tags? #refi.=?s? N*
DNJ #age im#ort=?e.am#le.:, &ava.te.t.:? N*
Dhtml*
D%ody*
Dh1*3our Order is confirmedPDIh1*
DN
Decimal(ormat twoDigits = new Decimal(ormat(?C.CC?9
!tring total+rice =
twoDigits.format(((!ho##ing-artsession.get'ttri%ute
(?!ho##ing-art?.get,otal+rice(9
N*
Dh1*,he total ammount is QDN=total+rice N*DIh1*
DN session.invalidate(9 N*
DI%ody*
DIhtml*
+,-- ShowProductCatalog%jsp --,/
DNJ #age content,y#e=?te.tIhtml9charset=@,()O?language=?&ava?N*
DNJ tagli% uri=?Istruts)tags? #refi.=?s? N*
DNJ #age im#ort=?e.am#le.:,&ava.net.:,&ava.te.t.:,&ava.util.:? N*
D&s#4useBean id = ?data? sco#e= ?re6uest?
class = ?e.am#le.+roductDataBean? I*
Dhtml*
D%ody*
DN)) -all get+roduct8ist( of the +roductDataBean to get the D0D #roduct
catalog and #ut it on the #roduct8ist
))N*
DN 8ist #roduct8ist = data.get+roduct8ist(9
Iterator #rod8istIterator = #roduct8ist.iterator(9
N*
D#*Dcenter*
DhB*D0D -atalogDIhB*
Dta%le %order4%lue %gcolor=?rg%("B5,""5,"BR?*
Dthread*
Dtr*
Dth*D0D NamesDIth*
Dth*5atingDIth*
Dth*3earDIth*
Dth*+riceDIth*
Dth*7uantityDIth*
Dth*'dd-artDIth*
DItr*
DIthread*
Dt%ody*
DN)) Dis#lay all D0D #roducts row %y row on the ta%le, add an add-art
%utton at the end of each row to allow clients to select
))N*
DN while (#rod8istIterator.hasNe.t(;
D0D movie = (D0D#rod8istIterator.ne.t(9
!tring movie7uantity = ?movie7uantity?9
N*
Dtr %gcolor=?red?*
Ds4form name=?addtosho##ingcart?
action=?addtosho##ingcart?
method=?+O!,?*
Dtd *DN= movie.getFovie( N*DItd*
Dtd *DN= movie.get5ating( N*DItd*
Dtd *DN= movie.get3ear( N*DItd*
Dtd *DN= movie.get+rice( N*DItd*
Dtd *Din#ut ty#e = te.t name = ?movie7uantity?
siAe =?1? I*DItd*
Dtd*Din#ut align=?right? ty#e=?su%mit? value=?'dd,o-art?*
Din#ut ty#e=?hidden? name= ?movieName?
value=HDN= movie.getFovie( N*H*
Din#ut ty#e=?hidden? name= ?movie5ate?
value=HDN= movie.get5ating( N*H*
Din#ut ty#e=?hidden? name= ?movie3ear?
value=HDN= movie.get3ear( N*H*
Din#ut ty#e=?hidden? name= ?movie+rice?
value=HDN= movie.get+rice( N*H*DItd*
DIs4form*
DItr*
DN >
N*
DIt%ody*
DIta%le*
D#*Dhr*
DN)) Dis#lay the current sho##ing -art %y including
Dis#lay!ho##ing-art.&s#
))N*
D&s#4include #age=?dis#laysho##ingcart.&s#? flush=?true? I*
DIcenter*
DI%ody*
DIhtml*
##0elcome%jsp
DNJ #age content,y#e=?te.tIhtml9charset=@,()O?language=?&ava?N*
DNJ tagli% uri=?Istruts)tags? #refi.=?s? N*
Dhtml*
Dhead*
Dtitle*$E8-OFE ,O D0D !<O++IN/DItitle*
DIhead*
D%ody*
Dh*+lease clic2 the %utton to show the Items...DIh*
Ds4form action = ?show?*
Ds4su%mit name=H-8I-SH value=H-8I-SH access2ey=?S? 2ey = ?!<O$?I*
DIs4form*
DI%ody*
DIhtml*
Step 2! Setting up the oracle driver
/o to the !ervices section which is %eside the #ro&ects and clic2 on that 4
Inside the Data%ases 5ight -lic2 on the Drivers and !elect )*New Drivers
' Dialog %o. will o#en and in that add the e.ecuta%le .&ar file from your +- which are
located in Oracle*+roduct*BB.B.C(version*d%MB*&d%c*li%
'fter adding the .&ar file clic2 OS.
Now in the Drivers you will see the New Driver which is Oracle ,hin. 5ight -lic2 on
the Oracle ,hin and #ress -onnect @sing T..
Now fill u# the <ost to %e localhost, +ort to %e B5"B and select service Id and give
your username and #assword in the res#ective columns and chec2 the show JDB-
@58.
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.
,hen e.#and the &d%c4Oracle ,hin to create a ta%le.
-reate a ta%le4
5ight -lic2 on the ,a%le to create a ta%le
-reate a +roducts ,a%le as shown %elow wit the res#ective values
Insert the 5ecord into the ,a%le
,o 0iew the inserted data 5ight -lic2 on the +roducts ,a%le and -lic2 view data
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
Output!
Now 5un 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
Now select the Fovie you li2e and enter the 6uantity in the res#ective field and clic2 on
'dd,o-art and the cart will u#date li2e this.
-lic2 on the -hec2out %utton and the -onfirmation #age will come