code and output for journal
TRANSCRIPT
-
8/17/2019 Code and Output for Journal
1/39
Stateless Enterprise Bean based Calculator Bean
index.jsp
Ente al6e o; 16m9e.
Ente al6e o; 16m9e
CalculatorBean.java
pac?age calce9
impot a7ax,e9,tatele00
impot a7ax,e9,&ocal'ean
@tatele00@&ocal'ean
p69lic cla00 alc6lato'ean A
p69lic int addBint n6m9e.Cint n6m9e A
et6n n6m9e.n6m9e
p69lic int 069tactBint n6m9e.Cint n6m9e A
et6n n6m9e.-n6m9e
-
8/17/2019 Code and Output for Journal
2/39
p69lic int m6ltiplyBint n6m9e.Cint n6m9e A et6n n6m9e.Gn6m9e
p69lic int di7ideBint n6m9e.Cint n6m9e A
et6n n6m9e./n6m9e
CalculatorFormServlet.javaimpot calce9,alc6lato'eanimpot a7a,io,(Exception
impot a7a,io,#int)iteimpot a7ax,e9,E:'
//impot a7ax,e9,E:'impot a7ax,0e7let,e7letException
impot a7ax,0e7let,annotation,)e9e7letimpot a7ax,0e7let,http,$ttpe7let
impot a7ax,0e7let,http,$ttpe7let4e56e0timpot a7ax,0e7let,http,$ttpe7let4e0pon0e
@)e9e7letBname="alc6latoFome7let"C 6l#atten0=A"/alc6latoFome7let"
p69lic cla00 alc6latoFome7let extend0 $ttpe7let A @E:'
pi7ate alc6lato'ean calc6lato'ean
/GG
G #oce00e0 e56e0t0 ;o 9oth $TT# HET and #Tmethod0,
G @paam e56e0t 0e7let e56e0t G @paam e0pon0e 0e7let e0pon0e
G @tho30 e7letException i; a 0e7let-0peci;ic eo occ60 G @tho30 (Exception i; an (/ eo occ60
G/
potected 7oid poce004e56e0tB$ttpe7let4e56e0t e56e0tC $ttpe7let4e0pon0ee0pon0e
tho30 e7letExceptionC (Exception A e0pon0e,0etontentTypeB"text/htmlcha0et=UTF-8"
int n6m9e.=(ntege,pa0e(ntBe56e0t,get#aameteB"n6m9e."
int n6m9e=(ntege,pa0e(ntBe56e0t,get#aameteB"n6m9e"
#int)ite o6t = e0pon0e,get)iteB
ty A
o6t,pintlnB"" o6t,pintlnB""
o6t,pintlnB"alc6latoFome7let"o6t,pintlnB""
o6t,pintlnB""
-
8/17/2019 Code and Output for Journal
3/39
o6t,pintlnB"Iddition o; 16m9e. and 16m9e ="calc6lato'ean,addBn6m9e.C n6m9e""
o6t,pintlnB"69taction o; 16m9e. and 16m9e ="calc6lato'ean,069tactBn6m9e.C n6m9e""
o6t,pintlnB"6ltiplication o; 16m9e. and 16m9e ="calc6lato'ean,m6ltiplyBn6m9e.C n6m9e""
o6t,pintlnB"Di7i0ion o; 16m9e. and 16m9e ="calc6lato'ean,di7ideBn6m9e.C n6m9e"" o6t,pintlnB"lic?$eeto go 9ac?"
o6t,pintlnB""
o6t,pintlnB""
;inally A
o6t,clo0eB
-
8/17/2019 Code and Output for Journal
4/39
-
8/17/2019 Code and Output for Journal
5/39
Statefull Enterprise Beam based Banking Operations Bean
index.jsp
(E 'an?
)elcome to (E 'an?
Fi0t 1ame 2 &a0t 1ame 2
Operations.jsp
(E 'an?
)elcome to (E 'an?
Ente Imo6nt to 9e tan0acted 2
hoo0e the opeation
Depo0it Imo6nt )ithda3 Imo6nt
Het 'alance
Imo6nt
processing.jsp
-
8/17/2019 Code and Output for Journal
6/39
(E 'an?
-
8/17/2019 Code and Output for Journal
7/39
i;Bamt
-
8/17/2019 Code and Output for Journal
8/39
;inally A
o6t,clo0eB transaction.java Servlet
pac?age 9ean
impot a7a,io,(Exceptionimpot a7a,io,#int)ite
impot a7ax,0e7let,e7letExceptionimpot a7ax,0e7let,http,$ttpe7let
impot a7ax,0e7let,http,$ttpe7let4e56e0timpot a7ax,0e7let,http,$ttpe7let4e0pon0e
p69lic cla00 tan0action extend0 $ttpe7let A
potected 7oid poce004e56e0tB$ttpe7let4e56e0t e56e0tC $ttpe7let4e0pon0e
e0pon0e tho30 e7letExceptionC (Exception A
e56e0t,get4e56e0tDi0patcheB"poce00ing,0p",;o3adBe56e0tC e0pon0e
-
8/17/2019 Code and Output for Journal
9/39
-
8/17/2019 Code and Output for Journal
10/39
-
8/17/2019 Code and Output for Journal
11/39
Message riven Bean emo
index.jsp
Te0t #age
e00age 'ean Te0ting
o6 e00age 2
MBean!.java
pac?age m9
impot a7a,6til,logging,&e7el
impot a7a,6til,logging,&ogge
impot a7ax,e9,Icti7ationon;ig#opetyimpot a7ax,e9,e00ageDi7en
impot a7ax,m0,:Exception
impot a7ax,m0,e00ageimpot a7ax,m0,e00age&i0tene
impot a7ax,m0,Texte00age
@e00ageDi7enBmapped1ame = "m0/de0t"C acti7ationon;ig = A @Icti7ationon;ig#opetyBpopety1ame = "ac?no3ledgeode"C popetyal6e =
"I6to-ac?no3ledge"C @Icti7ationon;ig#opetyBpopety1ame = "de0tinationType"C popetyal6e =
"a7ax,m0,K6e6e"
p69lic cla00 'ean. implement0 e00age&i0tene A
p69lic 'ean.B A
p69lic 7oid one00ageBe00age me00age A
Texte00age tm0g=n6ll tm0g=BTexte00ageme00age
-
8/17/2019 Code and Output for Journal
12/39
ty A y0tem,o6t,pintlnBtm0g,getTextB
catch B:Exception ex A &ogge,get&oggeB'ean.,cla00,get1ameB,logB&e7el,EE4EC n6llC ex
mbServlet.java
pac?age m9
impot a7a,io,(Exception
impot a7a,io,#int)iteimpot a7a,6til,logging,&e7el
impot a7a,6til,logging,&oggeimpot a7ax,annotation,4e0o6ce
impot a7ax,m0,onnectionimpot a7ax,m0,onnectionFactoy
impot a7ax,m0,:Exception
impot a7ax,m0,e00ageimpot a7ax,m0,e00age#od6ceimpot a7ax,m0,K6e6e
impot a7ax,m0,e00ionimpot a7ax,m0,Texte00age
impot a7ax,0e7let,e7letExceptionimpot a7ax,0e7let,http,$ttpe7let
impot a7ax,0e7let,http,$ttpe7let4e56e0timpot a7ax,0e7let,http,$ttpe7let4e0pon0e
p69lic cla00 m9e7let extend0 $ttpe7let A
@4e0o6ceBname = "m0/de0t"
pi7ate K6e6e de0t @4e0o6ceBname = "m0/56e6e"
pi7ate onnectionFactoy 56e6e
potected 7oid poce004e56e0tB$ttpe7let4e56e0t e56e0tC $ttpe7let4e0pon0ee0pon0e
tho30 e7letExceptionC (Exception A e0pon0e,0etontentTypeB"text/htmlcha0et=UTF-8"
#int)ite o6t = e0pon0e,get)iteB ting 0t=e56e0t,get#aameteB"m0g"
ty A 0end:e00ageToDe0tB0t
catch B:Exception ex A &ogge,get&oggeBm9e7let,cla00,get1ameB,logB&e7el,EE4EC n6llC ex
ty A
o6t,pintlnB""
o6t,pintlnB""
-
8/17/2019 Code and Output for Journal
13/39
o6t,pintlnB"e7let m9e7let"o6t,pintlnB""
o6t,pintlnB"" o6t,pintlnB"o6 e00age " 0t "ha0 9een 0ent to the e7e!!! #l0 chec?
the log" o6t,pintlnB""
o6t,pintlnB""
;inally A
o6t,clo0eB
pi7ate e00age ceate:e00ageFom0De0tBe00ion 0e00ionC 9ect me00ageDatatho30 :Exception A
// TD ceate and pop6late me00age to 0end Texte00age tm = 0e00ion,ceateTexte00ageB
tm,0etTextBme00ageData,totingB et6n tm
pi7ate 7oid 0end:e00ageToDe0tB9ect me00ageData tho30 :Exception A onnection connection = n6ll
e00ion 0e00ion = n6ll ty A
connection = 56e6e,ceateonnectionB
0e00ion = connection,ceatee00ionB;al0eC e00ion,IUTLIM1)&EDHE e00age#od6ce me00age#od6ce = 0e00ion,ceate#od6ceBde0t
me00age#od6ce,0endBceate:e00ageFom0De0tB0e00ionC me00ageData
;inally A i; B0e00ion != n6ll A
ty A 0e00ion,clo0eB
catch B:Exception e A &ogge,get&oggeBthi0,getla00B,get1ameB,logB&e7el,)I41(1HC "annot
clo0e 0e00ion"C e
i; Bconnection != n6ll A
connection,clo0eB
-
8/17/2019 Code and Output for Journal
14/39
-
8/17/2019 Code and Output for Journal
15/39
-
8/17/2019 Code and Output for Journal
16/39
-
8/17/2019 Code and Output for Journal
17/39
Entit" Bean emo t#roug# $ava %ersistence &%' ($%&)
index.jsp
$4 Depatment
$ello )old!
Employee 1ame
alay
empServlet.java
pac?age e9d
impot epa,Employee
impot epa,EmployeeFacade&ocalimpot a7a,io,(Exception
impot a7a,io,#int)iteimpot a7ax,e9,E:'
impot a7ax,0e7let,e7letExceptionimpot a7ax,0e7let,http,$ttpe7let
impot a7ax,0e7let,http,$ttpe7let4e56e0timpot a7ax,0e7let,http,$ttpe7let4e0pon0e
p69lic cla00 empe7let extend0 $ttpe7let A @E:'
pi7ate EmployeeFacade&ocal employeeFacade
potected 7oid poce004e56e0tB$ttpe7let4e56e0t e56e0tC $ttpe7let4e0pon0ee0pon0e
tho30 e7letExceptionC (Exception A
-
8/17/2019 Code and Output for Journal
18/39
Employee o9=ne3 EmployeeB o9,0et1ameBe56e0t,get#aameteB"ename"
o9,0etalayB(ntege,pa0e(ntBe56e0t,get#aameteB"e0alay" employeeFacade,ceateBo9
e0pon0e,0etontentTypeB"text/htmlcha0et=UTF-8" #int)ite o6t = e0pon0e,get)iteB ty A
o6t,pintlnB""
o6t,pintlnB"" o6t,pintlnB"$4 Depatment"
o6t,pintlnB"" o6t,pintlnB""
o6t,pintlnB" 1e3 Employee " e56e0t,get#aameteB"ename" "eated6cce00;6lly"
o6t,pintlnB"" o6t,pintlnB""
;inally A
o6t,clo0eB
Emplo"ee.javapac?age epa
impot a7a,io,eialiJa9le
impot a7ax,pe0i0tence,Entityimpot a7ax,pe0i0tence,Heneatedal6e
impot a7ax,pe0i0tence,HeneationType
impot a7ax,pe0i0tence,(d
@Entity
p69lic cla00 Employee implement0 eialiJa9le A pi7ate 0tatic ;inal long 0eiale0ionU(D = .&
@(d @Heneatedal6eB0tategy = HeneationType,(DE1T(T
pi7ate &ong id ting name
int 0alay
p69lic ting get1ameB A et6n name
p69lic 7oid 0et1ameBting name A
thi0,name = name
p69lic int getalayB A et6n 0alay
-
8/17/2019 Code and Output for Journal
19/39
p69lic 7oid 0etalayBint 0alay A
thi0,0alay = 0alay
p69lic &ong get(dB A et6n id
p69lic 7oid 0et(dB&ong id A
thi0,id = id
@7eide p69lic int ha0hodeB A
int ha0h = ha0h = Bid != n6ll N id,ha0hodeB 2
et6n ha0h
@7eide p69lic 9oolean e56al0B9ect o9ect A // TD2 )aning - thi0 method 3onOt 3o? in the ca0e the id ;ield0 ae not 0et
i; B!Bo9ect in0tanceo; Employee A et6n ;al0e
Employee othe = BEmployee o9ect
i; BBthi0,id == n6ll PP othe,id != n6ll QQ Bthi0,id != n6ll PP !thi0,id,e56al0Bothe,id A et6n ;al0e
et6n t6e
@7eide
p69lic ting totingB A et6n "epa,EmployeeRid=" id "S"
Emplo"eeFacade.javapac?age epa
impot a7a,6til,&i0t
impot a7ax,e9,tatele00impot a7ax,pe0i0tence,Entityanage
impot a7ax,pe0i0tence,#e0i0tenceontextimpot a7ax,pe0i0tence,K6ey
impot a7ax,pe0i0tence,citeia,iteiaK6eyimpot a7ax,pe0i0tence,citeia,4oot
@tatele00
p69lic cla00 EmployeeFacade implement0 EmployeeFacade&ocal A @#e0i0tenceontextB6nit1ame = "Entity'eanDemo-e9#U"
pi7ate Entityanage em
-
8/17/2019 Code and Output for Journal
20/39
p69lic 7oid ceateBEmployee employee A
em,pe0i0tBemployee
p69lic 7oid editBEmployee employee A
em,megeBemployee
p69lic 7oid emo7eBEmployee employee A
em,emo7eBem,megeBemployee
p69lic Employee ;indB9ect id A
et6n em,;indBEmployee,cla00C id
p69lic &i0t ;indIllB A
iteiaK6ey c5 = em,getiteia'6ildeB,ceateK6eyB
c5,0electBc5,;omBEmployee,cla00 et6n em,ceateK6eyBc5,get4e06lt&i0tB
p69lic &i0t ;ind4angeBintRS ange A
iteiaK6ey c5 = em,getiteia'6ildeB,ceateK6eyB c5,0electBc5,;omBEmployee,cla00
K6ey 5 = em,ceateK6eyBc5 5,0etax4e06lt0BangeR.S - angeRS
5,0etFi0t4e06ltBangeRS et6n 5,get4e06lt&i0tB
p69lic int co6ntB A
iteiaK6ey c5 = em,getiteia'6ildeB,ceateK6eyB 4oot t = c5,;omBEmployee,cla00
c5,0electBem,getiteia'6ildeB,co6ntBt K6ey 5 = em,ceateK6eyBc5
et6n BB&ong 5,getingle4e06ltB,intal6eB
Emplo"eeFacade*ocal.java
pac?age epa
impot a7a,6til,&i0timpot a7ax,e9,&ocal
@&ocal
p69lic inte;ace EmployeeFacade&ocal A
7oid ceateBEmployee employee
7oid editBEmployee employee
-
8/17/2019 Code and Output for Journal
21/39
7oid emo7eBEmployee employee
Employee ;indB9ect id
&i0t ;indIllB
&i0t ;ind4angeBintRS ange
int co6ntB
-
8/17/2019 Code and Output for Journal
22/39
-
8/17/2019 Code and Output for Journal
23/39
-
8/17/2019 Code and Output for Journal
24/39
'nternationali+ation in Struts,
index.jsp
Employee Fom 3ith m6ltiling6al 6ppot
en
e0
hi
Engli0h
pani0h
$indi
success.jsp
6cce00
-
8/17/2019 Code and Output for Journal
25/39
Emplo"ee.java
pac?age com,action
impot com,open0ymphony,x3o?,Iction6ppot
p69lic cla00 Employee extend0 Iction6ppotA
pi7ate ting name
pi7ate int age
p69lic ting get1ameB A
et6n name
p69lic 7oid 0et1ameBting name A
thi0,name = name
p69lic int getIgeB A
et6n age
p69lic 7oid 0etIgeBint age A
thi0,age = age
@7eide
p69lic ting exec6teB tho30 Exception A et6n UE
*ocale.javapac?age com,action
impot com,open0ymphony,x3o?,Iction6ppot
p69lic cla00 &ocale extend0 Iction6ppotA @7eide
p69lic ting exec6teBA et6n UE
sturts.xml
-
8/17/2019 Code and Output for Journal
26/39
/index,0p
/06cce00,0p
/index,0p
global.properties To change thi0 licen0e headeC choo0e &icen0e $eade0 in #oect #opetie0,
To change thi0 template ;ileC choo0e Tool0 Q Template0 and open the template in the edito,
glo9al,name=1ame
glo9al,age=Igeglo9al,069mit=69mitglo9al,heading=Fill thi0 ;om
glo9al,06cce00=6cce00;6lly I6thenticated
-
8/17/2019 Code and Output for Journal
27/39
-
8/17/2019 Code and Output for Journal
28/39
Struts2 based Web Application with Data Base connectivity
index.jsp
2ucce!!ul ,ogin 3ello##
error.jsp
-
8/17/2019 Code and Output for Journal
29/39
pac6age com7action+
impot !tatic com7open!ymphony7x1o670ction7E999+impot !tatic com7open!ymphony7x1o670ction72U&&E22+impot com7open!ymphony7x1o670ction2uppot+impot :a*a7!)l7;+
public cla!! ,ogin0ction extend! 0ction2uppot pi*ate 2ting u!ename+ pi*ate 2ting pa!!1od+ pi*ate 2ting name+
public 2ting getU!ename etun u!ename+ ?
public *oid !etU!ename2ting u!ename thi!7u!ename = u!ename+
?
public 2ting get(a!!1od etun pa!!1od+ ?
public *oid !et(a!!1od2ting pa!!1od thi!7pa!!1od = pa!!1od+ ?
public 2ting getame etun name+
?
public *oid !etame2ting name thi!7name = name+ ?
@*eide public 2ting execute 2ting et=E999+ &onnection conn=null+
ty 2ting ul=":dbc.my!)l.//localho!t/!tut!db"+ &la!!7oame"com7my!)l7:dbc7$i*e"+ conn=$i*eAanage7get&onnectionulB"oot"B""+ 2ting !)l="2E,E&T name F9A emplogin 53E9E u!ename=C 0$pa!!1od=C"+ (epaed2tatement p!=conn7pepae2tatement!)l+ p!7!et2tingB u!ename+ p!7!et2tingB pa!!1od+
-
8/17/2019 Code and Output for Journal
30/39
9e!ult2et !=p!7executeDuey+
1hile!7next name=!7get2ting+ et=2U&&E22+
?
? catchException e et=E999+ ? nally iconn#=null ty conn7clo!e+ ? catchException e
?
? ? etun et+
??struts.xml
!ucce!!7:!p eo7:!p
-
8/17/2019 Code and Output for Journal
31/39
-
8/17/2019 Code and Output for Journal
32/39
Create a structs, program using validators in xml for emplo"ee information
index.jsp-
"http2//333,3*,og/T4/html+/loo0e,dtd">
6cce00
Employee (n;omation i0 capt6ed 06cce00;6lly,
emplo"ee.java-
pac?age com,t6toial0point,0t6t0
impot com,open0ymphony,x3o?,Iction6ppot
p69lic cla00 Employee extend0 Iction6ppotA
pi7ate ting name
pi7ate int age
@7eide
p69lic ting exec6teB A
et6n UE
p69lic ting get1ameB A
et6n name
p69lic 7oid 0et1ameBting name A
thi0,name = name
p69lic int getIgeB A
et6n age
p69lic 7oid 0etI geBint age A
-
8/17/2019 Code and Output for Journal
33/39
thi0,age = age
@7eide
p69lic 7oid 7alidateB A
i; Bname == n6ll QQ name,timB,e56al0B""
A
addFieldEoB"name"C"The name i0 e56ied"
i; Bage < 8 QQ age > W
A
addFieldEoB"age"C"Ige m60t 9e in 9et3een 8 and W"
structs.xml-
-
8/17/2019 Code and Output for Journal
34/39
Output:-
-
8/17/2019 Code and Output for Journal
35/39
-
8/17/2019 Code and Output for Journal
36/39
Emplo"eevalidation.xml-
-
8/17/2019 Code and Output for Journal
37/39
Create a Sample Structs2 Program For RedirectAction
Index.jsp:
$ello )old
4ediect Iction
#lea0e ente yo6 name
Ne!orld.jsp:-
$ello )old
4ediect Iction
#lea0e ente yo6 name
/ello0orld&ction.java-
pac?age com,t6toial0point,0t6t0impot com,open0ymphony,x3o?,Iction6ppot
p69lic cla00 $ello)oldIction extend0 Iction6ppotA pi7ate ting name
pi7ate int age @7eide
p69lic ting exec6teB tho30 Exception A
et6n "06cce00"
p69lic ting get1ameB
A et6n name
p69lic 7oid 0et1ameBting name A
thi0,name = name
p69lic int getIgeB
A
-
8/17/2019 Code and Output for Journal
38/39
et6n age
p69lic 7oid 0etIgeBint age A thi0,age = age
Structs.xml-
/1e3)old,0p
/index,0p
Output:-
-
8/17/2019 Code and Output for Journal
39/39