Você está na página 1de 91

JSP

Nguyn Quc Nguyn

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

'hat(s 'rong with Servlets)


you have to send a long *+,- age that includes little code each *+,- tag "ust !e e"!edded in a String and sent using the rintln "ethod of the Print'riter o!.ect every single change will re/uire the intervention of the servlet rogra""er Sun understood this ro!le" and soon develo ed a solution$ +he result was JSP technology
0 Su orts two different styles for adding dyna"ic content 0 through scri ts and tags 0 Se arates resentation fro" i" le"entation logic

*ow 1o JavaServer Pages 'or2)


HTTP

8lient

'34 S35635

JSP 3N7%N3

1ata!ase

JSPs are rocessed here JSP 9iles

Ste s involved in rocessing a re/uest


Request is sent to JSP Engine Sends a request

'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!$)*+%>

+he !enefits of using JSP


Se aration of the content generation fro" the resentation
0 *+,-=>,- tags 0 Scri tlets# Java4eans

3" hasi?ing reusa!le co" onents


0 Java4eans# 3J4s

Si" lifying age develo "ent with tags


0 3asy@to@use# JSP technology s ecific tags

%ntegrated with J<33 and 3nter rise Java4eans

'hat does a JSP loo2 li2e)


+hree "ain JSP constructsA
0 1irectives
:llows one to control structure of the servlet

0 Scri ting 3le"ents


Bsed to include Java code

0 :ctions
S ecific tags that affect the runti"e !ehavior of the JSPs

:n 3&a" le
Fixed Template data

<HTML> <HEAD><TITLE>M,-i!st !o"!#m'.sp</TITLE></HEAD> <BODY> <$// M,-i!st !o"!#m'JS //>


JSP Directi e JSP Scriptlet

<%0 p#"e impo!t 1 ).#2#'&til'D#te) %>


JSP <% o&t'p!intln()Hello the!e$)*+ %> <B!> Expression

<%1 )3&!!ent 4#te is ) 5 new D#te(* %> </BODY> </HTML>

'hat ha
Bser 5e/uest

ens under the hood)

'e! Server= Servlet 3ngine New or "odified file)


Ces No

JSP *andler Servlet (Page 8o" ilation Servlet)

8o" ile into Servlet

3&ecute Servlet

'hat ha

ens under the hood) 0 :n 3&a" le

//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+ ; ;

'hat ha ens under the hood) 0 :n 3&a" le 0 (;)


<$// <se3l#ss'.sp //> <%0 p#"e l#n"&#"e 1 ).#2#) %> <%0 p#"e impo!t 1 )m, #67#"e'Simple3l#ss) %> <%$ Simple3l#ss m,O8. 1 new Simple3l#ss(*+ %> <%1 m,O8.'"etHei"ht(* %> <% m,O8.'setHei"ht(1:*+ %> <% o&t'p!intln(m,O8.'"etHei"ht(**+ %>

6arious 3le"ents in a JSP


1irectives
0 age
1efines infor"ation that will !e glo!ally availa!le for a JSP

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

1ifferent :ttri!utes of the Page 1irective


!ttribute
language D EFscri ting-anguageGE e&tends D EFclassNa"eGH i" ort D EFi" ort-istGH session D Htrue I falseH !uffer D Hnone I Fsi?e in 2!GH

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$

1ifferent :ttri!utes of the Page 1irective 0 (;)


!ttribute auto9lush D Htrue I falseH is+hreadSafe D Htrue I falseH info D EFte&tGH errorPage D EFerrorJB5-GH is3rrorPage D Htrue I falseH content+y e D EFctinfoGH De"inition 1eter"ines whether the out ut !uffer will !e flushed auto"atically# or whether it will raise an e&ce tion when the !uffer is full$ +he default is true$ S ecifies whether age can service "ore than one re/uest at a ti"e$ +he default is true$ S ecifies infor"ation a!out the JSP# accessed !y Ser let$getSer let%n"o&'$ can !e

+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

Bsing age :ttri!utes @ :n e&a" le


<%0 p#"e l#n"&#"e 1 ).#2#) impo!t 1 ).#2#'!mi'=> .#2#'&til'=) session 1 )t!&e) 8&ffe! 1 )1?78) #&to-l&sh 1 )t!&e) info 1 )p#"e 4i!e6ti2e .sp) e!!o! #"e 1 )E!!o!'.sp) isE!!o! #"e 1 )f#lse) isTh!e#4S#fe 1 )f#lse) %> <HTML> <TITLE> JS Elements </TITLE> <HEAD> <H1> JS Elements </H1> </HEAD> <BODY> </BODY> </HTML>

