Java Po Gram
Java Po Gram
ogr
am No.-1
Ai
m:Gi
veabr
iefdescr
ipt
ionofJDBC.
JDBC
JDBCst andsforJav aDatabaseConnect ivi
ty.JDBCi saJav aAPItoconnectand
executethequer ywit
ht hedat abase.Itisapar tofJav aSE( JavaStandar
dEdi t
ion)
.
JDBCAPIusesJDBCdr iver
st oconnectwi ththedat abase.WecanuseJDBCAPIt o
accesstabulardatastoredinanyr el
ationaldatabase.Byt hehelpofJDBCAPI ,wecan
save,update,delete and fetch dataf r
om t he database.Itisl i
ke Open Database
Connecti
vit
y( ODBC)prov i
dedbyMi crosoft.
Whyshoul
dweuseJDBC
Befor
eJDBC,ODBCAPIwast hedat abaseAPIt oconnectandexecut
ethequer y
wit
ht hedatabase.But,ODBCAPIusesODBCdr iverwhichi
swrit
teni
nCl anguage(i
.e.
pl
atform dependentandunsecur
ed).ThatiswhyJav ahasdefi
nedit
sownAPI( JDBC
API)thatusesJDBCdr iv
ers(
wri
tt
eninJaval anguage).
JDBCDr
iver
JDBCDr i
veri
sasof
twarecomponentt
hatenabl
esj
avaappl
i
cat
iont
oint
eractwi
th
t
hedatabase.Ther
ear
e4typesofJDBCdriv
ers:
1.JDBC-
ODBCbr
idgedr
iver
2.Nat
ive-
APIdr
iver(
par
ti
all
yjav
adr
iver
)
3.Net
wor
kPr
otocol
dri
ver(
ful
l
yjav
adr
iver
)
4.Thi
ndr
iver(
ful
l
yjav
adr
iver
)
1JDBC-
ODBCbr
idgedr
iver
TheJDBC-ODBCbridgedriverusesODBCdr ivert
oconnectt
othedatabase.The
JDBC-
ODBCbridgedr
iverconvert
sJDBCmet hodcallsi
ntot
heODBCfunct
ioncall
s.Iti
s
easyt
ouse.I
tcanbeeasilyconnectedtoanydatabase.
2Nat
ati
ve_
APIdr
iver
TheNati
veAPIdri
verusestheclient-si
deli
brar
iesofthedatabase.Thedr
iver
convert
sJDBC methodcal
lsint
onat i
vecal l
softhedatabaseAPI.Itisnotwri
tt
en
enti
rel
yinj
ava.I
t’
sper
for
manceupgradedt hanJDBC-ODBCbridgedr
iver
.
3Net
wor
kPr
otocol
dri
ver
1|
Page
TheNet wor kProt
ocoldri
verusesmi ddleware(appl
icati
onserver
)thatconver
ts
JDBCcallsdirectl
yorindir
ectl
yintot hevendor -
specif
icdatabaseprot
ocol.I
tisfull
y
wri
tt
eninjava.Nocl i
entsideli
braryisrequir
edbecauseofappl i
cat
ionserv
erthatcan
per
form manyt asksl
i
keauditi
ng,loadbalancing,l
ogginget
c.
4Thi
ndr
iver
Thethindri
verconver
tsJDBC cal lsdi
rect
lyint
ot hev endor-
speci
fi
cdat abase
prot
ocol.Thati
swhyitisknownast hindri
ver
.Iti
sfull
ywrit
teninJav al
anguage.Ithas
bett
erperfor
mancethanallotherdr
ivers.I
thasnosof t
warerequirementatcli
entside
orserversi
de.
Jav
aDat
abaseConnect
ivi
ty
Ther
ear
e7st
epst
oconnectanyj
avaappl
i
cat
ionwi
tht
hedat
abaseusi
ngJDBC.Thesest
epsar
eas
f
oll
ows:
o I
mpor
tPackage
o Regi
stert
heDr
ivercl
ass
o Cr
eat
econnect
ion
o Cr
eat
est
atement
o Execut
equer
ies
o Pr
ocesst
heResul
t
o Cl
oseconnect
ion
1I
mpor
tPackage
Thi
sisf
ormaki
ngt
heJDBCAPIcl
assesi
mmedi
atel
yav
ail
abl
etot
heappl
i
cat
ion
pr
ogr
am.
I
mpor
tjav
a.sql
.*
;
2Regi
stert
heDr
ivercl
ass
The
forName(
)
met
hodofClasscl
assi susedt
oregi
stert
hedr
ivercl
ass.Thi
s
met
hodisusedtody
nami
cal
l
yloadthedr
ivercl
ass.
Cl
ass.
for
Name(
“com.
mysql
,
jdbc.
Dri
ver
”);
3Cr
eat
eConnect
ion
2|
Page
The
getConnecti
on( )
method of Dr i
verManager cl
ass is used to establ
i
sh
connecti
onwi ththedat abase.Thisget Connecti
onmethodhast hr
eeparametersi.
e.
fi
rstisastri
ngthatgivesur lofthedatabase,secondi
sast r
ingthatgiv
esusernameof
databaseandthir
disal soast r
ingthatgivespasswordofdatabase.I
tfi
rstpar
ameteris
necessaryandothertwoar eoptional
.
Connect
ioncon=Dr
iver
Manager
.get
Connect
ion(
“ur
l”
,”
user
name”
,”
passwor
d”)
;
4Cr
eat
eSt
atement
The cr
eat
eStatement(
)met hod ofConnecti
on int
erf
ace is used to cr
eate
st
atement
.Theobjectofst
atementi
sresponsi
blet
oexecutequer
ieswiththedat
abase.
St
atementst=con.
creat
eSt
atement
();
5Execut
etheQuer
y
TheexecuteQuery(
)methodofSt
atementinter
facei
susedtoexecut
equer
iesto
t
hedatabase.Thismethodret
urnst
heobjectofResul
tSett
hatcanbeusedt
ogetall
the
r
ecor
dsofat able.
Resul
tSetr
s=st
.execut
eQuer
y(“
quer
y”)
;
6Pr
ocesst
heResul
t
Oncet
hequer
yhasbeenexecut
ed,
ther
ear
etwost
epst
obecar
ri
edout
:
1.Pr
ocesst
heout
putr
esul
tsett
ofet
cht
her
ows.
2.Ret
ri
evi
ngt
hecol
umnv
aluesoft
hecur
rentr
ow.
Thef ir
ststepisdonebyusingthenext()methodofResultSetobject
.Itcanexecuteonly
oner owatat ime.Forexecuti
ngal
lrowswecanusel oopandcondi ti
onstatementis
rs.next(
)thatretur
ntrueifallr
owisnotexecut edot
her wiseretur
nsf al
se.Thesecond
stepi scarr
iedoutbyusinggetXXX()met hodofrsobject.Ithasonepar ameterforany
columni nar ow.Parametercanbei ntegerthatgi
vescol umnnumberorst ri
ngthat
givescolumnname.
whi
l
e(r
s.next(
)){
Sy st
em.out.
pri
ntl
n(r
s.get
Int
(1)
+"
"+r
s.get
Str
ing(
2))
;
}
7Cl
osi
ngt
heConnect
ion
3|
Page
By cl
osi
ng connecti
on obj
ect st
atement and Resul
tSet wi
ll be cl
osed
automati
call
y.The cl
ose(
)method ofConnecti
on int
erf
ace i
s used t
o close t
he
connecti
on.
Con.
close(
);
4|
Page
Pr
ogr
am No.-2
Aim:WAPtopri
ntr
esul
tfr
om dat
abaseusi
ngJDBC.
Sour
ceCode:
packagejdbcconnect
ivi
ty;
//i
mpor tj
dbcpackage
i
mpor tj
ava.sql
.*
;
publ
i
ccl
assJDBCConnect
ivi
ty{
publi
cst ati
cv oidmai n( String[]args){
tr
y
{
//loadandr egisterj dbcdr iver
Class.forName( "
com. my sql.cj
.j
dbc.Driver"
);
//creat econnect iont ot hedat abase
Connect ion con =
Dri
verManager .getConnect ion("jdbc:my sql
://
localhost
:3306/demo", "
root
",
"
yadav @123" );
//creat et hest atement
Statementst=con. createSt at
ement ()
;
//execut equer y
Resul t
Setr s=st .execut eQuer y("Sel
ect*f r
om student")
;
//processt her esul t
while(rs.next (
))
{
Sy st
em. out.pri
nt l
n( r
s.get I
nt(1)+""+r s.
getSt
ring(2)+""+rs.
get
Str
ing(
3))
;
}
//closet heconnect i
on
con.close( );
}
catch(Except i
one)
{
System. out .pri
ntln("Except ion: "+e.getMessage( )
);
}
}
}
5|
Page
Out
put
:
6|
Page
Pr
ogr
am No.-3
Ai
m:WAPt
oupdat
equer
yindat
abaseusi
ngJDBC.
Sour
ceCode:
packagej
dbci
nser
t;
/
/impor
tjdbcpackage
i
mpor
tjav
a.sql
.*
;
publ
i
ccl
assJDBCI
nser
t{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
t
ry
{
/
/loadandr
egi
stert
hedr
iver
Cl
ass.
for
Name(
"com.
mysql
.cj
.
jdbc.
Dri
ver
");
/
/cr
eat
etheconnect
iont
odat
abase
Connecti
on con =
Dri
ver
Manager.get
Connect
ion(
"j
dbc:
mysql
:
//l
ocal
host
:3306/
demo"
, "
root
",
"
yadav@123");
/
/cr
eat
ethest
atement
St
atementst=con.
creat
eSt
atement
();
/
/execut
ethest
atement
i
nt i= st
.execut
eUpdat
e("
inser
tint
o st
udent v
alues(
15013017,
'
Atulkumar
j
ha'
,
'CSE'
)"
);
Sy
stem.
out
.pr
int
ln(
"val
uesi
nser
ted"
);
i
ntt=st.execut
eUpdat
e("
updat
est
udentsets_
name='
Atulkumar
'wher
erol
l
_no
=15013017"
);
Sy
stem.
out
.pr
int
ln(
"val
ueupdat
e")
;
/
/cl
oset
heconnect
ion
con.
close(
);
}
7|
Page
cat
ch(
Except
ione)
{
Sy
stem.
out
.pr
int
ln(
"Except
ion:
"+e.
get
Message(
));
}
}
}
Out
put
:
8|
Page
Pr
ogr
am No.-4
Ai
m:Gi
veabr
iefdescr
ipt
ionofSer
vlet
.
Ser
vlet
:
Serv
let
t
echnol ogyisusedt ocreateawebappl i
cati
on( resi
desatserversi
deand
generatesady namicwebpage) .Ser
vlet
technologyisrobustandscal abl
ebecauseof
j
avalanguage.Bef oreSer v
let
,CGI( CommonGat ewayInt
erface)scri
pti
nglanguagewas
common asa ser ver-
side programmi ng language.There ar e manyinter
facesand
cl
assesi ntheSer vl
etAPIsuchasSer vlet,Generi
cServl
et,HttpServ
let
,Servl
etRequest
,
Servl
etResponse,etc.
Adv
ant
ageofSer
vlet
1.Bet
terper
for
mance:
becausei
tcr
eat
esat
hreadf
oreachr
equest
,notpr
ocess.
2.Por
tabi
l
ity
:
becausei
tusesJav
alanguage.
3.Robust:
JVM managesSer v
let
s,sowedon'
tneedt
owor
ryaboutt
hememor
y
l
eak,garbagecol
l
ect
ion,
etc.
4.Secur
e:
becausei
tusesj
aval
anguage.
HTTP
The Hy per
text Transf
er Protocol(HTTP) is appli
cati
on-
levelprot
ocolfor
col
laborati
ve, distr
ibut
ed, hypermedia infor
mat
ion sy stems. It is the data
communi cati
onpr ot
ocolusedtoestabl
ishcommuni
cati
onbet weencli
entandser
ver
.
9|
Page
HTTPRequest
Therequestsentbythecomput
ert
oawebserver
,cont
ainsal
lsor
tsofpot
ent
ial
l
y
i
nter
est
inginf
ormation;
iti
sknownasHTTPr
equest
s.
1. TheRequest
-l
ine
2. TheRequest
edURI(
Uni
for
m Resour
ceI
dent
if
ier
)
3. TheRequestmet
hodandCont
ent
4. TheConnect
ioncont
rol
header
Ser
vletCont
ainer
Itprovi
desther unti
meenv ir
onmentf orJavaEE appl
icat
ions.Thecl i
ent/usercan
requestonl
yast ati
cWebPagesf rom theserv
er.I
ftheuserwantst oreadt heweb
pagesasperinputthent heservl
etcont
aineri
susedi
njava.Theservl
etcontai
neristhe
partofwebserverwhi chcanber uninaseparatepr
ocess.Wecancl assi
fytheservl
et
contai
nerst
atesinthreet y
pes:
1. St
andal
one:
Iti
sty
picalJav
a-basedser
ver
sinwhi
cht
heser
vletcont
ainerandt
he
webser
ver
sarethei
ntegr
alpar
tofasi
ngl
epr
ogr
am.Forexampl
e:-Tomcat
r
unni
ngbyi
tsel
f
2. I
n-pr
ocess:
Iti
ssepar
atedf
rom t
hewebser
ver
,becauseadi
ff
erentpr
ogr
am r
uns
wi
thi
ntheaddr
essspaceoft
hemai
nser
verasapl
ug-
in.Forexampl
e:-Tomcat
r
unni
nginsi
det
heJBoss.
3. Out
-of
-pr
ocess:
Theweb ser
verand ser
vletcont
ainerar
edi
ff
erentpr
ogr
ams
whi
ch ar
er un i
n a di
ff
erentprocess.Forperf
ormi
ng t
he communi cati
ons
bet
weenthem,webser
verusest
hepl ug-
inpr
ovi
dedbyt
heserv
letcont
ainer.
Li
feCy
cleofaCont
ainer
1Servletclassi
sloaded-Thecl
assl
oaderisresponsi
blet
oloadtheservl
etcl
ass.The
ser
vletclassisloadedwhenthefir
strequestfortheservl
eti
sr ecei
vedbytheweb
10|
Page
cont
ainer
.
2Servl
etinst
anceiscreated-Thewebcontai
nercr
eatesthei
nst
anceofaser
vletaf
ter
l
oadingtheservl
etclass.Theserv
leti
nst
anceiscreatedonl
yonceintheser
v l
etl
ife
cy
cle.
3ini
tmethodi
si nv
oked-Thewebcont ai
nercall
st heini
tmethodonlyonceaft
er
cr
eati
ngt
heser
vleti
nst
ance.Theini
tmethodisusedt oini
ti
ali
zet
heser
vlet
.Iti
sthel
i
fe
cy
clemet
hodofthej
avax.
servl
et.
Ser
vleti
nter
face.
publ
i
c v
oid
i
nit
(Ser
vlet
Conf
ig
conf
ig)
t
hrows
Ser
vlet
Except
ion
4servi
cemet hodi
sinvoked-Thewebcont ai
nercal l
stheserv i
cemet hodeacht i
me
whenrequestfort
heservl
etisr
ecei
ved.Ifserv
letisnotinit
ial
ized,i
tfoll
owst hefir
st
thr
eestepsasdescri
bedabovet
hencall
st heservi
cemet hod.Ifserv
letisi
nit
iali
zed,i
t
cal
lst
heservi
cemethod.Noti
cet
hatser
vletisi
nit
ial
izedonlyonce.
publi
c v
oid ser
vice(
Ser
vlet
Request
r
equest
,
Ser
vlet
Response
response)
t
hrows
S
er
vlet
Excepti
on,
IOExcepti
on
5dest r
oyedmet hodisinvoked-Thewebcont ai
nercal
l
sthedestroymethodbef
ore
removingtheservleti
nstancefr
om t
heservi
ce.Itgiv
estheser
vletanopport
uni
tyto
cleanupanyresourceforexampl
ememory,
threadetc.
publ
i
c v
oid
dest
roy
()
11|
Page
Pr
ogr
am No.-5
Ai
m:WAPt
ofi
ndgr
eat
ernumberusi
ngSer
vlet
.
Sour
ceCode:
-
--
--
--
--
Index.
html
--
--
--
--
--
--
-
<ht
ml>
<head>
<t
it
le>TODOsuppl
yat
it
le</
ti
tl
e>
<met
achar
set
="UTF-
8">
<met
aname="
viewpor
t"cont
ent
="wi
dth=dev
ice-
widt
h,i
nit
ial
-scal
e=1.
0">
<l
i
nkhr
ef="
boot
str
ap.
min.
css"r
el="
sty
lesheet
"ty
pe="
text
/css"
/>
</
head>
<body
>
<f
orm met
hod="
Get
"act
ion="
Great
er"
>
12|
Page
<di
v class="
cont
ainer bor
der
" st
yle=" mar
gin-
top:
50px; paddi
ng-
top:
15px;
wi
dth:
600px"
>
<di
vcl
ass="r
ow"
>
<l
abel
class="
col
-sm-
5">Ent
erf
ir
stnumber
</l
abel
>
<i
nputt
ype="
text
"cl
ass="
col
-sm-
6"name="
fi
rst
"pl
acehol
der
="f
ir
stnumber
">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="r
ow"
>
<l
abel
class="
col
-sm-
5">Ent
ersecondnumber
</l
abel
>
<i
nputt
ype="
text
"cl
ass="
col
-sm-
6 "name="
second"pl
acehol
der
="second
number
">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="r
ow"
>
<l
abel
class="
col
-sm-
5">Ent
ert
hir
dnumber
</l
abel
>
<i
nput t
ype="
text
" cl
ass="
col
-sm-
6 " name="
thi
rd" pl
acehol
der
="t
hir
d
number
">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<di
vcl
ass="
col
-sm-
5"></
div
>
<i
nputt
ype="
submi
t"cl
ass="
btnbt
n-pr
imar
y"name="
submi
t"st
yle="
mar
gin-
bot
tom:
10px;
">
</
div
>
</
div
>
</
for
m>
</
body
>
</
html
>
13|
Page
-
--
--
--
--
-Gr
eat
er.
jav
a--
--
--
--
--
-
i
mpor
tjav
a.i
o.I
OExcept
ion;
i
mpor
tjav
a.i
o.Pr
int
Wri
ter
;
i
mpor
tjav
ax.
ser
vlet
.Ser
vlet
Except
ion;
i
mpor
tjav
ax.
ser
vlet
.ht
tp.
Htt
pSer
vlet
;
i
mpor
tjav
ax.
ser
vlet
.ht
tp.
Htt
pSer
vlet
Request
;
i
mpor
tjav
ax.
ser
vlet
.ht
tp.
Htt
pSer
vlet
Response;
i
mpor
tjav
ax.
ser
vlet
.ht
tp.
Cooki
e;
publ
i
ccl
assGr
eat
erext
endsHt
tpSer
vlet{
pr
otect
ed v
oid pr
ocessRequest
(Ht
tpSer
vlet
Requestr
equest
,Ht
tpSer
vlet
Response
r
esponse)
t
hrowsSer
vlet
Except
ion,
IOExcept
ion{
r
esponse.
set
Cont
ent
Type(
"t
ext
/ht
ml;
char
set
=UTF-
8")
;
t
ry(
Pri
ntWr
it
erout=r
esponse.
get
Wri
ter
()){
i
ntnum1=I
nteger
.par
seI
nt(
request
.get
Par
amet
er(
"fi
rst
"))
;
i
ntnum2=I
nteger
.par
seI
nt(
request
.get
Par
amet
er(
"second"
));
i
ntnum3=I
nteger
.par
seI
nt(
request
.get
Par
amet
er(
"t
hir
d")
);
i
f(
num1>num2&&num1>num3)
out
.pr
int
ln(
num1+"i
sgr
eat
er"
);
el
sei
f(
num2>num1&&num2>num3)
out
.pr
int
ln(
num2+"
isgr
eat
er"
);
el
se
out
.pr
int
ln(
num3+"
isgr
eat
er"
);
}
}
14|
Page
Out
put
15|
Page
Pr
ogr
am No.-6
Ai
m:WAPt
oRegi
sterandLogi
npageusi
ngSer
vlet
.
Sour
ceCode:
-
--
--
--
--
--
--
Index.
html
--
--
--
--
--
--
--
--
-
<ht
ml>
<head>
<t
it
le>Si
gnUppage</
ti
tl
e>
<met
achar
set
="UTF-
8">
<met
aname="
viewpor
t"cont
ent
="wi
dth=dev
ice-
widt
h,i
nit
ial
-scal
e=1.
0">
<l
i
nkhr
ef="
css/
boot
str
ap.
min.
css"r
el="
sty
lesheet
"ty
pe="
text
/css"
/>
</
head>
<body
>
<f
orm met
hod="
Post
"act
ion="
SignUp"
>
<di
vcl
ass="
cont
ainerbor
der
"st
yle="
widt
h:500px;
mar
gin-
top:10px;
paddi
ng-
top:
10px"
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Name:
</l
abel
>
<i
nputt
ype="
text
"cl
ass="
col
-sm-
7"name="
name"pl
acehol
der
="Ful
lname"
>
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Emai
li
d:</
label
>
<i
nputt
ype="
text
"cl
ass="
col
-sm-
7"name="
emai
l
"pl
acehol
der
="Emai
li
d">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Phoneno.:
</l
abel
>
16|
Page
<i
nputt
ype="
text
"cl
ass="
col
-sm-
7"name="
phone"pl
acehol
der
="Phoneno.
">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Passwor
d:</
label
>
<i
nput t
ype="
text
" cl
ass="
col
-sm-
7" name="
passwor
d"
pl
acehol
der
="Passwor
d">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
8">I
fyoual
readyhav
eanaccountt
henl
ogi
n</
label
>
<ahr
ef="
Logi
n.ht
ml"cl
ass="
col
-sm-
2">Logi
n</
a>
</
div
>
<di
vcl
ass="
row"
>
<di
vcl
ass="
col
-sm-
3"></
div
>
<but
tont
ype="
submi
t"cl
ass="
btnbt
n-pr
imar
y">Si
gnup</
but
ton>
</
div
>
</
div
>
</
for
m>
</
body
>
</
html
>
-
--
--
--
--
--
--
-Logi
n.ht
ml-
--
--
--
--
--
--
--
--
-
<ht
ml>
<head>
<t
it
le>Logi
nPage</
ti
tl
e>
<met
achar
set
="UTF-
8">
<met
aname="
viewpor
t"cont
ent
="wi
dth=dev
ice-
widt
h,i
nit
ial
-scal
e=1.
0">
<l
i
nkhr
ef="
css/
boot
str
ap.
min.
css"r
el="
sty
lesheet
"ty
pe="
text
/css"
/>
17|
Page
</
head>
<body
>
<f
orm met
hod="
Post
"act
ion="
Logi
n">
<di
vcl
ass="
cont
ainerbor
der
"st
yle="
widt
h:500px;
mar
gin-
top:10px;
paddi
ng-
top:
10px"
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Name:
</l
abel
>
<i
nputcl
ass="
col
-sm-
7"t
ype="
text
"name="
name"pl
acehol
der
="Ful
lName"
>
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Passwor
d:</
label
>
<i
nput t
ype="
text
" cl
ass="
col
-sm-
7" name="
passwor
d"
pl
acehol
der
="Passwor
d">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<di
vcl
ass="
col
-sm-
3"></
div
>
<but
tont
ype="
submi
t"cl
ass="
btnbt
n-pr
imar
y">Logi
n</
but
ton>
</
div
>
</
div
>
</
for
m>
</
body
>
</
html
>
-
--
--
--
--
--
--
-Si
gnUp.
jav
a--
--
--
--
--
--
-
i
mpor
tjav
a.i
o.I
OExcept
ion;
i
mpor
tjav
a.i
o.Pr
int
Wri
ter
;
i
mpor
tjav
ax.
ser
vlet
.Ser
vlet
Except
ion;
i
mpor
tjav
ax.
ser
vlet
.ht
tp.
Htt
pSer
vlet
;
18|
Page
i
mpor
tjav
ax.
ser
vlet
.ht
tp.
Htt
pSer
vlet
Request
;
i
mpor
tjav
ax.
ser
vlet
.ht
tp.
Htt
pSer
vlet
Response;
i
mpor
tjav
ax.
ser
vlet
.ht
tp.
Cooki
e;
publ
i
ccl
assSi
gnUpext
endsHt
tpSer
vlet{
pr
otect
ed v
oid pr
ocessRequest
(Ht
tpSer
vlet
Requestr
equest
,Ht
tpSer
vlet
Response
r
esponse)
t
hrowsSer
vlet
Except
ion,
IOExcept
ion{
r
esponse.
set
Cont
ent
Type(
"t
ext
/ht
ml;
char
set
=UTF-
8")
;
t
ry(
Pri
ntWr
it
erout=r
esponse.
get
Wri
ter
()){
St
ri
ngname=r
equest
.get
Par
amet
er(
"name"
);
St
ri
ngemai
l=r
equest
.get
Par
amet
er(
"emai
l
");
St
ri
ngphone=r
equest
.get
Par
amet
er(
"phone"
);
St
ri
ngpasswor
d=r
equest
.get
Par
amet
er(
"passwor
d")
;
if
(name.l
ength(
)!
=0 && emai
l
.l
engt
h()
!=0 && phone.
lengt
h()
!=0 &&
password.
lengt
h()
!=0)
{
Cooki
ecooki
e=newCooki
e("
Name"
,name)
;
Cooki
ecooki
e1=newCooki
e("
Emai
l
",emai
l
);
Cooki
ecooki
e2=newCooki
e("
Phone"
,phone)
;
Cooki
ecooki
e3=newCooki
e("
Passwor
d",
passwor
d);
cooki
e.set
MaxAge(
60*
60*
24)
;
cooki
e1.
set
MaxAge(
60*
60*
24)
;
cooki
e2.
set
MaxAge(
60*
60*
24)
;
cooki
e3.
set
MaxAge(
60*
60*
24)
;
r
esponse.
addCooki
e(cooki
e);
r
esponse.
addCooki
e(cooki
e1)
;
r
esponse.
addCooki
e(cooki
e2)
;
r
esponse.
addCooki
e(cooki
e3)
;
19|
Page
out
.pr
int
ln(
"<b>Wel
com "+name+"
!</
b>"
);
out
.pr
int
ln(
"<br
>Youar
esuccessf
ull
ySi
gnUp"
);
}
el
se{
out
.pr
int
ln(
"<b>Somef
iel
dar
eempt
y!</
b>"
);
out
.pr
int
ln(
"<br
>Pl
easef
il
lal
lfi
eld.
")
;
}
}
}
}
-
--
--
--
--
--
--
-Logi
n.j
ava-
--
--
--
--
--
--
-
i
mpor
tjav
a.i
o.I
OExcept
ion;
i
mpor
tjav
a.i
o.Pr
int
Wri
ter
;
i
mpor
tjav
ax.
ser
vlet
.Ser
vlet
Except
ion;
i
mpor
tjav
ax.
ser
vlet
.annot
ati
on.
WebSer
vlet
;
i
mpor
tjav
ax.
ser
vlet
.ht
tp.
Htt
pSer
vlet
;
i
mpor
tjav
ax.
ser
vlet
.ht
tp.
Htt
pSer
vlet
Request
;
i
mpor
tjav
ax.
ser
vlet
.ht
tp.
Htt
pSer
vlet
Response;
i
mpor
tjav
ax.
ser
vlet
.ht
tp.
Cooki
e;
@WebSer
vlet
(ur
lPat
ter
ns={
"/Logi
n"}
)
publ
i
ccl
assLogi
next
endsHt
tpSer
vlet{
pr
otect
ed v
oid pr
ocessRequest
(Ht
tpSer
vlet
Requestr
equest
,Ht
tpSer
vlet
Response
r
esponse)
t
hrowsSer
vlet
Except
ion,
IOExcept
ion{
r
esponse.
set
Cont
ent
Type(
"t
ext
/ht
ml;
char
set
=UTF-
8")
;
t
ry(
Pri
ntWr
it
erout=r
esponse.
get
Wri
ter
()){
20|
Page
St
ri
ngName=r
equest
.get
Par
amet
er(
"name"
);
St
ri
ngPasswor
d=r
equest
.get
Par
amet
er(
"passwor
d")
;
Cooki
eck[
]=r
equest
.get
Cooki
es(
);
i
f(
Name.
lengt
h()
!=0&&Passwor
d.l
engt
h()
!=0)
{
i
f(
Name.
equal
s(ck[
0].
get
Val
ue(
))&&Passwor
d.equal
s(ck[
3].
get
Val
ue(
)))
{
out
.pr
int
("
<b>Wel
come!Youar
elogi
nin.
</b>"
);
out
.pr
int
("
<br
>"+ck[
0].
get
Val
ue(
));
out
.pr
int
("
<br
>"+ck[
1].
get
Val
ue(
));
out
.pr
int
("
<br
>"+ck[
2].
get
Val
ue(
));
}
el
se{
out
.pr
int
ln(
"<b>User
nameorPasswor
diswr
ong</
b>"
);
out
.pr
int
ln(
"<br
>Pl
easecheckagai
n."
);
}
}
el
se{
out
.pr
int
ln(
"<b>Somef
iel
dar
eempt
y!</
b>"
);
out
.pr
int
ln(
"<br
>Pl
easef
il
lal
lfi
elds.
")
;
}
}
}
}
21|
Page
Out
put-
22|
Page
Pr
ogr
am No.-7
Ai
m:Gi
veabr
iefdescr
ipt
ionofJSP.
JSP:
JSP t
echnologyisusedtocreatewebappl icat
ionjustli
keSer
vlettechnol
ogy.I
t
canbet houghtofasanextensi
ontoServletbecauseitpr
ov i
desmorefuncti
onali
tyt
han
ser
vletsuchasexpr essi
onl
anguage,JSTL,etc.
Adv
ant
ageofJSP:
1)Wecanuseal
lthef
eat
uresoft
heSer
vleti
nJSP.wecanal
sousei
mpl
i
citobj
ect
s,
predefi
nedtags,expr
essi
onl
anguageandCust
om t
agsi
nJSP,t
hatmakesJSP
developmenteasy.
2)JSPcanbeeasi
l
ymanagedbecausewecaneasi
l
ysepar
ateourbusi
nessl
ogi
c
wi
thpr
esent
ati
onl
ogi
c.
3)I
fJSPpagei
smodi
fi
ed,
wedon'
tneedt
orecompi
l
eandr
edepl
oyt
hepr
oject
.
4)I
nJSP,wecanusemanyt
agssuchasact
iont
ags,JSTL,cust
om t
ags,et
c.t
hat
r
educest
hecode.Mor
eov
er,
wecanuseEL,
impl
i
citobj
ect
s,et
c.
Li
feCy
cleofJSP:
23|
Page
Thesear
ethest
epsofJSPl
i
fecy
cle-
1) Tr
ansl
ati
onofJSPPage
2) Compi
l
ati
onofJSPPage
3) Cl
assl
oadi
ng(
thecl
assl
oaderl
oadscl
assf
il
e)
4) I
nst
ant
iat
ion(
Obj
ectoft
heGener
atedSer
vleti
scr
eat
ed)
.
5) I
nit
ial
i
zat
ion(t
hecont
aineri
nvokesj
spI
nit
()met
hod)
.
6) Requestpr
ocessi
ng(t
hecont
aineri
nvokes_
jspSer
vice(
)met
hod)
.
7) Dest
roy(t
hecont
aineri
nvokesj
spDest
roy
()met
hod)
.
JSPScr
ipt
ingEl
ement
s:
1)Scr
ipl
etTag:
Ascr
ipt
lett
agi
susedt
oexecut
ejav
asour
cecodei
nJSP.
<%
j
ava
sour
ce
code
%>
24|
Page
2)Expressi
onTag:Thecodepl acedwit
hin
JSP expressi
ont ag
i wr
s i
tt
entothe
outputstr
eam oft
heresponse.Soyouneednotwr i
teout.pr
int(
)towrit
edat
a.I
t
i
smai nlyusedt
opri
nttheval
uesofvari
ableormethod.
<%=
st
atement
%>
3)Decl
arat
ionTag:
The
JSPdecl
arat
iont
ag
i t
sused odecl
aref
iel
dsandmet
hods.
<%!
fi
eld
or
met
hod
decl
arat
ion
%>
JSPI
mpl
i
citObj
ect
s:
1)JSPRequest :The JSPrequest
isanimpl i
citobj
ectofty
peHtt
pServ
letRequesti.
e.
createdforeachjsprequestbyt hewebcont ai
ner.I
tcanbeusedtogetr equest
i
nf or
mationsuchaspar ameter,headerinformati
on,r
emoteaddr
ess,servername,
serverport
,contentty
pe,characterencodingetc.
4)JSPApplicat
ion:I
nJSP,applicat
ionisanimplici
tobj
ectofty Ser
pe v
letCont
ext
.
Theinst
anceofSer vl
etCont
extiscreatedonl
yoncebyt hewebcont
ainerwhen
appl
i
cati
onorpr oj
ectisdepl
oyedont heser
ver
.
5)JSPSession:I
nJSP,sessi
onisanimpli
citobjectoft
ypeHttpSessi
on.
TheJava
devel
opercanusethi
sobjecttoset,
getorr emoveattr
ibut
eort ogetsessi
on
i
nformati
on.
25|
Page
6)JSPExcepti
on:I
nJSP,except
ionisanimpli
citobj
ectoft
ypejav
a.l
ang.
Thr
owabl
e
cl
ass.Thisobj
ectcanbeusedt opri
nttheexcepti
on.Buti
tcanonlybeusedi
n
err
orpages.
7)JSPOut
:InJSP,
outi
sani
mpl
i
citobectoft
ypeJspWr
it
er.
26|
Page
Pr
ogr
am No.-8
Ai
m:WAPt
oaddt
wonumberusi
ngJSP.
Code:
-
--
--
--
--
index.
html
--
--
--
--
--
--
-
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>AddNumber
</t
it
le>
<met
achar
set
="UTF-
8">
<met
aname="
viewpor
t"cont
ent
="wi
dth=dev
ice-
widt
h,i
nit
ial
-scal
e=1.
0">
<l
i
nkhr
ef="
css/
boot
str
ap.
min.
css"r
el="
sty
lesheet
"ty
pe="
text
/css"
/>
</
head>
<body
>
<f
orm met
hod="
get
"act
ion="
Add.
jsp"
>
<di
vcl
ass="
cont
ainerbor
der
"st
yle="
mar
gin-
top:50px;
paddi
ng-
top:15px;wi
dth:
600px
">
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
5">Ent
erf
ir
stNumber:
</l
abel
>
<i
nputt
ype="
text
"cl
ass="
col
-sm-
6"name="
fi
rst
"pl
acehol
der
="Fi
rstnumber
">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
5">Ent
ersecondNumber:
</l
abel
>
<i
nputt
ype="
text
"cl
ass="
col
-sm-
6"name="
second"pl
acehol
der
="Second
number
">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
27|
Page
<di
vcl
ass="
col
-sm-
5"></
div
>
<inputt
ype="
submi
t"cl
ass="
btn bt
n-pr
imar
y"name="
Add"st
yle="
mar
gin-
bot
tom:
10px">
</
div
>
</
div
>
</
for
m>
</
body
>
</
html
>
-
--
--
--
--
--
-Add.
jsp-
--
--
--
--
--
--
<%@pagecont
ent
Type="
text
/ht
ml"pageEncodi
ng="
UTF-
8"%>
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<met
aht
tp-
equi
v="
Cont
ent
-Ty
pe"cont
ent
="t
ext
/ht
ml;
char
set
=UTF-
8">
<t
it
le>Sum</
ti
tl
e>
</
head>
<body
>
<%
i
nti
=Int
eger
.par
seI
nt(
request
.get
Par
amet
er(
"fi
rst
"))
;
i
ntj
=Int
eger
.par
seI
nt(
request
.get
Par
amet
er(
"second"
));
i
ntk=i
+j;
out
.pr
int
ln(
"Sum i
s:"
+k)
;
%>
</
body
>
</
html
>
28|
Page
Out
put–
29|
Page
Pr
ogr
am No.-9
Ai
m:WAPt
oregi
sterandl
ogi
npageusi
ngJSP.
Code:
-
--
--
--
--
--
--
-i
ndex.
html
--
--
--
--
--
--
-
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>TODOsuppl
yat
it
le</
ti
tl
e>
<met
achar
set
="UTF-
8">
<met
aname="
viewpor
t"cont
ent
="wi
dth=dev
ice-
widt
h,i
nit
ial
-scal
e=1.
0">
<l
i
nkhr
ef="
css/
boot
str
ap.
min.
css"r
el="
sty
lesheet
"ty
pe="
text
/css"
/>
</
head>
<body
>
<f
orm met
hod="
Post
"act
ion="
SignUp.
jsp"
>
<di
vcl
ass="
cont
ainerbor
der
"st
yle="
widt
h:500px;
mar
gin-
top:10px;
paddi
ng-
top:
10px"
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Name:
</l
abel
>
<i
nputt
ype="
text
"cl
ass="
col
-sm-
7"name="
name"pl
acehol
der
="Ful
lname"
>
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Emai
li
d:</
label
>
<i
nputt
ype="
text
"cl
ass="
col
-sm-
7"name="
emai
l
"pl
acehol
der
="Emai
li
d">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
30|
Page
<l
abel
class="
col
-sm-
4">Phoneno.:
</l
abel
>
<i
nputt
ype="
text
"cl
ass="
col
-sm-
7"name="
phone"pl
acehol
der
="Phoneno.
">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Passwor
d:</
label
>
<i
nput t
ype="
text
" cl
ass="
col
-sm-
7" name="
passwor
d"
pl
acehol
der
="Passwor
d">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
8">I
fyoual
readyhav
eanaccountt
henl
ogi
n</
label
>
<ahr
ef="
Logi
n.ht
ml"cl
ass="
col
-sm-
2">Logi
n</
a>
</
div
>
<di
vcl
ass="
row"
>
<di
vcl
ass="
col
-sm-
3"></
div
>
<but
tont
ype="
submi
t"cl
ass="
btnbt
n-pr
imar
y">Si
gnup</
but
ton>
</
div
>
</
div
>
</
for
m>
</
body
>
</
html
>
-
--
--
--
--
--
--
--
-Logi
n.ht
ml-
--
--
--
--
--
--
-
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>TODOsuppl
yat
it
le</
ti
tl
e>
<met
achar
set
="UTF-
8">
31|
Page
<met
aname="
viewpor
t"cont
ent
="wi
dth=dev
ice-
widt
h,i
nit
ial
-scal
e=1.
0">
<l
i
nkhr
ef="
css/
boot
str
ap.
min.
css"r
el="
sty
lesheet
"ty
pe="
text
/css"
/>
</
head>
<body
>
<f
orm met
hod="
Post
"act
ion="
Logi
n.j
sp"
>
<di
vcl
ass="
cont
ainerbor
der
"st
yle="
widt
h:500px;
mar
gin-
top:10px;
paddi
ng-
top:
10px"
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Name:
</l
abel
>
<i
nputcl
ass="
col
-sm-
7"t
ype="
text
"name="
name"pl
acehol
der
="Ful
lName"
>
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Passwor
d:</
label
>
<i
nput t
ype="
text
" cl
ass="
col
-sm-
7" name="
passwor
d"
pl
acehol
der
="Passwor
d">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<di
vcl
ass="
col
-sm-
3"></
div
>
<but
tont
ype="
submi
t"cl
ass="
btnbt
n-pr
imar
y">Logi
n</
but
ton>
</
div
>
</
div
>
</
for
m>
</
body
>
</
html
>
-
--
--
--
--
--
--
-Si
gnUp.
jsp-
--
--
--
--
--
--
-
<%@pagecont
ent
Type="
text
/ht
ml"pageEncodi
ng="
UTF-
8"%>
<!
DOCTYPEht
ml>
32|
Page
<ht
ml>
<head>
<met
aht
tp-
equi
v="
Cont
ent
-Ty
pe"cont
ent
="t
ext
/ht
ml;
char
set
=UTF-
8">
<t
it
le>Ri
gest
er</
ti
tl
e>
</
head>
<body
>
<%
St
ri
ngname=r
equest
.get
Par
amet
er(
"name"
);
St
ri
ngemai
l=r
equest
.get
Par
amet
er(
"emai
l
");
St
ri
ngphone=r
equest
.get
Par
amet
er(
"phone"
);
St
ri
ngpasswor
d=r
equest
.get
Par
amet
er(
"passwor
d")
;
if
(name.l
ength(
)!
=0 && emai
l
.l
engt
h()
!=0 && phone.
lengt
h()
!=0 &&
password.
lengt
h()
!=0)
{
Cooki
ecooki
e4=newCooki
e("
Name"
,name)
;
Cooki
ecooki
e5=newCooki
e("
Emai
l
",emai
l
);
Cooki
ecooki
e6=newCooki
e("
Phone"
,phone)
;
Cooki
ecooki
e7=newCooki
e("
Passwor
d",
passwor
d);
cooki
e4.
set
MaxAge(
24*
60*
60)
;
cooki
e5.
set
MaxAge(
24*
60*
60)
;
cooki
e6.
set
MaxAge(
24*
60*
60)
;
cooki
e7.
set
MaxAge(
24*
60*
60)
;
r
esponse.
addCooki
e(cooki
e4)
;
r
esponse.
addCooki
e(cooki
e5)
;
r
esponse.
addCooki
e(cooki
e6)
;
r
esponse.
addCooki
e(cooki
e7)
;
out
.pr
int
ln(
"Wel
come!"+name)
;
out
.pr
int
ln(
"<br
>Youar
eregi
stersuccessf
ull
y.
")
;
}
el
se{
33|
Page
out
.pr
int
ln(
"<b>Somef
iel
disempt
y!</
b>"
);
out
.pr
int
ln(
"<br
>Pl
easef
il
lal
lthef
iel
d."
);
}
%>
</
body
>
</
html
>
-
--
--
--
--
--
--
--
Logi
n.j
sp-
--
--
--
--
--
--
--
-
<%@pagecont
ent
Type="
text
/ht
ml"pageEncodi
ng="
UTF-
8"%>
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<met
aht
tp-
equi
v="
Cont
ent
-Ty
pe"cont
ent
="t
ext
/ht
ml;
char
set
=UTF-
8">
<t
it
le>Logi
n</
ti
tl
e>
</
head>
<body
>
<%
St
ri
ngName=r
equest
.get
Par
amet
er(
"name"
);
St
ri
ngPasswor
d=r
equest
.get
Par
amet
er(
"passwor
d")
;
Cooki
ecok[
]=r
equest
.get
Cooki
es(
);
i
f(
Name.
lengt
h()
!=0&&Passwor
d.l
engt
h()
!=0)
{
if(
Name.equals(
cok[
0].get
Value(
)) &&
Passwor
d.equal
s(cok[
3].
get
Value()
)){
out.
pri
ntl
n("
<b>Youar
eSuccessf
ull
yLogi
n!</
b>"
);
out
.pr
int
ln(
cok[
0].
get
Val
ue(
));
out
.pr
int
ln(
cok[
1].
get
Val
ue(
));
out
.pr
int
ln(
cok[
2].
get
Val
ue(
));
}
el
se{
out
.pr
int
ln(
"<b>User
nameorPasswor
diswr
ong!
</b>"
);
34|
Page
out
.pr
int
ln(
"<br
>Pl
easecheackagai
n."
);
}
}
el
se{
out
.pr
int
ln(
"<b>Somef
iel
disempt
y</
b>"
);
out
.pr
int
ln(
"<br
>Pl
easef
il
lal
lfi
eld"
);
}
%>
</
body
>
</
html
>
Out
put–
35|
Page
36|
Page
Pr
ogr
am No.-
Ai
m:Gi
veabr
iefdescr
ipt
ionofJav
aBeans.
Jav
aBean:
Accordi
ng t o Java whit
e paper
,itisar eusabl
e sof
twar
e component.A bean
encapsul
atesmanyobj ectsi
ntooneobj
ect,sowecanaccessthi
sobj
ectfr
om mul
tipl
e
pl
aces.Mor eover
,itpr
ovi
destheeasymaint
enance.
AJav
aBeani
saj
avacl
asst
hatshoul
dfol
l
owf
oll
owi
ngconv
ent
ions:
1) I
tshoul
dhav
eano-
argconst
ruct
or.
2) I
tmusti
mpl
ementt
hej
ava.
io.
Ser
ial
i
zabl
eint
erf
ace.
3) I
tshoul
dpr
ovi
demet
hodst
osetandgett
hev
aluesoft
hepr
oper
ti
es,knownas
get
terandset
termet
hods.
SinceJavaBeansar
ejavaClasses,t
hatcanbeusedinsi
deJSPt ocreat
edynami
c
pages.Ther
earethr
eebasi
ct agsthatal
l
owstouse,access,modi
fy,t
hejav
aBean
Componentandit
spr
oper
ty.
1jsp:
useBean:Thejsp:
useBeanacti
ontagisusedt olocateori nst
anti
ateabeanclass.
I
fbeanobjectoftheBeanclassisal
readycreated,i
tdoesn'tcreatethebeandependi
ng
onthescope.Butifobj
ectofbeani
snotcreated,iti
nst
anti
at esthebean.
<j
sp:useBean i
d= "
instanceName" scope= "
page |
r
equest
| sessi
on |
appli
cation"
cl
ass= "packageName. className"
type= "packageName. className"
beanName=" packageName. className> </j
sp:useBean>
a)id: i
susedt oident ifyt hebeani nt hespeci fi
edscope.
b)scope:r epresent st hescopeoft hebean.I tmaybepage,r equest,sessionor
application.Thedef aul tscopei spage.
c)cl ass: instant i
atest hespeci fi
ed bean cl assbuti tmusthav eno- ar
g orno
const ructorandmustnotbeabst ract.
d)t ype:pr ov i
dest hebeanadat at ypei fthebeanal readyexistsint hescope.I tis
mai nlyusedwi thcl assorbeanNameat tri
bute.I
fy ouusei twi t
houtcl assor
beanName, nobeani si nstanti
at ed.
e)beanName:i nstantiat es t he bean usi ng the jav a.
beans.Beans.i
nstantiat
e()
met hod.
2jsp:setProper t
y:Thej sp:set Propertyact iont agsetsapr opertyvalueorv al
uesi na
beanusingt heset termet hod.
<j
sp:setProper ty name=" bean"
proper ty="*"
/>
37|
Page
3j
sp:
get
Proper
ty:
Thej
sp:
get
Proper
tyact
iont
agr
etur
nst
hev
alueoft
hepr
oper
ty.
<j
sp:
get
Proper
ty
name="
obj
"
proper
ty="
name"
/
>
Pr
ogr
am No.-
Ai
m:WAPt
oregi
sterandl
ogi
npageusi
ngJav
aBean.
Code:
-
--
--
--
--
--
--
-i
ndex.
html
--
--
--
--
--
--
-
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>Si
gnUpPage</
ti
tl
e>
<met
achar
set
="UTF-
8">
<met
aname="
viewpor
t"cont
ent
="wi
dth=dev
ice-
widt
h,i
nit
ial
-scal
e=1.
0">
<l
i
nkhr
ef="
css/
boot
str
ap.
min.
css"r
el="
sty
lesheet
"ty
pe="
text
/css"
/>
</
head>
<body
>
<f
orm met
hod="
Post
"act
ion="
SignUp.
jsp"
>
<di
vcl
ass="
cont
ainerbor
der
"st
yle="
widt
h:500px;
mar
gin-
top:10px;
paddi
ng-
top:
10px"
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Name:
</l
abel
>
<i
nputt
ype="
text
"cl
ass="
col
-sm-
7"name="
name"pl
acehol
der
="Ful
lname"
>
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Emai
li
d:</
label
>
<i
nputt
ype="
text
"cl
ass="
col
-sm-
7"name="
emai
l
"pl
acehol
der
="Emai
li
d">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
38|
Page
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Phoneno.:
</l
abel
>
<i
nputt
ype="
text
"cl
ass="
col
-sm-
7"name="
phone"pl
acehol
der
="Phoneno.
">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Passwor
d:</
label
>
<i
nput t
ype="
text
" cl
ass="
col
-sm-
7" name="
passwor
d"
pl
acehol
der
="Passwor
d">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
8">I
fyoual
readyhav
eanaccountt
henl
ogi
n</
label
>
<ahr
ef="
Logi
n.ht
ml"cl
ass="
col
-sm-
2">Logi
n</
a>
</
div
>
<di
vcl
ass="
row"
>
<di
vcl
ass="
col
-sm-
3"></
div
>
<but
tont
ype="
submi
t"cl
ass="
btnbt
n-pr
imar
y">Si
gnup</
but
ton>
</
div
>
</
div
>
</
for
m>
</
body
>
</
html
>
-
--
--
--
--
--
--
-Logi
n.ht
ml-
--
--
--
--
--
--
-
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<t
it
le>Logi
nPage</
ti
tl
e>
39|
Page
<met
achar
set
="UTF-
8">
<met
aname="
viewpor
t"cont
ent
="wi
dth=dev
ice-
widt
h,i
nit
ial
-scal
e=1.
0">
<l
i
nkhr
ef="
css/
boot
str
ap.
min.
css"r
el="
sty
lesheet
"ty
pe="
text
/css"
/>
</
head>
<body
>
<f
orm met
hod="
Post
"act
ion="
Logi
n.j
sp"
>
<di
vcl
ass="
cont
ainerbor
der
"st
yle="
widt
h:500px;
mar
gin-
top:10px;
paddi
ng-
top:
10px"
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Name:
</l
abel
>
<i
nputcl
ass="
col
-sm-
7"t
ype="
text
"name="
name"pl
acehol
der
="Ful
lName"
>
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<l
abel
class="
col
-sm-
4">Passwor
d:</
label
>
<i
nput t
ype="
text
" cl
ass="
col
-sm-
7" name="
passwor
d"
pl
acehol
der
="Passwor
d">
<di
vcl
ass="
col
-sm-
1"></
div
>
</
div
><br
>
<di
vcl
ass="
row"
>
<di
vcl
ass="
col
-sm-
3"></
div
>
<but
tont
ype="
submi
t"cl
ass="
btnbt
n-pr
imar
y">Logi
n</
but
ton>
</
div
>
</
div
>
</
for
m>
</
body
>
</
html
>
-
--
--
--
--
--
--
--
-Si
gnUp.
jsp-
--
--
--
--
--
--
--
<%@pagecont
ent
Type="
text
/ht
ml"pageEncodi
ng="
UTF-
8"%>
40|
Page
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<met
aht
tp-
equi
v="
Cont
ent
-Ty
pe"cont
ent
="t
ext
/ht
ml;
char
set
=UTF-
8">
<t
it
le>Si
gnUp</
ti
tl
e>
</
head>
<body
>
<%
St
ri
ngname=r
equest
.get
Par
amet
er(
"name"
);
St
ri
ngemai
l=r
equest
.get
Par
amet
er(
"emai
l
");
St
ri
ngphone=r
equest
.get
Par
amet
er(
"phone"
);
St
ri
ngpasswor
d=r
equest
.get
Par
amet
er(
"passwor
d")
;
%>
<j
sp:
useBeani
d="
user
"scope="
sessi
on"cl
ass="
beans.
User
Beans"
/>
<%
if
(name.l
ength(
)!
=0 && emai
l
.l
engt
h()
!=0 && phone.
lengt
h()
!=0 &&
password.
lengt
h()
!=0)
{
user
.set
Name(
name)
;
user
.set
Emai
l
(emai
l
);
user
.set
Phone(
phone)
;
user
.set
Passwor
d(passwor
d);
out
.pr
int
ln(
"<b>Wel
come!"+name+</
b>)
;
out
.pr
int
ln(
"<br
>Youar
eregi
stersuccessf
ull
y.
")
;
}
el
se{
out
.pr
int
ln(
"<b>Somef
iel
dsar
eempt
y!</
b>"
);
out
.pr
int
ln(
"<br
>Pl
easef
il
lal
lfi
elds"
);
}
%>
41|
Page
</
body
>
</
html
>
-
--
--
--
--
--
--
--
Logi
n.j
sp-
--
--
--
--
--
--
--
--
-
<%@pagecont
ent
Type="
text
/ht
ml"pageEncodi
ng="
UTF-
8"%>
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<met
aht
tp-
equi
v="
Cont
ent
-Ty
pe"cont
ent
="t
ext
/ht
ml;
char
set
=UTF-
8">
<t
it
le>Logi
n</
ti
tl
e>
</
head>
<body
>
<%
St
ri
ngName=r
equest
.get
Par
amet
er(
"name"
);
St
ri
ngPasswor
d=r
equest
.get
Par
amet
er(
"passwor
d")
;
%>
<j
sp:
useBeani
d="
user
"scope="
sessi
on"cl
ass="
beans.
User
Beans"
/>
<%
i
f(
Name.
lengt
h()
!=0&&Passwor
d.l
engt
h()
!=0)
{
i
f(
Name.
equal
s(user
.get
Name(
))&&Passwor
d.equal
s(user
.get
Passwor
d()
)){
out
.pr
int
ln(
"<b>Wel
come!"+user
.get
Name(
)+"
</b>"
);
out
.pr
int
ln(
"<br
>"+user
.get
Emai
l
())
;
out
.pr
int
ln(
"<br
>"+user
.get
Phone(
));
out
.pr
int
ln(
"<br
>Youar
esuccessf
ull
yLogi
n."
);
}
el
se{
out
.pr
int
ln(
"<b>User
nameorPasswor
diswr
ong!
</b>"
);
out
.pr
int
ln(
"<br
>Pl
easecheckagai
n."
);
}
42|
Page
}
el
se{
out
.pr
int
ln(
"<b>Somef
iel
dar
eempt
y!</
b>"
);
out
.pr
int
ln(
"<br
>Pl
easef
il
lal
lfi
elds"
);
}
%>
</
body
>
</
html
>
-
--
--
--
--
--
--
-User
Beans.
jav
a--
--
--
--
--
--
--
--
--
packagebeans;
publ
i
ccl
assUser
Beans{
pr
ivat
eSt
ri
ngname;
pr
ivat
eSt
ri
ngemai
l
;
pr
ivat
eSt
ri
ngphone;
pr
ivat
eSt
ri
ngpasswor
d;
publ
i
cUser
Beans(
){
}
publ
i
cSt
ri
ngget
Name(
){
r
etur
nname;
}
publ
i
cvoi
dset
Name(
Str
ingname){
t
his.
name=name;
}
publ
i
cSt
ri
ngget
Emai
l
(){
r
etur
nemai
l
;
}
publ
i
cvoi
dset
Emai
l
(St
ri
ngemai
l
){
t
his.
emai
l=emai
l
;
43|
Page
}
publ
i
cSt
ri
ngget
Phone(
){
r
etur
nphone;
}
publ
i
cvoi
dset
Phone(
Str
ingphone){
t
his.
phone=phone;
}
publ
i
cSt
ri
ngget
Passwor
d(){
r
etur
npasswor
d;
}
publ
i
cvoi
dset
Passwor
d(St
ri
ngpasswor
d){
t
his.
passwor
d=passwor
d;
}
}
Out
put–
44|
Page
45|
Page
Pr
ogr
am No.-12
Ai
m:Gi
veabr
iefdescr
ipt
ionofRMI
.
RMI
:
The RMI
(RemoteMet hodI nvocat
ion)isanAPIthatprovi
desamechani sm t
o
createdistr
ibutedappl
icat
ioninjava.TheRMIal l
owsanobj
ecttoi
nvokemethodsonan
objectrunningi nanot
herJVM.TheRMIpr ovi
desremot
ecommuni cat
ionbet
weenthe
appli
cationsusingtwoobject st
s ub skel
and et
on.
St
ub:
Thest
ubi sanobject,act
sasagat ewayforthecli
entside.Alltheout goi
ng
request
sarerout
edthr
oughi t.I
tresi
desatthecli
entsi
deandr epresent
st heremote
object
.Whenthecal
l
eri
nv okesmethodonthestubobj
ect,
itdoesthefoll
owingtasks:
1.I
tini
ti
atesaconnect
ionwi
thr
emot
eVi
rt
ual
Machi
ne(
JVM)
,
2.I
twri
tesandt
ransmi
ts(
mar
shal
s)t
hepar
amet
erst
other
emot
eVi
rt
ualMachi
ne
(
JVM),
3.I
twai
tsf
ort
her
esul
t
4.I
treads(
unmar
shal
s)t
her
etur
nval
ueorexcept
ion,
and
5.I
tfi
nal
l
y,r
etur
nst
hev
aluet
othecal
l
er.
46|
Page
Skel
eton:
Theskel et
onisanobj ect,actsasagat
ewayfort
heserv
ersideobject
.Allt
he
i
ncomingr equestsareroutedt hr
oughit
.Whentheskel
etonrecei
vest
hei ncomi
ng
r
equest,
itdoest hef
oll
owingtasks:
1.I
treadst
hepar
amet
erf
ort
her
emot
emet
hod
2.I
tinv
okest
hemet
hodont
heact
ual
remot
eobj
ect
,and
3.I
twr
it
esandt
ransmi
ts(
mar
shal
s)t
her
esul
ttot
hecal
l
er.
St
epsf
orRMI
:
1.Cr
eat
ether
emot
eint
erf
ace
2.Pr
ovi
det
hei
mpl
ement
ati
onoft
her
emot
eint
erf
ace
3.Compi
l
etheimplement
ati
oncl
assandcr
eat
ethest
ubandskel
etonobj
ect
s
usi
ngt
her
mictool
4.St
artt
her
egi
str
yser
vicebyr
mir
egi
str
ytool
5.Cr
eat
eandst
artt
her
emot
eappl
i
cat
ion
6.Cr
eat
eandst
artt
hecl
i
entappl
i
cat
ion
1Cr
eat
eRemot
eInt
erf
ace:
Forcr
eat
ing t
he r
emot
ei nt
erf
ace,extend t
he Remoteint
erf
ace and decl
aret
he
Remot
eEx
cepti
onwit
hal
lthemethodsofther
emoteint
erf
ace.
i
mport
j
av a.
rmi.
*;
publ
i
c i
nterf
ace
Adder extends
Remot
e{
publ
i
c i
nt
add(i
nt
x,i
nt
y)throws
Remot
eExcept
ion;
}
2Pr
ovi
dei
mpl
ememat
iont
oRemot
eInt
erf
ace:
Now prov
ide t
he impl
ement
ation of t
he r
emotei
nter
face. For pr
ovi
ding t
he
i
mplement
ati
onoft
heRemotei
nterf
ace,
weneedto
1) Ei
therext
endt
heUni
cast
Remot
eObj
ectcl
ass,
2) oruset
heexpor
tObj
ect
()met
hodoft
heUni
cast
Remot
eObj
ectcl
ass
i
mpor
t
jav
a.r
mi.
*;
i
mpor
t
jav
a.r
mi.
serv
er.
*;
47|
Page
publ
i
c cl
ass
Adder
Remot
e ext
ends
Uni
cast
Remot
eObj
ect
i
mpl
ement
s Adder
{}
3Cr
eat
ethest
ubandskel
etonobj
ect
:
Nextst
epi st
ocreat
estubandskel
etonobj
ectsusi
ngthermicompi
l
er.Ther
mict
ool
i
nvokestheRMIcompil
erandcr
eat
esstubandskel
etonobj
ect
s.
4St
artt
heRegi
str
ySer
vice:
Nowstar
tt heregi
str
yserv
icebyusingther
mir
egi
str
ytool
.Ify
oudon'
tspeci
fyt
hepor
t
number
,itusesadefaul
tportnumber.
5Cr
eat
eandRunser
verAppl
i
cat
ion:
Now r
miser
vicesneedtobehost
edi
naserv
erpr
ocess.TheNami
ngcl
asspr
ovi
des
met
hodst
ogetandst or
ether
emot
eobj
ect
.
6Cr
eat
eandRunCl
i
entAppl
i
cat
ion:
Atthecli
entwearegett
ingt
hestubobj
ectbyt
hel
ookup(
)met
hodoft
heNami
ngcl
ass
andinv
okingthemethodonthi
sobj
ect.
48|
Page
Pr
ogr
am No.-13
Ai
m:WAPt
oshowi
mpl
ement
ati
onofRMI
.
Code:
-
--
--
--
--
--
-AddI
.j
ava-
--
--
--
--
--
-
i
mpor
tjav
a.r
mi.
Remot
e;
i
mpor
tjav
a.r
mi.
Remot
eExcept
ion;
publ
i
cint
erf
aceAddIext
endsRemot
e{
publ
i
cvoi
dget
Resul
t(
inta,
intb)t
hrowsRemot
eExcept
ion;
}
-
--
--
--
--
--
--
--
Add.
jav
a--
--
--
--
--
--
--
i
mpor
tjav
a.r
mi.
Remot
eExcept
ion;
publ
i
ccl
assAddi
mpl
ement
sAddI{
publ
i
cvoi
dget
Resul
t(
inta,
intb)t
hrowsRemot
eExcept
ion{
Sy
stem.
out
.pr
int
ln(
Str
ing.
for
mat
("
TheSum of%d,
%di
s%d"
,a,
b,a+b)
);
}
}
-
--
--
--
--
--
--
--
-Cl
i
ent
.j
ava-
--
--
--
--
--
-
i
mpor
tjav
a.r
mi.
regi
str
y.Locat
eRegi
str
y;
i
mpor
tjav
a.r
mi.
regi
str
y.Regi
str
y;
publ
i
ccl
assCl
i
ent{
pr
ivat
eCl
i
ent
(){
}
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
t
ry{
/
/Get
ti
ngt
her
egi
str
y
49|
Page
Regi
str
yregi
str
y=Locat
eRegi
str
y.get
Regi
str
y(nul
l
);
/
/Looki
ngupt
her
egi
str
yfort
her
emot
eobj
ect
AddIst
ub=(
AddI
)regi
str
y.l
ookup(
"Add"
);
/
/Cal
l
ingt
her
emot
emet
hodusi
ngt
heobt
ainedobj
ect
st
ub.
get
Resul
t(
3,4)
;
Sy
stem.
out
.pr
int
ln(
"Remot
emet
hodi
nvoked"
);
}cat
ch(
Except
ione){
Sy
stem.
out
.pr
int
ln(
"Cl
i
entexcept
ion:
"+e.
get
Message(
));
}
}
}
-
--
--
--
--
--
--
--
--
Ser
ver
.j
ava-
--
--
--
--
--
--
-
i
mpor
tjav
a.r
mi.
regi
str
y.Regi
str
y;
i
mpor
tjav
a.r
mi.
regi
str
y.Locat
eRegi
str
y;
i
mpor
tjav
a.r
mi.
ser
ver
.Uni
cast
Remot
eObj
ect
;
publ
i
ccl
assSer
verext
endsAdd{
publ
i
cSer
ver
(){
}
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
t
ry{
/
/Inst
ant
iat
ingt
heAddcl
ass
Addobj
=newAdd(
);
/
/Expor
ti
ngt
heobj
ectofi
mpl
ement
ati
oncl
ass
50|
Page
/
/(her
ewear
eexpor
ti
ngt
her
emot
eobj
ectt
othest
ub)
AddIskel
eton=(
AddI
)Uni
cast
Remot
eObj
ect
.expor
tObj
ect
(obj
,0)
;
/
/Bi
ndi
ngt
her
emot
eobj
ect(
stub)i
nther
egi
str
y
Regi
str
yregi
str
y=Locat
eRegi
str
y.get
Regi
str
y()
;
r
egi
str
y.bi
nd(
"Add"
,skel
eton)
;
Sy
stem.
out
.pr
int
ln(
"Ser
verSt
art
ed"
);
}cat
ch(
Except
ione){
Sy
stem.
out
.pr
int
ln(
"Ser
verexcept
ion:
"+e.
get
Message(
));
}
}
}
Out
put–
51|
Page