0% found this document useful (0 votes)
4 views15 pages

What Is JSP

JSP, or JavaServer Pages, is a technology developed by Sun Microsystems for creating dynamic web pages by embedding Java code within HTML. It offers advantages such as faster execution compared to other dynamic languages and seamless integration with Java APIs, but has disadvantages like difficulty in debugging and higher resource requirements. The JSP lifecycle includes compilation, initialization, execution, and cleanup phases, and it allows for the use of scripting elements to insert Java code into web pages.

Uploaded by

SMARTY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views15 pages

What Is JSP

JSP, or JavaServer Pages, is a technology developed by Sun Microsystems for creating dynamic web pages by embedding Java code within HTML. It offers advantages such as faster execution compared to other dynamic languages and seamless integration with Java APIs, but has disadvantages like difficulty in debugging and higher resource requirements. The JSP lifecycle includes compilation, initialization, execution, and cleanup phases, and it allows for the use of scripting elements to insert Java code into web pages.

Uploaded by

SMARTY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

WhatI

sJSP?
JSP isacol lect
ionoftechnologiesdevelopedbySunMi crosyst
ems.Itisusedt o
developwebpagesbyi nserti
ngJav acodeintot
heHTMLpagesbymaki ngspeci
alJSP
tags.Itcanconsistofei
therHTMLorXML( combinat
ionofbothisalsopossi
ble)wit
h
JSPact i
onsandcommands.Thef ullf
orm ofJSPisJavaServerPages.

WhyuseJSP?
Her
e,ar
ereasonsofusi
ngJSP:

 InJav aser verpagesJSP,t heexecut i


oni smuchf astercompar edtoother
dynamicl anguages.
 Iti
smuchbet terthanCommonGat ewayI nt
erf
ace( CGI).
 Javaser verpages( JSP) ar
ealway scompi l
edbeforei t
spr ocessedbytheser
ver
asitreducest heeffortoftheservertocreateprocess.
 Javaser verpagesar ebuiltoverServlet
sAPI .Hence,ithasaccesst oallJava
APIs,JNDI ,JDBCEJB, andothercomponent sofjava.
 JSPisani mpor t
antpar tofJav aEE( Enter
pri
seEdi t
ion),whichisaplatf
ormfor
enter
prise-l
evelapplicati
ons.

Di
sadv
ant
ageofJSP
 Iti
shar dt
ot r
aceJSPpageser r
orbecauseJSPpagesaret
ransl
atedtoservl
et.
 AsJSPout putisHTML, i
tisnotri
chinfeat
ures.
 Iti
sveryhardt odebugortr
aceerror
sbecauseJSPpagesarefi
rstt
ransl
atedint
o
servl
etsbeforethecompil
ati
onprocess.
 JSPpagesr equir
emor ediskspaceandt imet ohol
dJSPpagesast heyare
compiledont heserv
er.

Ser
vletVsJSP
Ser
vlet JSP

Ser
vlet
srunf
ast
ert
hanJSP. JSP r
unsslowerthanser
vletasi
ttakes
ti
met ocompil
etheprogr
am andconver
t
i
ntoserv
let
s.
I
tishar
dtowr
it
ecodei
nser
vlet
. It
'seasiert
o code i
n JSP compar
ed t
o
serv
let
s.

In MVC archi
tect
ure,ser
vletwor
ksasa I
nMVCar chit
ect
ure,JSPwor
ksasav
iew
contr
oll
er. f
ordi
spl
ayingout
put.

I
tshouldbeusewhent
her
eismor
edat
a JSP i
sgeneral
l
yused when ther
eisno
pr
ocessi
nginv
olv
ed. i
nvol
vementofmuchdat
aprocessi
ng.

Ther
ei snocust
om t
agwr
it
ingf
aci
l
ityi
n Youcaneasil
ybui
ldcust
om t
agst
hatcan
ser
vlet
s. di
rect
lycal
lJav
abeans.

Ser
vleti
saj
avacode. JSPi
saHTML-
basedcode.

I
t can accept al
l pr
otocol r
equest
s, I
tcanonl
yacceptHTTPr
equest
s.
i
ncl
udi
ngHTTP.

Youcanov
err
idet
heser
vice(
)met
hod. I
n JSP,y ou can'
tov
err
ide t
he ser
vice(
)
method.

InServ
let
,bydef
aul
t,sessi
onmanagement In JSP, session management i
s
i
s notenabled,userhas to enableit aut
omat
ical
l
yenabled.
expl
ici
tl
y.