,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 %>

So"e %" ortant Points


'hile using directives# re"e"!er the followingA
0 1irectives are "essages to the JSP container# that is# the JSP engine 0 1irectives do not store any results in the out ut !uffer 0 1irectives are rocessed when the JSP is initiali?ed

JSP Scri ting 3le"ents


Scri ting is a "echanis" for e"!edding code frag"ents directly in an *+,- age$ +he following three scri ting ele"ents are involved in JSP scri tingA 3& ressions Scri tlets 1eclarations

JSP Scri ting 3le"ents 0 (;)


3& ressions are evaluated to .ava$lang$String ty e result$ Synta&A <%1 expression %> 3&a" leA
3valuated to roduce (

roduce a

<HTML> <BODY> ? 5 ? eC&#ls <%1 ? 5 ? %> </BODY> </HTML>

JSP Scri ting 3le"ents 0 (<)


Scri tlets contain code that is valid for the language s ecified in the language directive$ Java is the only language that is resently availa!le$ Synta&A <% scriptlet_source; %> 3&a" leA
Scriptlet

<HTML> <BODY> <% o&t'p!intln()HELLO DOALD$$)*+ %> </BODY> </HTML>

JSP Scri ting 3le"ents 0 (K)


-i2e e& ressions# scri tlets also have access to i" licit varia!les$
<% St!in" C&e!,D#t# 1 !eC&est'"etE&e!,St!in"(*+ o&t'p!intln()Att#6he4 FET 4#t#G )5C&e!,D#t#*+ %>

JSP Scri ting 3le"ents 0 (L)


Scri tlets do not need co" lete Java state"ents# and o en !loc2s can affect the static *+,- outside the scri tlets$
<% if (M#th'!#n4om(* > :'H* 9 %> H#2e # <B> h#pp, </B> 8i!th4#,$$ <% ; else 9%> H#2e # <B> ni6e </B> 4#,$$ <% ; %>

JSP Scri ting 3le"ents 0 (M)


1eclarations are the definition of class@level varia!les and "ethods that are used in a JSP$ Synta&A <%$ Declaration; %> 3&a" leA
Declaration

<%$ 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'

JSP Standard :ctions


! JSP action directi e pro ides an eas) method to encapsulate common tasks$ These t)picall) create or act on ob*ects+ usuall) Ja a,eans$

F.s F.s F.s F.s F.s F.s F.s

Ause4eanG AsetPro ertyG AgetPro ertyG A ara"G AincludeG AforwardG A luginG

JSP Standard :ctions 0 (;)


F.s Ause4eanG
0 :ssociates an instance of a re@defined Java4ean with a given sco e and %1

F.s AsetPro ertyG


0 Sets the value of a !eanNs ro erty

F.s AgetPro ertyG


0 :ccesses the value of the s ecified ro erty of a !ean instance 0 8onverts it to a .ava$lang$String o!.ect 0 Places it in the i" licit out o!.ect

JSP Standard :ctions 0 (<)


F.s A ara"G
0 %s used to rovide the tag=value airs of infor"ation 0 %ncluded as su!@attri!utes of .s Ainclude# .s Aforward# and .s A lugin actions 0 +he synta& is as followsA
<.spGp#!#m n#me 1 )pName) 2#l&e 1 )pValue)/> <.spGp#!#m n#me 1 )pName) 2#l&e 1 )pValue)> </.spGp#!#m>

JSP Standard :ctions 0 (K)


F.s AincludeG
0 Provides a "echanis" for including additional static and dyna"ic resources in the current JSP 0 +he synta& is as followsA
<.spGin6l&4e p#"e 1 )urlSpec) fl&sh 1 )t!&e)/> <.spGin6l&4e p#"e 1 )urlSpec) fl&sh 1 )t!&e)> <.spGp#!#m '''/> </.spGin6l&4e>

Bsing F.s AincludeG @ :n 3&a" le


This JSP -ill search "or an emplo)ee.s name and title$ %t -ill also act as a header "or Emplo)eein"o$*sp$ <$// Emplhe#4e!'.sp //> <$// Fet emplo,eeJs n#me f!om the !eC&est //> <% o&t'p!intln()Emplo,eeG ) 5 !eC&est'"et #!#mete!()emplo,ee)**+ %> <$// Fet emplo,eeJs title f!om the !eC&est //> <% o&t'p!intln()TitleG ) 5 !eC&est'"et #!#mete!()title)**+ %>

Bsing F.s AincludeG @ :n 3&a" le 0 (;)


