unit 4 - jsp-part ii
TRANSCRIPT
-
8/9/2019 Unit 4 - JSP-Part II
1/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Cha%ter &Se%arat#n' ro'ramm#n' and
resentat#on)JS Techno"o'y
CSI3140*** Structures+ Techn#,ues+ and Standards
-
8/9/2019 Unit 4 - JSP-Part II
2/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
/ JS -%ress#on Lan'ua'e (-L$ maes #t%oss#b"e to eas#"y access a%%"#cat#on datastored #n Jaaeans com%onents.
-
8/9/2019 Unit 4 - JSP-Part II
3/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
${visits+1}#s an eam%"e of an -Le%ress#onembedded #n a JS document
25 #s the synta used #n JS documents tomar the conta#ned str#n' as an -Le%ress#on
6n -L e%ress#on can occur
/ In tem%"ate data) ea"uates to Jaa String/ 6s (%art of$ the a"ue of certa#n JS attr#butes)
ea"uates to data ty%e that de%ends on contet
-
8/9/2019 Unit 4 - JSP-Part II
4/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
/ -L "#tera"s)
true+ false
dec#ma" #nte'er+ f"oat#n' %o#nt+ sc#ent#f#c7notat#on numer#c "#tera"s
str#n's (s#n'"e7 or doub"e7,uoted$
null
-
8/9/2019 Unit 4 - JSP-Part II
5/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
/ -L ar#ab"e names) "#e Jaa
Can conta#n "etters+ d#'#ts+ 8 + and 2
9ust not be'#n :#th a d#'#t
9ust not be resered :ords)
-
8/9/2019 Unit 4 - JSP-Part II
6/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
/ -L o%erators) ;e"at#ona") + =+ ==+ !=
/ ect notat#on$ arentheses for 'rou%#n'
-
8/9/2019 Unit 4 - JSP-Part II
7/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
/ -L automat#c ty%e coners#on
6dd#t#on #s a":ays numer#c #n -L (? does notre%resent str#n' concatenat#on$
-
8/9/2019 Unit 4 - JSP-Part II
8/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
/ -L %ro#des a number of #m%"#c#t ob>ects
/ 9ost of these ob>ects are re"ated to but
not the same asthe JS #m%"#c#t ob>ects JS #m%"#c#t ob>ects cannot be accessedd#rect"y by name #n an -L e%ress#on+ but canbe accessed #nd#rect"y as %ro%ert#es of one of
the -L #m%"#c#t ob>ects
-
8/9/2019 Unit 4 - JSP-Part II
9/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
-
8/9/2019 Unit 4 - JSP-Part II
10/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
#ageontet) %ro#des access to JS #m%"#c#tob>ects -) -L e%ress#on #ageontet.request#s
reference to the JS requestob>ect
#age) JS #m%"#c#t ob>ect re%resent#n' theser"et #tse"f
/ JS ob>ects #age+ request+ session+ and
a##li'ationa"" hae get/ttri0ute2andset/ttri0ute2methods These ob>ects store -L sco%ed ar#ab"es (e.g.+
visits$
-
8/9/2019 Unit 4 - JSP-Part II
11/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
/ @ar#ab"e reference such as visits:#th#n an -Le%ress#on #s reso"ed #n one of t:o :ays) If the ar#ab"e reference #s #dent#ca" to the name of
one of the -L #m%"#c#t ob>ects+ then the reference
ea"uates to the #nd#cated #m%"#c#t ob>ect. If the ar#ab"e reference does not match one of the -L
#m%"#c#t ob>ect names+ then the JS #m%"#c#t ob>ects#age+ request+ session+ and a##li'ationaresearched 7 #n that order for an attr#bute ha#n' the
'#en ar#ab"e name./ If not found+ a"ue #s null/ If found+ a"ue #s 304e't
JS automat#ca""y casts th#s a"ue as needed
-
8/9/2019 Unit 4 - JSP-Part II
12/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
/ 6"" -L #m%"#c#t ob>ects ece%t#ageontet#m%"ement Jaa 9a%#nterface
/ In -L+ can access 9a% us#n' array orob>ect notat#on) Ser"et) request.get5ara"eter6#172
-L)#ara"8#19,or#ara".#1
-
8/9/2019 Unit 4 - JSP-Part II
13/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
/ 6rray=L#st access)
If -L sco%ed ar#ab"e a:arre%resents/ Jaa arrayA or
4ava.util.;ist
and #f -L sco%ed ar#ab"e indecan be cast to#nte'er
then can access e"ements of a@ar by/ a@arB#nde
/ a@ar.#nde
-
8/9/2019 Unit 4 - JSP-Part II
14/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS -%ress#on Lan'ua'e (-L$
/ unct#on ca""
unct#on name fo""o:ed by %arenthes#Eed+comma7se%arated "#st of -L e%ress#on
ar'uments Ta' "#brar#es def#ne a"" funct#ons
unct#on names usua""y #nc"ude a names%ace
%ref# assoc#ated :#th the ta' "#brary
-
8/9/2019 Unit 4 - JSP-Part II
15/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
/ Three ty%es of maru% e"ements) Scr#%t#n'
/ -) s'ri#tlet
/ Inserts Jaa code #nto ser"et D#rect#e
/ -) dire'tive.#age
/ Instructs JS trans"ator
6ct#on/ Standard) %ro#ded by JS #tse"f
/ Custom) %ro#ded by a ta' "#brary such as JSTL
-
8/9/2019 Unit 4 - JSP-Part II
16/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
/ T:o JSF d#rect#es dire'tive.#ageA some attr#butes)
'ontent#e
session) falseto turn off use of session
ob>ecterror5age) re"at#e G;L of a%%"#cat#on resource
to be re,uested #f an uncau'ht ece%t#on occurs #nth#s JS document
isrror5age) true to access -L #m%"#c#te'e#tionob>ect
dire'tive.in'lude) #m%ort :e""7formedF9L
-
8/9/2019 Unit 4 - JSP-Part II
17/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
/ JSTL #s d##ded #nto seera" funct#ona" areas+each :#th #ts o:n names%ace)
Names%ace %ref# #s
H ta'"#b %ref#Kcur#Khtt%)==>aa.sun.com=>s%=>st"=core M
-
8/9/2019 Unit 4 - JSP-Part II
18/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
-
8/9/2019 Unit 4 - JSP-Part II
19/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
/ Common ar#ab"es)
var/ ;e%resents name of a sco%ed ar#ab"e that #s
ass#'ned to by the act#on/ 9ust be a str#n' "#tera"+ not an -L e%ress#on
s'o#e
/ S%ec#f#es sco%e of sco%ed ar#ab"e as one of the"#tera"s #age+ request+ session+ ora##li'ation
-
8/9/2019 Unit 4 - JSP-Part II
20/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
setact#on Sett#n' (and creat#n'$ a sco%ed ar#ab"e
Sett#n'=creat#n' an e"ement of %a#
/ 6ctua""y+ th#s fa#"s at run t#me #n J*SD 1.3 (:h#chtreats -L #m%"#c#t ob>ect %a#s as read7on"y$
9a%
ey
-
8/9/2019 Unit 4 - JSP-Part II
21/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
setact#on
/ H ta'"#bur#Khtt%)==>aa.sun.com=>s%=>st"=core%ref#Kc M
/ Hhtm"M HheadM Ht#t"eMHc)setM Ta'-am%"eH=t#t"eM H=headM
/ HbodyM Hc)set arKsa"ary
sco%eKsess#on a"ueK2O000PO5=MHc)out a"ueK2sa"ary5=M
/ H=bodyM H=htm"M
-
8/9/2019 Unit 4 - JSP-Part II
22/54
-
8/9/2019 Unit 4 - JSP-Part II
23/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
/ H ta'"#b ur#Khtt%)==>aa.sun.com=>s%=>st"=core%ref#Kc M Hhtm"M HheadM Ht#t"eMHc)remoeMTa' -am%"eH=t#t"eM H=headM
/ HbodyM Hc)set arKsa"ary sco%eKsess#ona"ueK2O000PO5=M H%Mefore ;emoe @a"ue)
/ Hc)out a"ueK2sa"ary5=M
/ H=%M
/ Hc)remoe arKsa"ary=M
/ H%M6fter ;emoe @a"ue) Hc)out a"ueK2
sa"ary5=MH=%M/ H=bodyM H=htm"M
-
8/9/2019 Unit 4 - JSP-Part II
24/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
outact#on Norma""y used to :r#te a str#n' to the outJS
#m%"#c#t ob>ect
6utomat#ca""y esca%es F9L s%ec#a"characters
If a"ue #s nullout%ut #s em%ty str#n'/
-
8/9/2019 Unit 4 - JSP-Part II
25/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
urlact#on valueattr#bute #s a G;L to be :r#tten to theoutJS #m%"#c#t ob>ect
G;L!s be'#nn#n' :#th are assumed re"at#eto contet %ath
#ara"e"ements can be used to def#ne
%arameters that :#"" be G;L encoded
-
8/9/2019 Unit 4 - JSP-Part II
26/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
/ 6"ternat#e to the valueattr#bute (setand #ara"e"ements$ If e"ement has content+ th#s #s %rocessed to
%roduce a Stringused for value
-en oute"ement :#"" %roduce str#n'+ not:r#te to the outob>ect
6ss#'ns a"ue of ar#ab"e"ess(F9L esca%ed$ tosco%ed ar#ab"e 'lean
-
8/9/2019 Unit 4 - JSP-Part II
27/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
ifact#on Qenera" form #nc"udes sco%ed ar#ab"e to rece#e
testa"ue
/ The aboe maru% creates (or sets+ #f ar#ab"e a"ready e#sts$a sco%ed ar#ab"e named test?esult#n the defau"t #agesco%e and+ #f the cond#t#on #s true+ sends the conta#nedcharacter data to the outob>ect
6ss#'ned oo"ean a"ue
of testattr#bute
-
8/9/2019 Unit 4 - JSP-Part II
28/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
H ta'"#bur#Khtt%)==>aa.sun.com=>s%=>st"=core
%ref#Kc M Hhtm"M HheadM Ht#t"eMHc)#fMTa' -am%"eH=t#t"eM H=headM
HbodyM
Hc)set arKsa"ary sco%eKsess#ona"ueK2O000PO5=M
Hc)#f testK2sa"ary M O0005M
H%M9y sa"ary #s) Hc)out a"ueK2sa"ary5=MH%M
H=c)#fM H=bodyM H=htm"M
-
8/9/2019 Unit 4 - JSP-Part II
29/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
'@ooseact#on
-
8/9/2019 Unit 4 - JSP-Part II
30/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
fora'@act#on Gsed to #ncrement a ar#ab"e (:r#tes O+ 4+ R+ & to the
out ob>ect$)
Gsed to #terate oer a data structure (#terates throu'h@eader-L #m%"#c#t ob>ect and out%uts a "#stconta#n#n' one #tem for each header f#e"d #n the TT
re,uest be#n' %rocessed$)
-
8/9/2019 Unit 4 - JSP-Part II
31/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
JS 9aru%
fora'@act#on Can #terate oer array+ %a#+ olle'tion+Gterator+ nu"eration
-"ements of 9a% are %a#.ntr+ :h#chsu%%ort ey and a"ue -L %ro%ert#es)
-
8/9/2019 Unit 4 - JSP-Part II
32/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Jaaeans C"asses
/ JSTL Core act#ons are des#'ned to beused for s#m%"e+ %resentat#on7or#ented%ro'ramm#n' tass
/ 9ore so%h#st#cated %ro'ramm#n' tassshou"d st#"" be %erformed :#th a "an'ua'esuch as Jaa
/ Jaaeanstechno"o'y a""o:s a JSdocument to ca"" Jaa methods
-
8/9/2019 Unit 4 - JSP-Part II
33/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Jaaeans C"asses
/ -am%"e
/ ;e,u#rements) Jaaeans c"ass must
e #u0li'and not a0stra't Conta#n at "east one simple property design pattern
method (def#ned "ater$
-
8/9/2019 Unit 4 - JSP-Part II
34/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Jaaeans C"asses
/ Gs#n' a Jaaeans c"ass #n JS
-
8/9/2019 Unit 4 - JSP-Part II
35/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Jaaeans C"asses
/ Gs#n' a Jaaeans c"ass as sho:n)
C"ass must hae a defau"t(no7ar'ument$constructorto be #nstant#ated by useHean
/ 6utomat#ca""y su%%"#ed by Jaa #n th#s eam%"e
C"ass shou"d be"on' to a %aca'e(ao#dsneed for an i"#ort$
/ Th#s c"ass :ou"d 'o #n DH-GIJ'lasses"d#rectory
/ Instance of a Jaaeans c"ass #s a bean
-
8/9/2019 Unit 4 - JSP-Part II
36/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Jaaeans C"asses
/ S#m%"e %ro%erty des#'n %atterns T:o ty%es) 'etter and setter
/ oth re,u#re that the method be #u0li'
/ 'etter) no ar'uments
returns a a"ue (#.e.+ cannot be o#d$
name be'#ns :#th get(or is+ #f return ty%e #s 0oolean$fo""o:ed by u%%er case "etter
/ setter) one ar'ument (same ty%e as 'etter return a"ue$
@o#d (#.e.+ must not return a a"ue$
name be'#ns :#th setfo""o:ed by u%%er case "etter
-
8/9/2019 Unit 4 - JSP-Part II
37/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Jaaeans C"asses
/ -L ca""s s#m%"e %ro%erty des#'n method #nres%onse to access of bean %ro%erty)
6ttem%t to read %ro%erty 'enerates ca"" to
assoc#ated get=ismethod (or error #f nonee#sts$
6ttem%t to ass#'n a"ue to %ro%erty 'enerates
ca"" to assoc#ated setmethod (or error$
-
8/9/2019 Unit 4 - JSP-Part II
38/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Jaaeans C"asses
/ -am%"e settermethod
/ Ca""#n' setter from JS
-
8/9/2019 Unit 4 - JSP-Part II
39/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Jaaeans C"asses
/ S#m%"e %ro%erty des#'n %attern methodsassoc#ate bean %ro%ert#es:#th beans Name of bean %ro%erty obta#ned by remo#n'
'et=#s=set method %ref# and fo""o:#n' the ru"e)/ If rema#n#n' name be'#ns :#th t:o or more u%%er
case "etters+ bean %ro%erty name #s rema#n#n'name) set/:alue2/:alue
/ If rema#n#n' name be'#ns :#th a s#n'"e u%%er case"etter+ bean %ro%erty name #s rema#n#n' name :#thth#s "etter conerted to "o:er case)getDel'o"e2Ael'o"e
-
8/9/2019 Unit 4 - JSP-Part II
40/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Instant#at#n' eans
/ eans can be #nstant#ated by a ser"et andmade aa#"ab"e to JS #a sco%e ob>ects
Ser"et
JS) no need for useHeanact#on
${sessionS'o#e.testHean.Ael'o"e}
-
8/9/2019 Unit 4 - JSP-Part II
41/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Instant#at#n' eans
useHeanon"y #nstant#ates a bean #f onedoes not a"ready e#st+ can o%t#ona""y%erform #n#t#a"#Eat#on
-a"uated on"y #f useean #nstant#ates Testean
-
8/9/2019 Unit 4 - JSP-Part II
42/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Gs#n' eans
/ -am%"e)mort'a'eca"cu"at#on
-
8/9/2019 Unit 4 - JSP-Part II
43/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Gs#n' eans
Ca"" to'etayment($method
-
8/9/2019 Unit 4 - JSP-Part II
44/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Jaa 6I ean ro%ert#es
/ 9any Jaa 6I methods conform tos#m%"e %ro%erty des#'n %atterns
/ Can usua""y treat as bean %ro%ert#es
-
8/9/2019 Unit 4 - JSP-Part II
45/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Ta' L#brar#es
/ *ou"dn!t #t be n#cer to :r#te the mort'a'ea%% as
-
8/9/2019 Unit 4 - JSP-Part II
46/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Ta' L#brar#esGsed so s#n'"e root
-
8/9/2019 Unit 4 - JSP-Part II
47/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
Ta' L#brar#es
/ "ace custom ta' def#n#t#on #n a ta' f#"eha#n' the name of the custom act#on
"ortgage.tag
/ "ace ta' f#"e #n a ta' "#brary(e.g.+d#rectory conta#n#n' ta' f#"es$
DH-GIJtags
/ 6dd names%ace dec"arat#on for ta' "#brary
-
8/9/2019 Unit 4 - JSP-Part II
48/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
9@C
/ 9any :eb a%%s are based on the 9ode"7@#e:7Contro""er (9@C$ arch#tecture %attern
Contro""er
9ode" Com%onents
@#e:
TTre,uest
TTres%onse
-
8/9/2019 Unit 4 - JSP-Part II
49/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
9@C
/ Ty%#ca" JS #m%"ementat#on of 9@C
Contro""er(Jaa ser"et$
9ode" Com%onents(beans+ D9S$
@#e:
(JS document$
TTre,uest
TTres%onse
-
8/9/2019 Unit 4 - JSP-Part II
50/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
9@C
/ or:ard#n' an TT re,uest from aser"et to another com%onent)
y G;L
y name
-) =e""oCounter.>s%
-
8/9/2019 Unit 4 - JSP-Part II
51/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
9@C
-
8/9/2019 Unit 4 - JSP-Part II
52/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
9@C
/ o: does the contro""er no: :h#chcom%onent to for:ard to
get5at@Gnfo2a"ue of G;L!s can be used
-am%"e)/ ser"et ma%%#n' %attern #n Ae0."l)
/ G;L ends :#th)get5at@Gnfo2returns)
-
8/9/2019 Unit 4 - JSP-Part II
53/54
Dr. Thomas Tran CSI3140 Lecture Notes based on Dr. Jeffre Jacson!s
9@C
/ JS in'ludeact#on (not the same as thein'luded#rect#eU$
-ecute s%ec#f#edcom%onent and#nc"ude #ts out%ut#n %"ace of the#nc"ude e"ement
-
8/9/2019 Unit 4 - JSP-Part II
54/54
9@C
/ 6dd#n' %arameters to the requestob>ectseen by an #nc"uded com%onent)
requestob>ect seen by nav0ar.4s#:#"" #nc"ude%arameter named 'urrent5age:#th a"ue @o"e