I
n Servl
et,you hav
et oimplementbothIn JSP,business l
ogi
c is spl
itfr
om
busi
nesslogicand pr
esent
ation l
ogi
cin pr
esent
ati
onl
ogicusi
ngJav
aBeans.
thesi
ngl
ef i
l
e.

Modifi
cati
on i
n Servl
et fi
leisat ime JSPmodif
icat
ioni
sfast,asy
ouj
ustneed
consumingduetorel
oading,r
ecompi
l
ing, t
ocl
i
ckonerefr
eshbut
ton.
andrestar
ti
ngt
heserv
er.

Li
fecy
cleofJSP:
AJSPli
fecy
clei
sdefi
nedast
heprocessfrom i
tscr
eati
onti
llt
hedest
ruct
ion.Thi
sis
si
mil
art
oaservl
etl
i
fecycl
ewi
thanaddit
ionalst
epwhichisr
equir
edt
ocompi l
eaJSP
i
ntoser
vlet
.

Pat
hsFol
l
owedByJSP

Thef
oll
owi
ngar
ethepat
hsf
oll
owedbyaJSP−

 Compi
l
ati
on

 I
nit
ial
i
zat
ion

 Execut
ion

 Cl
eanup
ThefourmajorphasesofaJSPli
fecycl
ear
ever
ysi
mil
art
otheSer
vletLi
feCy
cle.The
fourphaseshav
ebeendescri
bedbel
ow−

JSPCompi
l
ati
on

WhenabrowserasksforaJSP,t
heJSPenginefi
rstcheckstoseewhet
heri
tneedsto
compi
l
ethepage.Ift
hepagehasneverbeencompil
ed,orift
heJSPhasbeenmodif
ied
si
ncei
twaslastcompil
ed,t
heJSPenginecompi
lesthepage.
Thecompi
l
ati
onpr
ocessi
nvol
vest
hreest
eps−

 Par
singt
heJSP.

 Tur
ningt
heJSPi
ntoaser
vlet
.
 Compi
l
ingt
heser
vlet
.

JSPI
nit
ial
i
zat
ion

Whenacont ai
nerloadsaJSP i
tinv
okesthej
spI
nit
()methodbeforeserv
ici
ngany
r
equest
s. If y ou need t o perfor
m JSP-speci
fi
c i ni
ti
ali
zati
on, over
ri
de
t
hejspI
nit
()method−

publ
i
cv oi
dj spIni
t(
){
//I
nit
ial
izationcode.
..
}
Typical
ly
,ini
ti
ali
zat
ionisperf
ormedonlyonceandaswi t
htheserv
letini
tmet hod,y
ou
general
lyini
ti
ali
zedatabaseconnect
ions,openf
il
es,andcreat
elookupt abl
esi nthe
j
spInitmethod.

JSPExecut
ion

Thi
sphaseoftheJSPl
i
fecy
cler
epr
esent
sal
lint
eract
ionswi
thr
equest
sunt
ilt
heJSP
i
sdest
royed.
WheneverabrowserrequestsaJSPandthepagehasbeenl
oadedandi
nit
ial
i
zed,t
he
JSPenginei
nvokesthe_jspSer
vice(
)met
hodi
ntheJSP.
The _j
spSer
vice(
) method takes anHt
tpSer
vlet
Requestand
anHtt
pSer
vlet
Responseasi
tspar
ameter
sasf
oll
ows−

v
oid_jspSer
vice(
HttpSer
vlet
Requestr
equest
,Ht
tpSer
vlet
Responser
esponse){
//Servi
cehandli
ngcode...
}
The_j
spSer
vice(
)methodofaJSPisinvokedonr
equestbasi
s.Thisi
sresponsi
blefor
gener
ati
ngtheresponseforthatr
equestandthi
smet hodi salsoresponsi
blefor
gener
ati
ngr
esponsestoal
lsev
enoftheHTTPmethods,i.
e,GET,POST,DELETE,etc.

JSPCl
eanup

Thedestr
uct
ionphaseoft
heJSPl
i
fecy
cler
epr
esent
swhenaJSPi
sbei
ngr
emov
ed
fr
om usebyacontai
ner
.
ThejspDestr
oy(
)methodistheJSP equiv
alentofthedestr
oymet hodforservl
ets.
Over
ride j
spDest
roy when y
ou need t
o perfor
m any cl
eanup,such as r
eleasi
ng
dat
abaseconnecti
onsorcl
osingopenf
il
es.
Thej
spDest
roy
()met
hodhast
hef
oll
owi
ngf
orm−