Emplo)eein"o$*sp <HTML> <TITLE> Emplo,ee Info!m#tion </TITLE> <BODY> <.spGin6l&4e p#"e1 )Emplhe#4e!'.sp) fl&sh1 )t!&e) >2#l&e1 )M#!th#) /> <.spGp#!#m n#me1 )emplo,ee) <.spGp#!#m n#me1 )title) 2#l&e1 )Do6to!)/> </.spGin6l&4e> </BODY> </HTML>

F.s AincludeG

3" loyeeinfo$.s
/*sp0include page 1 2Empl3

3" lheader$.s

JSP Standard :ctions 0 (L)


F.s AforwardG
0 3na!les the JSP engine to dis atch the current re/uest to a static resource# a servlet# or to another JSP at run@ ti"e 0 +he synta& is as followsA <.spGfo!w#!4 p#"e 1 )relativeURLSpec) /> <.spGfo!w#!4 p#"e 1 )relativeURLSpec) > <.spGp#!#m '''/> </.spGfo!w#!4>

Bsing F.s AforwardG @ :n 3&a" le s


9orward$.s
<HTML> <TITLE> <sin" the JS -o!w#!4 #6tion </TITLE> <BODY> <$// 3he67s whethe! the )6omp#n, i4) is eC&#l to 1 //> <% if ((!eC&est'"et #!#mete!()6omp#n,ID)**'eC&#ls()1)** 9%> <.spGfo!w#!4 p#"e1 )M#!th#Home'.sp)> <.spGp#!#m n#me1 )emplo,ee) 2#l&e1 )M#!th#) /> <.spGp#!#m n#me1 )title) 2#l&e1 )Do6to!) /> </.spGfo!w#!4> <%; else 9 o&t'p!intln()So!!,> no m#t6hin" 2#l&es fo&n4)*+ </BODY> </HTML>

;%>

Bsing F.s AforwardG @ :n 3&a" le 0 (;)


,artha*o"e$.s
<HTML> ' ' <BODY> <% o&t'p!intln()Emplo,eeG) 5 !eC&est'"et #!#mete!()emplo,ee)**+ o&t'p!intln()TitleG) 5 !eC&est'"et #!#mete!()title)**+ %> </BODY> </HTML>

F.s AforwardG
9orward$.s
/*sp0"or-ard page 1 24artha3

,artha*o"e$.s

JSP Standard :ctions 0 (M)


F.s A luginG
0 7enerates *+,- that contains the a ro riate client@ !rowser de endent constructs# such as O4J38+ or 3,431$ +his will ro" t the download of the re/uired Java lugin# and su!se/uent e&ecution of the a let or !ean$ 0 +he synta& is as followsA <.spGpl&"in t,pe 1 )plugin ype) 6o4e 1 )classfilename) 6o4e8#se 1 )relativeURLpat!) > <.spGp#!#ms> <.spGp#!#m K/>''' '''</.spGp#!#ms> </.spGpl&"in>

8o""ents and 8haracter Quoting 8onventions


S)ntax Purpose : JSP co""ent$ :ny e"!edded JSP scri ting ele"ents# directives# or actions are ignored !y the translator$ :n *+,- co""ent that is assed through to the resultant *+,-$ :ny e"!edded JSP codes are e&ecuted nor"ally$ +his is used in te" late te&t (static *+,-) in lace of HFOH$ +his is used in scri ting ele"ents in lace of HOGH$ : single /uote in an attri!ute that uses single /uotes$ : dou!le /uote in an attri!ute that uses dou!le /uotes$ OG in an attri!ute$ FO in an attri!ute$ +his is used as a deli"iter$

FO @@ $$$ @@ OG FP@@ $$$ @@G FQO OQG QN QH OQG FQO Q

%" licit O!.ects


Server@side o!.ects are defined !y the JSP container +hey are always availa!le in a JSP# without !eing declared +hese o!.ects deter"ineA
0 how to acce t the re/uest fro" the !rowser 0 how to send the res onse fro" the server 0 how session trac2ing can !e done

%" licit O!.ects 0 (;)


%mplicit 5ariable
application

T)pe .ava&$servlet$ Servlet8onte&t

Description +his is the servlet conte&t o!tained fro" a call to getServlet8onfig()$ get8onte&t()

Scope : lication

con"ig

.ava&$servlet$ Servlet8onfig

+his re resents the Servlet 8onfig for this JSP

Page

out

.ava&$servlet$ .s $Js 'riter

+his is the Js 'riter o!.ect to the out ut strea"

Page

%" licit O!.ects 0 (<)


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

%" licit O!.ects 0 (K)


response

Protocol@de endent su!ty e of either .ava&$servlet$Servlet 5es onse or .ava&$servlet$htt $ *tt Servlet5es ons e

+his re resents the res onse o!.ect triggered !y the re/uest

Page

session

.ava&$servlet$htt $ *tt Session

+his re resents the session o!.ect# if any# created for the client during an *++P re/uest

Session

%" licit O!.ects 0 (L)

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

Bsing %" licit O!.ects @ :n 3&a" le


<HTML> <TITLE> A J#2#Se!2e! #"e &sin" JS T#"s </TITLE> <BODY> <H?> An ex#mple &sin" 4iffe!ent session o8.e6ts </H?> <<L> <LI> <B> Exp!ession / </B> <BA> Yo&! hostn#me isG <%1 !eC&est'"etAemoteHost(* %> <LI> <B> S6!iptlet / </B> <BA> <% o&t'p!intln()#tt#6he4 FET 4#t#G) 5 !eC&est'"etE&e!,St!in"(**+ %>

Bsing %" licit O!.ects @ :n 3&a" le 0 (;)


<LI> <B> De6l#!#tion / </B> <BA> <%$ p!i2#te int #66ess3o&nt 1 :+ %> This p#"e h#s 8een #66esse4 <%1 55#66ess3o&nt %> time(s* sin6e the se!2e! w#s l#st !est#!te4 <LI> <B> Di!e6ti2e / </B> <BA> <%0 p#"e impo!t 1 ).#2#'&til'=) %> To4#,Ls D#te isG <%1 new D#te(* %> </<L> </BODY> </HTML>

%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

1ifference !etween a Java4ean and a Java class


: !ean is a Java class with the following additional characteristicsA

+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

Java4eans 1esign Patterns


Na"ing conventions for !ean "ethods and data "e"!ers 1esign atterns are i" ortant for intros ection
0 'hich "echanis" using co" onents "a2es its internal structure 2nown to the outside world)

,ethods are e& osed through reflection

: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+ ; ;

'hat 8onstitutes a Java4ean)


+he funda"ental arts of a Java4ean

,ethod ; rivate rotected u!lic ,ethod <

Pro erties

3vents to co""unicate with the outside world

3vents
9ires an event Source 4ean

5egisters interest with the source !ean

-istener 4ean

8an handle and res ond to this event

3vents 0 (;)
Other co" onents of the a lication

infor"s

4ound ro erty (value changes) :n : lication

8reating 4eans and Bsing +he" 0 (;)


4eans have "any ossi!le usesA
0 4eans in *+,- for"s 0 Java,ail :P% through !eans 0 Java 1ata!ase 8onnectivity through !eans

: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$

:dvantages of 4eans 0 (;)


Btility software can !e rovided to hel a erson configure a !ean +he configuration settings of a !ean can !e saved in the ersistent storage and restored at a later ti"e : !ean "ay register so that
0 it can receive events fro" other o!.ects 0 it can generate events that are sent to other o!.ects

Bsing JSP 4ean +ags


JSP rovides three !asic !ean tagsA 0 +o find and use the !ean 0 .s Ause4ean 0 +o set one or "ore ro erties 0 .s AsetPro erty 0 +o get a ro erty 0 .s AgetPro erty

+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

Bsing JSP 4ean +ags 0 (;)


getPropert)78ame&' setPropert)78ame&value'

+he JSP need not 2now the inner structure of the !ean 4ean

+he 4lac2 !o& a

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

<.spG&seBe#n i4 1 )bean_name) 6l#ss 1 )bean_class@ />

.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

Sco e of 4eans 0 (;)


session
0 +he instance will !e availa!le across a articular session !etween the !rowser of a articular "achine and the 'e! server 0 5eferences to this o!.ect will !e released only after the associated sessions end

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 AsetPro erty 0 (;)


+he !ean intros ection "ethod is used to discover what ro erties are resent# their na"es# whether these are si" le or inde&ed# their ty es and the accessor "ethods +he synta& is as followsA
<.spGset <.spGset p#!#m 1 <.spGset 2#l&e 1 !ope!t, n#me 1 )i") p!ope!t, 1 )=) /> !ope!t, n#me 1 )i") p!ope!t, 1 )propertyName) )parameterName)/> !ope!t, n#me 1 )i") p!ope!t, 1 )propertyName) )propertyValue)/>

.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!) %>

%nstantiate the 8ounter !ean

<.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>

Setting 4ean Pro erties


Pro erties can !e set through an *+,- for" too +he users can rovide values through the for" : JSP can !e used to ass these values to a !ean through the setPro erty tags 9or" values can !e assed li2e thisA
<.spGsetp!ope!t, n#me 1 )i") p!ope!t, 1 )propertyName) 2#l&e 1 )<%1 !eC&est'"et #!#mete!()formParam)* %>) />

Setting 4ean Pro erties 0 (;)


+he for" varia!le and the !ean ara"eter have the sa"e na"es <.spGset !ope!t, n#me 1 )i") p!ope!t, 1 )formParam) /> +he for" varia!le and the !ean ara"eter have different na"es <.spGset !ope!t, n#me 1 )i") p!ope!t, 1 )propertyName) p#!#m 1 )paramName) />

: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>

The Form 9 6alculator$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

Page Sco e 0 (;)


!n Example
<HTML> ' ' ' <%0 p#"e e!!o! #"e 1 )e!!o!p#"e'.sp) %> <%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 )p#"e) 6l#ss 1 )3o&nte!) /> %nstantiate the <B> The 6&!!ent 6o&nt fo! the 8ounter !ean 6o&nte! 8e#n isG </B> <%1 i4I6o&nte!'"et3o&nt(* %> ' ' '</HTML>

Page Sco e 0 (<)


+he age sco e is inter reted as Page8onte&t$P:73JS8OP3 :fter the co" letion of the current Servlet$service() "ethod call# the o!.ect reference is discarded
0 'hile generating the servlet# the JSP engine creates an o!.ect in the service() "ethod 0 +his o!.ect e&ists for every client re/uest to the resource

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

5e/uest Sco e 0 (;)


!n Example : ;sing the 6ounter 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 )!eC&est) 6l#ss 1 )3o&nte!) /> <% i4I6o&nte!'set3o&nt(1:*+ %> <.spGfo!w#!4 p#"e 1 )AeC&estBe#n'.sp) /> ' ' '</HTML> %nstantiate the 8ounter !ean

5e/uest Sco e 0 (<)


Request,ean$*sp
<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 )!eC&est) 6l#ss 1 )3o&nte!) /> <B> The 6&!!ent 6o&nt fo! the 6o&nte! 8e#n isG </B> <%1 i4I6o&nte!'"et3o&nt(* %> ' ' ' </HTML>

5e/uest Sco e 0 (K)


+he re/uest sco e is inter reted as Page8onte&t$53QB3S+JS8OP3 +he o!.ect is !ound to .ava&$servlet$.s $Page8onte&t +he o!.ect reference is availa!le as long as the *tt 5e/uest o!.ect e&ists# even if the re/uest is assed to different ages +he underlying generated servlet relies on !inding the o!.ect to the *tt Servlet5e/uest# using the set:ttri!ute(String 2ey# O!.ect value) "ethod in the *tt Servlet5e/uest

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

Session Sco e 0 (;)


!n Example : The "irst JSP
<HTML> ' ' ' <%0 p#"e l#n"&#"e 1 ).#2#) %> <%0 p#"e impo!t 1 )3o&nte!) %>

%nstantiate the 8ounter !ean

<.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>

Session Sco e 0 (<)


!n Example : The second JSP
<HTML> ' ' ' <%0 p#"e l#n"&#"e 1 ).#2#) %> <%0 p#"e impo!t 1 )3o&nte!) %>

%nstantiate the 8ounter !ean

<.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>

Session Sco e 0 (K)


'hen you !rowse the first JSP for the first ti"e# the Rcurrent count of the counter !eanE will !e e/ual to ;$ 'hen you !rowse the second JSP with the sa"e instance of the !rowser# the counter increases fro" the last value shown in the first JSP$ %f a new instance of the !rowser is o ened# the current count attri!ute will !e reset$ 3ach instance of a client creates its own instance of the HttpSession# which is where the 8ounter !ean is stored$

Session Sco e 0 (L)


+he session sco e is inter reted as Page8onte&t$S3SS%ONJS8OP3$ +he o!.ect is !ound to .ava&$servlet$.s $Page8onte&t$ +he generated servlet relies on !inding the o!.ect to *tt Session# using the setAttribute(String key, Object value) "ethod$ +he o!.ect is distinct for every client and is varia!le as long as the clientNs session is valid$

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$

lication Sco e 0 (;)


!n Example : The "irst JSP

<HTML>' ' ' <%0 p#"e l#n"&#"e 1 ).#2#) %> <%0 p#"e impo!t 1 )3o&nte!) %>

%nstantiate the 8ounter !ean

<.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>

lication Sco e 0 (<)


!n Example : The second JSP

<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

lication Sco e 0 (K)

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

lication Sco e 0 (L)

+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

Você também pode gostar