JSP J a v a Se r v e rP ag es( J SP)i sat e c hn ol o gyf o rd e v el o pi n gwe bp ag est h ats u pp or td y na mi mi cc on t e ntwh i c hh el p s d ev e l o pe r si n s er tj a v ac od ei nHTMLpa ge sbym ma a k i n gu s eo fs p ec i a lJ SPt a gs . Us i n gJ SP ,y o uc a nc o l l e c ti n pu tf r o mu s er st h r o ug hwe bp ag ef o r ms ,p r e s en tr e c or d sf r o m ad at a ba s eo ra n ot h er s o ur c e ,a ndc r e at ewe bp ag esd y na mi mi c a l l y .
Advant agesofJSP: v s .Pur eSe r v l e t s : I ti smor ec on v eni entt owr i t e( andt omodi f y ! )r eg ul a rHTMLt hant oha v ep l ent yofpr i nt l n s t a t e me me nt st h atg en er a t et h eHTML .
J SPPr o c es s i n g: Th ef o l l o wi n gs t e pse x pl a i nho wt h ewe bs e r v e rc r e at e st h ewe bp ag eu s i n gJ SP: •
•
•
•
•
Aswi t hano r ma lp ag e,y o u rb r o ws e rs e n dsa nHTTPr e qu es tt ot h ewe bs e r v e r . Th ewe bs e r v e rr e c o gn i z e st h att h eHTTP r e qu es ti sf o raJ SP p ag ea n df o r wa r d si tt oaJ SP e ng i n e. Th i si sd o neb yus i n gt h eURLorJ SPp ag ewh i c he nd swi t h. j sp i ns t eadof. ht ml . Th eJ SPe ng i n el o ad st h eJ SPp ag ef r o md i s kan dc on v er t si ti n t oas er v l e tc on t e nt .Th i sc on v er s i o ni s v er ys i mpl ei nwhi c hal lt empl at et e x ti sc on v er t edt op r i nt l n()s t at emen t sandal lJ SP el ement sar e c o nv e r t e dt oJ a v ac o det h ati mp l e me me nt st h ec o r r e s po nd i n gd y na mi mi cbe ha v i o ro ft h ep ag e. Th eJ SP e ng i n ec omp i l e st h es er v l e ti n t oa ne x ec ut a bl ec l a ssan df o r wa r d st h eo r i g i n alr e qu es tt oa s er v l etengi ne. Ap ar to ft h e we bs er v e rc al l e dt h es er v l e te ng i n el o ad st h e Se r v l e tc l a ssa nd e x ec ut e si t .Du r i n g e x ec ut i o n,t h es er v l e tp r o du ce sa no ut p uti nHTMLf o r ma t ,wh i c ht h es er v l e te ng i n ep as s est ot h ewe b s e r v e ri n s i d ea nHTTPr e s p on s e .
•
•
Th ewe bs e r v e rf o r wa r d st h eHTTPr e s po ns et oy ou rb r o ws e ri nt e r mso fs t a t i cHTMLc on t e nt . Fi n al l ywe bb r o ws e rh an dl e st h ed y na mi mi c a l l yg en er a t e dHTMLpa gei n s i d et h eHTTPr e s po ns eex a c t l y asi fi twer eas t at i cpage.
LI FECYCLE: AJ SP l i f ec y c l ec anbedefi nedast h e ent i r epr oc es sf r om i t sc r eat i ont i l lt hedes t r uc t i onwhi c hi ss i mi l art oa s er v l etl i f ec y cl ewi t hanaddi t i onal s t epwhi c hi sr e qui r edt oc ompi l eaJ SPi nt os er v l et . Th ef o l l o wi n ga r et h ep at h sf o l l o we db yaJ SP •
Compi l at i on
•
I n i t i al i z at i on
•
Ex ec ut i on
•
Cl eanup
J SPCo mp i l a t i o n:
Wh enab r o ws e ra s k sf o raJ SP ,t h eJ SPe ng i n efi r s tc h ec k st os e ewh et h eri tn ee dst oc o mp i l et h ep ag e.I ft h e p ag eh asne v e rb e e nc o mp i l e d ,o ri ft h eJ SP h asbe enmo di fi e ds i n c ei twa sl a s tc o mp i l e d,t h eJ SP e ng i n e c o mp i l e st h epa ge . Thecompi l at i onpr oc es si n vol v est hr e es t eps : •
Pa r s i n gt h eJ SP .
•
T ur ni ngt heJ SPi nt oas er v l et .
•
Compi l i ngt hes er v l e t .
J SPI ni t i al i z at i on: Wh en a c o nt a i n erl o ad s aJ SP i ti n v ok e st h ej s p I n i t ( )me t h od b ef o r es e r v i c i n ga nyr e qu es t s .I fy o un ee dt o per f or mJ SPs pec i fi ci ni t i al i z at i on,ov er r i det hej s pI ni t ( )met hod: public void jspInit(){ // Initialization code... }
T ypi c al l yi ni t i al i z at i oni sper f or medonl yonc eandaswi t ht hes er v l eti ni tmet hod,y ougener al l yi ni t i al i z edat abas e c onnec t i ons ,openfi l es ,andc r eat el ook upt abl esi nt hej s pI ni tmet hod.
J SPEx e c u t i o n: Thi sphas eoft heJ SPl i f ec y cl er epr es ent sal l i nt er ac t i onswi t hr eques t sunt i l t heJ SPi sdes t r oy ed. Wh en ev e rabr o ws e rr e qu es t saJ SPa ndt h ep ag eh asb ee nl o ad edan di n i t i a l i z e d,t h eJ SPe ng i n ei n v ok e st h e me t h odi nt h eJ SP . _j spSer vi ce( ) Th e_ j s p Se r v i c e ( )me t h odt a k e sa nHt t pSer vl et Request andanHt t pSer vl et Responsea si t sp ar a me t e r sa s f ol l ows : void _jspService(HttpServletRequest request, HttpServletResponse response) { // Service andlin! code... }
Th e_ j s pSe r v i c e( )me t h odo faJSPi si n v ok e do nc ep erar e qu es ta ndi sr e sp on si b l ef o rg e ne r a t i n gt h er e sp on se f o rt h atr e qu es ta ndt h i sme t h odi sal s or e sp on si b l ef o rg en er a t i n gr e sp on se st oa l ls e v eno ft h eHTTPme t h od s i e.GET,POST,DELETEet c.
J SPCl eanup: Th ed es t r u ct i o np ha seoft h eJ SPl i f ec y cl er e pr e se nt swh enaJ SPi sb ei n gr e mo v edf r o mu sebyaco nt a i n er . Thej s pDe st r oy ( ) me t h odi st h eJ SPe qu i v a l e nto ft h ed es t r o yme t h odf o rs e r v l e t s .Ov e r r i d ej s pDe s t r o ywh eny o u n ee dt ope r f o r ma nyc l e an up ,s uc ha sr e l e as i n gd at a ba seco nn ec t i o nsorc l o si n go pe nfi l e s.
Th ej s pDe s t r o y ( )me t h odha st h ef o l l o wi n gf o r m: public void jsp"estro#() { // $our cleanup code !oes ere. }
Thi st ut or i al wi l l gi v ebas i ci deaons i mpl es y nt ax( i e.el e ment s )i n vol v edwi t hJ SPd ev el o pment :
TheSc r i pt l et : As c r i p t l e tc anc on t a i na nynu mb ero fJ AVAl a ng ua ges t a t e me nt s ,v a r i a bl eo rme t h odd ec l a r a t i o ns ,o re x pr e ss i o ns t hatar ev al i di nt hepages cr i pt i ngl anguage. Fol l owi ngi st hes ynt axofSc r i pt l et : %& code 'ra!ent &
Fo l l o wi n gi st h es i mp l ea ndfi r s te x amp l ef o rJ SP: %tl %ead%titleHello *orld%/title%/ead %bod# Hello *orld+%br/ %& out.println($our I- address is request.!etReoteddr())0 & %/bod# %/tl
J SPDec l ar at i ons : Ad ec l a r a t i o nd ec l a r e so neo rmo r ev a r i a bl e so rme t h od st h aty o uc anus ei nJa v ac od el a t e ri nt h eJ SPfi l e .Yo u mu s td ec l a r et h ev a r i a bl eo rme t h odb ef o r ey o uu sei ti nt h eJ SPfi l e . Fol l owi ngi st hes y nt axofJ SPDec l ar at i ons : %&+..... "eclaration stateents... &
J SPEx p r e s s i o n: AJ SPe x pr e ss i o ne l e me ntc on t a i n sas c r i p t i n gl a ng ua gee x pr e ss i o nt h ati se v al u at e d,c on v er t e dt oaSt r i n g,a nd i n se r t e dwh er et h ee x pr e ss i o na pp ea r si nt h eJ SPfi l e .
Be ca us et h ev a l u eo fa ne x pr e ss i o ni sco nv e r t e dt oaSt r i n g,y o uc anu sea ne x pr e ss i o n wi t h i nal i n eo ft e x t , wh et h ero rn o ti ti st a gg edwi t hHTML ,i naJ SPfi l e Fo l l o wi n gi st h es y nt a xo fJ SPEx pr e ss i o n: %&1 e2pression & %tl %ead%title 3oent 4est%/title%/ead %bod# %p 4oda#5s date6 %&1 (ne7 java.util."ate()).to8ocaleStrin!()& %/p %/bod# %/tl
J SPCommen t s : J SPc o mme ntma r k st e x to rs t a t e me nt st h att h eJ SPc o nt a i n ers h ou l di g no r e .AJ SPc o mme nti su s ef u lwh eny o u wa ntt ohi d eo r" c o mme nto ut "p ar to fy o urJ SPp ag e. Fo l l o wi n gi st h es y n t a xofJ SPc o mme nt s : %&99 4is is :S- coent 99&
J SPI mpl i c i tObj ec t s : J SPs uppor t sni neaut omat i c al l ydefi nedv ar i abl es ,whi c har eal s oc al l e di mpl i c i tobj ec t s .Thes ev ar i abl esar e:
Obj e ct s
De sc r i pt i on
r eques t
Thi si st heHt t pSer vl et Request obj ec tas s oc i at edwi t ht her eques t .
r e s p o n s e
Thi si st heHt t pSer vl et Response obj ec tas s oc i a t edwi t ht her es pons et ot he c l i ent .
out
Thi si st hePr i nt Wr i t er obj ec tus edt os endout putt ot hec l i ent .
s es s i on
Thi si st heHt t pSessi on obj ec tas s oc i at edwi t ht her eques t .
appl i c at i on
Thi si st heSe r v l e t Cont e x t obj ec tas soc i at edwi t happl i c at i onc ont e xt .
config
Thi si st heSer vl et Config o bj ec tas s oc i at edwi t ht hepa ge.
pageCont ext
page
Ex c ep t i o n
Thi senc aps ul at esus eofs er v er s pec i fi cf eat ur esl i k ehi gherp er f or manc e J spWr i t e r s.
Th i si ss i mpl yas y no ny mf o rt hi s,a ndi su se dt oc al l t h eme t h od sde fin edby t het r ans l at eds er v l etc l as s.
TheEx c ept i on ob j e cta l l o wst h ee x c ep t i o nd at at ob ea cc es s edb y d es i g na t e dJ SP .
J SPDi r ec t i v es : AJ SPdi r ec t i v eaff ec t st heov er al ls t r uc t ur eoft hes er v l etc l as s.I tus ual l yhast hef ol l owi ngf or m: %&; directive attribute1value &
Ther ear et hr eet y pesofdi r ec t i v et ag:
Di r e ct i v e
De sc r i pt i on
<%@ pag e. . .%>
Defi nesp agedep endentat t r i but es ,s uc hass c r i p t i ngl anguage,er r orpage, a ndb uff er i n gr e qu i r e me nt s .
<%@ i nc l ude. . .%>
I nc l udesafi l edur i ngt het r ans l at i onphas e.
<%@ t agl i b. . .%>
Dec l ar esat agl i br ar y ,co nt ai ni ngc us t om ac t i ons ,us edi nt hepag e
J SPAc t i on s : J SP a ct i o nsus ec on s t r u ct si nXMLs y nt a xt oc on t r o lt h eb eh av i o ro ft h es er v l e te ng i n e.Yo uc and yn ami c al l y i n s er tafi l e ,r e us eJ a v a Be an sc o mp on en t s ,f o r wa r dt h eu s ert oa no t h erp ag e,o rg e ne r a t eHTMLf o rt h eJ a v a pl ugi n. Th er ei son l yon es y nt a xf o rt h eAc t i o ne l e me nt ,a si tc on f o r mst ot h eXMLs t a nd ar d : %jsp6action_nae attribute1value /
Ac t i onel emen t sar ebas i c al l ypr ede fin edf unc t i onsandt her ear ef ol l o wi ngJ SPac t i onsav ai l a bl e :
Synt ax
j s p: i nc l ude
Pur pose
I nc l udesafi l eatt het i met hepagei sr eques t ed
j s p: us eBean
Fi ndsori ns t ant i at esaJ av aBean
j s p: s et Pr oper t y
Set st hepr oper t yofaJ av aBean
j s p: get Pr oper t y
I ns er t st hepr oper t yofaJ av aBeani nt ot heout put
j s p: f or war d
For war dst her eques t ert oanewpage
j s p: pl ugi n
Gener at esbr owser speci ficc odet hatmakesanOBJECTorEMBEDt agf or t heJ av apl ugi n
j s p: el ement
Defi nesXMLel ement sdy nami c al l y .
j s p: at t r i but e
Defi nesdy nami c al l ydefi nedXMLel ement ' sat t r i but e.
j s p: body
Defi nesdy nami c al l ydefi nedXMLel ement ' sbody .
j s p: t ext
Us et owr i t et empl at et ex ti nJ SPpagesanddoc ument s.