Anda di halaman 1dari 11

SILVEROAKCOLLEGEOFENGINEERING&TECHNOLOGY

DEPARTMENTOFCOMPUTERENGI NEERI
NG/INFORMATI
ONTECHNOLOGY
MODULE-
3SOLUTION

Subj
ectName:AdvanceJav
a Semester
:VI
Subj
ectCode:
2160707 Academi
cSession:
Summer
’18

Q.
1 ConsiderBanktablewit
hattri
butesAccountNo,CustomerName, Balance,Phone
andAddr ess.Writ
eadatabaseapplicat
ionwhichallowsinsert
ion,updati
onand
delet
ionofrecordsinBanktable.Pr
intv
aluesofallcust
omer swhosebal anceis
great
erthan20,000.

Ans i
mpor
tjav
a.sql
.*
;

publ
i
ccl
asscr
ud{

publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs)t
hrowsExcept
ion

St
ri
ngur
l="
jdbc:
mysql
:
//l
ocal
host
/bankdb;

St
ri
nguname="
root
";

St
ri
ngpass="
socet
@123"
;

Cl
ass.
for
Name(
"com.
mysql
.j
dbc.
Dri
ver
");

Connect
ioncon1=Dr
iver
Manager
.get
Connect
ion(
url
,
uname,
pass)
;

St
atementst
1=con1.
creat
eSt
atement
();

i
f(
con1!
=nul
l
)

Sy
stem.
out
.pr
int
ln(
"Connect
ionest
abl
i
shedsuccessf
ull
y);

/
/inser
tmul
ti
pler
ecor
dswi
tht
hehel
pofPr
epar
edst
atement

Pr
epar
edSt
atementps=con1.
prepar
eSt
atement
("
inser
tint
obank
v
alues(
?,?
,?,
?,
?)"
);

ps.
set
Long(
1,1111111111)
; /
/Account
_no

ps.
set
Str
ing(
2,"
ABC"
); /
/Cust
omer
Name

ps.
set
Float
(3,
50000.
00)
; /
/Bal
ance

ps.
set
Long(
4,9999999999)
;//Phone

ps.
set
Str
ing(
5,”
Ahmedabad”
);/
/Addr
ess

ps.
execut
eUpdat
e()
;

ps.
set
Long(
1,2222222222)
; /
/Account
_no

ps.
set
Str
ing(
2,"
XYZ"
); /
/Cust
omer
Name

ps.
set
Float
(3,
10000.
00)
; /
/Bal
ance

ps.
set
Long(
4,8888888888)
;//Phone

ps.
set
Str
ing(
5,“
Gandhi
nagar
”);/
/Addr
ess

ps.
execut
eUpdat
e()
;

Sy
stem.
out
.pr
int
ln(
"Recor
d(s)i
nser
ted"
);

/
/Quer
ytoupdat
erecor
d

Str
ingquer
y1="
updat
ebanksetAddr
ess=’
Bar
oda'
wher
e
Addr
ess=’
Ahmedabad’
";

i
ntcount
=st
1.execut
eUpdat
e(quer
y1)
;

Sy
stem.
out
.pr
int
ln(
count
+"r
ecor
d(s)updat
ed"
);
/
/Quer
ytodel
eter
ecor
d

St
ri
ngquer
y2="
del
etef
rom bankwher
eAccount
_no=“
1111111111"
;

st
1.execut
eUpdat
e(quer
y2)
;

Sy
stem.
out
.pr
int
ln(
"Recor
ddel
etedsuccessf
ull
y!
!"
);

/
/Vi
ewi
nser
tedr
ecor
ds

St
ri
ngquer
y="
sel
ect*f
rom bankwher
eBal
ance>20000"
;

Resul
tSetr
s=st
1.execut
eQuer
y(quer
y);

whi
l
e(r
s.next
())

St
ri
ngac_
no=r
s.get
Long(
"Account
_no"
);

St
ri
ngname=r
s.get
Str
ing(
"Cust
omer
Name"
);

f
loatbal
=rs.
get
Float
("
Bal
ance"
);

i
ntphone=r
s.get
Long(
"Phone"
);

St
ri
ngaddr
=rs.
get
Str
ing(
"Addr
ess"
);

Sy
stem.
out
.pr
int
ln(
ac_
no+""
+name+""
+bal
+""
+phone+”“
+addr
);

Q.
2 Compar
eJSPwi
thSer
vlet
.

Ans

SERVLET JSP

Aser
vleti
saser
ver
-si
de JSPi
sani
nter
faceont
opofSer
vlet
s.I
nanot
herway
,we
pr
ogr
am andwr
it
ten cansayt
hatJSPsar
eext
ensi
onofser
vlet
stomi
nimi
zet
he

pur
elyonJav
a. ef
for
tofdev
eloper
stowr
it
eUserI
nter
facesusi
ngJav
a

pr
ogr
ammi
ng.

Ser
vlet
srunf
ast
ert
han JSPr
unssl
owerbecausei
thast
het
ransi
ti
onphasef
or

JSP conv
ert
ingf
rom JSPpaget
oaSer
vletf
il
e.Oncei
tis

conv
ert
edt
oaSer
vlett
heni
twi
l
lst
artt
hecompi
l
ati
on

Execut
esi
nsi
deaWeb AJSPpr
ogr
am i
scompi
l
edi
ntoaJav
aser
vletbef
ore

ser
ver
,suchasTomcat execut
ion.Oncei
tiscompi
l
edi
ntoaser
vlet
,it
'sl
i
fecy
cle

wi
l
lbesameasofser
vlet
.But
,JSPhasi
t'
sownAPIf
ort
he

l
i
fecy
cle.

Recei
vesHTTPr
equest
s Easi
ert
owr
it
ethanser
vlet
sasi
tissi
mil
art
oHTML.

f
rom user
sandpr
ovi
des

HTTPr
esponses

Wecannotbui
l
dany Oneoft
hekeyadv
ant
agei
swecanbui
l
dcust
om t
agsusi
ng

cust
om t
ags JSPAPI(
ther
eisasepar
atepackageav
ail
abl
eforwr
it
ing

t
hecust
om t
ags)whi
chcanbeav
ail
abl
east
her
e-usabl
e

component
swi
thl
otoff
lexi
bil
i
ty

Ser
vlethast
hel
i
fecy
cle JSPhast
hel
i
fecy
clemet
hodsofj
spI
nit
(),
_jspSer
vice(
)and

met
hodsi
nit
(),
ser
vice(
) j
spDest
roy
()

anddest
roy
()

Wr
it
teni
nJav
a,wi
tha JSPscanmakeuseoft
heJav
abeansi
nsi
det
hewebpages

f
ewaddi
ti
onal
API
s

speci
fi
ctot
hiski
ndof

pr
ocessi
ng.Si
ncei
tis
wr
it
teni
nJav
a,i
tfol
l
ows

al
ltheObj
ectOr
ient
ed

pr
ogr
ammi
ng

t
echni
ques.

I
nMVCar
chi
tect
ure I
nMVCar
chi
tect
ureJSPact
sasv
iew.

Ser
vletact
sas

cont
rol
l
er.

Ser
vletadv
ant
ages JSPPr
ovi
desanext
ensi
vei
nfr
ast
ruct
uref
or:

i
ncl
ude: 1.Tr
acki
ngsessi
ons.

1.Per
for
mance:
 
get 2.Managi
ngcooki
es.

l
oadeduponf
ir
st 3.Readi
ngandsendi
ngHTMLheader
s.

r
equestandr
emai
nsi
n 4.Par
singanddecodi
ngHTMLf
orm dat
a.

memor
yidenf
inat
ely
. 5.
 JSPi
sEf
fi
cient

Ever
yrequestf
oraJSPi
shandl
edbya

2.Si
mpl
i
cit
y: 
Runi
nsi
de si
mpl
eJav
athr
ead

cont
rol
l
edser
ver 6.
 JSPi
sScal
abl
e: 
Easyi
ntegr
ati
onwi
thot
herbackend

env
ironment
.Nospeci
fi
c ser
vices

cl
i
entsof
twar
eis 7.
 Seper
ati
onofr
oles:
 
Dev
eloper
s,Cont
entAut
hor
s/Gr
aphi
c

needed:
webbr
oseri
s Desi
gner
s/WebMast
ers

enough

3.Sessi
on

Management
 :

ov
ercomesHTTP'
s

st
atel
essnat
ure

4.Jav
aTechnol
ogy

:
 
net
wor
k

access,
Dat
abase
connect
ivi
ty,
j2ee

i
ntegr
ati
on

Q.
3 Explai
nJSP(i)page(
ii
)incl
ude(i
ii
)tagli
bdir
ecti
vewithat
tri
but
e,descr
ipt
ion.
Alsowrit
eexampleofjavasni
ppetforeachdir
ecti
ve.

Ans (
i) pagedi
rect
ive:
-

• Thepagedi rectivet aghol dst hei nstr


uctionst hatareusedbyaJSP
translat
ordur ingt hetranslati
onst ageoft heJSPl ifecycl
e.
• Thesei nstructionsaf fectdifferentpropertiesassociatedwiththewhole
JSPpage.
• Thepagedi r
ect i
vecanbeusedmul tipl
et i
mesi naJSPpage,andwhen
usedonanypar toftheJSPpageaut omaticall
yappl i
estotheenti
repage
(e.g.importat tr
ibute).
• Followingist hebasi csy ntaxofpagedi r
ectivetag:
<%@ pageat tr
ibute="value"%>
Example:
<%@ page  errorPage="my er r
orpage.j
sp" %>

(
ii
) i
ncl
udedi
rect
ive:
-

• Thei ncludedi recti


vei susedt oi ncludesaf i
ledur i
ngthetr
ansl
ati
on
phase.
• Thisdi r
ect i
vetell
st hecont ainert omer gethecontentofot
herext
ernal
fi
l
eswi t
ht hecur r
entJSPdur i
ngt hetranslat
ionphase.
• Youmaycodei ncludedi rectiv
esany whereinyourJSPpage.
• Thegener alusagefor m ofthisdir ect
iveisasfoll
ows:
<%@ i ncl
udef i
le=“fil
epat h"%>
Exampl e:
<%@ i ncludefil
e=“/Test .
jsp"%>

(
ii
i) t
agl
i
bdi
rect
ive:
-

• TheJav aServerPagesAPIal l
owsy out odef inecustom JSPtagsthat
l
ookl i
keHTMLorXMLt agsandat agl ibrar
yi sasetofuser-
def
inedt
ags
thati
mpl ementcustom behavior.
• Thet agli
bdirect
ivedeclaresthaty ourJSPpageusesasetofcust om
tags,identi
fi
est helocationoft hel ibrary,andpr ov
idesameansf or
i
dentifyi
ngthecustom tagsiny ourJSPpage.
• Thetaglibdir
ecti
vefoll
owst hef ol
l
owi ngsy ntax:
<%@ tagli
buri="
uri"pr
efix=“
unique_ prefi
x">

wher
etheur
iat
tri
but
eval
uer
esol
vest
oal
ocat
iont
hecont
ainer
under
standsandthepref
ixatt
ri
but
einf
ormsacont
ainerwhatbi
tsof
markuparecust
om act
ions.

Example:
<%@ tagl
i
buri
="ht
tp:
//www.exampl
e.com/
cust
li
b"pr
efi
x="
myt
ag"%>
<html>
<body><myt
ag:
hell
o/></body>
</ht
ml>

Q.
4 Expl
ainJSPObj
ectscope:
(i)Page(
ii
)Request(
ii
i)Sessi
on(
iv)Appl
i
cat
ionwi
th
example.

Ans (
i) JSPPageI
mpl
i
citObj
ect
 

InJSP, pagei sani mpl icitobjectoft


ypeObjectclass.
Thisobjecti
s
assignedt other ef
erenceofaut ogenerat
edservletcl
ass.Iti
swr i
tt
enas:
Objectpage=t hi s;
Forusingt hisobj ectitmustbecastt oServl
ettype.Forexample:
<%( HttpServlet)page.log( "message"
);%>
Since,i
ti soft ypeObj ecti tisl
essusedbecausey oucanuset hisobj
ect
directl
yi njsp.Forexampl e:
<%t hi
s.log("message" );%>

Exampl
e:-

<%@ pagel anguage="j


av a"contentTy
pe="text/
html;
char set=I
SO-8859-1"
pageEncoding="I
SO-8859- 1"%>
<!DOCTYPE ht ml PUBLI C " -/
/W3C//DTD HTML 4. 01 Tr ansi
tional
//EN"
"htt
p://www.w3.org/TR/html4/loose.
dtd"
>
<html>
<head>
<met ahttp-
equi
v="Content -
Type"content
="text/
html;charset=I
SO-8859-1">
<ti
tl
e>PageI mpli
citObject</ t
it
le>
</head>
<body >
<%St ri
ngpageName=page. toStri
ng(
);
out.pr
intl
n("
PageNamei s"+pageName) ;
%>
</body>
</html>
(
ii
) JSPRequestI
mpl
i
citObj
ect

• Therequestobjecti
saninstanceoft
hecl
assnamed
j
avax.ser
vlet
.ht
tp.Ht
tpSer
vlet
Request
.

• Thisobjectpr
ovidesmethodsthatl
etyouaccessalltheinf
ormati
on
that’
savail
ableaboutthecur
rentr
equest
,suchasr equestpar
ameter
s,
attr
ibut
es,headers,
andcookies.
• Itcanalsobeusedt oset,
getandremoveattr
ibut
esf rom t
hejspr
equest
scope.

Exampl
e:-

index.html
<form acti
on="wel
come.jsp"> 
 
<input t
ype="
text

name=" uname">  
<input t
ype="
submit"
 val
ue="go"><br
/>  
</form>
  
welcome. j
sp
<%   
Stri
ng name=request.
getParameter(
"uname"
);
  
out.pr
int("
wel
come "+name) ;
  
%> 

(
ii
i) JSPSessi
onI
mpl
i
citObj
ect

• Sessi
onr epresentsanobj ectofjavax.
ser
vlet
.http.
HttpSessi
oni
nterf
ace.
• Thesessionobj ectworkswi thHTTPpr ot
ocol only
.
• Fewmet hodsofHt t
pSessi onwhichsessi
onJSPobj ectcanuseare:
1.getAt
tri
bute(Stri
ngname)
2.setAt
tri
bute(Str
ingname, Obj ectval
ue)
3.getI
d()
4.i
sNew( )
5.setMaxInacti
veInter
val(
intt i
me)
6.i
nvali
date()
7.removeAttri
bute(Str
ingname)

Exampl
e:-

i
ndex.html
<html>
<head>
<ti
tl
e>Wel comePage: Entery ourname</ti
tl
e>
</head>
<body>
<form acti
on="session.j
sp">
<i
nputt ype="
text"name=" inputname">
<i
nputt ype="
submi t
"v al
ue=" cli
ckhere!
!"
><br/>
</
for
m>
</
body>
</
html>

session.j
sp
<html>
<head>
<ti
tl
e>Passi ngthei nputval
uet oasessionvari
able</
ti
tl
e>
</head>
<body >
<%St r
inguname=r equest.
getParameter(
"i
nputname");
out.
print("
Wel come" +uname) ;
session.setAttr
ibute("
sessname" ,
uname);%>
<ahr ef="output.j
sp">CheckOut putPageHere</a>
</body>
</html> 

output.
jsp
<html>
<head>
<ti
tl
e>Outputpage:Fetchingtheval
uefrom sessi
on</ti
tl
e>
</head>
<body>
<%St ri
ngname=( St
ring)sessi
on.
getAtt
ribut
e("
sessname");
out.pri
nt(
"Hel
loUser:Youhav eenter
edt hename:"+name);%>
</body>
</html> 

(
iv) JSPAppl
i
cat
ionI
mpl
i
citObj
ect

• JSPappl icat
ionimpl
icitobj
ecti
sinstanceofjavax .
serv
let.
Servl
etContext
i
mpl ement at
ion and it’
s used t
o gett he contexti nf
ormation and
attr
ibutesinJSP.
• Wecanusei tt
ogett heRequest
DispatcherobjectinJSPt oforwardt he
requestt oanotherresourceortoi ncl
udet her esponsef r
om another
resourceintheJSP.

count er.
jsp
<%@ pagei mport
="java.io.
*,
java.
util
.
*"%>
<html >
<head>
<tit
le>Appl i
cat
ionImpl i
citObjectExampl e</
ti
tl
e>
</head>
<body >
<%I ntegercounter=(Integer)appl
icati
on.get
Attr
ibute(
"number
OfVi
sit
s")
;
if(count er==nul
l||count er==0)
{count er=1; }
else
{count er=counter+1; }
application.
set
Attribute("
number OfVisi
ts"
,counter)
;%>
<h3>Tot
alnumberofhi
tst
othi
sPagei
s:<%=count
er%></
h3>
</
body>
</
html> 

Q.
5 DiscusstheExpr
essionLanguagefeatureofJSPandment
ionv
ari
ousi
mpl
i
cit
objectsavai
l
abl
eintheELwithdescri
ption.

Ans Expr
essi
onLanguagei
nJSP:
-

• JSP Expressi
on Language (
EL)makes itpossibl
eto easi
l
y access
appl
i
cationdatast
oredinJavaBeanscomponent
s.

• JSP ELal
l
owsy
out
ocr
eat
eexpr
essi
onsbot
h(a)ar
it
hmet
icand(
b)
l
ogi
cal.

• Wit
hinaJSPELexpr
essi
on,youcanuseint
egers,
float
ingpoi
ntnumbers,
st
ri
ngs,t
hebui
l
t-i
nconst
ant
st r
ueandfal
seforbooleanval
ues,andnul
l
.

Sy
ntaxofEL:

${
expr
essi
on}

what
everpr
esenti
nsi
debr
acesget
sev
aluat
edatr
unt
imeandbei
ngsent
t
othe

out
putst
ream.

Exampl
e1:
Expr
essi
onl
anguageev
aluat
est
heexpr
essi
ons

exampl
e1.
j
sp

<ht
ml>

<head>

<t
it
le>Expr
essi
onl
anguageexampl
e1</
ti
tl
e>

</
head>

<body
>${
1<2}${
1+2+3}</
body
>

</
html
>

Out
put
:
Tr
ue6

JSPELI
mpl
i
citObj
ect
s

JSPExpressi
onLanguagepr ov
idesmanyimpl
ici
tobjectst
hatwecanusetoget
att
ri
butesf r
om diff
erentscopesandparamet
erval
ues.Fol
l
owi ngarevar
iousJSPEL
Impli
citObject
s:

I
mpl
i
citobj
ect Descr
ipt
ion

pageScope Amapt
hatcont
ainsat
tr
ibut
esetwi
thpagescope.

r
equest
Scope Usedt
ogett
heat
tr
ibut
eval
uewi
thr
equestscope.

sessi
onScope Usedt
ogett
heat
tr
ibut
eval
uewi
thsessi
onscope.

appl
i
cat
ionScope Usedt
ogett
heat
tr
ibut
esv
aluef
rom appl
i
cat
ionscope.

Usedtogett
her
equestpar
amet
erv
alue,
ret
urnsasi
ngl
e
par
am
val
ue.
Usedt
ogettherequestparam v
aluesi
nanar r
ay,usef
ul
par
amVal
ues
whenr
equestparametercont
ainmulti
plev
alues.

header Usedt
ogetr
equestheaderi
nfor
mat
ion.

header
Val
ues HTTPr
equestheader
sascol
l
ect
ionsofst
ri
ngs.

Usedtogett hecont
exti
nitpar
ams,
wecan’
tusei
tfor
i
nit
Par
am
ser
vleti
nitparams.

cooki
e Usedt
ogetCooki
eval
ue.

SameasJSPi mpli
citpageContextobj
ect,usedt
ogett
he
pageCont
ext request,
sessi
onr
eferencesetc.exampleusagei
sgett
ing
requestHTTPMethodname.

Anda mungkin juga menyukai