publ
icvoi
djspDest
roy
(){
//Yourcl
eanupcodegoesher
e.
}
HowdoesaJSPf
unct
ion
Scr
ipt
ingEl
ement
s
JSPscri
pti
ngelementsal
l
owsy out
oinser
tJavacodei
ntoJav
aSer
vletgener
atedf
rom
JSPpage.Thesearet
hescript
ingel
ements:
scr
ipt
let
,decl
arat
ion,
expressi
on&comment .

1)Scr
ipt
let
 Ascri
ptl
etcancontai
nanynumberofJAVAlanguagestatement
s,v
ari
abl
e&
expr
essi
onsthatar
evali
dint
hepagescri
pti
nglanguage.
 I
tcan’
tcont
ainmet
hoddef
inat
ionbuty
oucancal
lmet
hodwi
thi
nscr
ipt
let

Fol
l
owingi
sthesy
ntaxofScr
ipt
let−
<%codefr
agment%>

Exampl
eofScr
ipt
let<% %>
1)Desi
gnasi
mpl ej
sppaget
odi
spl
ayf
oll
owi
ng
I
nput:-

Out
put:
-

Cli
entFilename:-Index.
html
<html>
<head>
<t
itl
e>Scr
ipt
letExampl
e</
ti
tl
e>

</head>
<body>
<for
m acti
on="
server
.j
sp"method="post
">
EnterName:<i
nputty
pe="
text"name="t1"
><br
>

<inputt
ype="
submi
t"name="
b1"v
alue="
send"
>
</
form>

</body>
</
html>
Ser
verFi
lename:
-ser
ver
.j
sp

<%@pagecont
entTy
pe="
text
/ht
ml"pageEncodi
ng="
UTF-
8"%>
<!
DOCTYPEhtml>
<html>
<head>

<ti
tl
e>Scr
ipt
letExampl
e</
ti
tl
e>
</head>
<body>
<%

Str
ingn=request.
get
Paramet
er(
"t1"
);
out.
pri
ntl
n("Welcome"+n)
;
%>
</body>
</
html>

Example
2) Designasi
mpl
ecal
cul
ator
Input
:-

Output:-
Addit
ion=15
Cli
entFileName: -i
ndex.
html
<html>
<head>
<ti
tl
e>SimpleCalcul
ator
</t
it
le>
</head>
<body>

<f
orm act
ion="
calcul
ator.j
sp"method="
get">
Enter1stNo:<inputtype="
text
"name="t1"><br
>
Enter2ndNo: <i
nputtype="t
ext"name="t
2"><br>
<inputt
ype="submit"name="add"val
ue="+">
<inputt
ype="
submi
t"name="
sub"v alue="-
">
<inputt
ype="
submi
t"name="
mult"v alue="
*">
<inputt
ype="
submi
t"name="
div
"v alue="/"
>
</
form>

</body>
</
html>

Ser
verFi
lename:
-cal
cul
ator
.j
sp

