Escolar Documentos
Profissional Documentos
Cultura Documentos
Overview
JavaServer Pages (JSP) is another Java technology for develo ing we! a lications JSP was released during the ti"e servlet technology had gained o ularity as one of the !est we! technologies availa!le JSP is not "eant to re lace servlets# however$ %n fact# JSP is an e&tension of the servlet technology# and it is co""on ractice to use !oth servlets and JSP ages in the sa"e we! a lications
8lient
'34 S35635
JSP 3N7%N3
1ata!ase
'e! 8lient
Response is sent to the client
'e! Server
Output is sent back
JSP 9ile
Servlet
*+,9ile
: Si" le 'e! :
lication
*ello'orld$ht"l
<HTML> <TITLE> A Simple HTML extension </TITLE> <BODY> <H1> Hello JS </BODY> </HTML> !o"!#mme!$ </H1> file with HTML
: Si" le 'e! :
lication 0 (;)
*ello'orld$.s
<HTML> <TITLE> A Simple HTML extension </TITLE> <BODY> <H1> Hello JS </BODY> </HTML> !o"!#mme!$ </H1> file with JS
: Si" le 'e! :
*ello'orld<$.s
<HTML> <TITLE> A Simple JS <BODY> <H1> <% o&t'p!intln()Hello JS </H1> </BODY> </HTML>
lication 0 (<)
file </TITLE>
!o"!#mme!$)*+%>
0 :ctions
S ecific tags that affect the runti"e !ehavior of the JSPs
:n 3&a" le
Fixed Template data
'hat ha
Bser 5e/uest
3&ecute Servlet
'hat ha
//Simple3l#ss'.#2# p#67#"e m, #67#"e+ p&8li6 6l#ss Simple3l#ss9 int hei"ht+ p&8li6 2oi4 Simple3l#ss(* 9 hei"ht 1 1::+ ; p&8li6 2oi4 setHei"ht(int h* 9 hei"ht 1 h+ ; p&8li6 int "etHei"ht(* 9 !et&!n hei"ht+ ; ;
0 include
Bsed to insert te&t and code at JSP translation ti"e
0 tagli!
1efines a "echanis" for e&tending the current set of JSP tags
De"inition
+his attri!ute tells the server a!out the language to !e used to co" lete the JSP file$ :t resent# Java is the only language that can !e used$ +his attri!ute defines the arent class that the generated servlet will e&tend$ +his attri!ute defines the list of ac2ages that will !e availa!le to this JSP$ *owever# a co""a "ust se arate each ac2age$ +his attri!ute deter"ines whether the session data will !e availa!le to this age$ +he default is true$ +his attri!ute deter"ines whether the out ut strea" is !uffered$ +he default value is # kb$
+his attri!ute re resents the relative B5- to the JSP that will handle e&ce tions$ +his attri!ute states whether or not the JSP is an errorPage$ +he default is "alse$ +his attri!ute re resents the character set of the res onse$ ,%,3 ty e and
,ore on 1irectives
include directive
0 Synta&
<%0 in6l&4e file 1 @!el#ti2e<ALspe6B %>
tagli! directive
0 Synta&
<%0 t#"li8 &!i 1 @tagLibraryURIB p!efix 1 @tagPrefixB %>
roduce a
<%$ p!i2#te st#ti6 int 6o&ntI#66ess 1 :+ %> This p#"e h#s 8een #66esse4 <%1 556o&ntI#66ess %> time(s* sin6e the se!2e! w#s !e8oote4'
F.s AincludeG
3" loyeeinfo$.s
/*sp0include page 1 2Empl3
3" lheader$.s
;%>
F.s AforwardG
9orward$.s
/*sp0"or-ard page 1 24artha3
,artha*o"e$.s
Description +his is the servlet conte&t o!tained fro" a call to getServlet8onfig()$ get8onte&t()
Scope : lication
con"ig
.ava&$servlet$ Servlet8onfig
Page
out
Page
.ava$lang$O!.ect
+his re resents the this o!.ect for the current instance of the JavaServer age +his is the age conte&t o!.ect for the JSP +his is the re/uest o!.ect that triggered the re/uest
Page
page6ontext
.ava&$servlet$ .s $Page8onte&t Protocol@de endent su!ty e of either .ava&$servlet$ Servlet5e/uest or .ava&$servlet$htt $ *tt Servlet5e/uest
Page
request
5e/uest
Protocol@de endent su!ty e of either .ava&$servlet$Servlet 5es onse or .ava&$servlet$htt $ *tt Servlet5es ons e
Page
session
+his re resents the session o!.ect# if any# created for the client during an *++P re/uest
Session
exception
.ava$lang$+hrowa!le
+his is an o!.ect of class .ava$lang$ +hrowa!le$ %t re resents the runti"e e&ce tion that resulted in a call to the error age$
Page
%ntroduction Java4eans
%s Java4ean a reusa!le co" onent) %s it any different fro" a regular Java class) :re there tools availa!le to develo !eans) *ow are these !eans ut to use in JSP codes)
'hat is a Java4ean)
Java4eans !rings co" onent technology to the Java latfor" 'ith the hel of the Java4eans :P%# you can create reusa!le and latfor"@inde endent co" onents +hese co" onents can !e co"!ined into a lets# a lications# or co" osite co" onents Java4ean or a 4ean# is a si" le Java class that follows a set of na"ing and design conventions# outlined !y the Java4eans s ecifications
+he class "ust !e instantia!le %t "ust have a default constructor %t "ust !e seriali?a!le %t "ust follow the Java4eans design atterns %t "ust follow the new Java ;$; :'+ delegation event "odel
:n 3&a" le of a Java4ean
p&8li6 6l#ss Towe! 9
Pro erty
p!i2#te flo#t hei"ht+ getPropertyName() p&8li6 flo#t "etHei"ht(* 9 !et&!n hei"ht+ ; p&8li6 2oi4 setHei"ht(flo#t h* 9 setPropertyName() hei"ht 1 h+ ; p&8li6 8oole#n isF!e#te!Hei"ht(int initi#lHei"ht> int fin#lHei"ht* 9 if((fin#lHei"ht / initi#lHei"ht* > :* 9 !et&!n t!&e+ ; else 9 !et&!n f#lse+; ; p&8li6 Towe!(* 9 hei"ht 1 (flo#t*1:'H+ ; ;
Pro erties
3vents
9ires an event Source 4ean
-istener 4ean
3vents 0 (;)
Other co" onents of the a lication
infor"s
:dvantages of 4eans
: !ean has the !enefits of JavaNs Rwrite once# run everywhereE "odel$ +he ro erties# events and "ethods of a !ean# which are e& osed to an a lication !uilder tool# can !e controlled$ : !ean "ay !e designed to o erate correctly in different locales that "a2e it useful glo!ally$
+hese are also 2nown as actions and are s ecific tags that affect the followingA
0 the runti"e !ehavior of the JSP 0 the res onses that are sent !ac2 to the client
+he JSP need not 2now the inner structure of the !ean 4ean
roach
.s Ause4ean
+his is used to associate a Java4ean in JSP$ %t ensures that the o!.ect can !e referenced fro" JSP# using an %1 and sco e +he synta& is as followsA
+he !ean class "ust !e availa!le to the JSP engine
.s Ause4ean 0 (;)
<.spG&seBe#n i4 1 )i"_name) 6l#ss 1 )bean_class) s6ope 1 )p#"eM!eC&estMsessionM#ppli6#tion) beanDetails / > !ean1etails is one ofA 6l#ss 1 )className) 6l#ss 1 )className) t,pe 1 )typeName) 8e#nN#me 1 )beanName) t,pe 1 )typeName) t,pe 1 )t,peN#me)
Sco e of 4eans
age
0 +he !ean will disa ear as soon as the current age finishes generating 0 5eferences to this o!.ect will only !e released after the res onse is sent !ac2 to the client
re/uest
0 +he !ean instance will !e availa!le as long as the re/uest o!.ect is availa!le 0 5eferences to this o!.ect will !e released only after the re/uest is rocessed co" letely
lication
0 +he instance will !e availa!le to all users and ages of the a lication 0 %t is released only when the run@ti"e environ"ent reclai"s the Servlet8onte&t
:n 3&a" le
' ' <.spG&seBe#n i4 1 )mp#!#m) s6ope 1 )session) 6l#ss 1 ).#2#'l#n"'St!in")> This is &se4 to inst#nti#te the 8e#n </.spG&seBe#n> <HTML> <BODY> <H1> Hello Do!l4$ </H1> </BODY> </HTML> +he sco e is set to the session
.s AsetPro erty
+his is used along with the use4ean action %t sets the values of si" le and inde&ed ro erties of a !ean in various waysA
0 :t re/uest ti"e# using the ara"eters in the re/uest o!.ect 0 :t re/uest ti"e# using the result of an evaluated e& ression 0 9ro" a s ecified string
.s AgetPro erty
+his action is co" le"entary to the .s AsetPro erty action %t is used to access the ro erties of a !ean %t converts the retrieved value to a String +he synta& is as followsA <.spG"et !ope!t, n#me 1 )i") p!ope!t, 1 )propertyName) />
:n 3&a" le
The ,ean 9 6ounter$*a a
p&8li6 6l#ss 3o&nte! 9 Pro erty int 6o&nt+ p&8li6 3o&nte!(* 9 6o&nt 1 :+ ; get ,ethod p&8li6 int "et3o&nt(* 9 6o&nt55+ set ,ethod !et&!n 6o&nt+ ; p&8li6 2oi4 set3o&nt(int 6* 9 6o&nt 1 6+ ; ;
:n 3&a" le 0 (;)
The JSP that uses the bean
<HTML> ' ' ' <%0 p#"e l#n"&#"e 1 ).#2#) %> <%0 p#"e impo!t 1 )3o&nte!) %>
<.spG&seBe#n i4 1 )i4I6o&nte!) s6ope 1 )session) 6l#ss 1 )3o&nte!) /> <.spGset !ope!t, n#me 1 )i4I6o&nte!) p!ope!t, 1 )6o&nt) p#!#m 1 )6o&nt) /> <.spG"et !ope!t, n#me 1 )i4I6o&nte!) p!ope!t, 1 )6o&nt) /> ' ' ' </HTML>
:n 3&a" le
The ,ean 9 6alc,ean$*a a
p&8li6 6l#ss 3#l6Be#n implements .#2#'io'Se!i#liO#8le 9 p!i2#te int 2#l&e1+ p!i2#te int 2#l&e?+ p&8li6 2oi4 setP#l&e1(int n&m1* 9 2#l&e1 1 n&m1+ ; p&8li6 2oi4 setP#l&e?(int n&m?* 9 2#l&e? 1 n&m?+ ; p&8li6 int "etS&m(* 9 !et&!n 2#l&e1 5 2#l&e?+ ; ;
:n 3&a" le 0 (;)
<HTML> ' ' ' Ente! #n, two n&m8e!s #n4 6li67 on the <B>3#l6&l#te</B> B&tton' <-OAM A3TION 1 )3#l6Be#n'.sp) METHOD 1 )FET)> <IN <T TY E 1 )TEQT) NAME 1 )2#l&e1)> <BA> <IN <T TY E 1 )TEQT) NAME 1 )2#l&e?)> <BA> <BA> <IN <T TY E 1 )S<BMIT) NAME 1 )3#l6&l#te) PAL<E 1 )3#l6&l#te)> ' ' ' </HTML>
:n 3&a" le 0 (<)
The JSP 9 6alc,ean$*sp
<.spG&seBe#n i4 1 )6#l6) 6l#ss 1 )8e#ns'3#l6Be#n)/> <.spGset !ope!t, n#me 1 )6#l6) p!ope!t, 1 )=)/> <HTML> ' ' ' <B> The s&m of the two n&m8e!s is </B> <.spG"et !ope!t, n#me 1 )6#l6) p!ope!t, 1 )s&m)/> ' ' ' </HTML>
Page Sco e
+he !ean will disa ear as soon as the current age finishes generating 5eferences to this o!.ect will only !e released after the res onse is sent !ac2 to the client O!.ects with age sco e are stored in the age8onte&t
5e/uest Sco e
+he !ean instance will !e availa!le as long as the re/uest o!.ect is availa!le 5eferences to this o!.ect will !e released only after the re/uest is rocessed co" letely +hese references are stored in the re/uest o!.ect
Session Sco e
4eans with session sco e are accessi!le only within ages rocessing re/uests that are in the sa"e session as the one in which the !ean was created 4eans cannot !e defined in a age whose age directive has an attri!ute sessionDfalse 5eferences to the session sco e are stored in the session o!.ect
<.spG&seBe#n i4 1 )i4I6o&nte!) s6ope 1 )session) 6l#ss 1 )3o&nte!) /> <H1> A Session 8e#nG The -i!st Ex#mple </H1> <B>The 6&!!ent 6o&nt fo! the 6o&nte! 8e#n isG </B> <%1 i4I6o&nte!'"et3o&nt(* %> ' ' ' </HTML>
<.spG&seBe#n i4 1 )i4I6o&nte!) s6ope 1 )session) 6l#ss 1 )3o&nte!) /> <H1> A Session 8e#nG The Se6on4 Ex#mple </H1> <B>The 6&!!ent 6o&nt fo! the 6o&nte! 8e#n isG </B> <%1 i4I6o&nte!'"et3o&nt(* %> ' ' ' </HTML>
lication Sco e
4eans with the application sco e are accessi!le within ages rocessing re/uests that are in the sa"e a lication s ace as the age in which these were created$ 5eferences to the o!.ect will !e released only when the runti"e environ"ent reclai"s the Ser let6ontext o!.ect$ 4eans with the a lication sco e are !est used when there is a need for sharing the infor"ation a"ong JSPs and servlets for the life of an a lication$
<HTML>' ' ' <%0 p#"e l#n"&#"e 1 ).#2#) %> <%0 p#"e impo!t 1 )3o&nte!) %>
<.spG&seBe#n i4 1 )i4I6o&nte!) s6ope 1 )#ppli6#tion) 6l#ss 1 )3o&nte!) /> <H1> An Appli6#tion 8e#nG The -i!st Ex#mple </H1> <B>The 6&!!ent 6o&nt fo! the 6o&nte! 8e#n isG </B> <%1 i4I6o&nte!'"et3o&nt(* %> ' ' ' </HTML>
<HTML> ' ' ' 8ounter !ean <%0 p#"e l#n"&#"e 1 ).#2#) %> <%0 p#"e impo!t 1 )3o&nte!) %> <.spG&seBe#n i4 1 )i4I6o&nte!) s6ope 1 )#ppli6#tion) 6l#ss 1 )3o&nte!) /> <H1> An Appli6#tion 8e#nG The Se6on4 Ex#mple </H1> <B>The 6&!!ent 6o&nt fo! the 6o&nte! 8e#n isG </B> <%1 i4I6o&nte!'"et3o&nt(* %> ' ' ' </HTML>
%nstantiate the
4oth these ages share the sa"e instance of the Counter !ean 3ach age incre"ents the other ageNs instance of the !ean +hese ages will share this sa"e instance# until the JSP engine is shut down
+he a lication sco e is inter reted as Page8onte&t$:PP-%8:+%ONJS8OP3 +he o!.ect is !ound to .ava&$servlet$Servlet8onte&t +his is the "ost ersistent sco e +he generated servlet relies on !inding the o!.ect to the Servlet8onte&t Bsing the set:ttri!ute(String 2ey# O!.ect value) "ethod in the Servlet8onte&t :ll clients access the sa"e o!.ect