<%@pagecont entType=" t
ext /ht
ml"pageEncoding="UTF-8"
%>
<html>
<head>
<tit
le>Cal culator
</ ti
tl
e>
</head>
<body >
<%
intc=I nteger .
parseInt(request.
get Paramet
er(
"t
1"));
intd=I nteger .
parseInt(request.getParamet
er(
"t2"
));
i
f(request .getParamet er (
"add")
!=null)
{
inte=c+d;
out .
print l
n("Additi
on=" +e);
}
i
f(request .getParamet er (
"sub")
!=null
)
{
intf=c- d;
out .
print l
n("Subtracti
on=" +f);
}
i
f(request .getParamet er (
"mult"
)!=null
)
{
intg=c* d;
out .
print l
n("Multi
plication="+g);
}
i
f(request .getParamet er (
"div
")!
=null)
{
inth=c/ d;
out .
print l
n("Divi
sion="+h) ;
}
%>
</body>
</
html>

2) Decl
arat
iont
ag

- I
fy ouwanttodefinemethodsorfi
elds,
youcanuseJSPdecl
arati
on.TheJSP
declar
ati
onissurroundedbythesi
gn<%!and%>.Forexample,i
fyouwantto
declar
eav ar
iabl
ex, y
oucanuseJSPdecl ar
ati
onasf
oll
ows:

<%! i
ntx=10; %>

- Thef
inal
semi
col
oni
srequi
red.

- Thedi f
ferencebetweenav ariabl
eusingdecl
arationandav ar
iabl
eisdecl
ared
usingscriptl
etisthatav ariabledecl
aredusingdecl ar
ati
ontagisaccessi
ble
byallmet hods,whil
eav ari
abl edecl
aredusingscriptl
eti
sonlyaccessi
bleto
the_jspservi
ce()methodoft hegener
atedservletfr
om theJSPpage.
Example
1) Desi gnasi mplecalculat
orbyusi ngDeclarati
ont ag
Input:-

Output:-
Addit
ion=15

Cli
entFilename:-i
ndex.
html
<html>
<head>
<ti
tl
e>Cal
cul
atorusi
ngDecl
arat
iont
ag</
ti
tl
e>
</head>
<body >
<f
or m act
ion="
declarati
on_server.
jsp"met hod="get"
>
Enter1stNo:<inputtype="t
ext"name=" t1"><br>
Enter2ndNo: <i
nputtype="text"name=" t2"
><br>
<inputt
ype="submit"name=" add"v alue="+">
<inputt
ype="submit"name=" sub"v alue="-"
>
<inputt
ype="submit"name=" mul t
"v alue="*"
>
<inputt
ype="submit"name=" div"value="/">
</form>

</body>
</
html>

Ser
verFi
lename:
-decl
arat
ion_
ser
ver
.j
sp

<%@pagecont entType=" t
ext/html"pageEncoding="UTF-
8"%>
<!
DOCTYPEht ml>
<html>
<head>
<tit
le>SimpleCalculatorusingDecl ar
ati
on</ti
tl
e>
</head>
<body >
<%!
inti=0,j
=0;
%>
<%
i
=Integer.
parseInt(r
equest.getParameter
("t
1"))
;
j
=Integer.
parseInt(r
equest.getParameter
("t
2"))
;
i
f(request
.getParamet er(
"add")!
=null
)
{
ints=additi
on();
out .
pri
nt(
"Addition="+s);
}
i
f(request
.getParamet er(
"sub")!
=null
)
{
ints=subtracti
on( )
;
out .
pri
nt(
"subtracti
on="+s);
}
i
f(request
.getParamet er(
"mult")
!=nul
l)
{
ints=mul t
ipli
cati
on( );
out .
print(
"Mul ti
pli
cat i
on="+s);
}
i
f(request.getParamet er("
div")
!=nul
l
)
{
ints=di vi
sion();
out .
print(
"Divisi
on=" +s);
}
%>
<%!
publicintaddi t
ion()
{
i
ntk=i +j;
returnk;
}
publicintsubt r
action()
{
i
ntk=i -j
;
returnk;
}
publicintmul ti
pli
cation()
{
i
ntk=i *j
;
returnk;
}
publicintdivisi
on()
{
i
ntk=i /j
;
returnk;
}
%>
</body >
</
html>

3.JSPexpr
essi
ont
ag

Thecodepl aced wi
thi
nJSP expr
ession t swr
agi it
tentotheoutputst
ream oft
he
response.Soyouneednotwr
iteout.
print
()towri
tedat
a.I
tismainl
yusedtopri
ntt
he
v
aluesofv
ari
abl
eormet
hod.

Thi
stagal
l
owt
hedev
elopert
oembedanyj
avaexpr
essi
onandi
sshor
tforout
.pr
int
ln(
).

Asemi
col
on(
;)doesnotappearatt
heendoft
hecodei
nsi
det
het
ag.

Sy
ntax:
-

<%= st
atement%>

Exampl
e:

Fi
l
e:i
ndex.
html

<ht
ml>

<body
>

<f
orm act
ion="
wel
come.
jsp"met
hod=”
post
”>

<i
nputt
ype="
text
"name="
uname"
><br
/>

<i
nputt
ype="
submi
t"v
alue="
go"
>

</
for
m>

</
body
>

</
html
>

Fi
l
e:wel
come.
jsp

<%@pagecont
ent
Type=”
text
/ht
ml”
%>

<ht
ml>

<body
>

<%

St
ri
ngs=r
equest
.get
Par
amet
er(
"uname"
);

%>

<%="
Wel
come"
+s%>

</
body
>
</
html
>

4.JSPcommentt
ag

JSP comment
saresimil
l
artoHTMLcomment s<!
--
Htmlcomment-
->exceptJSP
commentsar
enev
ersentt
otheuser
’sbr
owser
.

HTMLComment
sar
evi
si
blei
nthepagesour
se.

Sy
ntax:
-<%-
-JSPComment-
-%>

Exampl
e:-

<ht
ml>

<head>

<t
it
le>HTMLandJSPComment
s</
ti
tl
e>

</
head>

<body
>

<!
--
Thi
sisHTMLcomment
;vi
sibl
eint
hepagesour
ce-
->

<%-
-Thi
sisJSPcomment
;inv
isi
blei
nthepagesour
ce-
-%>

</
body
>

</
html
>

You might also like