Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Basic Java

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 385

J2SE

NOIDA:- D-108, Sector-2, Noida. Web:- www.cmcnoida.com Delhi Preet Vihar:- G-18, Preet Vihar. Web:- www.cmcvika mar!.com Faridabad:- "ama Pa#ace, $$nd %#oor, &''. Nee#am %#(over, )*ronda +howk, %aridabad. Web:- www.cmc,aridabad.com, Ph. No.:- -...//0201

JAVA OVERVIEW
+2+ 3imited i a S4b idiar( o, 5+S 3imited 1

6ava 'ro!rammin! #an!4a!e wa ori!ina##( deve#o'ed b( S4n 2icro ( tem , which wa initiated b( 6ame Go #in! and re#ea ed in 1--. a core com'onent o, S4n 2icro ( tem .S4n 6ava '#at,orm 76ava 1.0 862S9:;. ) o, December 08 the #ate t re#ea e o, the 6ava Standard 9dition i / 762S9;. With the advancement o, 6ava and it wide 'read 'o'4#arit(, m4#ti'#e con,i!4ration were b4i#t to 4ite vario4 t('e o, '#at,orm . 9<: 6299 ,or 9nter'ri e )''#ication , 6229 ,or 2obi#e )''#ication . S4n 2icro ( tem ha renamed the new 62 ver ion a 6ava S9, 6ava 99 and 6ava 29 re 'ective#(. 6ava i !4aranteed to be Write Once, Run Anywhere 6ava i : Object Oriented : $n *ava ever(thin! i an &b*ect. 6ava can be ea i#( e<tended ince it i ba ed on the &b*ect mode#. Platf r! inde"endent: =n#ike man( other 'ro!rammin! #an!4a!e inc#4din! + and +>> when 6ava i com'i#ed, it i not com'i#ed into '#at,orm 'eci,ic machine, rather into '#at,orm inde'endent b(te code. 5hi b(te code i di trib4ted over the web and inter'reted b( virt4a# 2achine 76V2; on whichever '#at,orm it i bein! r4n. #i!"le :6ava i de i!ned to be ea ( to #earn. $, (o4 4nder tand the ba ic conce't o, &&P *ava wo4#d be ea ( to ma ter. #ecure : With 6ava? ec4re ,eat4re it enab#e to deve#o' vir4 -,ree, tam'er-,ree ( tem . )4thentication techni@4e are ba ed on '4b#ic-ke( encr('tion. Architectural$ neutral :6ava com'i#er !enerate an architect4re-ne4tra# ob*ect ,i#e ,ormat which make the com'i#ed code to be e<ec4tab#e on man( 'roce or , with the 're ence 6ava r4ntime ( tem. P rtable :bein! architect4ra# ne4tra# and havin! no im'#ementation de'endent a 'ect o, the 'eci,ication make 6ava 'ortab#e. +om'i#er and 6ava i written in )NS$ + with a c#ean 'ortabi#it( bo4ndar( which i a P&S$A 4b et. R bu%t :6ava make an e,,ort to e#iminate error 'rone it4ation b( em'ha iBin! main#( on com'i#e time error checkin! and r4ntime checkin!. &ulti$threaded : With 6ava? m4#ti-threaded ,eat4re it i 'o ib#e to write 'ro!ram that can do man( ta k im4#taneo4 #(. 5hi de i!n ,eat4re a##ow deve#o'er to con tr4ct mooth#( r4nnin! interactive a''#ication . Inter"reted :6ava b(te code i tran #ated on the ,#( to native machine in tr4ction and i not tored an(where. 5he deve#o'ment 'roce i more ra'id and ana#(tica# ince the #inkin! i an incrementa# and #i!ht wei!ht 'roce . 'i(h Perf r!ance: With the 4 e o, 64 t-$n-5ime com'i#er 6ava enab#e hi!h 'er,ormance. Di%tributed :6ava i de i!ned ,or the di trib4ted environment o, the internet. Dyna!ic : 6ava i con idered to be more d(namic than + or +>> ince it i de i!ned to ada't to an evo#vin! environment. 6ava 'ro!ram can carr( e<ten ive amo4nt o, r4n-time in,ormation that can be 4 ed to veri,( and re o#ve acce e to ob*ect on r4n-time. 'i%t ry f Ja)a: 6ame Go #in! initiated the 6ava #an!4a!e 'ro*ect in 64ne 1--1 ,or 4 e in one o, hi man( et-to' bo< 'ro*ect . 5he #an!4a!e, initia##( ca##ed &ak a,ter an oak tree that tood o4t ide Go #in!? o,,ice, a# o went b( the name Green and ended 4' #ater renamed a 6ava, ,rom a #i t o, random word . S4n re#ea ed the ,ir t '4b#ic im'#ementation a 6ava 1.0 in 1--.. $t 'romi ed Write Once, Run Anywhere 7W&");, 'rovidin! no-co t r4n-time on 'o'4#ar '#at,orm . &n 11 November 200/, S4n re#ea ed m4ch o, 6ava a ,ree and o'en o4rce o,tware 4nder the term o, the GN= Genera# P4b#ic 3icen e 7GP3;. &n 8 2a( 2000 S4n ,ini hed the 'roce , makin! a## o, 6ava? core code ,ree and o'en- o4rce, a ide ,rom a ma## 'ortion o, code to which S4n did not ho#d the co'(ri!ht. * l% y u will need: %or 'er,ormin! the e<am'#e di c4 ed in thi t4toria#, (o4 wi## need a Penti4m 200-2CB com'4ter with a minim4m o, /D 2E o, ")2 7128 2E o, ")2 recommended;. Fo4 a# o wi## need the ,o##owin! o,tware : 3in4< 0.1 or Window -.G-8G2000GAP o'eratin! ( tem. 6ava 6DH . 2icro o,t Note'ad or an( other te<t editor 5hi t4toria# wi## 'rovide the nece ar( ki## to create G=$, networkin!, and Web a''#ication 4 in! 6ava. +2+ 3imited i a S4b idiar( o, 5+S 3imited 2

Ja)a En)ir n!ent #etu" Ee,ore we 'roceed ,4rther it i im'ortant that we et 4' the *ava environment correct#(. 5hi ection !4ide (o4 on how to down#oad and et 4' 6ava on (o4r machine. P#ea e ,o##ow the ,o##owin! te' to et 4' the environment. 6ava S9 i ,ree#( avai#ab#e ,rom the #ink Down#oad 6ava. So (o4 down#oad a ver ion ba ed on (o4r o'eratin! ( tem. %o##ow the in tr4ction to down#oad *ava and r4n the +e,e to in ta## 6ava on (o4r machine. &nce (o4 in ta##ed 6ava on (o4r machine, (o4 wo4#d need to et environment variab#e to 'oint to correct in ta##ation directorie : #ettin( u" the "ath f r wind w% -.../0P: ) 4min! (o4 have in ta##ed 6ava in c:\Program Files\java\jdk director(: "i!ht-c#ick on ?2( +om'4ter? and e#ect ?Pro'ertie ?. +#ick on the ?9nvironment variab#e ? b4tton 4nder the ?)dvanced? tab. Now a#ter the ?Path? variab#e o that it a# o contain the 'ath to the 6ava e<ec4tab#e. 9<am'#e, i, the 'ath i c4rrent#( et to ?+:IW$ND&WSISFS59212?, then chan!e (o4r 'ath to read ?+:IW$ND&WSISFS59212Jc:IPro!ram %i#e I*avaI*dkIbin?. #ettin( u" the "ath f r wind w% 12/13/&E: ) 4min! (o4 have in ta##ed 6ava in c:\Program Files\java\jdk director(: 9dit the ?+:Ia4toe<ec.bat? ,i#e and add the ,o##owin! #ine at the end: ?S95 P)5CKLP)5CLJ+:IPro!ram %i#e I*avaI*dkIbin? #ettin( u" the "ath f r 4inu,, 5NI0, # lari%, Free6#D: 9nvironment variab#e P)5C ho4#d be et to 'oint to where the *ava binarie have been in ta##ed. "e,er to (o4r he## doc4mentation i, (o4 have tro4b#e doin! thi . 9<am'#e, i, (o4 4 e bash a (o4r he##, then (o4 wo4#d add the ,o##owin! #ine to the end o, (o4r ?.ba hrc: e<'ort P)5CKG'athGtoG*ava:MP)5C? P "ular Ja)a Edit r%: 5o write (o4r *ava 'ro!ram (o4 wi## need a te<t editor. 5here are even more o'hi ticated $D9 avai#ab#e in the market. E4t ,or now, (o4 can con ider one o, the ,o##owin!: N te"ad : &n Window machine (o4 can 4 e an( im'#e te<t editor #ike Note'ad 7"ecommended ,or thi t4toria#;, 5e<tPad. Netbean% :i a 6ava $D9 that i o'en o4rce and ,ree which can be down#oaded ,rom Ecli"%e : i a# o a *ava $D9 deve#o'ed b( the ec#i' e o'en o4rce comm4nit( and can be down#oaded ,rom Ja)a 6a%ic #ynta, When we con ider a 6ava 'ro!ram it can be de,ined a a co##ection o, ob*ect that comm4nicate via invokin! each other method . 3et 4 now brie,#( #ook into what do c#a , ob*ect, method and in tance variab#e mean. Object $ &b*ect have tate and behavior . 9<am'#e: ) do! ha tate -co#or, name, breed a we## a behavior -wa!!in!, barkin!, eatin!. )n ob*ect i an in tance o, a c#a . 7la%% $ ) c#a can be de,ined a a tem'#ateG b#4e 'rint that de cribe the behavior G tate that ob*ect o, it t('e 4''ort. &eth d% $ ) method i ba ica##( a behavior. ) c#a can contain man( method . $t i in method where the #o!ic are written, data i mani'4#ated and a## the action are e<ec4ted. In%tance Variable% $ 9ach ob*ect ha it 4ni@4e et o, in tance variab#e . )n ob*ect? tate i created b( the va#4e a i!ned to the e in tance variab#e . Fir%t Ja)a Pr (ra!: 3et 4 #ook at a im'#e code that wo4#d 'rint the word Hello World. '4b#ic c#a 2(%ir t6avaPro!ram N GO 5hi i m( ,ir t *ava 'ro!ram. O 5hi wi## 'rint ?Ce##o Wor#d? a the o4t'4t OG '4b#ic tatic void main7Strin! 8:ar! ; N S( tem.o4t.'rint#n7PCe##o Wor#dP;J GG 'rint Ce##o Wor#d Q Q 3et #ook at how to ave the ,i#e, com'i#e and r4n the 'ro!ram. P#ea e ,o##ow the te' !iven be#ow: +2+ 3imited i a S4b idiar( o, 5+S 3imited

&'en note'ad and add the code a above. Save the ,i#e a : 2(%ir t6avaPro!ram.*ava. &'en a command 'rom't window and !o o the director( where (o4 aved the c#a . ) 4me it +:I. 5('e ? *avac 2(%ir t6avaPro!ram.*ava ? and 're enter to com'i#e (o4r code. $, there are no error in (o4r code the command 'rom't wi## take (o4 to the ne<t #ine.7 ) 4m'tion : 5he 'ath variab#e i et;. Now t('e ? *ava 2(%ir t6avaPro!ram ? to r4n (o4r 'ro!ram. Fo4 wi## be ab#e to ee ? Ce##o Wor#d ? 'rinted on the window. + : R *avac 2(%ir t6avaPro!ram.*ava + : R *ava 2(%ir t6avaPro!ram Ce##o Wor#d 6a%ic #ynta,: )bo4t 6ava 'ro!ram , it i ver( im'ortant to kee' in mind the ,o##owin! 'oint . 7a%e #en%iti)ity $ 6ava i ca e en itive which mean identi,ier 'ell and hell wo4#d have di,,erent meanin! in 6ava. 7la%% Na!e% $ %or a## c#a name the ,ir t #etter ho4#d be in =''er +a e. $, evera# word are 4 ed to ,orm a name o, the c#a each inner word ,ir t #etter ho4#d be in =''er +a e. 9<am'#e class MyFirstJavaClass &eth d Na!e% $ )## method name ho4#d tart with a 3ower +a e #etter. $, evera# word are 4 ed to ,orm the name o, the method, then each inner word? ,ir t #etter ho4#d be in =''er +a e. 9<am'#e public void myMethodName ! Pr (ra! File Na!e $ Name o, the 'ro!ram ,i#e ho4#d e<act#( match the c#a name. When avin! the ,i#e (o4 ho4#d ave it 4 in! the c#a name 7"emember *ava i ca e en itive; and a''end ?.*ava? to the end o, the name. 7i, the ,i#e name and the c#a name do not match (o4r 'ro!ram wi## not com'i#e;. 9<am'#e : ) 4me ?2(%ir t6avaPro!ram? i the c#a name. 5hen the ,i#e ho4#d be aved a "MyFirstJavaProgram#java" "ublic %tatic ) id !ain8#trin( ar(%9:; $ *ava 'ro!ram 'roce in! tart ,rom the main7; method which i a mandator( 'art o, ever( *ava 'ro!ram.. Ja)a Identifier%: )## *ava com'onent re@4ire name . Name 4 ed ,or c#a e , variab#e and method are ca##ed identi,ier . $n *ava there are evera# 'oint to remember abo4t identi,ier . 5he( are a ,o##ow : )## identi,ier ho4#d be!in with a #etter 7) to S or a to B ;, c4rrenc( character 7M; or an 4nder core 7T;. ),ter the ,ir t character identi,ier can have an( combination o, character . ) ke( word cannot be 4 ed a an identi,ier. 2o t im'ortant#( identi,ier are ca e en itive. 9<am'#e o, #e!a# identi,ier :a!e, M a#ar(, Tva#4e, TT1Tva#4e 9<am'#e o, i##e!a# identi,ier : 121abc, - a#ar( Ja)a & difier%: 3ike other #an!4a!e it i 'o ib#e to modi,( c#a e , method etc b( 4 in! modi,ier . 5here are two cate!orie o, modi,ier . Acce%% & difier% : de,a4#t, '4b#ic , 'rotected, 'rivate N n$acce%% & difier% : ,ina#, ab tract, trict,' We wi## be #ookin! into more detai# abo4t modi,ier in the ne<t ection. Ja)a Variable%: We wo4#d ee ,o##owin! t('e o, variab#e in 6ava: 3oca# Variab#e +#a Variab#e 7Static Variab#e ; $n tance Variab#e 7Non tatic variab#e ; Ja)a Array%: )rra( are ob*ect that tore m4#ti'#e variab#e o, the ame t('e. Cowever an )rra( it e#, i an ob*ect on the hea'. We wi## #ook into how to dec#are, con tr4ct and initia#iBe in the 4'comin! cha'ter . +2+ 3imited i a S4b idiar( o, 5+S 3imited D

Ja)a Enu!%: 9n4m were introd4ced in *ava ..0. 9n4m re trict a variab#e to have one o, on#( a ,ew 'rede,ined va#4e . 5he va#4e in thi en4merated #i t are ca##ed en4m . With the 4 e o, en4m it i 'o ib#e to red4ce the n4mber o, b4! in (o4r code. %or e<am'#e i, we con ider an a''#ication ,or a ,re h *4ice ho' it wo4#d be 'o ib#e to re trict the !#a iBe to ma##, medi4m and 3ar!e. 5hi wo4#d make 4re that it wo4#d not a##ow an(one to order an( iBe other than the ma##, medi4m or #ar!e. E,a!"le: c#a %re h64ice N en4m %re h64iceSiBeN S2)33, 29D=$2, 3)"G9 Q %re h64iceSiBe iBeJ Q '4b#ic c#a %re h64ice5e t N

'4b#ic tatic void main7Strin! ar! 8:;N %re h64ice *4ice K new %re h64ice7;J *4ice. iBe K %re h64ice. %re h64iceSiBe.29D=$2 J Q Q N te: en4m can be dec#ared a their own or in ide a c#a . 2ethod , variab#e , con tr4ctor can be de,ined in ide en4m a we##. Ja)a <eyw rd%: 5he ,o##owin! #i t how the re erved word in 6ava. 5he e re erved word ma( not be 4 ed a con tant or variab#e or an( other identi,ier name . ab trac a ert boo#ean break t b(te ca e catch char contin4 c#a con t de,a4#t e do do4b#e e# e en4m e<tend ,ina# ,ina##( ,#oat im'#ement ,or !oto i, im'ort in tanceo, int inter,ace #on! native new 'acka!e 'rivate 'rotected '4b#ic ret4rn hort tatic trict,' 4'er (nchroniBe witch thi throw d throw tran ient tr( void vo#ati#e whi#e 7 !!ent% in Ja)a 6ava 4''ort in!#e #ine and m4#ti-#ine comment ver( imi#ar to c and c>>. )## character avai#ab#e in ide an( comment are i!nored b( 6ava com'i#er. '4b#ic c#a 2(%ir t6avaPro!ramN GO 5hi i m( ,ir t *ava 'ro!ram. O 5hi wi## 'rint ?Ce##o Wor#d? a the o4t'4t O 5hi i an e<am'#e o, m4#ti-#ine comment . OG '4b#ic tatic void main7Strin! 8:ar! ;N GG 5hi i an e<am'#e o, in!#e #ine comment GO 5hi i a# o an e<am'#e o, in!#e #ine comment. OG +2+ 3imited i a S4b idiar( o, 5+S 3imited .

S( tem.o4t.'rint#n7PCe##o Wor#dP;J Q Q 5%in( 6lan= 4ine%: ) #ine containin! on#( white 'ace, 'o ib#( with a comment, i known a a b#ank #ine, and 6ava tota##( i!nore it. Inheritance: $n *ava c#a e can be derived ,rom c#a e . Ea ica##( i, (o4 need to create a new c#a and here i a#read( a c#a that ha ome o, the code (o4 re@4ire, then it i 'o ib#e to derive (o4r new c#a ,rom the a#read( e<i tin! code. 5hi conce't a##ow (o4 to re4 e the ,ie#d and method o, the e<i tin! c#a with o4t havin! to rewrite the code in a new c#a . $n thi cenario the e<i tin! c#a i ca##ed the 4'er c#a and the derived c#a i ca##ed the 4bc#a . Interface%: $n 6ava #an!4a!e an inter,ace can be de,ined a a contract between ob*ect on how to comm4nicate with each other. $nter,ace '#a( a vita# ro#e when it come to the conce't o, inheritance. )n inter,ace de,ine the method , a derivin! c#a 7 4bc#a ; ho4#d 4 e. E4t the im'#ementation o, the method i tota##( 4' to the 4bc#a . Ja)a $ Object% and 7la%%e% 6ava i an &b*ect &riented 3an!4a!e. ) a #an!4a!e that ha the &b*ect &riented ,eat4re 6ava 4''ort the ,o##owin! ,4ndamenta# conce't : Po#(mor'hi m $nheritance 9nca' 4#ation )b traction +#a e &b*ect $n tance 2ethod 2e a!e Par in! $n thi cha'ter we wi## #ook into the conce't +#a e and &b*ect . Object $ &b*ect have tate and behavior . 9<am'#e: ) do! ha tate -co#or, name, breed a we## a behavior -wa!!in!, barkin!, eatin!. )n ob*ect i an in tance o, a c#a . 7la%% $ ) c#a can be de,ined a a tem'#ateG b#4e 'rint that de cribe the behavior G tate that ob*ect o, it t('e 4''ort. Object% in Ja)a: 3et 4 now #ook dee' into what are ob*ect . $, we con ider the rea#-wor#d we can ,ind man( ob*ect aro4nd 4 , +ar , Do! , C4man etc. )## the e ob*ect have a tate and behavior. $, we con ider a do! then it tate i - name, breed, co#or, and the behavior i - barkin!, wa!!in!, r4nnin! $, (o4 com'are the o,tware ob*ect with a rea# wor#d ob*ect, the( have ver( imi#ar characteri tic . So,tware ob*ect a# o have a tate and behavior. ) o,tware ob*ect? tate i tored in ,ie#d and behavior i hown via method . So in o,tware deve#o'ment method o'erate on the interna# tate o, an ob*ect and the ob*ect-to-ob*ect comm4nication i done via method . 7la%%e% in Ja)a: ) c#a i a b#4e 'rint ,rom which individ4a# ob*ect are created. ) am'#e o, a c#a i !iven be#ow: '4b#ic c#a Do!N Strin! breedJ int a!eJ Strin! co#orJ void barkin!7;N Q void h4n!r(7;N Q +2+ 3imited i a S4b idiar( o, 5+S 3imited /

void #ee'in!7;N Q Q ) c#a

can contain an( o, the ,o##owin! variab#e t('e . 4 cal )ariable% + variab#e de,ined in ide method , con tr4ctor or b#ock are ca##ed #oca# variab#e . 5he variab#e wi## be dec#ared and initia#iBed within the method and the variab#e wi## be de tro(ed when the method ha com'#eted. In%tance )ariable% + $n tance variab#e are variab#e within a c#a b4t o4t ide an( method. 5he e variab#e are in tantiated when the c#a i #oaded. $n tance variab#e can be acce ed ,rom in ide an( method, con tr4ctor or b#ock o, that 'artic4#ar c#a . 7la%% )ariable% + +#a variab#e are variab#e dec#ared with in a c#a , o4t ide an( method, with the tatic ke(word. ) c#a can have an( n4mber o, method to acce the va#4e o, vario4 kind o, method . $n the above e<am'#e, barkin!7;, h4n!r(7; and #ee'in!7; are method . Ee#ow mentioned are ome o, the im'ortant to'ic that need to be di c4 ed when #ookin! into c#a e o, the 6ava 3an!4a!e. 7 n%truct r%: When di c4 in! abo4t c#a e one o, the mo t im'ortant 4b to'ic wo4#d be con tr4ctor . 9ver( c#a ha a con tr4ctor. $, we do not e<'#icit#( write a con tr4ctor ,or a c#a the *ava com'i#er b4i#d a de,a4#t con tr4ctor ,or that c#a . 9ach time a new ob*ect i created at #ea t one con tr4ctor wi## be invoked. 5he main r4#e o, con tr4ctor i that the( ho4#d have the ame name a the c#a . ) c#a can have more than one con tr4ctor. 9<am'#e o, a con tr4ctor i !iven be#ow: '4b#ic c#a P4''(N '4b#ic '4''(7;N Q '4b#ic '4''(7Strin! name;N GG 5hi con tr4ctor ha one 'arameter, $ame. Q Q 6ava a# o 4''ort Sin!#eton +#a e where (o4 wo4#d be ab#e to create on#( one in tance o, a c#a . 7reatin( an Object: ) mentioned 'revio4 #( a c#a 'rovide the b#4e'rint ,or ob*ect . So ba ica##( an ob*ect i created ,rom a c#a . $n *ava the new ke( word i 4 ed to create new ob*ect . 5here are three te' when creatin! an ob*ect ,rom a c#a : Declarati n + ) variab#e dec#aration with a variab#e name with an ob*ect t('e. In%tantiati n + 5he ?new? ke( word i 4 ed to create the ob*ect. Initiali>ati n + 5he ?new? ke(word i ,o##owed b( a ca## to a con tr4ctor. 5hi ca## initia#iBe the new ob*ect. 9<am'#e o, creatin! an ob*ect i !iven be#ow: '4b#ic c#a P4''(N '4b#ic P4''(7Strin! name;N GG 5hi con tr4ctor ha one 'arameter, $ame. S( tem.o4t.'rint#n7PPa ed Name i :P > name ;J Q '4b#ic tatic void main7Strin! 8:ar! ;N GG %o##owin! tatement wo4#d create an ob*ect m(P4''( P4''( m(P4''( K new P4''(7 Ptomm(P ;J Q Q $, we com'i#e and r4n the above 'ro!ram then it wo4#d 'rod4ce ,o##owin! re 4#t: Pa ed Name i :tomm( Acce%%in( In%tance Variable% and &eth d%: $n tance variab#e and method are acce ed via created ob*ect . 5o acce an in tance variab#e the ,4##( @4a#i,ied 'ath ho4#d be a ,o##ow : +2+ 3imited i a S4b idiar( o, 5+S 3imited 0

GO %ir t create an ob*ect OG &b*ect"e,erence K new +on tr4ctor7;J GO Now ca## a variab#e a ,o##ow OG &b*ect"e,erence.variab#eNameJ GO Now (o4 can ca## a c#a method a ,o##ow OG &b*ect"e,erence.2ethodName7;J E,a!"le: 5hi e<am'#e e<'#ain how to acce in tance variab#e and method o, a c#a : '4b#ic c#a P4''(N int '4''()!eJ '4b#ic P4''(7Strin! name;N GG 5hi con tr4ctor ha one 'arameter, $ame. S( tem.o4t.'rint#n7PPa ed Name i :P > name ;J Q '4b#ic void et)!e7 int a!e ;N '4''()!e K a!eJ Q '4b#ic int !et)!e7 ;N S( tem.o4t.'rint#n7PP4''(? a!e i :P > '4''()!e ;J ret4rn '4''()!eJ Q '4b#ic tatic void main7Strin! 8:ar! ;N GO &b*ect creation OG P4''( m(P4''( K new P4''(7 Ptomm(P ;J GO +a## c#a method to et '4''(? a!e OG m(P4''(. et)!e7 2 ;J GO +a## another c#a method to !et '4''(? a!e OG m(P4''(.!et)!e7 ;J GO Fo4 can acce in tance variab#e a ,o##ow a we## OG S( tem.o4t.'rint#n7PVariab#e Va#4e :P > m(P4''(.'4''()!e ;J Q Q $, we com'i#e and r4n the above 'ro!ram then it wo4#d 'rod4ce ,o##owin! re 4#t: Pa ed Name i :tomm( P4''(? a!e i :2 Variab#e Va#4e :2 # urce file declarati n rule%: ) the #a t 'art o, thi ection #et 4 now #ook into the o4rce ,i#e dec#aration r4#e . 5he e r4#e are e entia# when dec#arin! c#a e , import tatement and package tatement in a o4rce ,i#e. 5here can be on#( one '4b#ic c#a 'er o4rce ,i#e. ) o4rce ,i#e can have m4#ti'#e non '4b#ic c#a e . 5he '4b#ic c#a name ho4#d be the name o, the o4rce ,i#e a we## which ho4#d be a''ended b( +ja)a at the end. %or e<am'#e : 5he c#a name i . public class %mployee&' 5hen the o4rce ,i#e ho4#d be a 9m'#o(ee.*ava. $, the c#a i de,ined in ide a 'acka!e, then the 'acka!e tatement ho4#d be the ,ir t tatement in the o4rce ,i#e. $, im'ort tatement are 're ent then the( m4 t be written between the 'acka!e tatement and the c#a dec#aration. $, there are no 'acka!e tatement then the im'ort tatement ho4#d be the ,ir t #ine in the o4rce ,i#e. +2+ 3imited i a S4b idiar( o, 5+S 3imited 8

$m'ort and 'acka!e tatement wi## im'#( to a## the c#a e 're ent in the o4rce ,i#e. $t i not 'o ib#e to dec#are di,,erent im'ort andGor 'acka!e tatement to di,,erent c#a e in the o4rce ,i#e. +#a e have evera# acce #eve# and there are di,,erent t('e o, c#a e J ab tract c#a e , ,ina# c#a e etc. $ wi## be e<'#ainin! abo4t a## the e in the acce modi,ier cha'ter. )'art ,rom the above mentioned t('e o, c#a e , 6ava a# o ha ome 'ecia# c#a e ca##ed $nner c#a e and )non(mo4 c#a e . Ja)a Pac=a(e: $n im'#e it i a wa( o, cate!oriBin! the c#a e and inter,ace . When deve#o'in! a''#ication in 6ava, h4ndred o, c#a e and inter,ace wi## be written, there,ore cate!oriBin! the e c#a e i a m4 t a we## a make #i,e m4ch ea ier. I!" rt %tate!ent%: $n *ava i, a ,4##( @4a#i,ied name, which inc#4de the 'acka!e and the c#a name, i !iven then the com'i#er can ea i#( #ocate the o4rce code or c#a e . $m'ort tatement i a wa( o, !ivin! the 'ro'er #ocation ,or the com'i#er to ,ind that 'artic4#ar c#a . %or e<am'#e ,o##owin! #ine wo4#d a k com'i#er to #oad a## the c#a e avai#ab#e in director( *avaTin ta##ationG*avaGio : im'ort *ava.io.OJ A #i!"le 7a%e #tudy: %or o4r ca e t4d( we wi## be creatin! two c#a e . 5he( are 9m'#o(ee and 9m'#o(ee5e t. %ir t o'en note'ad and add the ,o##owin! code. "emember thi i the 9m'#o(ee c#a and the c#a i a '4b#ic c#a . Now ave thi o4rce ,i#e with the name 9m'#o(ee.*ava. 5he 9m'#o(ee c#a ha ,o4r c#a variab#e name, a!e, de i!nation and a#ar(. 5he c#a ha one e<'#icit#( de,ined con tr4ctor which take a 'arameter. im'ort *ava.io.OJ '4b#ic c#a 9m'#o(eeN Strin! nameJ int a!eJ Strin! de i!nationJ do4b#e a#ar(J GG 5hi i the con tr4ctor o, the c#a 9m'#o(ee '4b#ic 9m'#o(ee7Strin! name;N thi .name K nameJ Q GG ) i!n the a!e o, the 9m'#o(ee to the variab#e a!e. '4b#ic void em')!e7int em')!e;N a!e K em')!eJ Q GO ) i!n the de i!nation to the variab#e de i!nation.OG '4b#ic void em'De i!nation7Strin! em'De i!;N de i!nation K em'De i!J Q GO ) i!n the a#ar( to the variab#e a#ar(.OG '4b#ic void em'Sa#ar(7do4b#e em'Sa#ar(;N a#ar( K em'Sa#ar(J Q GO Print the 9m'#o(ee detai# OG '4b#ic void 'rint9m'#o(ee7;N S( tem.o4t.'rint#n7PName:P> name ;J S( tem.o4t.'rint#n7P)!e:P > a!e ;J S( tem.o4t.'rint#n7PDe i!nation:P > de i!nation ;J S( tem.o4t.'rint#n7PSa#ar(:P > a#ar(;J Q Q ) mentioned 'revio4 #( in thi t4toria# 'roce in! tart ,rom the main method. 5here,ore in-order ,or 4 to r4n thi 9m'#o(ee c#a there ho4#d be main method and ob*ect ho4#d be created. We wi## be creatin! a e'arate c#a ,or the e ta k . +2+ 3imited i a S4b idiar( o, 5+S 3imited -

Given be#ow i the %mployee(est c#a which create two in tance o, the c#a the method ,or each ob*ect to a i!n va#4e ,or each variab#e. Save the ,o##owin! code in 9m'#o(ee5e t.*ava ,i#e im'ort *ava.io.OJ '4b#ic c#a 9m'#o(ee5e tN '4b#ic tatic void main7Strin! ar! 8:;N GO +reate two ob*ect 4 in! con tr4ctor OG 9m'#o(ee em'&ne K new 9m'#o(ee7P6ame SmithP;J 9m'#o(ee em'5wo K new 9m'#o(ee7P2ar( )nneP;J GG $nvokin! method ,or each ob*ect created em'&ne.em')!e72/;J em'&ne.em'De i!nation7PSenior So,tware 9n!ineerP;J em'&ne.em'Sa#ar(71000;J em'&ne.'rint9m'#o(ee7;J em'5wo.em')!e721;J em'5wo.em'De i!nation7PSo,tware 9n!ineerP;J em'5wo.em'Sa#ar(7.00;J em'5wo.'rint9m'#o(ee7;J

9m'#o(ee and invoke

Q Q Now com'i#e both the c#a e and then r4n %mployee(est to ee the re 4#t a ,o##ow : + :R *avac 9m'#o(ee.*ava + :R vi 9m'#o(ee5e t.*ava + :R *avac 9m'#o(ee5e t.*ava + :R *ava 9m'#o(ee5e t Name:6ame Smith )!e:2/ De i!nation:Senior So,tware 9n!ineer Sa#ar(:1000.0 Name:2ar( )nne )!e:21 De i!nation:So,tware 9n!ineer Sa#ar(:.00.0 Ja)a 6a%ic Data *y"e% Variab#e are nothin! b4t re erved memor( #ocation to tore va#4e . 5hi mean that when (o4 create a variab#e (o4 re erve ome 'ace in memor(. Ea ed on the data t('e o, a variab#e, the o'eratin! ( tem a##ocate memor( and decide what can be tored in the re erved memor(. 5here,ore, b( a i!nin! di,,erent data t('e to variab#e , (o4 can tore inte!er , decima# , or character in the e variab#e . 5here are two data t('e avai#ab#e in 6ava: Primitive Data 5('e "e,erenceG&b*ect Data 5('e Pri!iti)e Data *y"e%: 5here are ei!ht 'rimitive data t('e 4''orted b( 6ava. Primitive data t('e are 'rede,ined b( the #an!4a!e and named b( a ke( word. 3et 4 now #ook into detai# abo4t the ei!ht 'rimitive data t('e . byte: E(te data t('e i a 8-bit i!ned two? com'#ement inte!er. 2inim4m va#4e i -128 7-2U0; 2a<im4m va#4e i 120 7inc#4 ive;72U0 -1; De,a4#t va#4e i 0 E(te data t('e i 4 ed to ave 'ace in #ar!e arra( , main#( in '#ace o, inte!er , ince a b(te i ,o4r time ma##er than an int. 9<am'#e : b(te a K 100 , b(te b K -.0 %h rt: Short data t('e i a 1/-bit i!ned two? com'#ement inte!er. +2+ 3imited i a S4b idiar( o, 5+S 3imited 10

2inim4m va#4e i -12,0/8 7-2U1.; 2a<im4m va#4e i 12,0/07inc#4 ive; 72U1. -1; Short data t('e can a# o be 4 ed to ave memor( a b(te data t('e. ) hort i 2 time than an int De,a4#t va#4e i 0. 9<am'#e : hort K 10000 , hort r K -20000 $nt data t('e i a 12-bit i!ned two? com'#ement inte!er. 2inim4m va#4e i - 2,1D0,D81,/D8.7-2U11; 2a<im4m va#4e i 2,1D0,D81,/D07inc#4 ive;.72U11 -1; $nt i !enera##( 4 ed a the de,a4#t data t('e ,or inte!ra# va#4e 4n#e memor(. 5he de,a4#t va#4e i 0. 9<am'#e : int a K 100000, int b K -200000 3on! data t('e i a /D-bit i!ned two? com'#ement inte!er. 2inim4m va#4e i --,221,102,01/,8.D,00.,808.7-2U/1; 2a<im4m va#4e i -,221,102,01/,8.D,00.,800 7inc#4 ive;. 72U/1 -1; 5hi t('e i 4 ed when a wider ran!e than int i needed. De,a4#t va#4e i 03. 9<am'#e : #on! a K 1000003, int b K -2000003

ma##er

int:

there i a concern abo4t

l n(:

fl at: %#oat data t('e i a in!#e-'reci ion 12-bit $999 0.D ,#oatin! 'oint. %#oat i main#( 4 ed to ave memor( in #ar!e arra( o, ,#oatin! 'oint n4mber . De,a4#t va#4e i 0.0,. %#oat data t('e i never 4 ed ,or 'reci e va#4e 4ch a c4rrenc(. 9<am'#e : ,#oat ,1 K 21D.., d uble: do4b#e data t('e i a do4b#e-'reci ion /D-bit $999 0.D ,#oatin! 'oint. 5hi data t('e i !enera##( 4 ed a the de,a4#t data t('e ,or decima# va#4e . !enera##( the de,a4#t choice. Do4b#e data t('e ho4#d never be 4 ed ,or 'reci e va#4e 4ch a c4rrenc(. De,a4#t va#4e i 0.0d. 9<am'#e : do4b#e d1 K 121.D b lean: boo#ean data t('e re're ent one bit o, in,ormation. 5here are on#( two 'o ib#e va#4e : tr4e and ,a# e. 5hi data t('e i 4 ed ,or im'#e ,#a! that track tr4eG,a# e condition . De,a4#t va#4e i ,a# e. 9<am'#e : boo#ean one K tr4e char: char data t('e i a in!#e 1/-bit =nicode character. 2inim4m va#4e i ?I40000? 7or 0;. 2a<im4m va#4e i ?I4,,,,? 7or /.,.1. inc#4 ive;. +har data t('e i 4 ed to tore an( character. 9<am'#e . char #etter) K?)? Reference Data *y"e%: "e,erence variab#e are created 4 in! de,ined con tr4ctor o, the c#a e . 5he( are 4 ed to acce ob*ect . 5he e variab#e are dec#ared to be o, a 'eci,ic t('e that cannot be chan!ed. %or e<am'#e, 9m'#o(ee, P4''( etc. +#a ob*ect , and vario4 t('e o, arra( variab#e come 4nder re,erence data t('e. De,a4#t va#4e o, an( re,erence variab#e i n4##. ) re,erence variab#e can be 4 ed to re,er to an( ob*ect o, the dec#ared t('e or an( com'atib#e t('e. 9<am'#e : )nima# anima# K new )nima#7P!ira,,eP;J Ja)a 4iteral%: +2+ 3imited i a S4b idiar( o, 5+S 3imited 11

) #itera# i a o4rce code re're entation o, a ,i<ed va#4e. 5he( are re're ented direct#( in the code witho4t an( com'4tation. 3itera# can be a i!ned to an( 'rimitive t('e variab#e. %or e<am'#e: b(te a K /8J char a K ?)? b(te, int, #on!, and hort can be e<'re ed in decima#7ba e 10;,he<adecima#7ba e 1/; or octa#7ba e 8; n4mber ( tem a we##. Pre,i< 0 i 4 ed to indicate octa# and 're,i< 0< indicate he<adecima# when 4 in! the e n4mber ( tem ,or #itera# . %or e<am'#e: int decima# K 100J int octa# K 01DDJ int he<a K 0</DJ Strin! #itera# in 6ava are 'eci,ied #ike the( are in mo t other #an!4a!e b( enc#o in! a e@4ence o, character between a 'air o, do4b#e @4ote . 9<am'#e o, trin! #itera# are: PCe##o Wor#dP PtwoIn#ine P PIP5hi i in @4ote IPP Strin! and char t('e o, #itera# can contain an( =nicode character . %or e<am'#e: char a K ?I40001?J Strin! a K PI40001PJ 6ava #an!4a!e 4''ort ,ew 'ecia# e ca'e e@4ence ,or Strin! and char #itera# a we##. 5he( are: N tati 7haracter re"re%ented n In New#ine 70<0a; Ir +arria!e ret4rn 70<0d; I, %orm,eed 70<0c; Ib Eack 'ace 70<08; I S'ace 70<20; It 5ab IP Do4b#e @4ote I? Sin!#e @4ote II Eack #a h Iddd &cta# character 7ddd; Ce<adecima# =N$+&D9 character I4<<<< 7<<<<; Ja)a Variable *y"e% $n 6ava, a## variab#e m4 t be dec#ared be,ore the( can be 4 ed. 5he ba ic ,orm o, a variab#e dec#aration i hown here: t('e identi,ier 8 K va#4e:8, identi,ier 8K va#4e: ...: J 5he type i one o, 6ava? datat('e . 5he ide$ti)ier i the name o, the variab#e. 5o dec#are more than one variab#e o, the 'eci,ied t('e, 4 e a comma- e'arated #i t. Cere are evera# e<am'#e o, variab#e dec#aration o, vario4 t('e . Note that ome inc#4de an initia#iBation. int a, b, cJ GG dec#are three int , a, b, and c. int d K 1, e, , K .J GG dec#are three more int , initia#iBin! GG d and ,. b(te B K 22J GG initia#iBe B. do4b#e 'i K 1.1D1.-J GG dec#are an a''ro<imation o, 'i. char < K ?<?J GG the variab#e < ha the va#4e ?<?. 5hi cha'ter wi## e<'#ain vario4 variab#e t('e avai#ab#e in 6ava 3an!4a!e. 5here are three kind o, variab#e in 6ava: 3oca# variab#e $n tance variab#e +#a G tatic variab#e 4 cal )ariable% : 3oca# variab#e are dec#ared in method , con tr4ctor , or b#ock . +2+ 3imited i a S4b idiar( o, 5+S 3imited 12

3oca# variab#e are created when the method, con tr4ctor or b#ock i entered and the variab#e wi## be de tro(ed once it e<it the method, con tr4ctor or b#ock. )cce modi,ier cannot be 4 ed ,or #oca# variab#e . 3oca# variab#e are vi ib#e on#( within the dec#ared method, con tr4ctor or b#ock. 3oca# variab#e are im'#emented at tack #eve# interna##(. 5here i no de,a4#t va#4e ,or #oca# variab#e o #oca# variab#e ho4#d be dec#ared and an initia# va#4e ho4#d be a i!ned be,ore the ,ir t 4 e. E,a!"le: Cere age i a #oca# variab#e. 5hi i de,ined in ide pup*ge ! method and it co'e i #imited to thi method on#(. '4b#ic c#a 5e tN '4b#ic void '4')!e7;N int a!e K 0J a!e K a!e > 0J S( tem.o4t.'rint#n7PP4''( a!e i : P > a!e;J Q '4b#ic tatic void main7Strin! ar! 8:;N 5e t te t K new 5e t7;J te t.'4')!e7;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: P4''( a!e i : 0 E,a!"le: %o##owin! e<am'#e 4 e age witho4t initia#iBin! it, o it wo4#d !ive an error at the time o, com'i#ation. '4b#ic c#a 5e tN '4b#ic void '4')!e7;N int a!eJ a!e K a!e > 0J S( tem.o4t.'rint#n7PP4''( a!e i : P > a!e;J Q '4b#ic tatic void main7Strin! ar! 8:;N 5e t te t K new 5e t7;J te t.'4')!e7;J Q Q 5hi wo4#d 'rod4ce ,o##owin! error whi#e com'i#in! it: 5e t.*ava:D:variab#e n4mber mi!ht not have been initia#iBed a!e K a!e > 0J U 1 error In%tance )ariable% : $n tance variab#e are dec#ared in a c#a , b4t o4t ide a method, con tr4ctor or an( b#ock. When a 'ace i a##ocated ,or an ob*ect in the hea' a #ot ,or each in tance variab#e va#4e i created. $n tance variab#e are created when an ob*ect i created with the 4 e o, the ke( word ?new? and de tro(ed when the ob*ect i de tro(ed. $n tance variab#e ho#d va#4e that m4 t be re,erenced b( more than one method, con tr4ctor or b#ock, or e entia# 'art o, an ob*ect. tate that m4 t be 're ent thro4!h o4t the c#a . $n tance variab#e can be dec#ared in c#a #eve# be,ore or a,ter 4 e. )cce modi,ier can be !iven ,or in tance variab#e . 5he in tance variab#e are vi ib#e ,or a## method , con tr4ctor and b#ock in the c#a . Norma##( it i recommended to make the e variab#e 'rivate 7acce #eve#;.Cowever vi ibi#it( ,or 4bc#a e can be !iven ,or the e variab#e with the 4 e o, acce modi,ier .

+2+ 3imited i a S4b idiar( o, 5+S 3imited

11

$n tance variab#e have de,a4#t va#4e . %or n4mber the de,a4#t va#4e i 0, ,or Eoo#ean it i ,a# e and ,or ob*ect re,erence it i n4##. Va#4e can be a i!ned d4rin! the dec#aration or within the con tr4ctor. $n tance variab#e can be acce ed direct#( b( ca##in! the variab#e name in ide the c#a . Cowever within tatic method and di,,erent c#a 7 when in tance variab#e are !iven acce ibi#it(; the ho4#d be ca##ed 4 in! the ,4##( @4a#i,ied name . +bject,e)ere$ce#-ariableName. E,a!"le: im'ort *ava.io.OJ '4b#ic c#a 9m'#o(eeN GG thi in tance variab#e i vi ib#e ,or an( chi#d c#a . '4b#ic Strin! nameJ GG a#ar( variab#e i vi ib#e in 9m'#o(ee c#a 'rivate do4b#e a#ar(J on#(.

GG 5he name variab#e i a i!ned in the con tr4ctor. '4b#ic 9m'#o(ee 7Strin! em'Name;N name K em'NameJ Q GG 5he a#ar( variab#e i a i!ned a va#4e. '4b#ic void etSa#ar(7do4b#e em'Sa#;N a#ar( K em'Sa#J Q GG 5hi method 'rint the em'#o(ee detai# . '4b#ic void 'rint9m'7;N S( tem.o4t.'rint#n7Pname : P > name ;J S( tem.o4t.'rint#n7P a#ar( :P > a#ar(;J Q '4b#ic tatic void main7Strin! ar! 8:;N 9m'#o(ee em'&ne K new 9m'#o(ee7P"an ikaP;J em'&ne. etSa#ar(71000;J em'&ne.'rint9m'7;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: name : "an ika a#ar( :1000.0 7la%%/%tatic )ariable% : +#a variab#e a# o known a tatic variab#e are dec#ared with the static ke(word in a c#a , b4t o4t ide a method, con tr4ctor or a b#ock. 5here wo4#d on#( be one co'( o, each c#a variab#e 'er c#a , re!ard#e o, how man( ob*ect are created ,rom it. Static variab#e are rare#( 4 ed other than bein! dec#ared a con tant . +on tant are variab#e that are dec#ared a '4b#icG'rivate, ,ina# and tatic. +on tant variab#e never chan!e ,rom their initia# va#4e. Static variab#e are tored in tatic memor(. $t i rare to 4 e tatic variab#e other than dec#ared ,ina# and 4 ed a either '4b#ic or 'rivate con tant . Static variab#e are created when the 'ro!ram tart and de tro(ed when the 'ro!ram to' . Vi ibi#it( i imi#ar to in tance variab#e . Cowever, mo t tatic variab#e are dec#ared '4b#ic ince the( m4 t be avai#ab#e ,or 4 er o, the c#a . De,a4#t va#4e are ame a in tance variab#e . %or n4mber the de,a4#t va#4e i 0, ,or Eoo#ean it i ,a# e and ,or ob*ect re,erence it i n4##. Va#4e can be a i!ned d4rin! the dec#aration or within the con tr4ctor. )dditiona##( va#4e can be a i!ned in 'ecia# tatic initia#iBer b#ock . +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D

Static variab#e can be acce ed b( ca##in! with the c#a name . ClassName#-ariableName. When dec#arin! c#a variab#e a '4b#ic tatic ,ina#, then variab#e name 7con tant ; are a## in 4''er ca e. $, the tatic variab#e are not '4b#ic and ,ina# the namin! (nta< i the ame a in tance and #oca# variab#e . E,a!"le: im'ort *ava.io.OJ '4b#ic c#a 9m'#o(eeN GG a#ar( variab#e i a 'rivate tatic variab#e 'rivate tatic do4b#e a#ar(J GG D9P)"529N5 i a con tant '4b#ic tatic ,ina# Strin! D9P)"529N5 K PDeve#o'ment PJ '4b#ic tatic void main7Strin! ar! 8:;N a#ar( K 1000J S( tem.o4t.'rint#n7D9P)"529N5>Pavera!e a#ar(:P> a#ar(;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Deve#o'ment avera!e a#ar(:1000 N te: $, the variab#e are acce ,rom an o4t ide c#a the con tant ho4#d be acce ed a 9m'#o(ee.D9P)"529N5 Ja)a & difier *y"e% 2odi,ier are ke(word that (o4 add to tho e de,inition to chan!e their meanin! . 5he 6ava #an!4a!e ha a wide variet( o, modi,ier , inc#4din! the ,o##owin!: Ja)a Acce%% & difier% 6ava 'rovide a n4mber o, acce modi,ier to et acce #eve# ,or c#a e , variab#e , method and con tr4ctor . 5he ,o4r acce #eve# are: Vi ib#e to the 'acka!e. the de,a4#t. No modi,ier are needed. Vi ib#e to the c#a on#( 7'rivate;. Vi ib#e to the wor#d 7'4b#ic;. Vi ib#e to the 'acka!e and a## 4bc#a e 7'rotected;. Default Acce%% & difier $ N =eyw rd: De,a4#t acce modi,ier mean we do not e<'#icit#( dec#are an acce modi,ier ,or a c#a , ,ie#d, method etc. ) variab#e or method dec#ared witho4t an( acce contro# modi,ier i avai#ab#e to an( other c#a in the ame 'acka!e. 5he ,ie#d in an inter,ace are im'#icit#( '4b#ic tatic ,ina# and the method in an inter,ace are b( de,a4#t '4b#ic. E,a!"le: Variab#e and method can be dec#ared witho4t an( modi,ier , a in the ,o##owin! e<am'#e : Strin! ver ion K P1...1PJ boo#ean 'roce &rder7; N ret4rn tr4eJ Q Pri)ate Acce%% & difier $ "ri)ate: 2ethod , Variab#e and +on tr4ctor that are dec#ared 'rivate can on#( be acce ed within the dec#ared c#a it e#,. Private acce modi,ier i the mo t re trictive acce #eve#. +#a and inter,ace cannot be 'rivate. Variab#e that are dec#ared 'rivate can be acce ed o4t ide the c#a i, '4b#ic !etter method are 're ent in the c#a . = in! the 'rivate modi,ier i the main wa( that an ob*ect enca' 4#ate it e#, and hide data ,rom the o4t ide wor#d. E,a!"le: 5he ,o##owin! c#a 4 e 'rivate acce contro#: '4b#ic c#a 3o!!er N 'rivate Strin! ,ormatJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.

'4b#ic Strin! !et%ormat7; N ret4rn thi .,ormatJ Q '4b#ic void et%ormat7Strin! ,ormat; N thi .,ormat K ,ormatJ Q Q Cere, the )ormat variab#e o, the 3o!!er c#a i 'rivate, o there? no wa( ,or other c#a e to retrieve or et it va#4e direct#(. So to make thi variab#e avai#ab#e to the o4t ide wor#d, we de,ined two '4b#ic method : getFormat !, which ret4rn the va#4e o, ,ormat, and setFormat .tri$g!, which et it va#4e. Public Acce%% & difier $ "ublic: ) c#a , method, con tr4ctor, inter,ace etc dec#ared '4b#ic can be acce ed ,rom an( other c#a . 5here,ore ,ie#d , method , b#ock dec#ared in ide a '4b#ic c#a can be acce ed ,rom an( c#a be#on!in! to the 6ava =niver e. Cowever i, the '4b#ic c#a we are tr(in! to acce i in a di,,erent 'acka!e, then the '4b#ic c#a ti## need to be im'orted. Eeca4 e o, c#a inheritance, a## '4b#ic method and variab#e o, a c#a are inherited b( it 4bc#a e . E,a!"le: 5he ,o##owin! ,4nction 4 e '4b#ic acce contro#: '4b#ic tatic void main7Strin!8: ar!4ment ; N GG ... Q 5he main7; method o, an a''#ication ha to be '4b#ic. &therwi e, it co4#d not be ca##ed b( a 6ava inter'reter 7 4ch a *ava; to r4n the c#a . Pr tected Acce%% & difier $ "r tected: Variab#e , method and con tr4ctor which are dec#ared 'rotected in a 4'erc#a can be acce ed on#( b( the 4bc#a e in other 'acka!e or an( c#a within the 'acka!e o, the 'rotected member ? c#a . 5he 'rotected acce modi,ier cannot be a''#ied to c#a and inter,ace . 2ethod , ,ie#d can be dec#ared 'rotected, however method and ,ie#d in a inter,ace cannot be dec#ared 'rotected. Protected acce !ive the 4bc#a a chance to 4 e the he#'er method or variab#e, whi#e 'reventin! a nonre#ated c#a ,rom tr(in! to 4 e it. E,a!"le: 5he ,o##owin! 'arent c#a 4 e 'rotected acce contro#, to a##ow it chi#d c#a override ope$.peaker ! method: c#a )4dioP#a(er N 'rotected boo#ean o'enS'eaker7S'eaker '; N GG im'#ementation detai# Q Q c#a Streamin!)4dioP#a(er N boo#ean o'enS'eaker7S'eaker '; N GG im'#ementation detai# Q Q Cere i, we de,ine ope$.peaker ! method a 'rivate then it wo4#d not be acce ib#e ,rom an( other c#a other than *udioPlayer. $, we de,ine it a '4b#ic then it wo4#d become acce ib#e to a## the o4t ide wor#d. E4t o4r inten ion i to e<'o e thi method to it 4bc#a on#(, that wh( we 4 ed protected modi,ier. Acce%% 7 ntr l and Inheritance: 5he ,o##owin! r4#e ,or inherited method are en,orced: 2ethod dec#ared '4b#ic in a 4'erc#a a# o m4 t be '4b#ic in a## 4bc#a e . 2ethod dec#ared 'rotected in a 4'erc#a m4 t either be 'rotected or '4b#ic in 4bc#a e J the( cannot be 'rivate. +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/

2ethod dec#ared witho4t acce contro# 7no modi,ier wa 4 ed; can be dec#ared more 'rivate in 4bc#a e . 2ethod dec#ared 'rivate are not inherited at a##, o there i no r4#e ,or them. Ja)a N n Acce%% & difier% 6ava 'rovide a n4mber o, non-acce modi,ier to achieve man( other ,4nctiona#it(. 5he static modi,ier ,or creatin! c#a method and variab#e 5he )i$al modi,ier ,or ,ina#iBin! the im'#ementation o, c#a e , method , and variab#e . 5he abstract modi,ier ,or creatin! ab tract c#a e and method . 5he sy$chro$i/ed and volatile modi,ier , which are 4 ed ,or thread . *he %tatic & difier: #tatic Variable%: 5he static ke( word i 4 ed to create variab#e that wi## e<i t inde'endent#( o, an( in tance created ,or the c#a . &n#( one co'( o, the tatic variab#e e<i t re!ard#e o, the n4mber o, in tance o, the c#a . Static variab#e are a# o known a c#a variab#e . 3oca# variab#e cannot be dec#ared tatic. #tatic &eth d%: 5he tatic ke( word i 4 ed to create method that wi## e<i t inde'endent#( o, an( in tance created ,or the c#a . Static method do not 4 e an( in tance variab#e o, an( ob*ect o, the c#a the( are de,ined in. Static method take a## the data ,rom 'arameter and com'4te omethin! ,rom tho e 'arameter , with no re,erence to variab#e . +#a variab#e and method can be acce ed 4 in! the c#a name ,o##owed b( a dot and the name o, the variab#e or method. E,a!"le: 5he tatic modi,ier i 4 ed to create c#a method and variab#e , a in the ,o##owin! e<am'#e: '4b#ic c#a $n tance+o4nter N 'rivate tatic int n4m$n tance K 0J 'rotected tatic int !et+o4nt7; N ret4rn n4m$n tance J Q 'rivate tatic void add$n tance7; N n4m$n tance >>J Q $n tance+o4nter7; N $n tance+o4nter.add$n tance7;J Q '4b#ic tatic void main7Strin!8: ar!4ment ; N S( tem.o4t.'rint#n7PStartin! with P > $n tance+o4nter.!et+o4nt7; > P in tance P;J ,or 7int i K 0J i V .00J >>i;N new $n tance+o4nter7;J Q S( tem.o4t.'rint#n7P+reated P > $n tance+o4nter.!et+o4nt7; > P in tance P;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Started with 0 in tance +reated .00 in tance *he final & difier: final Variable%: ) ,ina# variab#e can be e<'#icit#( initia#iBed on#( once. ) re,erence variab#e dec#ared ,ina# can never be rea i!ned to re,er to an di,,erent ob*ect. +2+ 3imited i a S4b idiar( o, 5+S 3imited 10

Cowever the data within the ob*ect can be chan!ed. So the tate o, the ob*ect can be chan!ed b4t not the re,erence. With variab#e , the )i$al modi,ier o,ten i 4 ed with static to make the con tant a c#a variab#e. E,a!"le: '4b#ic c#a 5e tN ,ina# int va#4e K 10J GG 5he ,o##owin! are e<am'#e o, dec#arin! con tant : '4b#ic tatic ,ina# int E&AW$D5C K /J tatic ,ina# Strin! 5$539 K P2ana!erPJ '4b#ic void chan!eVa#4e7;N va#4e K 12J GGwi## !ive an error Q Q final &eth d%: ) ,ina# method cannot be overridden b( an( 4bc#a e . ) mentioned 'revio4 #( the ,ina# modi,ier 'revent a method ,rom bein! modi,ied in a 4bc#a . 5he main intention o, makin! a method ,ina# wo4#d be that the content o, the method ho4#d not be chan!ed b( an( o4t ider. E,a!"le: Fo4 dec#are method 4 in! the )i$al modi,ier in the c#a dec#aration, a in the ,o##owin! e<am'#e: '4b#ic c#a 5e tN '4b#ic ,ina# void chan!eName7;N GG bod( o, method Q Q final 7la%%e%: 5he main '4r'o e o, 4 in! a c#a bein! dec#ared a )i$al i to 'revent the c#a ,rom bein! 4bc#a ed. $, a c#a i marked a ,ina# then no c#a can inherit an( ,eat4re ,rom the ,ina# c#a . E,a!"le: '4b#ic ,ina# c#a 5e t N GG bod( o, c#a Q *he ab%tract & difier: ab%tract 7la%%: )n ab tract c#a can never be in tantiated. $, a c#a i dec#ared a ab tract then the o#e '4r'o e i ,or the c#a to be e<tended. ) c#a cannot be both ab tract and ,ina#. 7 ince a ,ina# c#a cannot be e<tended;. $, a c#a contain ab tract method then the c#a ho4#d be dec#ared ab tract. &therwi e a com'i#e error wi## be thrown. )n ab tract c#a ma( contain both ab tract method a we## norma# method . E,a!"le: ab tract c#a +aravanN 'rivate do4b#e 'riceJ 'rivate Strin! mode#J 'rivate Strin! (earJ '4b#ic ab tract void !o%a t7;J GGan ab tract method '4b#ic ab tract void chan!e+o#or7;J Q ab%tract &eth d%: )n ab tract method i a method dec#ared with o4t an( im'#ementation. 5he method bod(7im'#ementation; i 'rovided b( the 4bc#a . )b tract method can never be ,ina# or trict. )n( c#a that e<tend an ab tract c#a m4 t im'#ement a## the ab tract method o, the 4'er c#a 4n#e the 4bc#a i a# o an ab tract c#a . $, a c#a contain one or more ab tract method then the c#a m4 t be dec#ared ab tract. )n ab tract c#a doe not need to contain ab tract method . 5he ab tract method end with a emico#on. 9<am'#e: '4b#ic ab tract am'#e7;J E,a!"le: '4b#ic ab tract c#a S4'er+#a N ab tract void m7;J GGab tract method +2+ 3imited i a S4b idiar( o, 5+S 3imited 18

Q c#a

S4b+#a e<tend S4'er+#a N GG im'#ement the ab tract method void m7;N ......... Q

Q *he %ynchr ni>ed & difier: 5he (nchroniBed ke( word 4 ed to indicate that a method can be acce ed b( on#( one thread at a time. 5he (nchroniBed modi,ier can be a''#ied with an( o, the ,o4r acce #eve# modi,ier . E,a!"le: '4b#ic (nchroniBed void howDetai# 7;N ....... Q *he tran%ient & difier: )n in tance variab#e i marked tran ient to indicate the 6V2 to ki' the 'artic4#ar variab#e when eria#iBin! the ob*ect containin! it. 5hi modi,ier i inc#4ded in the tatement that create the variab#e, 'recedin! the c#a or data t('e o, the variab#e. E,a!"le: '4b#ic tran ient int #imit K ..J GG wi## not 'er i t '4b#ic int bJ GG wi## 'er i t *he ) latile & difier: 5he vo#ati#e i 4 ed to #et the 6V2 know that a thread acce in! the variab#e m4 t a#wa( mer!e it own 'rivate co'( o, the variab#e with the ma ter co'( in the memor(. )cce in! a vo#ati#e variab#e (nchroniBe a## the cached co'ied o, the variab#e in the main memor(. Vo#ati#e can on#( be a''#ied to in tance variab#e which are o, t('e ob*ect or 'rivate. ) vo#ati#e ob*ect re,erence can be n4##. E,a!"le: '4b#ic c#a 2("4nnab#e im'#ement "4nnab#e N 'rivate vo#ati#e boo#ean activeJ '4b#ic void r4n7; N active K tr4eJ whi#e 7active; GG #ine 1 N GG ome code here Q Q '4b#ic void to'7; N active K ,a# eJ GG #ine 2 Q Q = 4a##(, r4n7; i ca##ed in one thread 7the one (o4 tart 4 in! the "4nnab#e;, and to'7; i ca##ed ,rom another thread. $, in #ine 1 the cached va#4e o, active i 4 ed, the #oo' ma( not to' when (o4 et active to ,a# e in #ine 2. 5hat? when (o4 want to 4 e volatile. 5o 4 e a modi,ier, (o4 inc#4de it ke(word in the de,inition o, a c#a , method, or variab#e. 5he modi,ier 'recede the re t o, the tatement, a in the ,o##owin! e<am'#e 7$ta#ic one ;: public c#a c#a Name N GG ... Q private boo#ean m(%#a!J static )i$al do4b#e week K -..J protected static )i$al int E&AW$D5C K D2J public static void main7Strin!8: ar!4ment ; N GG bod( o, method +2+ 3imited i a S4b idiar( o, 5+S 3imited 1-

Q Acce%% 7 ntr l & difier%: 6ava 'rovide a n4mber o, acce modi,ier to et acce #eve# ,or c#a e , variab#e , method and con tr4ctor . 5he ,o4r acce #eve# are: Vi ib#e to the 'acka!e. the de,a4#t. No modi,ier are needed. Vi ib#e to the c#a on#( 7'rivate;. Vi ib#e to the wor#d 7'4b#ic;. Vi ib#e to the 'acka!e and a## 4bc#a e 7'rotected;. N n Acce%% & difier%: 6ava 'rovide a n4mber o, non-acce modi,ier to achieve man( other ,4nctiona#it(. 5he static modi,ier ,or creatin! c#a method and variab#e 5he )i$al modi,ier ,or ,ina#iBin! the im'#ementation o, c#a e , method , and variab#e . 5he abstract modi,ier ,or creatin! ab tract c#a e and method . 5he sy$chro$i/ed and volatile modi,ier , which are 4 ed ,or thread . Ja)a 6a%ic O"erat r% 6ava 'rovide a rich et o, o'erator to mani'4#ate variab#e . We can divide a## the 6ava o'erator into the ,o##owin! !ro4' : )rithmetic &'erator "e#ationa# &'erator Eitwi e &'erator 3o!ica# &'erator ) i!nment &'erator 2i c &'erator *he Arith!etic O"erat r%: )rithmetic o'erator are 4 ed in mathematica# e<'re ion in the ame wa( that the( are 4 ed in a#!ebra. 5he ,o##owin! tab#e #i t the arithmetic o'erator : ) 4me inte!er variab#e ) ho#d 10 and variab#e E ho#d 20 then: O"erat De%cri"ti n E,a!"le r > )ddition - )dd va#4e on either ide o, the o'erator ) > E wi## !ive 10 S4btraction - S4btract ri!ht hand o'erand ,rom #e,t hand ) - E wi## !ive -10 o'erand O 24#ti'#ication - 24#ti'#ie va#4e on either ide o, the o'erator ) O E wi## !ive 200 G Divi ion - Divide #e,t hand o'erand b( ri!ht hand o'erand E G ) wi## !ive 2 2od4#4 - Divide #e,t hand o'erand b( ri!ht hand o'erand and L E L ) wi## !ive 0 ret4rn remainder >> $ncrement - $ncrea e the va#4e o, o'erand b( 1 E>> !ive 21 -Decrement - Decrea e the va#4e o, o'erand b( 1 E-- !ive 1*he Relati nal O"erat r%: 5here are ,o##owin! re#ationa# o'erator 4''orted b( 6ava #an!4a!e ) 4me variab#e ) ho#d 10 and variab#e E ho#d 20 then: O"erat De%cri"ti n E,a!"le r +heck i, the va#4e o, two o'erand are e@4a# or not, i, (e then 7) KK E; i not KK condition become tr4e. tr4e. +heck i, the va#4e o, two o'erand are e@4a# or not, i, va#4e WK 7) WK E; i tr4e. are not e@4a# then condition become tr4e. +heck i, the va#4e o, #e,t o'erand i !reater than the va#4e o, R 7) R E; i not tr4e. ri!ht o'erand, i, (e then condition become tr4e. +heck i, the va#4e o, #e,t o'erand i #e than the va#4e o, ri!ht V 7) V E; i tr4e. o'erand, i, (e then condition become tr4e. +heck i, the va#4e o, #e,t o'erand i !reater than or e@4a# to the 7) RK E; i not RK va#4e o, ri!ht o'erand, i, (e then condition become tr4e. tr4e. VK +heck i, the va#4e o, #e,t o'erand i #e than or e@4a# to the 7) VK E; i tr4e. +2+ 3imited i a S4b idiar( o, 5+S 3imited 20

va#4e o, ri!ht o'erand, i, (e then condition become tr4e. *he 6itwi%e O"erat r%: 6ava de,ine evera# bitwi e o'erator which can be a''#ied to the inte!er t('e , #on!, int, hort, char, and b(te. Eitwi e o'erator work on bit and 'er,orm bit b( bit o'eration. ) 4me i, a K /0J and b K 11J Now in binar( ,ormat the( wi## be a ,o##ow : a K 0011 1100 b K 0000 1101 ----------------aXb K 0000 1100 aYb K 0011 1101 aUb K 0011 0001 Za K 1100 0011 5he ,o##owin! tab#e #i t the bitwi e o'erator : ) 4me inte!er variab#e ) ho#d /0 and variab#e E ho#d 11 then: O"erat De%cri"ti n E,a!"le r 7) X E; wi## !ive Einar( )ND &'erator co'ie a bit to the re 4#t i, it e<i t in both X 12 which i 0000 o'erand . 1100 7) Y E; wi## !ive /1 Y Einar( &" &'erator co'ie a bit i, it e<i t in eather o'erand. which i 0011 1101 7) U E; wi## !ive DEinar( A&" &'erator co'ie the bit i, it i et in one o'erand b4t U which i 0011 not both. 0001 7Z) ; wi## !ive -/0 Einar( &ne +om'#ement &'erator i 4nar( and ha the e,ect o, Z which i 1100 ?,#i''in!? bit . 0011 ) VV 2 wi## !ive Einar( 3e,t Shi,t &'erator. 5he #e,t o'erand va#4e i moved #e,t VV 2D0 which i 1111 b( the n4mber o, bit 'eci,ied b( the ri!ht o'erand. 0000 Einar( "i!ht Shi,t &'erator. 5he #e,t o'erand va#4e i moved ) RR 2 wi## !ive 1. RR ri!ht b( the n4mber o, bit 'eci,ied b( the ri!ht o'erand. which i 1111 Shi,t ri!ht Bero ,i## o'erator. 5he #e,t o'erand va#4e i moved ) RRR2 wi## !ive RRR ri!ht b( the n4mber o, bit 'eci,ied b( the ri!ht o'erand and 1. which i 0000 hi,ted va#4e are ,i##ed 4' with Bero . 1111 *he 4 (ical O"erat r%: 5he ,o##owin! tab#e #i t the #o!ica# o'erator : ) 4me boo#ean variab#e ) ho#d tr4e and variab#e E ho#d ,a# e then: O"erat De%cri"ti n E,a!"le r +a##ed 3o!ica# )ND o'erator. $, both the o'erand are non Bero XX 7) XX E; i ,a# e. then then condition become tr4e. +a##ed 3o!ica# &" &'erator. $, an( o, the two o'erand are non YY 7) YY E; i tr4e. Bero then then condition become tr4e. +a##ed 3o!ica# N&5 &'erator. = e to rever e the #o!ica# tate o, W it o'erand. $, a condition i tr4e then 3o!ica# N&5 o'erator wi## W7) XX E; i tr4e. make ,a# e. *he A%%i(n!ent O"erat r%: 5here are ,o##owin! a i!nment o'erator 4''orted b( 6ava #an!4a!e: O"erat De%cri"ti n E,a!"le r K Sim'#e a i!nment o'erator, ) i!n va#4e ,rom ri!ht ide + K ) > E wi## +2+ 3imited i a S4b idiar( o, 5+S 3imited 21

a i!ne va#4e o, ) > E into + + >K ) i )dd )ND a i!nment o'erator, $t add ri!ht o'erand to the #e,t >K e@4iva#ent to + K o'erand and a i!n the re 4#t to #e,t o'erand +>) + -K ) i S4btract )ND a i!nment o'erator, $t 4btract ri!ht o'erand -K e@4iva#ent to + K ,rom the #e,t o'erand and a i!n the re 4#t to #e,t o'erand +-) + OK ) i 24#ti'#( )ND a i!nment o'erator, $t m4#ti'#ie ri!ht o'erand with OK e@4iva#ent to + K the #e,t o'erand and a i!n the re 4#t to #e,t o'erand +O) + GK ) i Divide )ND a i!nment o'erator, $t divide #e,t o'erand with the GK e@4iva#ent to + K ri!ht o'erand and a i!n the re 4#t to #e,t o'erand +G) + LK ) i 2od4#4 )ND a i!nment o'erator, $t take mod4#4 4 in! two LK e@4iva#ent to + K o'erand and a i!n the re 4#t to #e,t o'erand +L) + VVK 2 i ame VVK 3e,t hi,t )ND a i!nment o'erator a + K + VV 2 + RRK 2 i ame RRK "i!ht hi,t )ND a i!nment o'erator a + K + RR 2 + XK 2 i ame a XK Eitwi e )ND a i!nment o'erator +K+X2 + UK 2 i ame a UK bitwi e e<c#4 ive &" and a i!nment o'erator +K+U2 + YK 2 i ame a YK bitwi e inc#4 ive &" and a i!nment o'erator +K+Y2 &i%c O"erat r% 5here are ,ew other o'erator 4''orted b( 6ava 3an!4a!e. 7 nditi nal O"erat r 8 ? : ;: +onditiona# o'erator i a# o known a the ternar( o'erator. 5hi o'erator con i t o, three o'erand and i 4 ed to eva#4ate boo#ean e<'re ion . 5he !oa# o, the o'erator i to decide which va#4e ho4#d be a i!ned to the variab#e. 5he o'erator i written a : variab#e < K 7e<'re ion; [ va#4e i, tr4e : va#4e i, ,a# e %o##owin! i the e<am'#e: '4b#ic c#a 5e t N o'erand to #e,t ide o'erand '4b#ic tatic void main7Strin! ar! 8:;N int a , bJ a K 10J b K 7a KK 1; [ 20: 10J S( tem.o4t.'rint#n7 PVa#4e o, b i : P > b ;J b K 7a KK 10; [ 20: 10J S( tem.o4t.'rint#n7 PVa#4e o, b i : P > b ;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Va#4e o, b i : 10 Va#4e o, b i : 20 in%tanceOf O"erat r: 5hi o'erator i 4 ed on#( ,or ob*ect re,erence variab#e . 5he o'erator check whether the ob*ect i o, a 'artic4#ar t('e7c#a t('e or inter,ace t('e;. in tance&, o'erator i wriiten a : 7 &b*ect re,erence variab#e ; in tance&, 7c#a Ginter,ace t('e; +2+ 3imited i a S4b idiar( o, 5+S 3imited 22

$, the ob*ect re,erred b( the variab#e on the #e,t ide o, the o'erator 'a e the $S-) check ,or the c#a Ginter,ace t('e on the ri!ht ide then the re 4#t wi## be tr4e. %o##owin! i the e<am'#e: Strin! name K K ?6ame ?J boo#ean re 4#t K name in tance&, Strin!J GG 5hi wi## ret4rn tr4e ince name i t('e o, Strin! 5hi o'erator wi## ti## ret4rn tr4e i, the ob*ect bein! com'ared i the a i!nment com'atib#e with the t('e on the ri!ht. %o##owin! i one more e<am'#e: c#a Vehic#e NQ '4b#ic c#a +ar e<tend Vehic#e N '4b#ic tatic void main7Strin! ar! 8:;N Vehic#e a K new +ar7;J boo#ean re 4#t K a in tanceo, +arJ S( tem.o4t.'rint#n7 re 4#t;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: tr4e Precedence f Ja)a O"erat r%: &'erator 'recedence determine the !ro4'in! o, term in an e<'re ion. 5hi a,,ect how an e<'re ion i eva#4ated. +ertain o'erator have hi!her 'recedence than other J ,or e<am'#e, the m4#ti'#ication o'erator ha hi!her 'recedence than the addition o'erator: %or e<am'#e < K 0 > 1 O 2J Cere < i a i!ned 11, not 20 beca4 e o'erator O ha hi!her 'recedenace than > o it ,ir t !et m4#ti'#ied with 1O2 and then add into 0. Cere o'erator with the hi!he t 'recedence a''ear at the to' o, the tab#e, tho e with the #owe t a''ear at the bottom. Within an e<'re ion, hi!her 'recedenace o'erator wi## be eva#4ated ,ir t. A%% ciati)it 7ate( ry O"erat r y Po t,i< 7; 8: . 7dot o'erator; 3e,t to ri!ht =nar( >> - - W Z "i!ht to #e,t 24#ti'#icative O G L 3e,t to ri!ht )dditive >3e,t to ri!ht Shi,t RR RRR VV 3e,t to ri!ht "e#ationa# R RK V VK 3e,t to ri!ht 9@4a#it( KK WK 3e,t to ri!ht Eitwi e )ND X 3e,t to ri!ht Eitwi e A&" U 3e,t to ri!ht Eitwi e &" Y 3e,t to ri!ht 3o!ica# )ND XX 3e,t to ri!ht 3o!ica# &" YY 3e,t to ri!ht +onditiona# [: "i!ht to #e,t K >K -K OK GK LK RRK VVK XK UK Y ) i!nment "i!ht to #e,t K +omma , 3e,t to ri!ht Ja)a 4 "% $ f r, while and d +++while 5here ma( be a it4tion when we need to e<ec4te a b#ock o, code evera# n4mber o, time , and i o,ten re,erred to a a #oo'. 6ava ha ver( ,#e<ib#e three #oo'in! mechani m . Fo4 can 4 e one o, the ,o##owin! three #oo' : whi#e 3oo' do...whi#e 3oo' ,or 3oo' ) o, *ava . the e$ha$ced )or loop wa introd4ced. 5hi i main#( 4 ed ,or )rra( . *he while 4 ": ) whi#e #oo' i a contro# tr4ct4re that a##ow (o4 to re'eat a ta k a certain n4mber o, time . #ynta,: 5he (nta< o, a whi#e #oo' i : +2+ 3imited i a S4b idiar( o, 5+S 3imited 21

whi#e7Eoo#eanTe<'re ion; N GGStatement Q When e<ec4tin!, i, the boolea$0e1pressio$ re 4#t i tr4e then the action in ide the #oo' wi## be e<ec4ted. 5hi wi## contin4e a #on! a the e<'re ion re 4#t i tr4e. Cere ke( 'oint o, the 2hile #oo' i that the #oo' mi!ht not ever r4n. When the e<'re ion i te ted and the re 4#t i ,a# e, the #oo' bod( wi## be ki''ed and the ,ir t tatement a,ter the whi#e #oo' wi## be e<ec4ted. E,a!"le: '4b#ic c#a 5e t N '4b#ic tatic void main7Strin! ar! 8:; N int < K 10J whi#e7 < V 20 ; N S( tem.o4t.'rint7Pva#4e o, < : P > < ;J <>>J S( tem.o4t.'rint7PInP;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: va#4e o, < : 10 va#4e o, < : 11 va#4e o, < : 12 va#4e o, < : 11 va#4e o, < : 1D va#4e o, < : 1. va#4e o, < : 1/ va#4e o, < : 10 va#4e o, < : 18 va#4e o, < : 1*he d +++while 4 ": ) do...whi#e #oo' i imi#ar to a whi#e #oo', e<ce't that a do...whi#e #oo' i !4aranteed to e<ec4te at #ea t one time. #ynta,: 5he (nta< o, a do...whi#e #oo' i : do N GGStatement Qwhi#e7Eoo#eanTe<'re ion;J Notice that the Eoo#ean e<'re ion a''ear at the end o, the #oo', o the tatement in the #oo' e<ec4te once be,ore the Eoo#ean i te ted. $, the Eoo#ean e<'re ion i tr4e, the ,#ow o, contro# *4m' back 4' to do, and the tatement in the #oo' e<ec4te a!ain. 5hi 'roce re'eat 4nti# the Eoo#ean e<'re ion i ,a# e. E,a!"le: '4b#ic c#a 5e t N '4b#ic tatic void main7Strin! ar! 8:;N int < K 10J doN S( tem.o4t.'rint7Pva#4e o, < : P > < ;J <>>J S( tem.o4t.'rint7PInP;J Qwhi#e7 < V 20 ;J Q Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 2D

5hi wo4#d 'rod4ce ,o##owin! re 4#t: va#4e o, < : 10 va#4e o, < : 11 va#4e o, < : 12 va#4e o, < : 11 va#4e o, < : 1D va#4e o, < : 1. va#4e o, < : 1/ va#4e o, < : 10 va#4e o, < : 18 va#4e o, < : 1*he f r 4 ": ) ,or #oo' i a re'etition contro# tr4ct4re that a##ow (o4 to e,,icient#( write a #oo' that need to e<ec4te a 'eci,ic n4mber o, time . ) ,or #oo' i 4 e,4# when (o4 know how man( time a ta k i to be re'eated. #ynta,: 5he (nta< o, a ,or #oo' i : ,or7initia#iBationJ Eoo#eanTe<'re ionJ 4'date; N GGStatement Q Cere i the ,#ow o, contro# in a ,or #oo': 5he initia#iBation te' i e<ec4ted ,ir t, and on#( once. 5hi te' a##ow (o4 to dec#are and initia#iBe an( #oo' contro# variab#e . Fo4 are not re@4ired to '4t a tatement here, a #on! a a emico#on a''ear . Ne<t, the Eoo#ean e<'re ion i eva#4ated. $, it i tr4e, the bod( o, the #oo' i e<ec4ted. $, it i ,a# e, the bod( o, the #oo' doe not e<ec4te and ,#ow o, contro# *4m' to the ne<t tatement 'a t the ,or #oo'. ),ter the bod( o, the ,or #oo' e<ec4te , the ,#ow o, contro# *4m' back 4' to the 4'date tatement. 5hi tatement a##ow (o4 to 4'date an( #oo' contro# variab#e . 5hi tatement can be #e,t b#ank, a #on! a a emico#on a''ear a,ter the Eoo#ean e<'re ion. 5he Eoo#ean e<'re ion i now eva#4ated a!ain. $, it i tr4e, the #oo' e<ec4te and the 'roce re'eat it e#, 7bod( o, #oo', then 4'date te',then Eoo#ean e<'re ion;. ),ter the Eoo#ean e<'re ion i ,a# e, the ,or #oo' terminate . E,a!"le: '4b#ic c#a 5e t N '4b#ic tatic void main7Strin! ar! 8:; N ,or7int < K 10J < V 20J < K <>1; N S( tem.o4t.'rint7Pva#4e o, < : P > < ;J S( tem.o4t.'rint7PInP;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: va#4e o, < : 10 va#4e o, < : 11 va#4e o, < : 12 va#4e o, < : 11 va#4e o, < : 1D va#4e o, < : 1. va#4e o, < : 1/ va#4e o, < : 10 va#4e o, < : 18 va#4e o, < : 1Enhanced f r l " in Ja)a: ) o, *ava . the enhanced ,or #oo' wa introd4ced. 5hi i main#( 4 ed ,or )rra( . +2+ 3imited i a S4b idiar( o, 5+S 3imited 2.

#ynta,: 5he (nta< o, enhanced ,or #oo' i : ,or7dec#aration : e<'re ion; N GGStatement Q Declarati n . 5he new#( dec#ared b#ock variab#e, which i o, a t('e com'atib#e with the e#ement o, the arra( (o4 are acce in!. 5he variab#e wi## be avai#ab#e within the ,or b#ock and it va#4e wo4#d be the ame a the c4rrent arra( e#ement. E,"re%%i n . 5hi eva#4ate to the arra( (o4 need to #oo' thro4!h. 5he e<'re ion can be an arra( variab#e or method ca## that ret4rn an arra(. E,a!"le: '4b#ic c#a 5e t N '4b#ic tatic void main7Strin! ar! 8:;N int 8: n4mber K N10, 20, 10, D0, .0QJ ,or7int < : n4mber ;N S( tem.o4t.'rint7 < ;J S( tem.o4t.'rint7P,P;J Q S( tem.o4t.'rint7PInP;J Strin! 8: name KNP6ame P, P3arr(P, P5omP, P3ac(PQJ ,or7 Strin! name : name ; N S( tem.o4t.'rint7 name ;J S( tem.o4t.'rint7P,P;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 10,20,10,D0,.0, 6ame ,3arr(,5om,3ac(, *he brea= <eyw rd: 5he break ke(word i 4 ed to to' the entire #oo'. 5he break ke(word m4 t be 4 ed in ide an( #oo' or a witch tatement. 5he break ke(word wi## to' the e<ec4tion o, the innermo t #oo' and tart e<ec4tin! the ne<t #ine o, code a,ter the b#ock. #ynta,: 5he (nta< o, a break i a in!#e tatement in ide an( #oo': breakJ E,a!"le: '4b#ic c#a 5e t N '4b#ic tatic void main7Strin! ar! 8:; N int 8: n4mber K N10, 20, 10, D0, .0QJ ,or7int < : n4mber ; N i,7 < KK 10 ; N breakJ Q S( tem.o4t.'rint7 < ;J S( tem.o4t.'rint7PInP;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 10 20 +2+ 3imited i a S4b idiar( o, 5+S 3imited 2/

*he c ntinue <eyw rd: 5he co$ti$ue ke(word can be 4 ed in an( o, the #oo' contro# tr4ct4re . $t ca4 e the #oo' to immediate#( *4m' to the ne<t iteration o, the #oo'. $n a ,or #oo', the contin4e ke(word ca4 e ,#ow o, contro# to immediate#( *4m' to the 4'date tatement. $n a whi#e #oo' or doGwhi#e #oo', ,#ow o, contro# immediate#( *4m' to the Eoo#ean e<'re ion. #ynta,: 5he (nta< o, a contin4e i a in!#e tatement in ide an( #oo': contin4eJ E,a!"le: '4b#ic c#a 5e t N '4b#ic tatic void main7Strin! ar! 8:; N int 8: n4mber K N10, 20, 10, D0, .0QJ ,or7int < : n4mber ; N i,7 < KK 10 ; N contin4eJ Q S( tem.o4t.'rint7 < ;J S( tem.o4t.'rint7PInP;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 10 20 D0 .0 Ja)a Deci%i n &a=in( 5here are two t('e o, deci ion makin! tatement in 6ava. 5he( are: i, tatement witch tatement *he if #tate!ent: )n i, tatement con i t o, a Eoo#ean e<'re ion ,o##owed b( one or more tatement . #ynta,: 5he (nta< o, an i, tatement i : i,7Eoo#eanTe<'re ion; N GGStatement wi## e<ec4te i, the Eoo#ean e<'re ion i tr4e Q $, the boo#ean e<'re ion eva#4ate to tr4e then the b#ock o, code in ide the i, tatement wi## be e<ec4ted. $, not the ,ir t et o, code a,ter the end o, the i, tatement7a,ter the c#o in! c4r#( brace; wi## be e<ec4ted. E,a!"le: '4b#ic c#a 5e t N '4b#ic tatic void main7Strin! ar! 8:;N int < K 10J i,7 < V 20 ;N S( tem.o4t.'rint7P5hi i i, tatementP;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 5hi i i, tatement *he if+++el%e #tate!ent: +2+ 3imited i a S4b idiar( o, 5+S 3imited 20

)n i, tatement can be ,o##owed b( an o'tiona# else tatement, which e<ec4te when the Eoo#ean e<'re ion i ,a# e. #ynta,: 5he (nta< o, a i,...e# e i : i,7Eoo#eanTe<'re ion;N GG9<ec4te when the Eoo#ean e<'re ion i tr4e Qe# eN GG9<ec4te when the Eoo#ean e<'re ion i ,a# e Q E,a!"le: '4b#ic c#a 5e t N '4b#ic tatic void main7Strin! ar! 8:;N int < K 10J i,7 < V 20 ;N S( tem.o4t.'rint7P5hi i i, tatementP;J Qe# eN S( tem.o4t.'rint7P5hi i e# e tatementP;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 5hi i e# e tatement *he if+++el%e if+++el%e #tate!ent: )n i, tatement can be ,o##owed b( an o'tiona# else i)###else tatement, which i ver( 4 e,4## to te t vario4 condition 4 in! in!#e i,...e# e i, tatement. When 4 in! i, , e# e i, , e# e tatement there are ,ew 'oint to kee' in mind. )n i, can have Bero or one e# e? and it m4 t come a,ter an( e# e i,? . )n i, can have Bero to man( e# e i,? and the( m4 t come be,ore the e# e. &nce an e# e i, 4cceed , none o, he remainin! e# e i,? or e# e? wi## be te ted. #ynta,: 5he (nta< o, a i,...e# e i : i,7Eoo#eanTe<'re ion 1;N GG9<ec4te when the Eoo#ean e<'re ion 1 i tr4e Qe# e i,7Eoo#eanTe<'re ion 2;N GG9<ec4te when the Eoo#ean e<'re ion 2 i tr4e Qe# e i,7Eoo#eanTe<'re ion 1;N GG9<ec4te when the Eoo#ean e<'re ion 1 i tr4e Qe# e N GG9<ec4te when the none o, the above condition i tr4e. Q E,a!"le: '4b#ic c#a 5e t N '4b#ic tatic void main7Strin! ar! 8:;N int < K 10J i,7 < KK 10 ;N S( tem.o4t.'rint7PVa#4e o, A i Qe# e i,7 < KK 20 ;N S( tem.o4t.'rint7PVa#4e o, A i Qe# e i,7 < KK 10 ;N S( tem.o4t.'rint7PVa#4e o, A i Qe# eN S( tem.o4t.'rint7P5hi i e# e Q Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 28 10P;J 20P;J 10P;J tatementP;J

Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Va#4e o, A i 10 Ne%ted if+++el%e #tate!ent: $t i a#wa( #e!a# to ne t i,-e# e tatement , which mean (o4 can 4 e one i, or e# e i, tatement in ide another i, or e# e i, tatement. #ynta,: 5he (nta< ,or a ne ted i,...e# e i a ,o##ow : i,7Eoo#eanTe<'re ion 1;N GG9<ec4te when the Eoo#ean e<'re ion 1 i tr4e i,7Eoo#eanTe<'re ion 2;N GG9<ec4te when the Eoo#ean e<'re ion 2 i tr4e Q Q Fo4 can ne t else i)###else in the imi#ar wa( a we have ne ted i) tatement. E,a!"le: '4b#ic c#a 5e t N '4b#ic tatic void main7Strin! ar! 8:;N int < K 10J int ( K 10J i,7 < KK 10 ;N i,7 ( KK 10 ;N S( tem.o4t.'rint7PA K 10 and F K 10P;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: A K 10 and F K 10 *he %witch #tate!ent: ) s2itch tatement a##ow a variab#e to be te ted ,or e@4a#it( a!ain t a #i t o, va#4e . 9ach va#4e i ca##ed a ca e, and the variab#e bein! witched on i checked ,or each ca e. #ynta,: 5he (nta< o, enhanced ,or #oo' i : witch7e<'re ion;N ca e va#4e : GGStatement breakJ GGo'tiona# ca e va#4e : GGStatement breakJ GGo'tiona# GGFo4 can have an( n4mber o, ca e tatement . de,a4#t : GG&'tiona# GGStatement Q 5he ,o##owin! r4#e a''#( to a witch tatement: 5he variab#e 4 ed in a witch tatement can on#( be a b(te, hort, int, char or Strin!. Fo4 can have an( n4mber o, ca e tatement within a witch. 9ach ca e i ,o##owed b( the va#4e to be com'ared to and a co#on. 5he va#4e ,or a ca e m4 t be the ame data t('e a the variab#e in the witch, and it m4 t be a con tant or a #itera#. When the variab#e bein! witched on i e@4a# to a ca e, the tatement ,o##owin! that ca e wi## e<ec4te 4nti# a break tatement i reached. When a break tatement i reached, the witch terminate , and the ,#ow o, contro# *4m' to the ne<t #ine ,o##owin! the witch tatement. Not ever( ca e need to contain a break. $, no break a''ear , the ,#ow o, contro# wi## )all through to 4b e@4ent ca e 4nti# a break i reached. +2+ 3imited i a S4b idiar( o, 5+S 3imited 2-

) s2itch tatement can have an o'tiona# de,a4#t ca e, which m4 t a''ear at the end o, the witch. 5he de,a4#t ca e can be 4 ed ,or 'er,ormin! a ta k when none o, the ca e i tr4e. No break i needed in the de,a4#t ca e. E,a!"le: '4b#ic c#a 5e t N '4b#ic tatic void main7Strin! ar! 8:;N char !rade K ar! 80:.char)t70;J witch7!rade; N ca e ?)? : S( tem.o4t.'rint#n7P9<ce##entWP;J breakJ ca e ?E? : ca e ?+? : S( tem.o4t.'rint#n7PWe## doneP;J breakJ ca e ?D? : S( tem.o4t.'rint#n7PFo4 'a edP;J ca e ?%? : S( tem.o4t.'rint#n7PEetter tr( a!ainP;J breakJ de,a4#t : S( tem.o4t.'rint#n7P$nva#id !radeP;J Q S( tem.o4t.'rint#n7PFo4r !rade i P > !rade;J Q Q +om'i#e and r4n above 'ro!ram 4 in! vario4 command #ine ar!4ment . 5hi wo4#d 'rod4ce ,o##owin! re 4#t: M *ava 5e t a $nva#id !rade Fo4r !rade i a a M *ava 5e t ) 9<ce##entW Fo4r !rade i a ) M *ava 5e t + We## done Fo4r !rade i a + M Ja)a $ Nu!ber% 7la%% Norma##(, when we work with N4mber , we 4 e 'rimitive data t('e 4ch a b(te, int, #on!, do4b#e etc. E,a!"le: int i K .000J ,#oat !'a K 11./.J b(te ma k K 0<a,J Cowever in deve#o'ment we come acro it4ation were we need to 4 e ob*ect in tead o, 'rimitive data t('e . $n-order to achieve thi 6ava 'rovide wra''er c#a e ,or each 'rimitive data t('e. )## the wra''er c#a e 7 $nte!er, 3on!, E(te, Do4b#e, %#oat, Short; are 4bc#a e o, the ab tract c#a N4mber.

+2+ 3imited i a S4b idiar( o, 5+S 3imited

10

5hi wra''in! i taken care o, b( the com'i#er 5he 'roce i ca##ed bo<in!. So when a 'rimitive i 4 ed when an ob*ect i re@4ired the com'i#er bo<e the 'rimitive t('e in it wra''er c#a . Simi#ar#( the com'i#er 4nbo<e the ob*ect to a 'rimitive a we##. 5he Nu!ber i 'art o, the *ava.#an! 'acka!e. Cere i an e<am'#e o, bo<in! and 4nbo<in!: '4b#ic c#a 5e tN '4b#ic tatic void main7Strin! ar! 8:;N $nte!er < K .J GG bo<e int to an $nte!er ob*ect < K < > 10J GG 4nbo<e the $nte!er to a int S( tem.o4t.'rint#n7<;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 1. When < i a i!ned inte!er va#4e , the com'i#er bo<e the inte!er beca4 e < i inte!er ob*ect . 3ater, < i 4nbo<ed o that the( can be added a inte!er . Nu!ber &eth d%: Cere i the #i t o, the in tance method that a## the 4bc#a e o, the N4mber c#a im'#ement: # &eth d% with De%cri"ti n N <<<Va#4e7; 1 +onvert the va#4e o, this N4mber ob*ect to the <<< data t('e and ret4rned it. com'are5o7; 2 +om'are this N4mber ob*ect to the ar!4ment. e@4a# 7; 1 Determine whether this n4mber ob*ect i e@4a# to the ar!4ment. va#4e&,7; D "et4rn an $nte!er ob*ect ho#din! the va#4e o, the 'eci,ied 'rimitive. toStrin!7; . "et4rn a Strin! ob*ect re're entin! the va#4e o, 'eci,ied int or $nte!er. 'ar e$nt7; / 5hi method i 4 ed to !et the 'rimitive data t('e o, a certain Strin!. ab 7; 0 "et4rn the ab o#4te va#4e o, the ar!4ment. cei#7; 8 "et4rn the ma##e t inte!er that i !reater than or e@4a# to the ar!4ment. "et4rned a a do4b#e. ,#oor7; - "et4rn the #ar!e t inte!er that i #e than or e@4a# to the ar!4ment. "et4rned a a do4b#e. rint7; 10 "et4rn the inte!er that i c#o e t in va#4e to the ar!4ment. "et4rned a a do4b#e. ro4nd7; 11 "et4rn the c#o e t #on! or int, a indicated b( the method? ret4rn t('e, to the ar!4ment. 12 min7; +2+ 3imited i a S4b idiar( o, 5+S 3imited 11

"et4rn the ma##er o, the two ar!4ment . ma<7; 11 "et4rn the #ar!er o, the two ar!4ment . e<'7; 1D "et4rn the ba e o, the nat4ra# #o!arithm , e, to the 'ower o, the ar!4ment. #o!7; 1. "et4rn the nat4ra# #o!arithm o, the ar!4ment. 'ow7; 1/ "et4rn the va#4e o, the ,ir t ar!4ment rai ed to the 'ower o, the econd ar!4ment. @rt7; 10 "et4rn the @4are root o, the ar!4ment. in7; 18 "et4rn the ine o, the 'eci,ied do4b#e va#4e. co 7; 1"et4rn the co ine o, the 'eci,ied do4b#e va#4e. tan7; 20 "et4rn the tan!ent o, the 'eci,ied do4b#e va#4e. a in7; 21 "et4rn the arc ine o, the 'eci,ied do4b#e va#4e. aco 7; 22 "et4rn the arcco ine o, the 'eci,ied do4b#e va#4e. atan7; 21 "et4rn the arctan!ent o, the 'eci,ied do4b#e va#4e. atan27; 2D +onvert rectan!4#ar coordinate 7<, (; to 'o#ar coordinate 7r, theta; and ret4rn theta. toDe!ree 7; 2. +onvert the ar!4ment to de!ree to"adian 7; 2/ +onvert the ar!4ment to radian . random7; 20 "et4rn a random n4mber. Ja)a $ 7haracter 7la%% Norma##(, when we work with character , we 4 e 'rimitive data t('e char. E,a!"le: char ch K ?a?J GG =nicode ,or 4''erca e Greek ome!a character char 4ni+har K ?I401-)?J GG an arra( o, char char8: char)rra( KN ?a?, ?b?, ?c?, ?d?, ?e? QJ Cowever in deve#o'ment we come acro it4ation were we need to 4 e ob*ect in tead o, 'rimitive data t('e . $n-order to achieve thi 6ava 'rovide wra''er c#a e 7haracter ,or 'rimitive data t('e char. 5he +haracter c#a o,,er a n4mber o, 4 e,4# c#a 7i.e., tatic; method ,or mani'4#atin! character . Fo4 can create a +haracter ob*ect with the +haracter con tr4ctor: +haracter ch K new +haracter7?a?;J 5he 6ava com'i#er wi## a# o create a +haracter ob*ect ,or (o4 4nder ome circ4m tance . %or e<am'#e, i, (o4 'a a 'rimitive char into a method that e<'ect an ob*ect, the com'i#er a4tomatica##( convert the char to a +haracter ,or (o4. 5hi ,eat4re i ca##ed a4tobo<in! or 4nbo<in!, i, the conver ion !oe the other wa(. E,a!"le: GG Cere ,o##owin! 'rimitive char ?a? GG i bo<ed into the +haracter ob*ect ch +2+ 3imited i a S4b idiar( o, 5+S 3imited 12

+haracter ch K ?a?J GG Cere 'rimitive ?<? i bo<ed ,or method te t, GG ret4rn i 4nbo<ed to char ?c? char c K te t7?<?;J E%ca"e #e@uence%: ) character 'receded b( a back #a h 7I; i an e ca'e e@4ence and ha 'ecia# meanin! to the com'i#er. 5he new#ine character 7In; ha been 4 ed ,re@4ent#( in thi t4toria# in S( tem.o4t.'rint#n7; tatement to advance to the ne<t #ine a,ter the trin! i 'rinted. %o##owin! tab#e how the 6ava e ca'e e@4ence : E%ca"e De%cri"ti n #e@uence It $n ert a tab in the te<t at thi 'oint. Ib $n ert a back 'ace in the te<t at thi 'oint. In $n ert a new#ine in the te<t at thi 'oint. Ir $n ert a carria!e ret4rn in the te<t at thi 'oint. I, $n ert a ,orm ,eed in the te<t at thi 'oint. $n ert a in!#e @4ote character in the te<t at thi I? 'oint. $n ert a do4b#e @4ote character in the te<t at thi IP 'oint. II $n ert a back #a h character in the te<t at thi 'oint. When an e ca'e e@4ence i enco4ntered in a 'rint tatement, the com'i#er inter'ret it accordin!#(. E,a!"le: $, (o4 want to '4t @4ote within @4ote (o4 m4 t 4 e the e ca'e e@4ence, IP, on the interior @4ote : '4b#ic c#a 5e t N '4b#ic tatic void main7Strin! ar! 8:; N S( tem.o4t.'rint#n7PShe aid IPCe##oWIP to me.P;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: She aid PCe##oWP to me. 7haracter &eth d%: Cere i the #i t o, the im'ortant in tance method that a## the 4bc#a e o, the +haracter c#a im'#ement: # &eth d% with De%cri"ti n N i 3etter7; 1 Determine whether the 'eci,ied char va#4e i a #etter. i Di!it7; 2 Determine whether the 'eci,ied char va#4e i a di!it. i White 'ace7; 1 Determine whether the 'eci,ied char va#4e i white 'ace. i =''er+a e7; D Determine whether the 'eci,ied char va#4e i 4''erca e. i 3ower+a e7; . Determine whether the 'eci,ied char va#4e i #owerca e. to=''er+a e7; / "et4rn the 4''erca e ,orm o, the 'eci,ied char va#4e. to3ower+a e7; 0 "et4rn the #owerca e ,orm o, the 'eci,ied char va#4e. 8 toStrin!7; "et4rn a Strin! ob*ect re're entin! the 'eci,ied character va#4ethat i , a one+2+ 3imited i a S4b idiar( o, 5+S 3imited

11

character trin!. %or a com'#ete #i t o, method , '#ea e re,er to the *ava.#an!.+haracter )P$ 'eci,ication. Ja)a $ #trin( 7la%% Strin! , which are wide#( 4 ed in 6ava 'ro!rammin!, are a e@4ence o, character . $n the 6ava 'ro!rammin! #an!4a!e, trin! are ob*ect . 5he 6ava '#at,orm 'rovide the Strin! c#a to create and mani'4#ate trin! . 7reatin( #trin(%: 5he mo t direct wa( to create a trin! i to write: Strin! !reetin! K PCe##o wor#dWPJ Whenever it enco4nter a trin! #itera# in (o4r code, the com'i#er create a Strin! ob*ect with it va#4ein thi ca e, PCe##o wor#dW?. ) with an( other ob*ect, (o4 can create Strin! ob*ect b( 4 in! the new ke(word and a con tr4ctor. 5he Strin! c#a ha e#even con tr4ctor that a##ow (o4 to 'rovide the initia# va#4e o, the trin! 4 in! di,,erent o4rce , 4ch a an arra( o, character : '4b#ic c#a Strin!DemoN '4b#ic tatic void main7Strin! ar! 8:;N char8: he##o)rra( K N ?h?, ?e?, ?#?, ?#?, ?o?, ?.?QJ Strin! he##oStrin! K new Strin!7he##o)rra(;J S( tem.o4t.'rint#n7 he##oStrin! ;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: he##o N te: 5he Strin! c#a i imm4tab#e, o that once it i created a Strin! ob*ect cannot be chan!ed. $, there i a nece it( to make a#ot o, modi,ication to Strin! o, character then (o4 ho4#d 4 e Strin! E4,,er X Strin! E4i#der +#a e . Ja)a $ #trin( 6uffer A #trin( 6uilder 7la%%e% 5he #trin(6uffer and #trin(6uilder c#a e are 4 ed when there i a nece it( to make a#ot o, modi,ication to Strin! o, character . =n#ike Strin! ob*ect o, t('e Strin!E4,,er and Strin!b4i#der can be modi,ied over and over a!ain with o4t #eavin! behind a#ot o, new 4n4 ed ob*ect . 5he Strin!E4i#der c#a wa introd4ced a o, 6ava . and the main di,,erence between the Strin!E4,,er and Strin!E4i#der i that Strin!E4i#der method are not thread a,e7not S(nchroni ed;. $t i recommended to 4 e #trin(6uilder whenever 'o ib#e beca4 e it i ,a ter than Strin!E4,,er. Cowever i, thread a,et( i nece ar( the be t o'tion i Strin!E4,,er ob*ect . E,a!"le: '4b#ic c#a 5e tN '4b#ic tatic void main7Strin! ar! 8:;N Strin!E4,,er E4,,er K new Strin!E4,,er7P te tP;J E4,,er.a''end7P Strin! E4,,erP;J S( tem.o4.'rint#n7 E4,,er;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: te t Strin! E4,,er #trin(6uffer &eth d%: Cere i the #i t o, im'ortant method 4''orted b( Strin!E4,,er c#a : # &eth d% with De%cri"ti n N '4b#ic Strin!E4,,er a''end7Strin! ; 1 ='date the va#4e o, the ob*ect that invoked the method. 5he method take boo#ean, char, int, #on!, Strin! etc. '4b#ic Strin!E4,,er rever e7; 2 5he method rever e the va#4e o, the Strin!E4,,er ob*ect that invoked the method. 1 '4b#ic de#ete7int tart, int end; +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D

De#ete the trin! tartin! ,rom tart inde< 4nti# end inde<. '4b#ic in ert7int o,, et, int i; D 5hi method in ert an trin! at the 'o ition mentioned b( o,, et. re'#ace7int tart, int end, Strin! tr; . 5hi method re'#ace the character in a 4b trin! o, thi Strin!E4,,er with character in the 'eci,ied Strin!. Cere i the #i t o, other method 79<ce't et method ; which are ver( imi#ar to Strin! c#a : # &eth d% with De%cri"ti n N int ca"acity8; 1 "et4rn the c4rrent ca'acit( o, the Strin! b4,,er. char charAt8int inde,; 2 5he 'eci,ied character o, the e@4ence c4rrent#( re're ented b( the trin! b4,,er, a indicated b( the inde< ar!4ment, i ret4rned. ) id en%ure7a"acity8int !ini!u!7a"acity; 1 9n 4re that the ca'acit( o, the b4,,er i at #ea t e@4a# to the 'eci,ied minim4m. ) id (et7har%8int %rc6e(in, int %rcEnd, char9: d%t, int d%t6e(in; D +haracter are co'ied ,rom thi trin! b4,,er into the de tination character arra( d t. int inde,Of8#trin( %tr; . "et4rn the inde< within thi trin! o, the ,ir t occ4rrence o, the 'eci,ied 4b trin!. int inde,Of8#trin( %tr, int fr !Inde,; / "et4rn the inde< within thi trin! o, the ,ir t occ4rrence o, the 'eci,ied 4b trin!, tartin! at the 'eci,ied inde<. int la%tInde,Of8#trin( %tr; 0 "et4rn the inde< within thi trin! o, the ri!htmo t occ4rrence o, the 'eci,ied 4b trin!. int la%tInde,Of8#trin( %tr, int fr !Inde,; 8 "et4rn the inde< within thi trin! o, the #a t occ4rrence o, the 'eci,ied 4b trin!. int len(th8; "et4rn the #en!th 7character co4nt; o, thi trin! b4,,er. ) id %et7harAt8int inde,, char ch; 10 5he character at the 'eci,ied inde< o, thi trin! b4,,er i et to ch. ) id %et4en(th8int new4en(th; 11 Set the #en!th o, thi Strin! b4,,er. 7har#e@uence %ub#e@uence8int %tart, int end; 12 "et4rn a new character e@4ence that i a 4b e@4ence o, thi e@4ence. #trin( %ub%trin(8int %tart; "et4rn a new Strin! that contain a 4b e@4ence o, character c4rrent#( contained in 11 thi Strin!E4,,er.5he 4b trin! be!in at the 'eci,ied inde< and e<tend to the end o, the Strin!E4,,er. #trin( %ub%trin(8int %tart, int end; 1D "et4rn a new Strin! that contain a 4b e@4ence o, character c4rrent#( contained in thi Strin!E4,,er. #trin( t #trin(8; 1. +onvert to a trin! re're entin! the data in thi trin! b4,,er.

#trin( 4en(th: 2ethod 4 ed to obtain in,ormation abo4t an ob*ect are known a acce or method . &ne acce or method that (o4 can 4 e with trin! i the #en!th7; method, which ret4rn the n4mber o, character contained in the trin! ob*ect. ),ter the ,o##owin! two #ine o, code have been e<ec4ted, #en e@4a# 10: '4b#ic c#a Strin!Demo N +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.

'4b#ic tatic void main7Strin! ar! 8:; N Strin! 'a#indrome K PDot aw $ wa 5odPJ int #en K 'a#indrome.#en!th7;J S( tem.o4t.'rint#n7 PStrin! 3en!th i : P > #en ;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Strin! 3en!th i : 10 7 ncatenatin( #trin(%: 5he Strin! c#a inc#4de a method ,or concatenatin! two trin! : trin!1.concat7 trin!2;J 5hi ret4rn a new trin! that i trin!1 with trin!2 added to it at the end. Fo4 can a# o 4 e the concat7; method with trin! #itera# , a in: P2( name i P.concat7PSaraP;J Strin! are more common#( concatenated with the > o'erator, a in: PCe##o,P > P wor#dP > PWP which re 4#t in: PCe##o, wor#dWP 3et 4 #ook at the ,o##owin!e e<am'#e: '4b#ic c#a Strin!Demo N '4b#ic tatic void main7Strin! ar! 8:; N Strin! trin!1 K P aw $ wa PJ S( tem.o4t.'rint#n7PDot P > trin!1 > P5odP;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Dot aw $ wa 5od 7reatin( F r!at #trin(%: Fo4 have 'rint,7; and ,ormat7; method to 'rint o4t'4t with ,ormatted n4mber . 5he Strin! c#a ha an e@4iva#ent c#a method, ,ormat7;, that ret4rn a Strin! ob*ect rather than a PrintStream ob*ect. = in! Strin!? tatic ,ormat7; method a##ow (o4 to create a ,ormatted trin! that (o4 can re4 e, a o''o ed to a one-time 'rint tatement. %or e<am'#e, in tead o,: S( tem.o4t.'rint,7P5he va#4e o, the ,#oat variab#e i P > PL,, whi#e the va#4e o, the inte!er P > Pvariab#e i Ld, and the trin! P > Pi L P, ,#oatVar, intVar, trin!Var;J (o4 can write: Strin! , J , K Strin!.,ormat7P5he va#4e o, the ,#oat variab#e i P > PL,, whi#e the va#4e o, the inte!er P > Pvariab#e i Ld, and the trin! P > Pi L P, ,#oatVar, intVar, trin!Var;J S( tem.o4t.'rint#n7, ;J #trin( &eth d%: Cere i the #i t method 4''orted b( Strin! c#a : # &eth d% with De%cri"ti n N char char)t7int inde<; 1 "et4rn the character at the 'eci,ied inde<. int com'are5o7&b*ect o; 2 +om'are thi Strin! to another &b*ect. int com'are5o7Strin! anotherStrin!; 1 +om'are two trin! #e<ico!ra'hica##(. int com'are5o$!nore+a e7Strin! tr; D +om'are two trin! #e<ico!ra'hica##(, i!norin! ca e di,,erence . . Strin! concat7Strin! tr; +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/

/ 0 8 10 11 12 11 1D 1. 1/ 10 18 120 21 22 21 2D 2. 2/ 20 28 2-

+oncatenate the 'eci,ied trin! to the end o, thi trin!. boo#ean content9@4a# 7Strin!E4,,er b; "et4rn tr4e i, and on#( i, thi Strin! re're ent the ame e@4ence o, character a the 'eci,ied Strin!E4,,er. tatic Strin! co'(Va#4e&,7char8: data; "et4rn a Strin! that re're ent the character e@4ence in the arra( 'eci,ied. tatic Strin! co'(Va#4e&,7char8: data, int o,, et, int co4nt; "et4rn a Strin! that re're ent the character e@4ence in the arra( 'eci,ied. boo#ean end With7Strin! 4,,i<; 5e t i, thi trin! end with the 'eci,ied 4,,i<. boo#ean e@4a# 7&b*ect an&b*ect; +om'are thi trin! to the 'eci,ied ob*ect. boo#ean e@4a# $!nore+a e7Strin! anotherStrin!; +om'are thi Strin! to another Strin!, i!norin! ca e con ideration . 9ncode thi Strin! into a e@4ence o, b(te 4 in! the '#at,orm? de,a4#t char et, torin! the re 4#t into a new b(te arra(. 9ncode thi Strin! into a e@4ence o, b(te 4 in! the named char et, torin! the re 4#t into a new b(te arra(. void !et+har 7int rcEe!in, int rc9nd, char8: d t, int d tEe!in; +o'ie character ,rom thi trin! into the de tination character arra(. int ha h+ode7; "et4rn a ha h code ,or thi trin!. int inde<&,7int ch; "et4rn the inde< within thi trin! o, the ,ir t occ4rrence o, the 'eci,ied character. int inde<&,7int ch, int ,rom$nde<; "et4rn the inde< within thi trin! o, the ,ir t occ4rrence o, the 'eci,ied character, tartin! the earch at the 'eci,ied inde<. int inde<&,7Strin! tr; "et4rn the inde< within thi trin! o, the ,ir t occ4rrence o, the 'eci,ied 4b trin!. int inde<&,7Strin! tr, int ,rom$nde<; "et4rn the inde< within thi trin! o, the ,ir t occ4rrence o, the 'eci,ied 4b trin!, tartin! at the 'eci,ied inde<. Strin! intern7; "et4rn a canonica# re're entation ,or the trin! ob*ect. int #a t$nde<&,7int ch; "et4rn the inde< within thi trin! o, the #a t occ4rrence o, the 'eci,ied character. int #a t$nde<&,7int ch, int ,rom$nde<; "et4rn the inde< within thi trin! o, the #a t occ4rrence o, the 'eci,ied character, earchin! backward tartin! at the 'eci,ied inde<. int #a t$nde<&,7Strin! tr; "et4rn the inde< within thi trin! o, the ri!htmo t occ4rrence o, the 'eci,ied 4b trin!. int #a t$nde<&,7Strin! tr, int ,rom$nde<; "et4rn the inde< within thi trin! o, the #a t occ4rrence o, the 'eci,ied 4b trin!, earchin! backward tartin! at the 'eci,ied inde<. int #en!th7; "et4rn the #en!th o, thi trin!. boo#ean matche 7Strin! re!e<; 5e## whether or not thi trin! matche the !iven re!4#ar e<'re ion. boo#ean re!ion2atche 7boo#ean i!nore+a e, int to,, et, Strin! other, int oo,, et, int #en; 5e t i, two trin! re!ion are e@4a#. 5e t i, two trin! re!ion are e@4a#. "et4rn a new trin! re 4#tin! ,rom re'#acin! a## occ4rrence o, o#d+har in thi trin! with +2+ 3imited i a S4b idiar( o, 5+S 3imited

10

new+har. "e'#ace each 4b trin! o, thi trin! that matche the !iven re!4#ar e<'re ion with the 10 !iven re'#acement. "e'#ace the ,ir t 4b trin! o, thi trin! that matche the !iven re!4#ar e<'re ion with 11 the !iven re'#acement. 12 S'#it thi trin! aro4nd matche o, the !iven re!4#ar e<'re ion. 11 S'#it thi trin! aro4nd matche o, the !iven re!4#ar e<'re ion. 1D 5e t i, thi trin! tart with the 'eci,ied 're,i<. 1. 5e t i, thi trin! tart with the 'eci,ied 're,i< be!innin! a 'eci,ied inde<. 1/ "et4rn a new character e@4ence that i a 4b e@4ence o, thi e@4ence. 10 "et4rn a new trin! that i a 4b trin! o, thi trin!. 18 "et4rn a new trin! that i a 4b trin! o, thi trin!. 1- +onvert thi trin! to a new character arra(. +onvert a## o, the character in thi Strin! to #ower ca e 4 in! the r4#e o, the de,a4#t D0 #oca#e. +onvert a## o, the character in thi Strin! to #ower ca e 4 in! the r4#e o, the !iven D1 3oca#e. D2 5hi ob*ect 7which i a#read( a trin!W; i it e#, ret4rned. +onvert a## o, the character in thi Strin! to 4''er ca e 4 in! the r4#e o, the de,a4#t D1 #oca#e. +onvert a## o, the character in thi Strin! to 4''er ca e 4 in! the r4#e o, the !iven DD 3oca#e. D. "et4rn a co'( o, the trin!, with #eadin! and trai#in! white 'ace omitted. D/ "et4rn the trin! re're entation o, the 'a ed data t('e ar!4ment. Ja)a $ Array% 6ava 'rovide a data tr4ct4re, the array, which tore a ,i<ed- iBe e@4entia# co##ection o, e#ement o, the ame t('e. )n arra( i 4 ed to tore a co##ection o, data, b4t it i o,ten more 4 e,4# to think o, an arra( a a co##ection o, variab#e o, the ame t('e. $n tead o, dec#arin! individ4a# variab#e , 4ch a n4mber0, n4mber1, ..., and n4mber--, (o4 dec#are one arra( variab#e 4ch a n4mber and 4 e n4mber 80:, n4mber 81:, and ..., n4mber 8--: to re're ent individ4a# variab#e . 5hi t4toria# introd4ce how to dec#are arra( variab#e , create arra( , and 'roce arra( 4 in! inde<ed variab#e . Declarin( Array Variable%: 5o 4 e an arra( in a 'ro!ram, (o4 m4 t dec#are a variab#e to re,erence the arra(, and (o4 m4 t 'eci,( the t('e o, arra( the variab#e can re,erence. Cere i the (nta< ,or dec#arin! an arra( variab#e: data5('e8: arra("e,VarJ GG 're,erred wa(. or data5('e arra("e,Var8:J GG work b4t not 're,erred wa(. N te: 5he t(#e data*y"e9: arrayRefVar i 're,erred. 5he t(#e data*y"e arrayRefVar9: come ,rom the +G+>> #an!4a!e and wa ado'ted in 6ava to accommodate +G+>> 'ro!rammer . E,a!"le: 5he ,o##owin! code ni''et are e<am'#e o, thi (nta<: do4b#e8: m(3i tJ GG 're,erred wa(. or do4b#e m(3i t8:J GG work b4t not 're,erred wa(. 7reatin( Array%: Fo4 can create an arra( b( 4 in! the new o'erator with the ,o##owin! (nta<: arra("e,Var K new data5('e8arra(SiBe:J 5he above tatement doe two thin! : $t create an arra( 4 in! new data5('e8arra(SiBe:J $t a i!n the re,erence o, the new#( created arra( to the variab#e arra("e,Var. Dec#arin! an arra( variab#e, creatin! an arra(, and a i!nin! the re,erence o, the arra( to the variab#e can be combined in one tatement, a hown be#ow: data5('e8: arra("e,Var K new data5('e8arra(SiBe:J +2+ 3imited i a S4b idiar( o, 5+S 3imited 18

)#ternative#( (o4 can create arra( a ,o##ow : data5('e8: arra("e,Var K Nva#4e0, va#4e1, ..., va#4ekQJ 5he arra( e#ement are acce ed thro4!h the inde,. )rra( indice are 0-ba edJ that i , the( tart ,rom 0 to arrayRefVar+len(th$B. E,a!"le: %o##owin! tatement dec#are an arra( variab#e, m(3i t, create an arra( o, 10 e#ement o, do4b#e t('e, and a i!n it re,erence to m(3i t.: do4b#e8: m(3i t K new do4b#e810:J %o##owin! 'ict4re re're ent arra( m(3i t. Cere m(3i t ho#d ten do4b#e va#4e and the indice are ,rom 0 to -.

Pr ce%%in( Array%: When 'roce in! arra( e#ement , we o,ten 4 e either ,or #oo' or ,oreach #oo' beca4 e a## o, the e#ement in an arra( are o, the ame t('e and the iBe o, the arra( i known. E,a!"le: Cere i a com'#ete e<am'#e o, howin! how to create, initia#iBe and 'roce arra( : '4b#ic c#a 5e t)rra( N '4b#ic tatic void main7Strin!8: ar! ; N do4b#e8: m(3i t K N1.-, 2.-, 1.D, 1..QJ GG Print a## the arra( e#ement ,or 7int i K 0J i V m(3i t.#en!thJ i>>; N S( tem.o4t.'rint#n7m(3i t8i: > P P;J Q GG S4mmin! a## e#ement do4b#e tota# K 0J ,or 7int i K 0J i V m(3i t.#en!thJ i>>; N tota# >K m(3i t8i:J Q S( tem.o4t.'rint#n7P5ota# i P > tota#;J GG %indin! the #ar!e t e#ement do4b#e ma< K m(3i t80:J ,or 7int i K 1J i V m(3i t.#en!thJ i>>; N i, 7m(3i t8i: R ma<; ma< K m(3i t8i:J Q S( tem.o4t.'rint#n7P2a< i P > ma<;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 1.2.1.D 1.. 5ota# i 11.0 +2+ 3imited i a S4b idiar( o, 5+S 3imited 1-

2a< i 1.. *he f reach 4 "%: 6DH 1.. introd4ced a new ,or #oo', known a ,oreach #oo' or enhanced ,or #oo', which enab#e (o4 to traver e the com'#ete arra( e@4entia##( witho4t 4 in! an inde< variab#e. E,a!"le: 5he ,o##owin! code di '#a( a## the e#ement in the arra( m(3i t: '4b#ic c#a 5e t)rra( N '4b#ic tatic void main7Strin!8: ar! ; N do4b#e8: m(3i t K N1.-, 2.-, 1.D, 1..QJ GG Print a## the arra( e#ement ,or 7do4b#e e#ement: m(3i t; N S( tem.o4t.'rint#n7e#ement;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 1.2.1.D 1.. Pa%%in( Array% t &eth d%: 64 t a (o4 can 'a 'rimitive t('e va#4e to method , (o4 can a# o 'a arra( to method . %or e<am'#e, the ,o##owin! method di '#a( the e#ement in an int arra(: '4b#ic tatic void 'rint)rra(7int8: arra(; N ,or 7int i K 0J i V arra(.#en!thJ i>>; N S( tem.o4t.'rint7arra(8i: > P P;J Q Q Fo4 can invoke it b( 'a in! an arra(. %or e<am'#e, the ,o##owin! tatement invoke the 'rint)rra( method to di '#a( 1, 1, 2, /, D, and 2: 'rint)rra(7new int8:N1, 1, 2, /, D, 2Q;J Returnin( an Array fr ! a &eth d: ) method ma( a# o ret4rn an arra(. %or e<am'#e, the method hown be#ow ret4rn an arra( that i the rever a# o, another arra(: '4b#ic tatic int8: rever e7int8: #i t; N int8: re 4#t K new int8#i t.#en!th:J ,or 7int i K 0, * K re 4#t.#en!th - 1J i V #i t.#en!thJ i>>, *--; N re 4#t8*: K #i t8i:J Q ret4rn re 4#tJ Q *he Array% 7la%%: 5he *ava.4ti#.)rra( c#a contain vario4 tatic method ,or ortin! and earchin! arra( , com'arin! arra( , and ,i##in! arra( e#ement . 5he e method are over#oaded ,or a## 'rimitive t('e . # &eth d% with De%cri"ti n N "ublic %tatic int binary#earch8Object9: a, Object =ey; Searche the 'eci,ied arra( o, &b*ect 7 E(te, $nt , do4b#e etc; ,or the 'eci,ied va#4e 1 4 in! the binar( earch a#!orithm. 5he arra( m4 t be orted 'rior to makin! thi ca##. 5hi ret4rn inde< o, the earch ke(, i, it i contained in the #i tJ otherwi e, 7-7in ertion 'oint > 1;. 2 "ublic %tatic b lean e@ual%8l n(9: a, l n(9: a-; "et4rn tr4e i, the two 'eci,ied arra( o, #on! are e@4a# to one another. 5wo arra( are con idered e@4a# i, both arra( contain the ame n4mber o, e#ement , and a## +2+ 3imited i a S4b idiar( o, 5+S 3imited D0

corre 'ondin! 'air o, e#ement in the two arra( are e@4a#. 5hi ret4rn tr4e i, the two arra( are e@4a#. Same method co4#d be 4 ed b( a## other 'remitive data t('e 7 E(te, hort, $nt etc.; "ublic %tatic ) id fill8int9: a, int )al; 1 ) i!n the 'eci,ied int va#4e to each e#ement o, the 'eci,ied arra( o, int . Same method co4#d be 4 ed b( a## other 'remitive data t('e 7 E(te, hort, $nt etc.; "ublic %tatic ) id % rt8Object9: a; Sort the 'eci,ied arra( o, ob*ect into a cendin! order, accordin! to the nat4ra# orderin! D o, it e#ement . Same method co4#d be 4 ed b( a## other 'remitive data t('e 7 E(te, hort, $nt etc.; Ja)a $ Date A *i!e 6ava 'rovide the Date c#a avai#ab#e in ja)a+util 'acka!e, thi c#a enca' 4#ate the c4rrent date and time. 5he Date c#a 4''ort two con tr4ctor . 5he ,ir t con tr4ctor initia#iBe the ob*ect with the c4rrent date and time. Date7 ; 5he ,o##owin! con tr4ctor acce't one ar!4ment that e@4a# the n4mber o, mi##i econd that have e#a' ed ince midni!ht, 6an4ar( 1, 1-00 Date7#on! mi##i ec; &nce (o4 have a Date ob*ect avai#ab#e, (o4 can ca## an( o, the ,o##owin! 4''ort method to '#a( with date : # &eth d% with De%cri"ti n N b lean after8Date date; 1 "et4rn tr4e i, the invokin! Date ob*ect contain a date that i #ater than the one 'eci,ied b( date, otherwi e, it ret4rn ,a# e. b lean bef re8Date date; 2 "et4rn tr4e i, the invokin! Date ob*ect contain a date that i ear#ier than the one 'eci,ied b( date, otherwi e, it ret4rn ,a# e. Object cl ne8 ; 1 D4'#icate the invokin! Date ob*ect. int c !"are* 8Date date; +om'are the va#4e o, the invokin! ob*ect with that o, date. "et4rn 0 i, the va#4e are D e@4a#. "et4rn a ne!ative va#4e i, the invokin! ob*ect i ear#ier than date. "et4rn a 'o itive va#4e i, the invokin! ob*ect i #ater than date. int c !"are* 8Object bj; . &'erate identica##( to com'are5o7Date; i, ob* i o, c#a Date. &therwi e, it throw a +#a +a t9<ce'tion. b lean e@ual%8Object date; / "et4rn tr4e i, the invokin! Date ob*ect contain the ame time and date a the one 'eci,ied b( date, otherwi e, it ret4rn ,a# e. l n( (et*i!e8 ; 0 "et4rn the n4mber o, mi##i econd that have e#a' ed ince 6an4ar( 1, 1-00. int ha%h7 de8 ; 8 "et4rn a ha h code ,or the invokin! ob*ect. ) id %et*i!e8l n( ti!e; - Set the time and date a 'eci,ied b( time, which re're ent an e#a' ed time in mi##i econd ,rom midni!ht, 6an4ar( 1, 1-00 #trin( t #trin(8 ; 10 +onvert the invokin! Date ob*ect into a trin! and ret4rn the re 4#t. Cettin( 7urrent Date A *i!e 5hi i ver( ea ( to !et c4rrent date and time in 6ava. Fo4 can 4 e a im'#e Date ob*ect with to.tri$g ! method to 'rint c4rrent date and time a ,o##ow : +2+ 3imited i a S4b idiar( o, 5+S 3imited D1

im'ort *ava.4ti#.DateJ '4b#ic c#a DateDemo N '4b#ic tatic void main7Strin! ar! 8:; N GG $n tantiate a Date ob*ect Date date K new Date7;J GG di '#a( time and date 4 in! toStrin!7; S( tem.o4t.'rint#n7date.toStrin!7;;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 2on 2a( 0D 0-:.1:.2 +D5 200Date 7 !"ari% n: 5here are ,o##owin! three wa( to com'are two date : Fo4 can 4 e !et5ime7 ; to obtain the n4mber o, mi##i econd that have e#a' ed ince midni!ht, 6an4ar( 1, 1-00, ,or both ob*ect and then com'are the e two va#4e . Fo4 can 4 e the method be,ore7 ;, a,ter7 ;, and e@4a# 7 ;. Eeca4 e the 12th o, the month come be,ore the 18th, ,or e<am'#e, new Date7--, 2, 12;.be,ore7new Date 7--, 2, 18;; ret4rn tr4e. Fo4 can 4 e the com'are5o7 ; method, which i de,ined b( the +om'arab#e inter,ace and im'#emented b( Date. Date F r!attin( u%in( #i!"leDateF r!at: Sim'#eDate%ormat i a concrete c#a ,or ,ormattin! and 'ar in! date in a #oca#e- en itive manner. Sim'#eDate%ormat a##ow (o4 to tart b( choo in! an( 4 er-de,ined 'attern ,or date-time ,ormattin!. %or e<am'#e: im'ort *ava.4ti#.OJ im'ort *ava.te<t.OJ '4b#ic c#a DateDemo N '4b#ic tatic void main7Strin! ar! 8:; N Date dNow K new Date7 ;J Sim'#eDate%ormat ,t K new Sim'#eDate%ormat 7P9 ((((.22.dd ?at? hh:mm: S( tem.o4t.'rint#n7P+4rrent Date: P > ,t.,ormat7dNow;;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: +4rrent Date: S4n 200D.00.18 at 0D:1D:0- P2 PD5 #i!"le DateF r!at f r!at c de%: 5o 'eci,( the time ,ormat 4 e a time 'attern trin!. $n thi 'attern, a## )S+$$ #etter are re erved a 'attern #etter , which are de,ined a the ,o##owin!: 7haract De%cri"ti n E,a!"le er G 9ra de i!nator )D ( Fear in ,o4r di!it 2001 2 2onth in (ear 64#( or 00 d Da( in month 10 Co4r in ).2.GP.2. h 12 71Z12; C Co4r in da( 70Z21; 22 m 2in4te in ho4r 10 Second in min4te .. S 2i##i econd 21D 9 Da( in week 54e da( D Da( in (ear 1/0 +2+ 3imited i a S4b idiar( o, 5+S 3imited D2

a BBBP;J

% w W a k H B

Da( o, week in month Week in (ear Week in month ).2.GP.2. marker Co4r in da( 71Z2D; Co4r in ).2.GP.2. 70Z11; 5ime Bone

2 7 econd Wed. in 64#(; D0 1 P2 2D 10 9a tern Standard 5ime De#imiter \

? 9 ca'e ,or te<t P Sin!#e @4ote Date F r!attin( u%in( "rintf: Date and time ,ormattin! can be done ver( ea i#( 4 in! "rintf method. Fo4 4 e a two-#etter ,ormat, tartin! with t and endin! in one o, the #etter o, the tab#e !iven be#ow. %or e<am'#e: im'ort *ava.4ti#.DateJ '4b#ic c#a DateDemo N

'4b#ic tatic void main7Strin! ar! 8:; N GG $n tantiate a Date ob*ect Date date K new Date7;J GG di '#a( time and date 4 in! toStrin!7; Strin! tr K Strin!.,ormat7P+4rrent DateG5ime : LtcP, date ;J S( tem.o4t.'rint,7 tr;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: +4rrent DateG5ime : Sat Dec 1. 1/:10:.0 2S5 2012 $t wo4#d be a bit i##( i, (o4 had to 4''#( the date m4#ti'#e time to ,ormat each 'art. %or that rea on, a ,ormat trin! can indicate the inde< o, the ar!4ment to be ,ormatted. 5he inde< m4 t immediate#( ,o##ow the L, and it m4 t be terminated b( a M. %or e<am'#e: im'ort *ava.4ti#.DateJ '4b#ic c#a DateDemo N '4b#ic tatic void main7Strin! ar! 8:; N GG $n tantiate a Date ob*ect Date date K new Date7;J GG di '#a( time and date 4 in! toStrin!7; S( tem.o4t.'rint,7PL1M L2MtE L2Mtd, L2MtFP, PD4e date:P, date;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: D4e date: %ebr4ar( 0-, 200D )#ternative#(, (o4 can 4 e the V ,#a!. $t indicate that the ame ar!4ment a in the 'recedin! ,ormat 'eci,ication ho4#d be 4 ed a!ain. %or e<am'#e: im'ort *ava.4ti#.DateJ '4b#ic c#a DateDemo N

'4b#ic tatic void main7Strin! ar! 8:; N GG $n tantiate a Date ob*ect Date date K new Date7;J +2+ 3imited i a S4b idiar( o, 5+S 3imited D1

GG di '#a( ,ormatted date S( tem.o4t.'rint,7PL LtE LVte, LVtFP, PD4e date:P, date;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: D4e date: %ebr4ar( 0-, 200D Date and *i!e 7 n)er%i n 7haracter%: 7haract De%cri"ti n er c % D 5 r " F ( + E b n d e ) a * +om'#ete date and time

E,a!"le 2on 2a( 0D 0-:.1:.2 +D5 200200D-02-002G0-G200D 18:0.:10/:0.:1- 'm 18:0. 200D 0D 20 %ebr4ar( %eb 02 01 2onda( 2on 0/-

$S& 8/01 date =.S. ,ormatted date 7monthGda(G(ear; 2D-ho4r time 12-ho4r time 2D-ho4r time, no econd %o4r-di!it (ear 7with #eadin! Beroe ; 3a t two di!it o, the (ear 7with #eadin! Beroe ; %ir t two di!it o, the (ear 7with #eadin! Beroe ; %4## month name )bbreviated month name 5wo-di!it month 7with #eadin! Beroe ; 5wo-di!it da( 7with #eadin! Beroe ; 5wo-di!it da( 7witho4t #eadin! Beroe ; %4## weekda( name )bbreviated weekda( name 5hree-di!it da( o, (ear 7with #eadin! Beroe ; 5wo-di!it ho4r 7with #eadin! Beroe ;, between 00 and C 18 21 5wo-di!it ho4r 7witho4t #eadin! Beroe ;, between 0 k 18 and 21 5wo-di!it ho4r 7with #eadin! Beroe ;, between 01 and $ 0/ 12 5wo-di!it ho4r 7witho4t #eadin! Beroe ;, between 1 # / and 12 2 5wo-di!it min4te 7with #eadin! Beroe ; 0. S 5wo-di!it econd 7with #eadin! Beroe ; 13 5hree-di!it mi##i econd 7with #eadin! Beroe ; 0D0 N Nine-di!it nano econd 7with #eadin! Beroe ; 0D0000000 P =''erca e mornin! or a,ternoon marker P2 ' 3owerca e mornin! or a,ternoon marker 'm B "%+ 822 n4meric o,, et ,rom G25 -0800 S 5ime Bone PS5 Second ince 1-00-01-01 00:00:00 G25 100888D11] 2i##i econd ince 1-00-01-01 00:00:00 G25 100888D11-0D0 5here are other 4 e,4# c#a e re#ated to Date and time. %or more detai# (o4 can re,er to 6ava Standard doc4mentation. Par%in( #trin(% int Date%: 5he Sim'#eDate%ormat c#a ha ome additiona# method , notab#( 'ar e7 ; , which trie to 'ar e a trin! accordin! to the ,ormat tored in the !iven Sim'#eDate%ormat ob*ect. %or e<am'#e: im'ort *ava.4ti#.OJ im'ort *ava.te<t.OJ +2+ 3imited i a S4b idiar( o, 5+S 3imited DD

'4b#ic c#a

DateDemo N

'4b#ic tatic void main7Strin! ar! 8:; N Sim'#eDate%ormat ,t K new Sim'#eDate%ormat 7P((((-22-ddP;J Strin! in'4t K ar! .#en!th KK 0 [ P1818-11-11P : ar! 80:J S( tem.o4t.'rint7in'4t > P Par e a P;J Date tJ tr( N t K ,t.'ar e7in'4t;J S( tem.o4t.'rint#n7t;J Q catch 7Par e9<ce'tion e; N S( tem.o4t.'rint#n7P=n'ar eab#e 4 in! P > ,t;J Q Q Q ) am'#e r4n o, the above 'ro!ram wo4#d 'rod4ce ,o##owin! re 4#t: M *ava DateDemo 1818-11-11 Par e a Wed Nov 11 00:00:00 G25 1818 M *ava DateDemo 2000-12-01 2000-12-01 Par e a Sat Dec 01 00:00:00 G25 2000 #lee"in( f r a While: Fo4 can #ee' ,or an( 'eriod o, time ,rom one mi##i econd 4' to the #i,etime o, (o4r com'4ter. %or e<am'#e, ,o##owin! 'ro!ram wo4#d #ee' ,or 10 econd : im'ort *ava.4ti#.OJ '4b#ic c#a S#ee'Demo N '4b#ic tatic void main7Strin! ar! 8:; N tr( N S( tem.o4t.'rint#n7new Date7 ; > PInP;J 5hread. #ee'7.O/0O10;J S( tem.o4t.'rint#n7new Date7 ; > PInP;J Q catch 79<ce'tion e; N S( tem.o4t.'rint#n7PGot an e<ce'tionWP;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: S4n 2a( 01 18:0D:D1 G25 200S4n 2a( 01 18:0D:.1 G25 200&ea%urin( Ela"%ed *i!e: Sometime (o4 ma( need to mea 4re 'oint in time in mi##i econd . So #et? re-write above e<am'#e once a!ain: im'ort *ava.4ti#.OJ '4b#ic c#a Di,,Demo N

'4b#ic tatic void main7Strin! ar! 8:; N tr( N #on! tart K S( tem.c4rrent5ime2i##i 7 ;J S( tem.o4t.'rint#n7new Date7 ; > PInP;J 5hread. #ee'7.O/0O10;J S( tem.o4t.'rint#n7new Date7 ; > PInP;J #on! end K S( tem.c4rrent5ime2i##i 7 ;J +2+ 3imited i a S4b idiar( o, 5+S 3imited D.

#on! di,, K end - tartJ S( tem.o4t.'rint#n7PDi,,erence i : P > di,,;J Q catch 79<ce'tion e; N S( tem.o4t.'rint#n7PGot an e<ce'tionWP;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: S4n 2a( 01 18:1/:.1 G25 200S4n 2a( 01 18:1/:.0 G25 200Di,,erence i : .--1 Cre( rian7alendar 7la%%: Gre!orian+a#endar i a concrete im'#ementation o, a +a#endar c#a that im'#ement the norma# Gre!orian ca#endar with which (o4 are ,ami#iar. $ did not di c4 +a#ender c#a in thi t4toria#, (o4 can #ook tandard 6ava doc4mentation ,or thi . 5he (etIn%tance8 ; method o, +a#endar ret4rn a Gre!orian+a#endar initia#iBed with the c4rrent date and time in the de,a4#t #oca#e and time Bone. Gre!orian+a#endar de,ine two ,ie#d : )D and E+. 5he e re're ent the two era de,ined b( the Gre!orian ca#endar. 5here are a# o evera# con tr4ctor ,or Gre!orian+a#endar ob*ect : # 7 n%truct r with De%cri"ti n N Cre( rian7alendar8; 1 +on tr4ct a de,a4#t Gre!orian+a#endar 4 in! the c4rrent time in the de,a4#t time Bone with the de,a4#t #oca#e. Cre( rian7alendar8int year, int ! nth, int date; 2 +on tr4ct a Gre!orian+a#endar with the !iven date et in the de,a4#t time Bone with the de,a4#t #oca#e. Cre( rian7alendar8int year, int ! nth, int date, int h ur, int !inute; 1 +on tr4ct a Gre!orian+a#endar with the !iven date and time et ,or the de,a4#t time Bone with the de,a4#t #oca#e. Cre( rian7alendar8int year, int ! nth, int date, int h ur, int !inute, int %ec nd; D +on tr4ct a Gre!orian+a#endar with the !iven date and time et ,or the de,a4#t time Bone with the de,a4#t #oca#e. Cre( rian7alendar84 cale a4 cale; . +on tr4ct a Gre!orian+a#endar ba ed on the c4rrent time in the de,a4#t time Bone with the !iven #oca#e. Cre( rian7alendar8*i!eD ne > ne; / +on tr4ct a Gre!orian+a#endar ba ed on the c4rrent time in the !iven time Bone with the de,a4#t #oca#e. Cre( rian7alendar8*i!eD ne > ne, 4 cale a4 cale; 0 +on tr4ct a Gre!orian+a#endar ba ed on the c4rrent time in the !iven time Bone with the !iven #oca#e. Cere i the #i t o, ,ew 4 e,4# 4''ort method 'rovided b( Gre!orian+a#endar c#a : # &edth % with De%cri"ti n N ) id add8int field, int a! unt; 1 )dd the 'eci,ied 7 i!ned; amo4nt o, time to the !iven time ,ie#d, ba ed on the ca#endar? r4#e . "r tected ) id c !"uteField%8; 2 +onvert =5+ a mi##i econd to time ,ie#d va#4e . "r tected ) id c !"ute*i!e8; 1 &verride +a#endar +onvert time ,ie#d va#4e to =5+ a mi##i econd . +2+ 3imited i a S4b idiar( o, 5+S 3imited D/

b lean e@ual%8Object bj; +om'are thi Gre!orian+a#endar to an ob*ect re,erence. int (et8int field; . Get the va#4e ,or a !iven time ,ie#d. int (etActual&a,i!u!8int field; / "et4rn the ma<im4m va#4e that thi ,ie#d co4#d have, !iven the c4rrent date. int (etActual&ini!u!8int field; 0 "et4rn the minim4m va#4e that thi ,ie#d co4#d have, !iven the c4rrent date. int (etCreate%t&ini!u!8int field; 8 "et4rn hi!he t minim4m va#4e ,or the !iven ,ie#d i, varie . Date (etCre( rian7han(e8; Get the Gre!orian +a#endar chan!e date. int (et4ea%t&a,i!u!8int field; 10 "et4rn #owe t ma<im4m va#4e ,or the !iven ,ie#d i, varie . int (et&a,i!u!8int field; 11 "et4rn ma<im4m va#4e ,or the !iven ,ie#d. Date (et*i!e8; 12 Get thi +a#endar? c4rrent time. l n( (et*i!eIn&illi%8; 11 Get thi +a#endar? c4rrent time a a #on!. *i!eD ne (et*i!eD ne8; 1D Get the time Bone. int (et&ini!u!8int field; 1. "et4rn minim4m va#4e ,or the !iven ,ie#d. int ha%h7 de8; 1/ &verride ha h+ode. b lean i%4ea"Eear8int year; 10 Determine i, the !iven (ear i a #ea' (ear. ) id r ll8int field, b lean u"; 18 )dd or 4btract 74'Gdown; a in!#e 4nit o, time on the !iven time ,ie#d witho4t chan!in! #ar!er ,ie#d . ) id %et8int field, int )alue; 1Set the time ,ie#d with the !iven va#4e. ) id %et8int year, int ! nth, int date; 20 Set the va#4e ,or the ,ie#d (ear, month, and date. ) id %et8int year, int ! nth, int date, int h ur, int !inute; 21 Set the va#4e ,or the ,ie#d (ear, month, date, ho4r, and min4te. ) id %et8int year, int ! nth, int date, int h ur, int !inute, int %ec nd; 22 Set the va#4e ,or the ,ie#d (ear, month, date, ho4r, min4te, and econd. ) id %etCre( rian7han(e8Date date; 21 Set the Gre!orian+a#endar chan!e date. ) id %et*i!e8Date date; 2D Set thi +a#endar? c4rrent time with the !iven Date. ) id %et*i!eIn&illi%8l n( !illi%; 2. Set thi +a#endar? c4rrent time ,rom the !iven #on! va#4e. ) id %et*i!eD ne8*i!eD ne )alue; 2/ Set the time Bone with the !iven time Bone va#4e. #trin( t #trin(8; 20 "et4rn a trin! re're entation o, thi ca#endar. E,a!"le: im'ort *ava.4ti#.OJ D +2+ 3imited i a S4b idiar( o, 5+S 3imited D0

'4b#ic c#a

Gre!orian+a#endarDemo N

'4b#ic tatic void main7Strin! ar! 8:; N Strin! month 8: K N P6anP, P%ebP, P2arP, P)'rP, P2a(P, P64nP, P64#P, P)4!P, PSe'P, P&ctP, PNovP, PDecPQJ int (earJ GG +reate a Gre!orian ca#endar initia#iBed GG with the c4rrent date and time in the GG de,a4#t #oca#e and timeBone. Gre!orian+a#endar !ca#endar K new Gre!orian+a#endar7;J GG Di '#a( c4rrent time and date in,ormation. S( tem.o4t.'rint7PDate: P;J S( tem.o4t.'rint7month 8!ca#endar.!et7+a#endar.2&N5C;:;J Ja)a $ Re(ular E,"re%%i n% 6ava 'rovide the *ava.4ti#.re!e< 'acka!e ,or 'attern matchin! with re!4#ar e<'re ion . 6ava re!4#ar e<'re ion are ver( imi#ar to the Per# 'ro!rammin! #an!4a!e and ver( ea ( to #earn. ) re!4#ar e<'re ion i a 'ecia# e@4ence o, character that he#' (o4 match or ,ind other trin! or et o, trin! , 4 in! a 'ecia#iBed (nta< he#d in a 'attern. 5he( can be 4 ed to earch, edit, or mani'4#ate te<t and data. 5he *ava.4ti#.re!e< 'acka!e 'rimari#( con i t o, the ,o##owin! three c#a e : Pattern 7la%%: ) Pattern ob*ect i a com'i#ed re're entation o, a re!4#ar e<'re ion. 5he Pattern c#a 'rovide no '4b#ic con tr4ctor . 5o create a 'attern, (o4 m4 t ,ir t invoke one o, it '4b#ic tatic com'i#e method , which wi## then ret4rn a Pattern ob*ect. 5he e method acce't a re!4#ar e<'re ion a the ,ir t ar!4ment. &atcher 7la%%: ) 2atcher ob*ect i the en!ine that inter'ret the 'attern and 'er,orm match o'eration a!ain t an in'4t trin!. 3ike the Pattern c#a , 2atcher de,ine no '4b#ic con tr4ctor . Fo4 obtain a 2atcher ob*ect b( invokin! the matcher method on a Pattern ob*ect. Pattern#ynta,E,ce"ti n: ) PatternS(nta<9<ce'tion ob*ect i an 4nchecked e<ce'tion that indicate a (nta< error in a re!4#ar e<'re ion 'attern. 7a"turin( Cr u"%: +a't4rin! !ro4' are a wa( to treat m4#ti'#e character a a in!#e 4nit. 5he( are created b( '#acin! the character to be !ro4'ed in ide a et o, 'arenthe e . %or e<am'#e, the re!4#ar e<'re ion 7do!; create a in!#e !ro4' containin! the #etter PdP, PoP, and P!P. +a't4rin! !ro4' are n4mbered b( co4ntin! their o'enin! 'arenthe e ,rom #e,t to ri!ht. $n the e<'re ion 77);7E7+;;;, ,or e<am'#e, there are ,o4r 4ch !ro4' : 77);7E7+;;; 7); 7E7+;; 7+; 5o ,ind o4t how man( !ro4' are 're ent in the e<'re ion, ca## the !ro4'+o4nt method on a matcher ob*ect. 5he !ro4'+o4nt method ret4rn an int howin! the n4mber o, ca't4rin! !ro4' 're ent in the matcher? 'attern. 5here i a# o a 'ecia# !ro4', !ro4' 0, which a#wa( re're ent the entire e<'re ion. 5hi !ro4' i not inc#4ded in the tota# re'orted b( !ro4'+o4nt. E,a!"le: %o##owin! e<am'#e i##4 trate how to ,ind a di!it trin! ,rom the !iven a#'han4meric trin!: im'ort *ava.4ti#.re!e<.2atcherJ im'ort *ava.4ti#.re!e<.PatternJ '4b#ic c#a "e!e<2atche N '4b#ic tatic void main7 Strin! ar! 8: ;N GG Strin! to be canned to ,ind the 'attern. Strin! #ine K P5hi order wa '#ace ,or ]51000W &H[PJ +2+ 3imited i a S4b idiar( o, 5+S 3imited D8

Strin! 'attern K P7.O;7IId>;7.O;PJ GG +reate a Pattern ob*ect Pattern r K Pattern.com'i#e7'attern;J GG Now create matcher ob*ect. 2atcher m K r.matcher7#ine;J i, 7m.,ind7 ;; N S( tem.o4t.'rint#n7P%o4nd va#4e: P > m.!ro4'70; ;J S( tem.o4t.'rint#n7P%o4nd va#4e: P > m.!ro4'71; ;J S( tem.o4t.'rint#n7P%o4nd va#4e: P > m.!ro4'72; ;J Q e# e N S( tem.o4t.'rint#n7PN& 2)5+CP;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: %o4nd va#4e: 5hi order wa '#ace ,or ]51000W &H[ %o4nd va#4e: 5hi order wa '#ace ,or ]5100 %o4nd va#4e: 0 Re(ular E,"re%%i n #ynta,: Cere i the tab#e #i tin! down a## the re!4#ar e<'re ion metacharacter (nta< avai#ab#e in 6ava: #ube,"re%%i &atche% n U 2atche be!innin! o, #ine. M 2atche end o, #ine. 2atche an( in!#e character e<ce't new#ine. = in! m o'tion a##ow it to . match new#ine a we##. 8...: 2atche an( in!#e character in bracket . 8U...: 2atche an( in!#e character not in bracket I) Ee!innin! o, entire trin! IB 9nd o, entire trin! IS 9nd o, entire trin! e<ce't a##owab#e ,ina# #ine terminator. reO 2atche 0 or more occ4rrence o, 'recedin! e<'re ion. re> 2atche 1 or more o, the 'revio4 thin! re[ 2atche 0 or 1 occ4rrence o, 'recedin! e<'re ion. reN nQ 2atche e<act#( n n4mber o, occ4rrence o, 'recedin! e<'re ion. reN n,Q 2atche n or more occ4rrence o, 'recedin! e<'re ion. reN n, mQ 2atche at #ea t n and at mo t m occ4rrence o, 'recedin! e<'re ion. aY b 2atche either a or b. 7re; Gro4' re!4#ar e<'re ion and remember matched te<t. 7[: re; Gro4' re!4#ar e<'re ion witho4t rememberin! matched te<t. 7[R re; 2atche inde'endent 'attern witho4t backtrackin!. Iw 2atche word character . IW 2atche nonword character . I 2atche white 'ace. 9@4iva#ent to 8ItInIrI,:. IS 2atche nonwhite 'ace. Id 2atche di!it . 9@4iva#ent to 80--:. ID 2atche nondi!it . I) 2atche be!innin! o, trin!. IS 2atche end o, trin!. $, a new#ine e<i t , it matche *4 t be,ore new#ine. IB 2atche end o, trin!. IG 2atche 'oint where #a t match ,ini hed. In Eack-re,erence to ca't4re !ro4' n4mber PnP Ib 2atche word bo4ndarie when o4t ide bracket . 2atche back 'ace 70<08; +2+ 3imited i a S4b idiar( o, 5+S 3imited

D-

when in ide bracket . IE 2atche nonword bo4ndarie . In, It, etc. 2atche new#ine , carria!e ret4rn , tab , etc. I] 9 ca'e 7@4ote; a## character 4' to I9 I9 9nd @4otin! be!4n with I] &eth d% f the &atcher 7la%%: Cere i the #i t o, 4 e,4# in tance method : Inde, &eth d%: $nde< method 'rovide 4 e,4# inde< va#4e that how 'reci e#( where the match wa ,o4nd in the in'4t trin!: # &eth d% with De%cri"ti n N "ublic int %tart8; 1 "et4rn the tart inde< o, the 'revio4 match. "ublic int %tart8int (r u"; 2 "et4rn the tart inde< o, the 4b e@4ence ca't4red b( the !iven !ro4' d4rin! the 'revio4 match o'eration. "ublic int end8; 1 "et4rn the o,, et a,ter the #a t character matched. "ublic int end8int (r u"; D "et4rn the o,, et a,ter the #a t character o, the 4b e@4ence ca't4red b( the !iven !ro4' d4rin! the 'revio4 match o'eration. #tudy &eth d%: St4d( method review the in'4t trin! and ret4rn a boo#ean indicatin! whether or not the 'attern i ,o4nd: # &eth d% with De%cri"ti n N "ublic b lean l =in(At8; 1 )ttem't to match the in'4t e@4ence, tartin! at the be!innin! o, the re!ion, a!ain t the 'attern. "ublic b lean find8; 2 )ttem't to ,ind the ne<t 4b e@4ence o, the in'4t e@4ence that matche the 'attern. "ublic b lean find8int %tart 1 "e et thi matcher and then attem't to ,ind the ne<t 4b e@4ence o, the in'4t e@4ence that matche the 'attern, tartin! at the 'eci,ied inde<. "ublic b lean !atche%8; D )ttem't to match the entire re!ion a!ain t the 'attern. Re"lace!ent &eth d%: "e'#acement method are 4 e,4# method ,or re'#acin! te<t in an in'4t trin!: # &eth d% with De%cri"ti n N "ublic &atcher a""endRe"lace!ent8#trin(6uffer %b, #trin( re"lace!ent; 1 $m'#ement a non-termina# a''end-and-re'#ace te'. "ublic #trin(6uffer a""end*ail8#trin(6uffer %b; 2 $m'#ement a termina# a''end-and-re'#ace te'. "ublic #trin( re"laceAll8#trin( re"lace!ent; 1 "e'#ace ever( 4b e@4ence o, the in'4t e@4ence that matche the 'attern with the !iven re'#acement trin!. "ublic #trin( re"laceFir%t8#trin( re"lace!ent; D "e'#ace the ,ir t 4b e@4ence o, the in'4t e@4ence that matche the 'attern with the !iven re'#acement trin!. . "ublic %tatic #trin( @u teRe"lace!ent8#trin( %; "et4rn a #itera# re'#acement Strin! ,or the 'eci,ied Strin!. 5hi method 'rod4ce a +2+ 3imited i a S4b idiar( o, 5+S 3imited .0

Strin! that wi## work a a #itera# re'#acement in the a''end"e'#acement method o, the 2atcher c#a . *he start and end &eth d%: %o##owin! i the e<am'#e that co4nt the n4mber o, time the word Pcat P a''ear in the in'4t trin!: im'ort *ava.4ti#.re!e<.2atcherJ im'ort *ava.4ti#.re!e<.PatternJ '4b#ic c#a "e!e<2atche N 'rivate tatic ,ina# Strin! "9G9A K PIIbcatIIbPJ 'rivate tatic ,ina# Strin! $NP=5 K Pcat cat cat cattie catPJ '4b#ic tatic void main7 Strin! ar! 8: ;N Pattern ' K Pattern.com'i#e7"9G9A;J 2atcher m K '.matcher7$NP=5;J GG !et a matcher ob*ect int co4nt K 0J whi#e7m.,ind7;; N co4nt>>J S( tem.o4t.'rint#n7P2atch n4mber P>co4nt;J S( tem.o4t.'rint#n7P tart7;: P>m. tart7;;J S( tem.o4t.'rint#n7Pend7;: P>m.end7;;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 2atch n4mber 1 tart7;: 0 end7;: 1 2atch n4mber 2 tart7;: D end7;: 0 2atch n4mber 1 tart7;: 8 end7;: 11 2atch n4mber D tart7;: 1end7;: 22 Fo4 can ee that thi e<am'#e 4 e word bo4ndarie to en 4re that the #etter PcP PaP PtP are not mere#( a 4b trin! in a #on!er word. $t a# o !ive ome 4 e,4# in,ormation abo4t where in the in'4t trin! the match ha occ4rred. 5he tart method ret4rn the tart inde< o, the 4b e@4ence ca't4red b( the !iven !ro4' d4rin! the 'revio4 match o'eration, and end ret4rn the inde< o, the #a t character matched, '#4 one. *he matches and lookingAt &eth d%: 5he matche and #ookin!)t method both attem't to match an in'4t e@4ence a!ain t a 'attern. 5he di,,erence, however, i that matche re@4ire the entire in'4t e@4ence to be matched, whi#e #ookin!)t doe not. Eoth method a#wa( tart at the be!innin! o, the in'4t trin!. Cere i the e<am'#e e<'#ainin! the ,4nctiona#it(: im'ort *ava.4ti#.re!e<.2atcherJ im'ort *ava.4ti#.re!e<.PatternJ '4b#ic c#a "e!e<2atche N 'rivate tatic ,ina# Strin! "9G9A K P,ooPJ 'rivate tatic ,ina# Strin! $NP=5 K P,oooooooooooooooooPJ 'rivate tatic Pattern 'atternJ +2+ 3imited i a S4b idiar( o, 5+S 3imited .1

'rivate tatic 2atcher matcherJ '4b#ic tatic void main7 Strin! ar! 8: ;N 'attern K Pattern.com'i#e7"9G9A;J matcher K 'attern.matcher7$NP=5;J S( tem.o4t.'rint#n7P+4rrent "9G9A i : P>"9G9A;J S( tem.o4t.'rint#n7P+4rrent $NP=5 i : P>$NP=5;J S( tem.o4t.'rint#n7P#ookin!)t7;: P>matcher.#ookin!)t7;;J S( tem.o4t.'rint#n7Pmatche 7;: P>matcher.matche 7;;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: +4rrent "9G9A i : ,oo +4rrent $NP=5 i : ,ooooooooooooooooo #ookin!)t7;: tr4e matche 7;: ,a# e *he replaceFirst and replaceAll &eth d%: 5he re'#ace%ir t and re'#ace)## method re'#ace te<t that matche a !iven re!4#ar e<'re ion. ) their name indicate, re'#ace%ir t re'#ace the ,ir t occ4rrence, and re'#ace)## re'#ace a## occ4rence . Cere i the e<am'#e e<'#ainin! the ,4nctiona#it(: im'ort *ava.4ti#.re!e<.2atcherJ im'ort *ava.4ti#.re!e<.PatternJ '4b#ic c#a "e!e<2atche N 'rivate tatic Strin! "9G9A K Pdo!PJ 'rivate tatic Strin! $NP=5 K P5he do! a( meow. P > P)## do! a( meow.PJ 'rivate tatic Strin! "9P3)+9 K PcatPJ '4b#ic tatic void main7Strin!8: ar! ; N Pattern ' K Pattern.com'i#e7"9G9A;J GG !et a matcher ob*ect 2atcher m K '.matcher7$NP=5;J $NP=5 K m.re'#ace)##7"9P3)+9;J S( tem.o4t.'rint#n7$NP=5;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 5he cat a( meow. )## cat a( meow. *he appendReplacement and appendTail &eth d%: 5he 2atcher c#a a# o 'rovide a''end"e'#acement and a''end5ai# method ,or te<t re'#acement. Cere i the e<am'#e e<'#ainin! the ,4nctiona#it(: im'ort *ava.4ti#.re!e<.2atcherJ im'ort *ava.4ti#.re!e<.PatternJ '4b#ic c#a "e!e<2atche N 'rivate tatic Strin! "9G9A K PaObPJ 'rivate tatic Strin! $NP=5 K Paab,ooaab,ooab,oobPJ 'rivate tatic Strin! "9P3)+9 K P-PJ '4b#ic tatic void main7Strin!8: ar! ; N Pattern ' K Pattern.com'i#e7"9G9A;J GG !et a matcher ob*ect 2atcher m K '.matcher7$NP=5;J Strin!E4,,er b K new Strin!E4,,er7;J whi#e7m.,ind7;;N +2+ 3imited i a S4b idiar( o, 5+S 3imited

.2

m.a''end"e'#acement7 b,"9P3)+9;J Q m.a''end5ai#7 b;J S( tem.o4t.'rint#n7 b.toStrin!7;;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: -,oo-,oo-,ooPattern#ynta,E,ce"ti n 7la%% &eth d%: ) PatternS(nta<9<ce'tion i an 4nchecked e<ce'tion that indicate a (nta< error in a re!4#ar e<'re ion 'attern. 5he PatternS(nta<9<ce'tion c#a 'rovide the ,o##owin! method to he#' (o4 determine what went wron!: # &eth d% with De%cri"ti n N "ublic #trin( (etDe%cri"ti n8; 1 "etrieve the de cri'tion o, the error. "ublic int (etInde,8; 2 "etrieve the error inde<. "ublic #trin( (etPattern8; 1 "etrieve the erroneo4 re!4#ar e<'re ion 'attern. "ublic #trin( (et&e%%a(e8; "et4rn a m4#ti-#ine trin! containin! the de cri'tion o, the (nta< error and it inde<, the D erroneo4 re!4#ar e<'re ion 'attern, and a vi 4a# indication o, the error inde< within the 'attern. S( tem.o4t.'rint#n7(ear K !ca#endar.!et7+a#endar.F9)";;J S( tem.o4t.'rint7P5ime: P;J S( tem.o4t.'rint7!ca#endar.!et7+a#endar.C&="; > P:P;J S( tem.o4t.'rint7!ca#endar.!et7+a#endar.2$N=59; > P:P;J S( tem.o4t.'rint#n7!ca#endar.!et7+a#endar.S9+&ND;;J GG 5e t i, the c4rrent (ear i a #ea' (ear i,7!ca#endar.i 3ea'Fear7(ear;; N S( tem.o4t.'rint#n7P5he c4rrent (ear i a #ea' (earP;J Q e# e N S( tem.o4t.'rint#n7P5he c4rrent (ear i not a #ea' (earP;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Date: )'r 22 2005ime: 11:2.:20 5he c4rrent (ear i not a #ea' (ear %or a com'#ete #i t o, con tant avai#ab#e in +a#ender c#a , (o4 can re,er to tandard 6ava doc4mentation. Ja)a $ &eth d% ) 6ava method i a co##ection o, tatement that are !ro4'ed to!ether to 'er,orm an o'eration. When (o4 ca## the S( tem.o4t.'rint#n method, ,or e<am'#e, the ( tem act4a##( e<ec4te evera# tatement in order to di '#a( a me a!e on the con o#e. Now (o4 wi## #earn how to create (o4r own method with or witho4t ret4rn va#4e , invoke a method with or witho4t 'arameter , over#oad method 4 in! the ame name , and a''#( method ab traction in the 'ro!ram de i!n. 7reatin( a &eth d: $n !enera#, a method ha the ,o##owin! (nta<: modi,ier ret4rnVa#4e5('e methodName7#i t o, 'arameter ; N GG 2ethod bod(J Q +2+ 3imited i a S4b idiar( o, 5+S 3imited .1

) method de,inition con i t o, a method header and a method bod(. Cere are a## the 'art o, a method: & difier%: 5he modi,ier, which i o'tiona#, te## the com'i#er how to ca## the method. 5hi de,ine the acce t('e o, the method. Return *y"e: ) method ma( ret4rn a va#4e. 5he ret4rnVa#4e5('e i the data t('e o, the va#4e the method ret4rn . Some method 'er,orm the de ired o'eration witho4t ret4rnin! a va#4e. $n thi ca e, the ret4rnVa#4e5('e i the ke(word ) id. &eth d Na!e: 5hi i the act4a# name o, the method. 5he method name and the 'arameter #i t to!ether con tit4te the method i!nat4re. Para!eter%: ) 'arameter i #ike a '#aceho#der. When a method i invoked, (o4 'a a va#4e to the 'arameter. 5hi va#4e i re,erred to a act4a# 'arameter or ar!4ment. 5he 'arameter #i t re,er to the t('e, order, and n4mber o, the 'arameter o, a method. Parameter are o'tiona#J that i , a method ma( contain no 'arameter . &eth d 6 dy: 5he method bod( contain a co##ection o, tatement that de,ine what the method doe .

N te: $n certain other #an!4a!e , method are re,erred to a 'roced4re and ,4nction . ) method with a nonvoid ret4rn va#4e t('e i ca##ed a ,4nctionJ a method with a void ret4rn va#4e t('e i ca##ed a 'roced4re. E,a!"le: Cere i the o4rce code o, the above de,ined method ca##ed ma<7;. 5hi method take two 'arameter n4m1 and n4m2 and ret4rn the ma<im4m between the two: GOO "et4rn the ma< between two n4mber OG '4b#ic tatic int ma<7int n4m1, int n4m2; N int re 4#tJ i, 7n4m1 R n4m2; re 4#t K n4m1J e# e re 4#t K n4m2J ret4rn re 4#tJ Q 7allin( a &eth d: $n creatin! a method, (o4 !ive a de,inition o, what the method i to do. 5o 4 e a method, (o4 have to ca## or invoke it. 5here are two wa( to ca## a methodJ the choice i ba ed on whether the method ret4rn a va#4e or not. When a 'ro!ram ca## a method, 'ro!ram contro# i tran ,erred to the ca##ed method. ) ca##ed method ret4rn contro# to the ca##er when it ret4rn tatement i e<ec4ted or when it method-endin! c#o in! brace i reached. $, the method ret4rn a va#4e, a ca## to the method i 4 4a##( treated a a va#4e. %or e<am'#e: int #ar!er K ma<710, D0;J $, the method ret4rn void, a ca## to the method m4 t be a tatement. %or e<am'#e, the method 'rint#n ret4rn void. 5he ,o##owin! ca## i a tatement: S( tem.o4t.'rint#n7PWe#come to 6avaWP;J E,a!"le: %o##owin! i the e<am'#e to demon trate how to de,ine a method and how to ca## it: '4b#ic c#a 5e t2a< N GOO 2ain method OG +2+ 3imited i a S4b idiar( o, 5+S 3imited .D

'4b#ic tatic void main7Strin!8: ar! ; N int i K .J int * K 2J int k K ma<7i, *;J S( tem.o4t.'rint#n7P5he ma<im4m between P > i > P and P > * > P i P > k;J Q GOO "et4rn the ma< between two n4mber OG '4b#ic tatic int ma<7int n4m1, int n4m2; N int re 4#tJ i, 7n4m1 R n4m2; re 4#t K n4m1J e# e re 4#t K n4m2J ret4rn re 4#tJ Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 5he ma<im4m between . and 2 i . 5hi 'ro!ram contain the main method and the ma< method. 5he main method i *4 t #ike an( other method e<ce't that it i invoked b( the 6V2. 5he main method? header i a#wa( the ame, #ike the one in thi e<am'#e, with the modi,ier '4b#ic and tatic, ret4rn va#4e t('e void, method name main, and a 'arameter o, the Strin!8: t('e. Strin!8: indicate that the 'arameter i an arra( o, Strin!. *he ) id <eyw rd: 5hi ection how how to dec#are and invoke a void method. %o##owin! e<am'#e !ive a 'ro!ram that dec#are a method named 'rintGrade and invoke it to 'rint the !rade ,or a !iven core. E,a!"le: '4b#ic c#a 5e tVoid2ethod N '4b#ic tatic void main7Strin!8: ar! ; N 'rintGrade708..;J Q '4b#ic tatic void 'rintGrade7do4b#e core; N i, 7 core RK -0.0; N S( tem.o4t.'rint#n7?)?;J Q e# e i, 7 core RK 80.0; N S( tem.o4t.'rint#n7?E?;J Q e# e i, 7 core RK 00.0; N S( tem.o4t.'rint#n7?+?;J Q e# e i, 7 core RK /0.0; N S( tem.o4t.'rint#n7?D?;J Q e# e N S( tem.o4t.'rint#n7?%?;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: + Cere the 'rintGrade method i a void method. $t doe not ret4rn an( va#4e. ) ca## to a void method m4 t be a tatement. So, it i invoked a a tatement in #ine 1 in the main method. 5hi tatement i #ike an( 6ava tatement terminated with a emico#on. +2+ 3imited i a S4b idiar( o, 5+S 3imited ..

Pa%%in( Para!eter% by Value%: When ca##in! a method, (o4 need to 'rovide ar!4ment , which m4 t be !iven in the ame order a their re 'ective 'arameter in the method 'eci,ication. 5hi i known a 'arameter order a ociation. %or e<am'#e, the ,o##owin! method 'rint a me a!e n time : '4b#ic tatic void nPrint#n7Strin! me a!e, int n; N ,or 7int i K 0J i V nJ i>>; S( tem.o4t.'rint#n7me a!e;J Q Cere, (o4 can 4 e nPrint#n7PCe##oP, 1; to 'rint PCe##oP three time . 5he nPrint#n7PCe##oP, 1; tatement 'a e the act4a# trin! 'arameter, PCe##oP, to the 'arameter, me a!eJ 'a e 1 to nJ and 'rint PCe##oP three time . Cowever, the tatement nPrint#n71, PCe##oP; wo4#d be wron!. When (o4 invoke a method with a 'arameter, the va#4e o, the ar!4ment i 'a ed to the 'arameter. 5hi i re,erred to a 'a -b(-va#4e. $, the ar!4ment i a variab#e rather than a #itera# va#4e, the va#4e o, the variab#e i 'a ed to the 'arameter. 5he variab#e i not a,,ected, re!ard#e o, the chan!e made to the 'arameter in ide the method. %or im'#icit(, 6ava 'ro!rammer o,ten a( 'a in! an ar!4ment < to a 'arameter (, which act4a##( mean 'a in! the va#4e o, < to (. E,a!"le: %o##owin! i a 'ro!ram that demon trate the e,,ect o, 'a in! b( va#4e. 5he 'ro!ram create a method ,or wa''in! two variab#e . 5he wa' method i invoked b( 'a in! two ar!4ment . $ntere tin!#(, the va#4e o, the ar!4ment are not chan!ed a,ter the method i invoked. '4b#ic c#a 5e tPa E(Va#4e N '4b#ic tatic void main7Strin!8: ar! ; N int n4m1 K 1J int n4m2 K 2J S( tem.o4t.'rint#n7PEe,ore wa' method, n4m1 i P > n4m1 > P and n4m2 i P > n4m2;J GG $nvoke the wa' method wa'7n4m1, n4m2;J S( tem.o4t.'rint#n7P),ter wa' method, n4m1 i P > n4m1 > P and n4m2 i P > n4m2;J Q GOO 2ethod to wa' two variab#e OG '4b#ic tatic void wa'7int n1, int n2; N S( tem.o4t.'rint#n7PIt$n ide the wa' methodP;J S( tem.o4t.'rint#n7PItItEe,ore wa''in! n1 i P > n1 > P n2 i P > n2;J GG Swa' n1 with n2 int tem' K n1J n1 K n2J n2 K tem'J S( tem.o4t.'rint#n7PItIt),ter wa''in! n1 i P > n1 > P n2 i P > n2;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Ee,ore wa' method, n4m1 i 1 and n4m2 i 2 $n ide the wa' method Ee,ore wa''in! n1 i 1 n2 i 2 ),ter wa''in! n1 i 2 n2 i 1 ),ter wa' method, n4m1 i 1 and n4m2 i 2 O)erl adin( &eth d%: 5he ma< method that wa 4 ed ear#ier work on#( with the int data t('e. E4t what i, (o4 need to ,ind which o, two ,#oatin!-'oint n4mber ha the ma<im4m va#4e[ 5he o#4tion i to create another method with the ame name b4t di,,erent 'arameter , a hown in the ,o##owin! code: +2+ 3imited i a S4b idiar( o, 5+S 3imited ./

'4b#ic tatic do4b#e ma<7do4b#e n4m1, do4b#e n4m2; N i, 7n4m1 R n4m2; ret4rn n4m1J e# e ret4rn n4m2J Q $, (o4 ca## ma< with int 'arameter , the ma< method that e<'ect int 'arameter wi## be invokedJ i, (o4 ca## ma< with do4b#e 'arameter , the ma< method that e<'ect do4b#e 'arameter wi## be invoked. 5hi i re,erred to a !eth d )erl adin(J that i , two method have the ame name b4t di,,erent 'arameter #i t within one c#a . 5he 6ava com'i#er determine which method i 4 ed ba ed on the method i!nat4re. &ver#oadin! method can make 'ro!ram c#earer and more readab#e. 2ethod that 'er,orm c#o e#( re#ated ta k ho4#d be !iven the ame name. &ver#oaded method m4 t have di,,erent 'arameter #i t . Fo4 cannot over#oad method ba ed on di,,erent modi,ier or ret4rn t('e . Sometime there are two or more 'o ib#e matche ,or an invocation o, a method d4e to imi#ar method i!nat4re, o the com'i#er cannot determine the mo t 'eci,ic match. 5hi i re,erred to a ambi!4o4 invocation. *he #c "e f Variable%: 5he co'e o, a variab#e i the 'art o, the 'ro!ram where the variab#e can be re,erenced. ) variab#e de,ined in ide a method i re,erred to a a #oca# variab#e. 5he co'e o, a #oca# variab#e tart ,rom it dec#aration and contin4e to the end o, the b#ock that contain the variab#e. ) #oca# variab#e m4 t be dec#ared be,ore it can be 4 ed. ) 'arameter i act4a##( a #oca# variab#e. 5he co'e o, a method 'arameter cover the entire method. ) variab#e dec#ared in the initia# action 'art o, a ,or #oo' header ha it co'e in the entire #oo'. E4t a variab#e dec#ared in ide a ,or #oo' bod( ha it co'e #imited in the #oo' bod( ,rom it dec#aration to the end o, the b#ock that contain the variab#e a hown be#ow:

Fo4 can dec#are a #oca# variab#e with the ame name m4#ti'#e time in di,,erent non-ne tin! b#ock in a method, b4t (o4 cannot dec#are a #oca# variab#e twice in ne ted b#ock . 5%in( 7 !!and$4ine Ar(u!ent%: Sometime (o4 wi## want to 'a in,ormation into a 'ro!ram when (o4 r4n it. 5hi i accom'#i hed b( 'a in! command-#ine ar!4ment to main7 ;. ) command-#ine ar!4ment i the in,ormation that direct#( ,o##ow the 'ro!ram? name on the command #ine when it i e<ec4ted. 5o acce the command-#ine ar!4ment in ide a 6ava 'ro!ram i @4ite ea (.the( are tored a trin! in the Strin! arra( 'a ed to main7 ;. E,a!"le: 5he ,o##owin! 'ro!ram di '#a( a## o, the command-#ine ar!4ment that it i ca##ed with: '4b#ic c#a +ommand3ine N '4b#ic tatic void main7Strin! ar! 8:;N ,or7int iK0J iVar! .#en!thJ i>>;N S( tem.o4t.'rint#n7Par! 8P > i > P:: P > ar! 8i:;J Q Q Q 5r( e<ec4tin! thi 'ro!ram, a hown here: +2+ 3imited i a S4b idiar( o, 5+S 3imited .0

*ava +ommand3ine thi i a command #ine 200 -100 5hi wo4#d 'rod4ce ,o##owin! re 4#t: ar! 80:: thi ar! 81:: i ar! 82:: a ar! 81:: command ar! 8D:: #ine ar! 8.:: 200 ar! 8/:: -100 *he 7 n%truct r%: ) con tr4ctor initia#iBe an ob*ect when it i created. $t ha the ame name a it c#a and i (ntactica##( imi#ar to a method. Cowever, con tr4ctor have no e<'#icit ret4rn t('e. 5('ica##(, (o4 wi## 4 e a con tr4ctor to !ive initia# va#4e to the in tance variab#e de,ined b( the c#a , or to 'er,orm an( other tart4' 'roced4re re@4ired to create a ,4##( ,ormed ob*ect. )## c#a e have con tr4ctor , whether (o4 de,ine one or not, beca4 e 6ava a4tomatica##( 'rovide a de,a4#t con tr4ctor that initia#iBe a## member variab#e to Bero. Cowever, once (o4 de,ine (o4r own con tr4ctor, the de,a4#t con tr4ctor i no #on!er 4 ed. E,a!"le: Cere i a im'#e e<am'#e that 4 e a con tr4ctor: GG ) im'#e con tr4ctor. c#a 2(+#a N int <J GG %o##owin! i the con tr4ctor 2(+#a 7; N < K 10J Q Q Fo4 wo4#d ca## con tr4ctor to initia#iBe ob*ect a ,o##ow : '4b#ic c#a +on Demo N '4b#ic tatic void main7Strin! ar! 8:; N 2(+#a t1 K new 2(+#a 7;J 2(+#a t2 K new 2(+#a 7;J S( tem.o4t.'rint#n7t1.< > P P > t2.<;J Q Q 2o t o,ten (o4 wi## need a con tr4ctor that acce't one or more 'arameter . Parameter are added to a con tr4ctor in the ame wa( that the( are added to a method:*4 t dec#are them in ide the 'arenthe e a,ter the con tr4ctor? name. E,a!"le: Cere i a im'#e e<am'#e that 4 e a con tr4ctor: GG ) im'#e con tr4ctor. c#a 2(+#a N int <J GG %o##owin! i the con tr4ctor 2(+#a 7int i ; N < K iJ Q Q Fo4 wo4#d ca## con tr4ctor to initia#iBe ob*ect a ,o##ow : '4b#ic c#a +on Demo N '4b#ic tatic void main7Strin! ar! 8:; N 2(+#a t1 K new 2(+#a 7 10 ;J 2(+#a t2 K new 2(+#a 7 20 ;J S( tem.o4t.'rint#n7t1.< > P P > t2.<;J Q +2+ 3imited i a S4b idiar( o, 5+S 3imited

.8

Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 10 20 Variable Ar(u!ent%8)ar$ar(%;: 6DH 1.. enab#e (o4 to 'a a variab#e n4mber o, ar!4ment o, the ame t('e to a method. 5he 'arameter in the method i dec#ared a ,o##ow : t('eName... 'arameterName $n the method dec#aration, (o4 'eci,( the t('e ,o##owed b( an e##i' i 7...; &n#( one variab#e-#en!th 'arameter ma( be 'eci,ied in a method, and thi 'arameter m4 t be the #a t 'arameter. )n( re!4#ar 'arameter m4 t 'recede it. E,a!"le: '4b#ic c#a Varar! Demo N '4b#ic tatic void main7Strin! ar! 8:; N GG +a## method with variab#e ar! 'rint2a<71D, 1, 1, 2, ./..;J 'rint2a<7new do4b#e8:N1, 2, 1Q;J Q '4b#ic tatic void 'rint2a<7 do4b#e... n4mber ; N i, 7n4mber .#en!th KK 0; N S( tem.o4t.'rint#n7PNo ar!4ment 'a edP;J ret4rnJ Q do4b#e re 4#t K n4mber 80:J ,or 7int i K 1J i V n4mber .#en!thJ i>>; i, 7n4mber 8i: R re 4#t; re 4#t K n4mber 8i:J S( tem.o4t.'rint#n7P5he ma< va#4e i P > re 4#t;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 5he ma< va#4e i ./.. 5he ma< va#4e i 1.0 *he finali>e8 ; &eth d: $t i 'o ib#e to de,ine a method that wi## be ca##ed *4 t be,ore an ob*ect? ,ina# de tr4ction b( the !arba!e co##ector. 5hi method i ca##ed finali>e8 ;, and it can be 4 ed to en 4re that an ob*ect terminate c#ean#(. %or e<am'#e, (o4 mi!ht 4 e ,ina#iBe7 ; to make 4re that an o'en ,i#e owned b( that ob*ect i c#o ed. 5o add a ,ina#iBer to a c#a , (o4 im'#( de,ine the ,ina#iBe7 ; method. 5he 6ava r4ntime ca## that method whenever it i abo4t to rec(c#e an ob*ect o, that c#a . $n ide the ,ina#iBe7 ; method (o4 wi## 'eci,( tho e action that m4 t be 'er,ormed be,ore an ob*ect i de tro(ed. 5he ,ina#iBe7 ; method ha thi !enera# ,orm: 'rotected void ,ina#iBe7 ; N GG ,ina#iBation code here Q Cere, the ke(word 'rotected i a 'eci,ier that 'revent acce to ,ina#iBe7 ; b( code de,ined o4t ide it c#a . 5hi mean that (o4 cannot know when.or even i,.,ina#iBe7 ; wi## be e<ec4ted. %or e<am'#e, i, (o4r 'ro!ram end be,ore !arba!e co##ection occ4r , ,ina#iBe7 ; wi## not e<ec4te. Ja)a $ #trea!%, File% and I/O 5he *ava.io 'acka!e contain near#( ever( c#a (o4 mi!ht ever need to 'er,orm in'4t and o4t'4t 7$G&; in 6ava. )## the e tream re're ent an in'4t o4rce and an o4t'4t de tination. 5he tream in the *ava.io 'acka!e 4''ort man( data 4ch a 'rimitive , &b*ect, #oca#iBed character etc. +2+ 3imited i a S4b idiar( o, 5+S 3imited .-

) tream can be de,ined a a e@4ence o, data. 5he $n'4tStream i 4 ed to read data ,rom a o4rce and the &4t'4tStream i 4 ed ,or writin! data to a de tination. 6ava doe 'rovide tron!, ,#e<ib#e 4''ort ,or $G& a it re#ate to ,i#e and network b4t thi t4toria# cover ver( ba ic ,4nction#it( re#ated to tream and $G&. We wo4#d ee mo t common#( 4 ed e<am'#e one b( one: Readin( 7 n% le In"ut: 6ava in'4t con o#e i accom'#i hed b( readin! ,rom #y%te!+in. 5o obtain a character-ba ed tream that i attached to the con o#e, (o4 wra' #y%te!+in in a 6ufferedReader ob*ect, to create a character tream. Cere i mo t common (nta< to obtain E4,,ered"eader: E4,,ered"eader br K new E4,,ered"eader7new $n'4tStream"eader7S( tem.in;;J &nce E4,,ered"eader i obtained, we can 4 e read7 ; method to reach a character or read3ine7 ; method to read a trin! ,rom the con o#e. Readin( 7haracter% fr ! 7 n% le: 5o read a character ,rom a E4,,ered"eader, we wo4#d read7 ; method who e (ta< i a ,o##ow : int read7 ; throw $&9<ce'tion 9ach time that read7 ; i ca##ed, it read a character ,rom the in'4t tream and ret4rn it a an inte!er va#4e. $t ret4rn .1 when the end o, the tream i enco4ntered. ) (o4 can ee, it can throw an $&9<ce'tion. 5he ,o##owin! 'ro!ram demon trate read7 ; b( readin! character ,rom the con o#e 4nti# the 4 er t('e a P@P: GG = e a E4,,ered"eader to read character ,rom the con o#e. im'ort *ava.io.OJ '4b#ic c#a E""ead N '4b#ic tatic void main7Strin! ar! 8:; throw $&9<ce'tion N char cJ GG +reate a E4,,ered"eader 4 in! S( tem.in E4,,ered"eader br K new E4,,ered"eader7new $n'4tStream"eader7S( tem.in;;J S( tem.o4t.'rint#n7P9nter character , ?@? to @4it.P;J GG read character do N c K 7char; br.read7;J S( tem.o4t.'rint#n7c;J Q whi#e7c WK ?@?;J Q Q Cere i a am'#e r4n: 9nter character , ?@? to @4it. 121abc@ 1 2 1 a b c @ Readin( #trin(% fr ! 7 n% le: 5o read a trin! ,rom the ke(board, 4 e the ver ion o, read3ine7 ; that i a member o, the E4,,ered"eader c#a . $t !enera# ,orm i hown here: Strin! read3ine7 ; throw $&9<ce'tion 5he ,o##owin! 'ro!ram demon trate E4,,ered"eader and the read3ine7 ; method. 5he 'ro!ram read and di '#a( #ine o, te<t 4nti# (o4 enter the word PendP: GG "ead a trin! ,rom con o#e 4 in! a E4,,ered"eader. im'ort *ava.io.OJ '4b#ic c#a E""ead3ine N +2+ 3imited i a S4b idiar( o, 5+S 3imited /0

'4b#ic tatic void main7Strin! ar! 8:; throw $&9<ce'tion N GG +reate a E4,,ered"eader 4 in! S( tem.in E4,,ered"eader br K new E4,,ered"eader7new $n'4tStream"eader7S( tem.in;;J Strin! trJ S( tem.o4t.'rint#n7P9nter #ine o, te<t.P;J S( tem.o4t.'rint#n7P9nter ?end? to @4it.P;J do N tr K br.read3ine7;J S( tem.o4t.'rint#n7 tr;J Q whi#e7W tr.e@4a# 7PendP;;J Q Q Cere i a am'#e r4n: 9nter #ine o, te<t. 9nter ?end? to @4it. 5hi i #ine one 5hi i #ine one 5hi i #ine two 5hi i #ine two end end Writin( 7 n% le Out"ut: +on o#e o4t'4t i mo t ea i#( accom'#i hed with "rint8 ; and "rintln8 ;, de cribed ear#ier. 5he e method are de,ined b( the c#a Print#trea! which i the t('e o, the ob*ect re,erenced b( #y%te!+ ut. 9ven tho4!h S( tem.o4t i a b(te tream, 4 in! it ,or im'#e 'ro!ram o4t'4t i ti## acce'tab#e. Eeca4 e PrintStream i an o4t'4t tream derived ,rom &4t'4tStream, it a# o im'#ement the #ow-#eve# method write7 ;. 5h4 , write7 ; can be 4 ed to write to the con o#e. 5he im'#e t ,orm o, write7 ; de,ined b( PrintStream i hown here: void write7int b(teva#; 5hi method write to the tream the b(te 'eci,ied b( b(teva#. )#tho4!h b(teva# i dec#ared a an inte!er, on#( the #ow-order ei!ht bit are written. E,a!"le: Cere i a hort e<am'#e that 4 e write7 ; to o4t'4t the character P)P ,o##owed b( a new#ine to the creen: im'ort *ava.io.OJ GG Demon trate S( tem.o4t.write7;. '4b#ic c#a WriteDemo N '4b#ic tatic void main7Strin! ar! 8:; N int bJ b K ?)?J S( tem.o4t.write7b;J S( tem.o4t.write7?In?;J Q Q 5hi wo4#d 'rod4ce im'#( ?)? character on the o4t'4t creen. ) N te: Fo4 wi## not o,ten 4 e write7 ; to 'er,orm con o#e o4t'4t beca4 e 'rint7 ; and 'rint#n7 ; are 4b tantia##( ea ier to 4 e. Readin( and Writin( File%: ) de cribed ear#ier, ) tream can be de,ined a a e@4ence o, data. 5he $n'4tStream i 4 ed to read data ,rom a o4rce and the &4t'4tStream i 4 ed ,or writin! data to a de tination. Cere i a hierarch( o, c#a e to dea# with $n'4t and &4t'4t tream .

+2+ 3imited i a S4b idiar( o, 5+S 3imited

/1

5he two im'ortant tream are %i#e$n'4tStream and %i#e&4t'4tStream which wo4#d be di c4 ed in thi t4toria#: FileIn"ut#trea!: 5hi tream i 4 ed ,or readin! data ,rom the ,i#e . &b*ect can be created 4 in! the ke(word new and there are evera# t('e o, con tr4ctor avai#ab#e. %o##owin! con tr4ctor take a ,i#e name a a trin! to create an in'4t tream ob*ect to read the ,i#e.: $n'4tStream , K new %i#e$n'4tStream7P+:G*avaGhe##oP;J %o##owin! con tr4ctor take a ,i#e ob*ect to create an in'4t tream ob*ect to read the ,i#e. %ir t we create a ,i#e ob*ect 4 in! %i#e7; method a ,o##ow : %i#e , K new %i#e7P+:G*avaGhe##oP;J $n'4tStream , K new %i#e$n'4tStream7,;J &nce (o4 have 3$put.tream ob*ect in hand then there i a #i t o, he#'er method which can be 4 ed to read to tream or to do other o'eration on the tream. # &eth d% with De%cri"ti n N "ublic ) id cl %e8; thr w% IOE,ce"ti nFG 1 5hi method c#o e the ,i#e o4t'4t tream. "e#ea e an( ( tem re o4rce a ociated with the ,i#e. 5hrow an $&9<ce'tion. "r tected ) id finali>e8;thr w% IOE,ce"ti n FG 5hi method c#ean 4' the connection to the ,i#e. 9n 4re that the c#o e method o, thi ,i#e 2 o4t'4t tream i ca##ed when there are no more re,erence to thi tream. 5hrow an $&9<ce'tion. "ublic int read8int r;thr w% IOE,ce"ti nFG 1 5hi method read the 'eci,ied b(te o, data ,rom the $n'4tStream. "et4rn an int. "et4rn the ne<t b(te o, data and -1 wi## be ret4rned i, it? end o, ,i#e. "ublic int read8byte9: r; thr w% IOE,ce"ti nFG D 5hi method read r.#en!th b(te ,rom the in'4t tream into an arra(. "et4rn the tota# n4mber o, b(te read. $, end o, ,i#e -1 wi## be ret4rned. "ublic int a)ailable8; thr w% IOE,ce"ti nFG . Give the n4mber o, b(te that can be read ,rom thi ,i#e in'4t tream. "et4rn an int. 5here are other im'ortant in'4t tream avai#ab#e, ,or more detai# (o4 can re,er to the ,o##owin! #ink : E(te)rra($n'4tStream Data$n'4tStream FileOut"ut#trea!: %i#e&4t'4tStream i 4 ed to create a ,i#e and write data into it.5he tream wo4#d create a ,i#e, i, it doe n?t a#read( e<i t, be,ore o'enin! it ,or o4t'4t. Cere are two con tr4ctor which can be 4 ed to create a %i#e&4t'4tStream ob*ect. %o##owin! con tr4ctor take a ,i#e name a a trin! to create an in'4t tream ob*ect to write the ,i#e.: &4t'4tStream , K new %i#e&4t'4tStream7P+:G*avaGhe##oP; +2+ 3imited i a S4b idiar( o, 5+S 3imited /2

%o##owin! con tr4ctor take a ,i#e ob*ect to create an o4t'4t tream ob*ect to write the ,i#e. %ir t we create a ,i#e ob*ect 4 in! %i#e7; method a ,o##ow : %i#e , K new %i#e7P+:G*avaGhe##oP;J &4t'4tStream , K new %i#e&4t'4tStream7,;J &nce (o4 have +utput.tream ob*ect in hand then there i a #i t o, he#'er method which can be 4 ed to write to tream or to do other o'eration on the tream. # &eth d% with De%cri"ti n N "ublic ) id cl %e8; thr w% IOE,ce"ti nFG 1 5hi method c#o e the ,i#e o4t'4t tream. "e#ea e an( ( tem re o4rce a ociated with the ,i#e. 5hrow an $&9<ce'tion. "r tected ) id finali>e8;thr w% IOE,ce"ti n FG 5hi method c#ean 4' the connection to the ,i#e. 9n 4re that the c#o e method o, thi ,i#e 2 o4t'4t tream i ca##ed when there are no more re,erence to thi tream. 5hrow an $&9<ce'tion. "ublic ) id write8int w;thr w% IOE,ce"ti nFG 1 5hi method write the 'eci,ied b(te to the o4t'4t tream. "ublic ) id write8byte9: w; D Write w.#en!th b(te ,rom the mentioned b(te arra( to the &4t'4tStream. 5here are other im'ortant o4t'4t tream avai#ab#e, ,or more detai# (o4 can re,er to the ,o##owin! #ink : E(te)rra(&4t'4tStream Data&4t'4tStream E,a!"le: %o##owin! i the e<am'#e to demon trate $n'4tStream and &4t'4tStream: im'ort *ava.io.OJ '4b#ic c#a ,i#eStream5e tN

'4b#ic tatic void main7Strin! ar! 8:;N tr(N b(te bWrite 8: K N11,21,1,D0,.QJ &4t'4tStream o K new %i#e&4t'4tStream7P+:Gte t.t<tP;J ,or7int <K0J < V bWrite.#en!th J <>>;N o .write7 bWrite8<: ;J GG write the b(te Q o .c#o e7;J $n'4tStream i K new %i#e$n'4tStream7P+:Gte t.t<tP;J int iBe K i .avai#ab#e7;J ,or7int iK0J iV iBeJ i>>;N S( tem.o4t.'rint77char;i .read7; > P P;J Q i .c#o e7;J Qcatch7$&9<ce'tion e;N S( tem.o4t.'rint7P9<ce'tionP;J Q Q Q 5he above code wo4#d create ,i#e te t.t<t and wo4#d write !iven n4mber in binar( ,ormat. Same wo4#d be o4t'4t on the tdo4t creen. File Na)i(ati n and I/O: 5here are evera# other c#a e that we wo4#d be !oin! thro4!h to !et to know the ba ic o, %i#e Navi!ation and $G&. %i#e +#a %i#e"eader +#a +2+ 3imited i a S4b idiar( o, 5+S 3imited /1

%i#eWriter +#a Direct rie% in Ja)a: 7reatin( Direct rie%: 5here are two 4 e,4# File 4ti#it( method which can be 4 ed to create directorie : 5he !=dir8 ; method create a director(, ret4rnin! tr4e on 4cce and ,a# e on ,ai#4re. %ai#4re indicate that the 'ath 'eci,ied in the %i#e ob*ect a#read( e<i t , or that the director( cannot be created beca4 e the entire 'ath doe not e<i t (et. 5he !=dir%8; method create both a director( and a## the 'arent o, the director(. %o##owin! e<am'#e create PGtm'G4 erG*avaGbinP director(: im'ort *ava.io.%i#eJ '4b#ic c#a +reateDir N '4b#ic tatic void main7Strin! ar! 8:; N Strin! dirname K PGtm'G4 erG*avaGbinPJ %i#e d K new %i#e7dirname;J GG +reate director( now. d.mkdir 7;J Q Q +om'i#e and e<ec4te above code to create PGtm'G4 erG*avaGbinP. N te: 6ava a4tomatica##( take care o, 'ath e'arator on =N$A and Window a 'er convention . $, (o4 4 e a ,orward #a h 7G; on a Window ver ion o, 6ava, the 'ath wi## ti## re o#ve correct#(. Readin( Direct rie%: ) director( i a %i#e that contain a #i t o, other ,i#e and directorie . When (o4 create a %i#e ob*ect and it i a director(, the i Director(7 ; method wi## ret4rn tr4e. Fo4 can ca## #i t7 ; on that ob*ect to e<tract the #i t o, other ,i#e and directorie in ide. 5he 'ro!ram hown here i##4 trate how to 4 e #i t7 ; to e<amine the content o, a director(: im'ort *ava.io.%i#eJ '4b#ic c#a Dir3i t N '4b#ic tatic void main7Strin! ar! 8:; N Strin! dirname K PG*avaPJ %i#e ,1 K new %i#e7dirname;J i, 7,1.i Director(7;; N S( tem.o4t.'rint#n7 PDirector( o, P > dirname;J Strin! 8: K ,1.#i t7;J ,or 7int iK0J i V .#en!thJ i>>; N %i#e , K new %i#e7dirname > PGP > 8i:;J i, 7,.i Director(7;; N S( tem.o4t.'rint#n7 8i: > P i a director(P;J Q e# e N S( tem.o4t.'rint#n7 8i: > P i a ,i#eP;J Q Q Q e# e N S( tem.o4t.'rint#n7dirname > P i not a director(P;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Director( o, Gm( @# bin i a director( #ib i a director( demo i a director( te t.t<t i a ,i#e "9)D29 i a ,i#e inde<.htm# i a ,i#e inc#4de i a director( +2+ 3imited i a S4b idiar( o, 5+S 3imited /D

Ja)a $ E,ce"ti n% 'andlin( )n e<ce'tion i a 'rob#em that ari e d4rin! the e<ec4tion o, a 'ro!ram. )n e<ce'tion can occ4r ,or man( di,,erent rea on , inc#4din! the ,o##owin!: ) 4 er ha entered inva#id data. ) ,i#e that need to be o'ened cannot be ,o4nd. ) network connection ha been #o t in the midd#e o, comm4nication , or the 6V2 ha r4n o4t o, memor(. Some o, the e e<ce'tion are ca4 ed b( 4 er error, other b( 'ro!rammer error, and other b( 'h( ica# re o4rce that have ,ai#ed in ome manner. 5o 4nder tand how e<ce'tion hand#in! work in 6ava, (o4 need to 4nder tand the three cate!orie o, e<ce'tion : 7hec=ed e,ce"ti n%: ) checked e<ce'tion i an e<ce'tion that i t('ica##( a 4 er error or a 'rob#em that cannot be ,ore een b( the 'ro!rammer. %or e<am'#e, i, a ,i#e i to be o'ened, b4t the ,i#e cannot be ,o4nd, an e<ce'tion occ4r . 5he e e<ce'tion cannot im'#( be i!nored at the time o, com'i#ation. Runti!e e,ce"ti n%: ) r4ntime e<ce'tion i an e<ce'tion that occ4r that 'robab#( co4#d have been avoided b( the 'ro!rammer. ) o''o ed to checked e<ce'tion , r4ntime e<ce'tion are i!nored at the time o, com'#iation. Err r%: 5he e are not e<ce'tion at a##, b4t 'rob#em that ari e be(ond the contro# o, the 4 er or the 'ro!rammer. 9rror are t('ica##( i!nored in (o4r code beca4 e (o4 can rare#( do an(thin! abo4t an error. %or e<am'#e, i, a tack over,#ow occ4r , an error wi## ari e. 5he( are a# o i!nored at the time o, com'i#ation. E,ce"ti n 'ierarchy: )## e<ce'tion c#a e are 4bt('e o, the *ava.#an!.9<ce'tion c#a . 5he e<ce'tion c#a i a 4bc#a o, the 5hrowab#e c#a . &ther than the e<ce'tion c#a there i another 4bc#a ca##ed 9rror which i derived ,rom the 5hrowab#e c#a . 9rror are not norma##( tra''ed ,orm the 6ava 'ro!ram . 5he e condition norma##( ha''en in ca e o, evere ,ai#4re , which are not hand#ed b( the *ava 'ro!ram . 9rror are !enerated to indicate error !enerated b( the r4ntime environment. 9<am'#e : 6V2 i o4t o, 2emor(. Norma##( 'ro!ram cannot recover ,rom error . 5he 9<ce'tion c#a ha two main 4bc#a e : $&9<ce'tion c#a and "4ntime9<ce'tion +#a .

Cere i a #i t o, mo t common checked and 4nchecked 6ava? E4i#t-in 9<ce'tion . E,ce"ti n% &eth d%: %o##owin! i the #i t o, im'ortant medthod avai#ab#e in the 5hrowab#e c#a . # &eth d% with De%cri"ti n N "ublic #trin( (et&e%%a(e8; 1 "et4rn a detai#ed me a!e abo4t the e<ce'tion that ha occ4rred. 5hi me a!e i initia#iBed in the 5hrowab#e con tr4ctor. "ublic *hr wable (et7au%e8; 2 "et4rn the ca4 e o, the e<ce'tion a re're ented b( a 5hrowab#e ob*ect. +2+ 3imited i a S4b idiar( o, 5+S 3imited

/.

"ublic #trin( t #trin(8; "et4rn the name o, the c#a concatenated with the re 4#t o, !et2e a!e7; "ublic ) id "rint#tac=*race8; D Print the re 4#t o, toStrin!7; a#on! with the tack trace to S( tem.err, the error o4t'4t tream. "ublic #tac=*raceEle!ent 9: (et#tac=*race8; "et4rn an arra( containin! each e#ement on the tack trace. 5he e#ement at inde< 0 . re're ent the to' o, the ca## tack, and the #a t e#ement in the arra( re're ent the method at the bottom o, the ca## tack. "ublic *hr wable fillIn#tac=*race8; / %i## the tack trace o, thi 5hrowab#e ob*ect with the c4rrent tack trace, addin! to an( 'revio4 in,ormation in the tack trace. 7atchin( E,ce"ti n%: ) method catche an e<ce'tion 4 in! a combination o, the try and catch ke(word . ) tr(Gcatch b#ock i '#aced aro4nd the code that mi!ht !enerate an e<ce'tion. +ode within a tr(Gcatch b#ock i re,erred to a 'rotected code, and the (nta< ,or 4 in! tr(Gcatch #ook #ike the ,o##owin!: tr( N GGProtected code Qcatch79<ce'tionName e1; N GG+atch b#ock Q ) catch tatement invo#ve dec#arin! the t('e o, e<ce'tion (o4 are tr(in! to catch. $, an e<ce'tion occ4r in 'rotected code, the catch b#ock 7or b#ock ; that ,o##ow the tr( i checked. $, the t('e o, e<ce'tion that occ4rred i #i ted in a catch b#ock, the e<ce'tion i 'a ed to the catch b#ock m4ch a an ar!4ment i 'a ed into a method 'arameter. E,a!"le: 5he ,o##owin! i an arra( i dec#ared with 2 e#ement . 5hen the code trie to acce the 1rd e#ement o, the arra( which throw an e<ce'tion. GG %i#e Name : 9<ce'5e t.*ava im'ort *ava.io.OJ '4b#ic c#a 9<ce'5e tN 1 '4b#ic tatic void main7Strin! ar! 8:;N tr(N int a8: K new int82:J S( tem.o4t.'rint#n7P)cce e#ement three :P > a81:;J Qcatch7)rra($nde<&4t&,Eo4nd 9<ce'tion e;N S( tem.o4t.'rint#n7P9<ce'tion thrown :P > e;J Q S( tem.o4t.'rint#n7P&4t o, the b#ockP;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 9<ce'tion thrown :*ava.#an!.)rra($nde<&4t&,Eo4nd 9<ce'tion: 1 &4t o, the b#ock &ulti"le catch 6l c=%: ) tr( b#ock can be ,o##owed b( m4#ti'#e catch b#ock . 5he (nta< ,or m4#ti'#e catch b#ock #ook #ike the ,o##owin!: tr( N GGProtected code Qcatch79<ce'tion5('e1 e1; N GG+atch b#ock Qcatch79<ce'tion5('e2 e2; +2+ 3imited i a S4b idiar( o, 5+S 3imited //

N GG+atch b#ock Qcatch79<ce'tion5('e1 e1; N GG+atch b#ock Q 5he 'revio4 tatement demon trate three catch b#ock , b4t (o4 can have an( n4mber o, them a,ter a in!#e tr(. $, an e<ce'tion occ4r in the 'rotected code, the e<ce'tion i thrown to the ,ir t catch b#ock in the #i t. $, the data t('e o, the e<ce'tion thrown matche 9<ce'tion5('e1, it !et ca4!ht there. $, not, the e<ce'tion 'a e down to the econd catch tatement. 5hi contin4e 4nti# the e<ce'tion either i ca4!ht or ,a## thro4!h a## catche , in which ca e the c4rrent method to' e<ec4tion and the e<ce'tion i thrown down to the 'revio4 method on the ca## tack. E,a!"le: Cere i code e!ment howin! how to 4 e m4#ti'#e tr(Gcatch tatement . tr( N ,i#e K new %i#e$n'4tStream7,i#eName;J < K 7b(te; ,i#e.read7;J Qcatch7$&9<ce'tion i; N i.'rintStack5race7;J ret4rn -1J Qcatch7%i#eNot%o4nd9<ce'tion ,; GGNot va#idW N ,.'rintStack5race7;J ret4rn -1J Q *he thr w%/thr w <eyw rd%: $, a method doe not hand#e a checked e<ce'tion, the method m4 t dec#are it 4 in! the thr w% ke(word. 5he throw ke(word a''ear at the end o, a method? i!nat4re. Fo4 can throw an e<ce'tion, either a new#( in tantiated one or an e<ce'tion that (o4 *4 t ca4!ht, b( 4 in! the thr w ke(word. 5r( to 4nder tand the di,,erent in throw and throw ke(word . 5he ,o##owin! method dec#are that it throw a "emote9<ce'tion: im'ort *ava.io.OJ '4b#ic c#a c#a Name N '4b#ic void de'o it7do4b#e amo4nt; throw "emote9<ce'tion N GG 2ethod im'#ementation throw new "emote9<ce'tion7;J Q GG"emainder o, c#a de,inition Q ) method can dec#are that it throw more than one e<ce'tion, in which ca e the e<ce'tion are dec#ared in a #i t e'arated b( comma . %or e<am'#e, the ,o##owin! method dec#are that it throw a "emote9<ce'tion and an $n 4,,icient%4nd 9<ce'tion: im'ort *ava.io.OJ '4b#ic c#a c#a Name N '4b#ic void withdraw7do4b#e amo4nt; throw "emote9<ce'tion, $n 4,,icient%4nd 9<ce'tion N GG 2ethod im'#ementation Q GG"emainder o, c#a de,inition Q *he finally <eyw rd 5he ,ina##( ke(word i 4 ed to create a b#ock o, code that ,o##ow a tr( b#ock. ) ,ina##( b#ock o, code a#wa( e<ec4te , whether or not an e<ce'tion ha occ4rred. +2+ 3imited i a S4b idiar( o, 5+S 3imited /0

= in! a ,ina##( b#ock a##ow (o4 to r4n an( c#ean4'-t('e tatement that (o4 want to e<ec4te, no matter what ha''en in the 'rotected code. ) ,ina##( b#ock a''ear at the end o, the catch b#ock and ha the ,o##owin! (nta<: tr( N GGProtected code Qcatch79<ce'tion5('e1 e1; N GG+atch b#ock Qcatch79<ce'tion5('e2 e2; N GG+atch b#ock Qcatch79<ce'tion5('e1 e1; N GG+atch b#ock Q,ina##( N GG5he ,ina##( b#ock a#wa( e<ec4te . Q E,a!"le: '4b#ic c#a 9<ce'5e tN '4b#ic tatic void main7Strin! ar! 8:;N int a8: K new int82:J tr(N S( tem.o4t.'rint#n7P)cce e#ement three :P > a81:;J Qcatch7)rra($nde<&4t&,Eo4nd 9<ce'tion e;N S( tem.o4t.'rint#n7P9<ce'tion thrown :P > e;J Q ,ina##(N a80: K /J S( tem.o4t.'rint#n7P%ir t e#ement va#4e: P >a80:;J S( tem.o4t.'rint#n7P5he ,ina##( tatement i e<ec4tedP;J Q Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 9<ce'tion thrown :*ava.#an!.)rra($nde<&4t&,Eo4nd 9<ce'tion: 1 %ir t e#ement va#4e: / 5he ,ina##( tatement i e<ec4ted Note the ,o##owin!: ) catch c#a4 e cannot e<i t witho4t a tr( tatement. $t i not com'4# or( to have ,ina##( c#a4 e when ever a tr(Gcatch b#ock i 're ent. 5he tr( b#ock cannot be 're ent witho4t either catch c#a4 e or ,ina##( c#a4 e. )n( code cannot be 're ent in between the tr(, catch, ,ina##( b#ock . Declarin( y u wn E,ce"ti n: Fo4 can create (o4r own e<ce'tion in 6ava. Hee' the ,o##owin! 'oint in mind when writin! (o4r own e<ce'tion c#a e : )## e<ce'tion m4 t be a chi#d o, 5hrowab#e. $, (o4 want to write a checked e<ce'tion that i a4tomatica##( en,orced b( the Cand#e or Dec#are "4#e, (o4 need to e<tend the 9<ce'tion c#a . $, (o4 want to write a r4ntime e<ce'tion, (o4 need to e<tend the "4ntime9<ce'tion c#a . We can de,ine o4r own 9<ce'tion c#a a be#ow: c#a 2(9<ce'tion e<tend 9<ce'tionN Q Fo4 *4 t need to e<tend the 9<ce'tion c#a to create (o4r own 9<ce'tion c#a . 5he e are con idered to be checked e<ce'tion . 5he ,o##owin! $n 4,,icient%4nd 9<ce'tion c#a i a 4 er-de,ined e<ce'tion +2+ 3imited i a S4b idiar( o, 5+S 3imited /8

that e<tend the 9<ce'tion c#a , makin! it a checked e<ce'tion. )n e<ce'tion c#a c#a , containin! 4 e,4# ,ie#d and method . E,a!"le: GG %i#e Name $n 4,,icient%4nd 9<ce'tion.*ava im'ort *ava.io.OJ '4b#ic c#a $n 4,,icient%4nd 9<ce'tion e<tend 9<ce'tion N 'rivate do4b#e amo4ntJ '4b#ic $n 4,,icient%4nd 9<ce'tion7do4b#e amo4nt; N thi .amo4nt K amo4ntJ Q '4b#ic do4b#e !et)mo4nt7; N ret4rn amo4ntJ Q Q 5o demon trate 4 in! o4r 4 er-de,ined e<ce'tion, the ,o##owin! +heckin!)cco4nt c#a withdraw7; method that throw an $n 4,,icient%4nd 9<ce'tion. GG %i#e Name +heckin!)cco4nt.*ava im'ort *ava.io.OJ

i #ike an( other

contain a

'4b#ic c#a +heckin!)cco4nt N 'rivate do4b#e ba#anceJ 'rivate int n4mberJ '4b#ic +heckin!)cco4nt7int n4mber; N thi .n4mber K n4mberJ Q '4b#ic void de'o it7do4b#e amo4nt; N ba#ance >K amo4ntJ Q '4b#ic void withdraw7do4b#e amo4nt; throw $n 4,,icient%4nd 9<ce'tion N i,7amo4nt VK ba#ance; N ba#ance -K amo4ntJ Q e# e N do4b#e need K amo4nt - ba#anceJ throw new $n 4,,icient%4nd 9<ce'tion7need ;J Q Q '4b#ic do4b#e !etEa#ance7; N ret4rn ba#anceJ Q '4b#ic int !etN4mber7; N ret4rn n4mberJ Q Q 5he ,o##owin! EankDemo 'ro!ram demon trate invokin! the de'o it7; and withdraw7; method o, +heckin!)cco4nt. +2+ 3imited i a S4b idiar( o, 5+S 3imited

/-

GG %i#e Name EankDemo.*ava '4b#ic c#a EankDemo N '4b#ic tatic void main7Strin! 8: ar! ; N +heckin!)cco4nt c K new +heckin!)cco4nt7101;J S( tem.o4t.'rint#n7PDe'o itin! M.00...P;J c.de'o it7.00.00;J tr( N S( tem.o4t.'rint#n7PInWithdrawin! M100...P;J c.withdraw7100.00;J S( tem.o4t.'rint#n7PInWithdrawin! M/00...P;J c.withdraw7/00.00;J Qcatch7$n 4,,icient%4nd 9<ce'tion e; N S( tem.o4t.'rint#n7PSorr(, b4t (o4 are hort MP > e.!et)mo4nt7;;J e.'rintStack5race7;J Q Q Q +om'i#e a## the above three ,i#e and r4n EankDemo, thi wo4#d 'rod4ce ,o##owin! re 4#t: De'o itin! M.00... Withdrawin! M100... Withdrawin! M/00... Sorr(, b4t (o4 are hort M200.0 $n 4,,icient%4nd 9<ce'tion at +heckin!)cco4nt.withdraw7+heckin!)cco4nt.*ava:2.; at EankDemo.main7EankDemo.*ava:11; 7 !! n E,ce"ti n%: $n *ava it i 'o ib#e to de,ine two cater!orie o, 9<ce'tion and 9rror . JV& E,ce"ti n%: - 5he e are e<ce'tion Gerror that are e<c#4 ive#( or #o!ica##( thrown b( the 6V2. 9<am'#e : N4##Pointer9<ce'tion, )rra($nde<&4t&,Eo4nd 9<ce'tion, +#a +a t9<ce'tion, Pr (ra!!atic e,ce"ti n% . 5he e e<ce'tion are thrown e<'#icit#( b( the a''#ication or the )P$ 'ro!rammer 9<am'#e : $##e!a#)r!4ment9<ce'tion, $##e!a#State9<ce'tion. Ja)a $ Inheritance $nheritance can be de,ined a the 'roce where one ob*ect ac@4ire the 'ro'ertie o, another. With the 4 e o, inheritance the in,ormation i made mana!eab#e in a hierarchica# order. When we ta#k abo4t inheritance the mo t common#( 4 ed ke(word wo4#d be e,tend% and i!"le!ent%. 5he e word wo4#d determine whether one ob*ect $S-) t('e o, another. E( 4 in! the e ke(word we can make one ob*ect ac@4ire the 'ro'ertie o, another ob*ect. I#$A Relati n%hi": $S-) i a wa( o, a(in! : 5hi ob*ect i a t('e o, that ob*ect. 3et 4 ee how the e,tend% ke(word i 4 ed to achieve inheritance. '4b#ic c#a )nima#N Q '4b#ic c#a 2amma# e<tend )nima#N Q '4b#ic c#a Q "e'ti#e e<tend )nima#N

'4b#ic c#a Do! e<tend 2amma#N Q Now ba ed on the above e<am'#e, $n &b*ect &riented term ,o##owin! are tr4e: +2+ 3imited i a S4b idiar( o, 5+S 3imited 00

)nima# i the 4'erc#a o, 2amma# c#a . )nima# i the 4'erc#a o, "e'ti#e c#a . 2amma# and "e'ti#e are 4b c#a e o, )nima# c#a . Do! i the 4bc#a o, both 2amma# and )nima# c#a e . Now i, we con ider the $S-) re#ation hi' we can a(: 2amma# $S-) )nima# "e'ti#e $S-) )nima# Do! $S-) 2amma# Cence : Do! $S-) )nima# a we## With 4 e o, the e<tend ke(word the 4bc#a e wi## be ab#e to inherit a## the 'ro'ertie o, the 4'erc#a e<ce't ,or the 'rivate 'ro'ertie o, the 4'erc#a . We can a 4re that 2amma# i act4a##( an )nima# with the 4 e o, the in tance o'erator. E,a!"le: '4b#ic c#a Do! e<tend 2amma#N '4b#ic tatic void main7Strin! ar! 8:;N )nima# a K new )nima#7;J 2amma# m K new 2amma#7;J Do! d K new Do!7;J S( tem.o4t.'rint#n7m in tanceo, )nima#;J S( tem.o4t.'rint#n7d in tanceo, 2amma#;J S( tem.o4t.'rint#n7d in tanceo, )nima#;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: tr4e tr4e tr4e Since we have a !ood 4nder tandin! o, the e,tend% ke(word #et 4 #ook into how the i!"le!ent% ke(word i 4 ed to !et the $S-) re#ation hi'. 5he i!"le!ent% ke(word i 4 ed b( c#a e b( inherit ,rom inter,ace . $nter,ace can never be e<tended b( the c#a e . E,a!"le: '4b#ic inter,ace )nima# NQ '4b#ic c#a Q 2amma# im'#ement )nima#N

'4b#ic c#a Do! e<tend 2amma#N Q *he in%tance f <eyw rd: 3et 4 4 e the in%tance f o'erator to check determine whether 2amma# i act4a##( an )nima#, and do! i act4a##( an )nima# inter,ace )nima#NQ c#a 2amma# im'#ement )nima#NQ

'4b#ic c#a Do! e<tend 2amma#N '4b#ic tatic void main7Strin! ar! 8:;N 2amma# m K new 2amma#7;J Do! d K new Do!7;J S( tem.o4t.'rint#n7m in tanceo, )nima#;J S( tem.o4t.'rint#n7d in tanceo, 2amma#;J S( tem.o4t.'rint#n7d in tanceo, )nima#;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 01

Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: tr4e tr4e tr4e 'A#$A relati n%hi": 5he e re#ation hi' are main#( ba ed on the 4 a!e. 5hi determine whether a certain c#a 'A#$A certain thin!. 5hi re#ation hi' he#' to red4ce d4'#ication o, code a we## a b4! . 3et 4 #ook into an e<am'#e: '4b#ic c#a Vehic#eNQ '4b#ic c#a S'eedNQ '4b#ic c#a Van e<tend Vehic#eN 'rivate S'eed 'J Q 5hi how that c#a Van C)S-) S'eed. E( havin! a e'arate c#a ,or S'eed we do not have to '4t the entire code that be#on! to 'eed in ide the Van c#a ., which make it 'o ib#e to re4 e the S'eed c#a in m4#ti'#e a''#ication . $n &b*ect &riented ,eat4re the 4 er do not need to bother abo4t which ob*ect i doin! the rea# work. 5o achieve thi , the Van c#a hide the im'#ementation detai# ,rom the 4 er o, the Van c#a . S& ba ica##( what ha''en i the 4 er wo4#d a k the Van c#a to do a certain action and the Vann c#a wi## either do the work b( it e#, or a k another c#a to 'er,orm the action. ) ver( im'ortant ,act to remember i that 6ava on#( 4''ort on#( in!#e inheritance. 5hi mean that a c#a cannot e<tend more than one c#a . 5here,ore ,o##owin! i i##e!a#: '4b#ic c#a e<tend )nima#, 2amma#NQ Cowever a c#a can im'#ement one or more inter,ace . 5hi ha made 6ava !et rid o, the im'o ibi#it( o, m4#ti'#e inheritance Ja)a $ O)erridin( $n the 'revio4 cha'ter we ta#ked abo4t 4'er c#a e and 4b c#a e . $, a c#a inherit a method ,rom it 4'er c#a , then there i a chance to override the method 'rovided that it i not marked ,ina#. 5he bene,it o, overridin! i : abi#it( to de,ine a behavior that? 'eci,ic to the 4b c#a t('e. Which mean a 4bc#a can im'#ement a 'arent c#a method ba ed on it re@4irement. $n ob*ect oriented term , overridin! mean to override the ,4nctiona#it( o, an( e<i tin! method. E,a!"le: 3et 4 #ook at an e<am'#e. c#a )nima#N '4b#ic void move7;N S( tem.o4t.'rint#n7P)nima# can moveP;J Q Q c#a Do! e<tend )nima#N

'4b#ic void move7;N S( tem.o4t.'rint#n7PDo! can wa#k and r4nP;J Q Q '4b#ic c#a 5e tDo!N

'4b#ic tatic void main7Strin! ar! 8:;N )nima# a K new )nima#7;J GG )nima# re,erence and ob*ect )nima# b K new Do!7;J GG )nima# re,erence b4t Do! ob*ect a.move7;JGG r4n the method in )nima# c#a b.move7;JGG"4n the method in Do! c#a +2+ 3imited i a S4b idiar( o, 5+S 3imited 02

Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: )nima# can move Do! can wa#k and r4n $n the above e<am'#e (o4 can ee that the even tho4!h b i a t('e o, )nima# it r4n the move method in the Do! c#a . 5he rea on ,or thi i : $n com'i#e time the check i made on the re,erence t('e. Cowever in the r4ntime 6V2 ,i!4re o4t the ob*ect t('e and wo4#d r4n the method that be#on! to that 'artic4#ar ob*ect. 5here,ore in the above e<am'#e, the 'ro!ram wi## com'i#e 'ro'er#( ince )nima# c#a ha the method move. 5hen at the r4ntime it r4n the method 'eci,ic ,or that ob*ect. +on ider the ,o##owin! e<am'#e : c#a )nima#N '4b#ic void move7;N S( tem.o4t.'rint#n7P)nima# can moveP;J Q Q c#a Do! e<tend )nima#N

'4b#ic void move7;N S( tem.o4t.'rint#n7PDo! can wa#k and r4nP;J Q '4b#ic void bark7;N S( tem.o4t.'rint#n7PDo! can barkP;J Q Q '4b#ic c#a 5e tDo!N

'4b#ic tatic void main7Strin! ar! 8:;N )nima# a K new )nima#7;J GG )nima# re,erence and ob*ect )nima# b K new Do!7;J GG )nima# re,erence b4t Do! ob*ect a.move7;JGG r4n the method in )nima# c#a b.move7;JGG"4n the method in Do! c#a b.bark7;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 5e tDo!.*ava:10: cannot ,ind (mbo# (mbo# : method bark7; #ocation: c#a )nima# b.bark7;J U 5hi 'ro!ram wi## throw a com'i#e time error ince b? re,erence t('e )nima# doe n?t have a method b( the name o, bark. Rule% f r !eth d )erridin(: 5he ar!4ment #i t ho4#d be e<act#( the ame a that o, the overridden method. 5he ret4rn t('e ho4#d be the ame or a 4bt('e o, the ret4rn t('e dec#ared in the ori!ina# overridden method in the 4'er c#a . 5he acce #eve# cannot be more re trictive than the overridden method? acce #eve#. %or e<am'#e: i, the 4'er c#a method i dec#ared '4b#ic then the overriddin! method in the 4b c#a cannot be either 'rivate or 'rotected. $n tance method can be overridden on#( i, the( are inherited b( the 4bc#a . ) method dec#ared ,ina# cannot be overridden. ) method dec#ared tatic cannot be overridden b4t can be re-dec#ared. +2+ 3imited i a S4b idiar( o, 5+S 3imited 01

$, a method cannot be inherited then it cannot be overridden. ) 4bc#a within the ame 'acka!e a the in tance? 4'erc#a can override an( 4'erc#a method that i not dec#ared 'rivate or ,ina#. ) 4bc#a in a di,,erent 'acka!e can on#( override the non-,ina# method dec#ared '4b#ic or 'rotected. )n overridin! method can throw an( 4ncheck e<ce'tion , re!ard#e o, whether the overridden method throw e<ce'tion or not. Cowever the overridin! method ho4#d not throw checked e<ce'tion that are new or broader than the one dec#ared b( the overridden method. 5he overridin! method can throw narrower or ,ewer e<ce'tion than the overridden method. +on tr4ctor cannot be overridden. 5%in( the %u"er =eyw rd: When invokin! a 4'erc#a ver ion o, an overridden method the %u"er ke(word i 4 ed. c#a )nima#N '4b#ic void move7;N S( tem.o4t.'rint#n7P)nima# can moveP;J Q Q c#a Do! e<tend )nima#N

'4b#ic void move7;N 4'er.move7;J GG invoke the 4'er c#a method S( tem.o4t.'rint#n7PDo! can wa#k and r4nP;J Q Q '4b#ic c#a 5e tDo!N

'4b#ic tatic void main7Strin! ar! 8:;N )nima# b K new Do!7;J GG )nima# re,erence b4t Do! ob*ect b.move7;J GG"4n the method in Do! c#a Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: )nima# can move Do! can wa#k and r4n Ja)a $ P ly! r"hi%! Po#(mor'hi m i the abi#it( o, an ob*ect to take on man( ,orm . 5he mo t common 4 e o, 'o#(mor'hi m in &&P occ4r when a 'arent c#a re,erence i 4 ed to re,er to a chi#d c#a ob*ect. )n( *ava ob*ect that can 'a more than one $S-) te t i con idered to be 'o#(mor'hic. $n 6ava, a## *ava ob*ect are 'o#(mor'hic ince an( ob*ect wi## 'a the $S-) te t ,or their own t('e and ,or the c#a &b*ect. $t i im'ortant to know that the on#( 'o ib#e wa( to acce an ob*ect i thro4!h a re,erence variab#e. ) re,erence variab#e can be o, on#( one t('e. &nce dec#ared the t('e o, a re,erence variab#e cannot be chan!ed. 5he re,erence variab#e can be rea i!ned to other ob*ect 'rovided that it i not dec#ared ,ina#. 5he t('e o, the re,erence variab#e wo4#d determine the method that it can invoke on the ob*ect. ) re,erence variab#e can re,er to an( ob*ect o, it dec#ared t('e or an( 4bt('e o, it dec#ared t('e. ) re,erence variab#e can be dec#ared a a c#a or inter,ace t('e. E,a!"le: 3et 4 #ook at an e<am'#e. '4b#ic inter,ace Ve!etarianNQ '4b#ic c#a )nima#NQ '4b#ic c#a Deer e<tend )nima# im'#ement Ve!etarianNQ +2+ 3imited i a S4b idiar( o, 5+S 3imited 0D

Now the Deer c#a i con idered to be 'o#(mor'hic ince thi ha m4#ti'#e inheritance. %o##owin! are tr4e ,or the above e<am'#e: ) Deer $S-) )nima# ) Deer $S-) Ve!etarian ) Deer $S-) Deer ) Deer $S-) &b*ect When we a''#( the re,erence variab#e ,act to a Deer ob*ect re,erence, the ,o##owin! dec#aration are #e!a#: Deer d K new Deer7;J )nima# a K dJ Ve!etarian v K dJ &b*ect o K dJ )## the re,erence variab#e d,a,v,o re,er to the ame Deer ob*ect in the hea'. Virtual &eth d%: $n thi ection, $ wi## how (o4 how the behavior o, overridden method in 6ava a##ow (o4 to take advanta!e o, 'o#(mor'hi m when de i!nin! (o4r c#a e . We a#read( have di c4 ed method overridin!, where a chi#d c#a can override a method in it 'arent. )n overridden method i e entia##( hidden in the 'arent c#a , and i not invoked 4n#e the chi#d c#a 4 e the 4'er ke(word within the overridin! method. GO %i#e name : 9m'#o(ee.*ava OG '4b#ic c#a 9m'#o(ee N 'rivate Strin! nameJ 'rivate Strin! addre J 'rivate int n4mberJ '4b#ic 9m'#o(ee7Strin! name, Strin! addre , int n4mber; N S( tem.o4t.'rint#n7P+on tr4ctin! an 9m'#o(eeP;J thi .name K nameJ thi .addre K addre J thi .n4mber K n4mberJ Q '4b#ic void mai#+heck7; N S( tem.o4t.'rint#n7P2ai#in! a check to P > thi .name > P P > thi .addre ;J Q '4b#ic Strin! toStrin!7; N ret4rn name > P P > addre > P P > n4mberJ Q '4b#ic Strin! !etName7; N ret4rn nameJ Q '4b#ic Strin! !et)ddre 7; N ret4rn addre J Q '4b#ic void et)ddre 7Strin! new)ddre ; N addre K new)ddre J Q '4b#ic int !etN4mber7; N ret4rn n4mberJ Q Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 0.

Now 4''o e we e<tend 9m'#o(ee c#a a ,o##ow : GO %i#e name : Sa#ar(.*ava OG '4b#ic c#a Sa#ar( e<tend 9m'#o(ee N 'rivate do4b#e a#ar(J GG)nn4a# a#ar( '4b#ic Sa#ar(7Strin! name, Strin! addre , int n4mber, do4b#e a#ar(; N 4'er7name, addre , n4mber;J etSa#ar(7 a#ar(;J Q '4b#ic void mai#+heck7; N S( tem.o4t.'rint#n7PWithin mai#+heck o, Sa#ar( c#a P;J S( tem.o4t.'rint#n7P2ai#in! check to P > !etName7; > P with a#ar( P > a#ar(;J Q '4b#ic do4b#e !etSa#ar(7; N ret4rn a#ar(J Q '4b#ic void etSa#ar(7do4b#e newSa#ar(; N i,7newSa#ar( RK 0.0; N a#ar( K newSa#ar(J Q Q '4b#ic do4b#e com'4tePa(7; N S( tem.o4t.'rint#n7P+om'4tin! a#ar( 'a( ,or P > !etName7;;J ret4rn a#ar(G.2J Q Q Now (o4 t4d( the ,o##owin! 'ro!ram care,4##( and tr( to determine it o4t'4t: GO %i#e name : Virt4a#Demo.*ava OG '4b#ic c#a Virt4a#Demo N '4b#ic tatic void main7Strin! 8: ar! ; N Sa#ar( K new Sa#ar(7P2ohd 2ohta himP, P)mbehta, =PP, 1, 1/00.00;J 9m'#o(ee e K new Sa#ar(7P6ohn )dam P, PEo ton, 2)P, 2, 2D00.00;J S( tem.o4t.'rint#n7P+a## mai#+heck 4 in! Sa#ar( re,erence --P;J .mai#+heck7;J S( tem.o4t.'rint#n7PIn +a## mai#+heck 4 in! 9m'#o(ee re,erence--P;J e.mai#+heck7;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: +on tr4ctin! an 9m'#o(ee +on tr4ctin! an 9m'#o(ee +a## mai#+heck 4 in! Sa#ar( re,erence -Within mai#+heck o, Sa#ar( c#a 2ai#in! check to 2ohd 2ohta him with a#ar( 1/00.0 +2+ 3imited i a S4b idiar( o, 5+S 3imited 0/

+a## mai#+heck 4 in! 9m'#o(ee re,erence-Within mai#+heck o, Sa#ar( c#a 2ai#in! check to 6ohn )dam with a#ar( 2D00.0 Cere we in tantiate two Sa#ar( ob*ect . one 4 in! a Sa#ar( re,erence , and the other 4 in! an 9m'#o(ee re,erence e. Whi#e invokin! s#mailCheck ! the com'i#er ee mai#+heck7; in the Sa#ar( c#a at com'i#e time, and the 6V2 invoke mai#+heck7; in the Sa#ar( c#a at r4n time. $nvokin! mai#+heck7; on e i @4ite di,,erent beca4 e e i an 9m'#o(ee re,erence. When the com'i#er ee e#mailCheck !, the com'i#er ee the mai#+heck7; method in the 9m'#o(ee c#a . Cere, at com'i#e time, the com'i#er 4 ed mai#+heck7; in 9m'#o(ee to va#idate thi tatement. )t r4n time, however, the 6V2 invoke mai#+heck7; in the Sa#ar( c#a . 5hi behavior i re,erred to a virt4a# method invocation, and the method are re,erred to a virt4a# method . )## method in 6ava behave in thi manner, whereb( an overridden method i invoked at r4n time, no matter what data t('e the re,erence i that wa 4 ed in the o4rce code at com'i#e time. Ja)a $ Ab%tracti n )b traction re,er to the abi#it( to make a c#a ab tract in &&P. )n ab tract c#a i one that cannot be in tantiated. )## other ,4nctiona#it( o, the c#a ti## e<i t , and it ,ie#d , method , and con tr4ctor are a## acce ed in the ame manner. Fo4 *4 t cannot create an in tance o, the ab tract c#a . $, a c#a i ab tract and cannot be in tantiated, the c#a doe not have m4ch 4 e 4n#e it i 4bc#a ed. 5hi i t('ica##( how ab tract c#a e come abo4t d4rin! the de i!n 'ha e. ) 'arent c#a contain the common ,4nctiona#it( o, a co##ection o, chi#d c#a e , b4t the 'arent c#a it e#, i too ab tract to be 4 ed on it own. Ab%tract 7la%%: = e the ab%tract ke(word to dec#are a c#a ab tract. 5he ke(word a''ear in the c#a dec#aration omewhere be,ore the c#a ke(word. GO %i#e name : 9m'#o(ee.*ava OG '4b#ic ab tract c#a 9m'#o(ee N 'rivate Strin! nameJ 'rivate Strin! addre J 'rivate int n4mberJ '4b#ic 9m'#o(ee7Strin! name, Strin! addre , int n4mber; N S( tem.o4t.'rint#n7P+on tr4ctin! an 9m'#o(eeP;J thi .name K nameJ thi .addre K addre J thi .n4mber K n4mberJ Q '4b#ic do4b#e com'4tePa(7; N S( tem.o4t.'rint#n7P$n ide 9m'#o(ee com'4tePa(P;J ret4rn 0.0J Q '4b#ic void mai#+heck7; N S( tem.o4t.'rint#n7P2ai#in! a check to P > thi .name > P P > thi .addre ;J Q '4b#ic Strin! toStrin!7; N ret4rn name > P P > addre > P P > n4mberJ Q '4b#ic Strin! !etName7; N ret4rn nameJ Q '4b#ic Strin! !et)ddre 7; N +2+ 3imited i a S4b idiar( o, 5+S 3imited 00

ret4rn addre J Q '4b#ic void et)ddre 7Strin! new)ddre N addre K new)ddre J Q '4b#ic int !etN4mber7; N ret4rn n4mberJ Q

Q Notice that nothin! i di,,erent in thi 9m'#o(ee c#a . 5he c#a i now ab tract, b4t it ti## ha three ,ie#d , even method , and one con tr4ctor. Now i, (o4 wo4#d tr( a ,o##ow : GO %i#e name : )b tractDemo.*ava OG '4b#ic c#a )b tractDemo N '4b#ic tatic void main7Strin! 8: ar! ; N GO %o##owin! i not a##owed and wo4#d rai e error OG 9m'#o(ee e K new 9m'#o(ee7PGeor!e W.P, PCo4 ton, 5AP, D1;J S( tem.o4t.'rint#n7PIn +a## mai#+heck 4 in! 9m'#o(ee re,erence--P;J e.mai#+heck7;J Q Q When (o4 wo4#d com'i#e above c#a then (o4 wo4#d !et ,o##owin! error: 9m'#o(ee.*ava:D/: 9m'#o(ee i ab tractJ cannot be in tantiated 9m'#o(ee e K new 9m'#o(ee7PGeor!e W.P, PCo4 ton, 5AP, D1;J U 1 error E,tendin( Ab%tract 7la%%: We can e<tend 9m'#o(ee c#a in norma# wa( a ,o##ow : GO %i#e name : Sa#ar(.*ava OG '4b#ic c#a Sa#ar( e<tend 9m'#o(ee N 'rivate do4b#e a#ar(J GG)nn4a# a#ar( '4b#ic Sa#ar(7Strin! name, Strin! addre , int n4mber, do4b#e a#ar(; N 4'er7name, addre , n4mber;J etSa#ar(7 a#ar(;J Q '4b#ic void mai#+heck7; N S( tem.o4t.'rint#n7PWithin mai#+heck o, Sa#ar( c#a P;J S( tem.o4t.'rint#n7P2ai#in! check to P > !etName7; > P with a#ar( P > a#ar(;J Q '4b#ic do4b#e !etSa#ar(7; N ret4rn a#ar(J Q '4b#ic void etSa#ar(7do4b#e newSa#ar(; N i,7newSa#ar( RK 0.0; N a#ar( K newSa#ar(J +2+ 3imited i a S4b idiar( o, 5+S 3imited

08

Q Q '4b#ic do4b#e com'4tePa(7; N S( tem.o4t.'rint#n7P+om'4tin! a#ar( 'a( ,or P > !etName7;;J ret4rn a#ar(G.2J Q Q Cere we cannot in tantiate a new 9m'#o(ee, b4t i, we in tantiate a new Sa#ar( ob*ect, the Sa#ar( ob*ect wi## inherit the three ,ie#d and even method ,rom 9m'#o(ee. GO %i#e name : )b tractDemo.*ava OG '4b#ic c#a )b tractDemo N '4b#ic tatic void main7Strin! 8: ar! ; N Sa#ar( K new Sa#ar(7P2ohd 2ohta himP, P)mbehta, =PP, 1, 1/00.00;J Sa#ar( e K new Sa#ar(7P6ohn )dam P, PEo ton, 2)P, 2, 2D00.00;J S( tem.o4t.'rint#n7P+a## mai#+heck 4 in! Sa#ar( re,erence --P;J .mai#+heck7;J S( tem.o4t.'rint#n7PIn +a## mai#+heck 4 in! 9m'#o(ee re,erence--P;J e.mai#+heck7;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: +on tr4ctin! an 9m'#o(ee +on tr4ctin! an 9m'#o(ee +a## mai#+heck 4 in! Sa#ar( re,erence -Within mai#+heck o, Sa#ar( c#a 2ai#in! check to 2ohd 2ohta him with a#ar( 1/00.0 +a## mai#+heck 4 in! 9m'#o(ee re,erence-Within mai#+heck o, Sa#ar( c#a 2ai#in! check to 6ohn )dam with a#ar( 2D00. Ab%tract &eth d%: $, (o4 want a c#a to contain a 'artic4#ar method b4t (o4 want the act4a# im'#ementation o, that method to be determined b( chi#d c#a e , (o4 can dec#are the method in the 'arent c#a a ab tract. 5he ab tract ke(word i a# o 4 ed to dec#are a method a ab tract.)n ab tract method con i t o, a method i!nat4re, b4t no method bod(. )b tract method wo4#d have no de,inition, and it i!nat4re i ,o##owed b( a emico#on, not c4r#( brace a ,o##ow : '4b#ic ab tract c#a 9m'#o(ee N 'rivate Strin! nameJ 'rivate Strin! addre J 'rivate int n4mberJ '4b#ic ab tract do4b#e com'4tePa(7;J GG"emainder o, c#a de,inition Q Dec#arin! a method a ab tract ha two re 4#t : 5he c#a m4 t a# o be dec#ared ab tract. $, a c#a contain an ab tract method, the c#a be ab tract a we##. )n( chi#d c#a m4 t either override the ab tract method or dec#are it e#, ab tract. +2+ 3imited i a S4b idiar( o, 5+S 3imited

m4 t

0-

) chi#d c#a that inherit an ab tract method m4 t override it. $, the( do not, the( m4 t be ab tract,and an( o, their chi#dren m4 t override it. 9vent4a##(, a de cendant c#a ha to im'#ement the ab tract methodJ otherwi e, (o4 wo4#d have a hierarch( o, ab tract c#a e that cannot be in tantiated. $, Sa#ar( i e<tendin! 9m'#o(ee c#a then it i re@4ired to im'#ement com'4tePa(7; method a ,o##ow : GO %i#e name : Sa#ar(.*ava OG '4b#ic c#a Sa#ar( e<tend 9m'#o(ee N 'rivate do4b#e a#ar(J GG )nn4a# a#ar( '4b#ic do4b#e com'4tePa(7; N S( tem.o4t.'rint#n7P+om'4tin! a#ar( 'a( ,or P > !etName7;;J ret4rn a#ar(G.2J Q GG"emainder o, c#a de,inition Q Ja)a $ Enca"%ulati n 9nca' 4#ation i one o, the ,o4r ,4ndamenta# &&P conce't . 5he other three are inheritance, 'o#(mor'hi m, and ab traction. 9nca' 4#ation i the techni@4e o, makin! the ,ie#d in a c#a 'rivate and 'rovidin! acce to the ,ie#d via '4b#ic method . $, a ,ie#d i dec#ared 'rivate, it cannot be acce ed b( an(one o4t ide the c#a , thereb( hidin! the ,ie#d within the c#a . %or thi rea on, enca' 4#ation i a# o re,erred to a data hidin!. 9nca' 4#ation can be de cribed a a 'rotective barrier that 'revent the code and data bein! random#( acce ed b( other code de,ined o4t ide the c#a . )cce to the data and code i ti!ht#( contro##ed b( an inter,ace. 5he main bene,it o, enca' 4#ation i the abi#it( to modi,( o4r im'#emented code witho4t breakin! the code o, other who 4 e o4r code. With thi ,eat4re 9nca' 4#ation !ive maintainabi#it(, ,#e<ibi#it( and e<ten ibi#it( to o4r code. E,a!"le: 3et 4 #ook at an e<am'#e that de'ict enca' 4#ation: GO %i#e name : 9nca'5e t.*ava OG '4b#ic c#a 9nca'5e tN 'rivate Strin! nameJ 'rivate Strin! idN4mJ 'rivate int a!eJ '4b#ic int !et)!e7;N ret4rn a!eJ Q '4b#ic Strin! !etName7;N ret4rn nameJ Q '4b#ic Strin! !et$dN4m7;N ret4rn idN4mJ Q '4b#ic void et)!e7 int new)!e;N a!e K new)!eJ Q '4b#ic void etName7Strin! newName;N name K newNameJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 80

Q '4b#ic void et$dN4m7 Strin! new$d;N idN4m K new$dJ Q Q 5he '4b#ic method are the acce 'oint to thi c#a ? ,ie#d ,rom the o4t ide *ava wor#d. Norma##( the e method are re,erred a !etter and etter . 5here,ore an( c#a that want to acce the variab#e ho4#d acce them thro4!h the e !etter and etter . 5he variab#e o, the 9nca'5e t c#a can be acce a be#ow:: GO %i#e name : "4n9nca'.*ava OG '4b#ic c#a "4n9nca'N '4b#ic tatic void main7Strin! ar! 8:;N 9nca'5e t enca' K new 9nca'5e t7;J enca'. etName7P6ame P;J enca'. et)!e720;J enca'. et$dN4m7P121D1m P;J S( tem.o4t.'rint7PName : P > enca'.!etName7;> P )!e : P> enca'.!et)!e7;;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Name : 6ame )!e : 20 6enefit% f Enca"%ulati n: 5he ,ie#d o, a c#a can be made read-on#( or write-on#(. ) c#a can have tota# contro# over what i tored in it ,ie#d . 5he 4 er o, a c#a do not know how the c#a tore it data. ) c#a can chan!e the data t('e o, a ,ie#d, and 4 er o, the c#a do not need to chan!e an( o, their code. Ja)a $ Interface% )n inter,ace i a co##ection o, ab tract method . ) c#a im'#ement an inter,ace, thereb( inheritin! the ab tract method o, the inter,ace. )n inter,ace i not a c#a . Writin! an inter,ace i imi#ar to writin! a c#a , b4t the( are two di,,erent conce't . ) c#a de cribe the attrib4te and behavior o, an ob*ect. )n inter,ace contain behavior that a c#a im'#ement . =n#e the c#a that im'#ement the inter,ace i ab tract, a## the method o, the inter,ace need to be de,ined in the c#a . )n inter,ace i imi#ar to a c#a in the ,o##owin! wa( : )n inter,ace can contain an( n4mber o, method . )n inter,ace i written in a ,i#e with a +ja)a e<ten ion, with the name o, the inter,ace matchin! the name o, the ,i#e. 5he b(tecode o, an inter,ace a''ear in a +cla%% ,i#e. $nter,ace a''ear in 'acka!e , and their corre 'ondin! b(tecode ,i#e m4 t be in a director( tr4ct4re that matche the 'acka!e name. Cowever, an inter,ace i di,,erent ,rom a c#a in evera# wa( , inc#4din!: Fo4 cannot in tantiate an inter,ace. )n inter,ace doe not contain an( con tr4ctor . )## o, the method in an inter,ace are ab tract. )n inter,ace cannot contain in tance ,ie#d . 5he on#( ,ie#d that can a''ear in an inter,ace m4 t be dec#ared both tatic and ,ina#. )n inter,ace i not e<tended b( a c#a J it i im'#emented b( a c#a . )n inter,ace can e<tend m4#ti'#e inter,ace . Declarin( Interface%: 5he interface ke(word i 4 ed to dec#are an inter,ace. Cere i a im'#e e<am'#e to dec#are an inter,ace: E,a!"le: +2+ 3imited i a S4b idiar( o, 5+S 3imited 81

3et 4 #ook at an e<am'#e that de'ict enca' 4#ation: GO %i#e name : Name&,$nter,ace.*ava OG im'ort *ava.#an!.OJ GG)n( n4mber o, im'ort tatement '4b#ic inter,ace Name&,$nter,ace N GG)n( n4mber o, ,ina#, tatic ,ie#d GG)n( n4mber o, ab tract method dec#aration I Q $nter,ace have the ,o##owin! 'ro'ertie : )n inter,ace i im'#icit#( ab tract. Fo4 do not need to 4 e the ab%tract ke(word when dec#arin! an inter,ace. 9ach method in an inter,ace i a# o im'#icit#( ab tract, o the ab tract ke(word i not needed. 2ethod in an inter,ace are im'#icit#( '4b#ic. E,a!"le: GO %i#e name : )nima#.*ava OG inter,ace )nima# N '4b#ic void eat7;J '4b#ic void trave#7;J Q I!"le!entin( Interface%: When a c#a im'#ement an inter,ace, (o4 can think o, the c#a a i!nin! a contract, a!reein! to 'er,orm the 'eci,ic behavior o, the inter,ace. $, a c#a doe not 'er,orm a## the behavior o, the inter,ace, the c#a m4 t dec#are it e#, a ab tract. )c#a 4 e the i!"le!ent% ke(word to im'#ement an inter,ace. 5he im'#ement ke(word a''ear in the c#a dec#aration ,o##owin! the e<tend 'ortion o, the dec#aration. GO %i#e name : 2amma#$nt.*ava OG '4b#ic c#a 2amma#$nt im'#ement )nima#N '4b#ic void eat7;N S( tem.o4t.'rint#n7P2amma# eat P;J Q '4b#ic void trave#7;N S( tem.o4t.'rint#n7P2amma# trave# P;J Q '4b#ic int no&,3e! 7;N ret4rn 0J Q '4b#ic tatic void main7Strin! ar! 8:;N 2amma#$nt m K new 2amma#$nt7;J m.eat7;J m.trave#7;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 2amma# eat 2amma# trave# When overridin! method de,ined in inter,ace there are evera# r4#e to be ,o##owed: +hecked e<ce'tion ho4#d not be dec#ared on im'#ementation method other than the one dec#ared b( the inter,ace method or 4bc#a e o, tho e dec#ared b( the inter,ace method. 5he i!nat4re o, the inter,ace method and the ame ret4rn t('e or 4bt('e ho4#d be maintained when overridin! the method . +2+ 3imited i a S4b idiar( o, 5+S 3imited 82

)n im'#ementation c#a it e#, can be ab tract and i, o inter,ace method need not be im'#emented. When im'#ementation inter,ace there are evera# r4#e : ) c#a can im'#ement more than one inter,ace at a time. ) c#a can e<tend on#( one c#a , b4t im'#ement man( inter,ace. )n inter,ace can e<tend another inter,ace, imi#ar#( to the wa( that a c#a can e<tend another c#a . E,tendin( Interface%: )n inter,ace can e<tend another inter,ace, imi#ar#( to the wa( that a c#a can e<tend another c#a . 5he e,tend% ke(word i 4 ed to e<tend an inter,ace, and the chi#d inter,ace inherit the method o, the 'arent inter,ace. 5he ,o##owin! S'ort inter,ace i e<tended b( Cocke( and %ootba## inter,ace . GG%i#ename: S'ort .*ava '4b#ic inter,ace S'ort N '4b#ic void etCome5eam7Strin! name;J '4b#ic void etVi itin!5eam7Strin! name;J Q GG%i#ename: %ootba##.*ava '4b#ic inter,ace %ootba## e<tend S'ort N '4b#ic void home5eamScored7int 'oint ;J '4b#ic void vi itin!5eamScored7int 'oint ;J '4b#ic void end&,]4arter7int @4arter;J Q GG%i#ename: Cocke(.*ava '4b#ic inter,ace Cocke( e<tend S'ort N '4b#ic void homeGoa#Scored7;J '4b#ic void vi itin!Goa#Scored7;J '4b#ic void end&,Period7int 'eriod;J '4b#ic void overtimePeriod7int ot;J Q 5he Cocke( inter,ace ha ,o4r method , b4t it inherit two ,rom S'ort J th4 , a c#a that im'#ement Cocke( need to im'#ement a## i< method . Simi#ar#(, a c#a that im'#ement %ootba## need to de,ine the three method ,rom %ootba## and the two method ,rom S'ort . E,tendin( &ulti"le Interface%: ) 6ava c#a can on#( e<tend one 'arent c#a . 24#ti'#e inheritance i not a##owed. $nter,ace are not c#a e , however, and an inter,ace can e<tend more than one 'arent inter,ace. 5he e<tend ke(word i 4 ed once, and the 'arent inter,ace are dec#ared in a comma- e'arated #i t. %or e<am'#e, i, the Cocke( inter,ace e<tended both S'ort and 9vent, it wo4#d be dec#ared a : '4b#ic inter,ace Cocke( e<tend S'ort , 9vent *a((in( Interface%: 5he mo t common 4 e o, e<tendin! inter,ace occ4r when the 'arent inter,ace doe not contain an( method . %or e<am'#e, the 2o4 e3i tener inter,ace in the *ava.awt.event 'acka!e e<tended *ava.4ti#.9vent3i tener, which i de,ined a : 'acka!e *ava.4ti#J '4b#ic inter,ace 9vent3i tener NQ )n inter,ace with no method in it i re,erred to a a ta((in( inter,ace. 5here are two ba ic de i!n '4r'o e o, ta!!in! inter,ace : 7reate% a c !! n "arent: ) with the 9vent3i tener inter,ace, which i e<tended b( doBen o, other inter,ace in the 6ava )P$, (o4 can 4 e a ta!!in! inter,ace to create a common 'arent amon! a !ro4' o, inter,ace . %or e<am'#e, when an inter,ace e<tend 9vent3i tener, the 6V2 know that thi 'artic4#ar inter,ace i !oin! to be 4 ed in an event de#e!ation cenario. +2+ 3imited i a S4b idiar( o, 5+S 3imited 81

Add% a data ty"e t a cla%%: 5hi it4ation i where the term ta!!in! come ,rom. ) c#a that im'#ement a ta!!in! inter,ace doe not need to de,ine an( method 7 ince the inter,ace doe not have an(;, b4t the c#a become an inter,ace t('e thro4!h 'o#(mor'hi m. Ja)a $ Pac=a(e% Packa!e are 4 ed in 6ava in-order to 'revent namin! con,#ict , to contro# acce , to make earchin!G#ocatin! and 4 a!e o, c#a e , inter,ace , en4meration and annotation ea ier etc. ) Packa!e can be de,ined a a !ro4'in! o, re#ated t('e 7c#a e , inter,ace , en4meration and annotation ; 'rovidin! acce 'rotection and name 'ace mana!ement. Some o, the e<i tin! 'acka!e in 6ava are:: ja)a+lan( - b4nd#e the ,4ndamenta# c#a e ja)a+i - c#a e ,or in'4t , o4t'4t ,4nction are b4nd#ed in thi 'acka!e Pro!rammer can de,ine their own 'acka!e to b4nd#e !ro4' o, c#a e Ginter,ace etc. $t i a !ood 'ractice to !ro4' re#ated c#a e im'#emented b( (o4 o that a 'ro!rammer can ea i#( determine that the c#a e , inter,ace , en4meration , annotation are re#ated. Since the 'acka!e create a new name 'ace there won?t be an( name con,#ict with name in other 'acka!e . = in! 'acka!e , it i ea ier to 'rovide acce contro# and it i a# o ea ier to #ocate the re#ated c#a ed. 7reatin( a "ac=a(e: When creatin! a 'acka!e, (o4 ho4#d choo e a name ,or the 'acka!e and '4t a "ac=a(e tatement with that name at the to' o, ever( o4rce ,i#e that contain the c#a e , inter,ace , en4meration , and annotation t('e that (o4 want to inc#4de in the 'acka!e. 5he "ac=a(e tatement ho4#d be the ,ir t #ine in the o4rce ,i#e. 5here can be on#( one 'acka!e tatement in each o4rce ,i#e, and it a''#ie to a## t('e in the ,i#e. $, a 'acka!e tatement i not 4 ed then the c#a , inter,ace , en4meration , and annotation t('e wi## be '4t into an 4nnamed 'acka!e. E,a!"le: 3et 4 #ook at an e<am'#e that create a 'acka!e ca##ed ani!al%. $t i common 'ractice to 4 e #owerca ed name o, 'acka!e to avoid an( con,#ict with the name o, c#a e , inter,ace . P4t an inter,ace in the 'acka!e a$imals: GO %i#e name : )nima#.*ava OG 'acka!e anima# J inter,ace )nima# N '4b#ic void eat7;J '4b#ic void trave#7;J Q Now '4t an im'#ementation in the ame 'acka!e a$imals: 'acka!e anima# J GO %i#e name : 2amma#$nt.*ava OG '4b#ic c#a 2amma#$nt im'#ement )nima#N '4b#ic void eat7;N S( tem.o4t.'rint#n7P2amma# eat P;J Q '4b#ic void trave#7;N S( tem.o4t.'rint#n7P2amma# trave# P;J Q '4b#ic int no&,3e! 7;N ret4rn 0J Q '4b#ic tatic void main7Strin! ar! 8:;N 2amma#$nt m K new 2amma#$nt7;J m.eat7;J m.trave#7;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 8D

Q Q Now (o4 com'i#e the e two ,i#e and '4t them in a 4b-director( ca##ed ani!al% and tr( to r4n a ,o##ow : M mkdir anima# M c' )nima#.c#a 2amma#$nt.c#a anima# M *ava anima# G2amma#$nt 2amma# eat 2amma# trave# *he i!" rt <eyw rd: $, a c#a want to 4 e another c#a in the ame 'acka!e, the 'acka!e name doe not need to be 4 ed. +#a e in the ame 'acka!e ,ind each other witho4t an( 'ecia# (nta<. E,a!"le: Cere a c#a named Eo i added to the 'a(ro## 'acka!e that a#read( contain 9m'#o(ee. 5he Eo can then re,er to the 9m'#o(ee c#a witho4t 4 in! the 'a(ro## 're,i<, a demon trated b( the ,o##owin! Eo c#a . 'acka!e 'a(ro##J '4b#ic c#a Eo N '4b#ic void 'a(9m'#o(ee79m'#o(ee e; N e.mai#+heck7;J Q Q What ha''en i, Eo i not in the 'a(ro## 'acka!e[ 5he Eo c#a m4 t then 4 e one o, the ,o##owin! techni@4e ,or re,errin! to a c#a in a di,,erent 'acka!e. 5he ,4##( @4a#i,ied name o, the c#a can be 4 ed. %or e<am'#e: 'a(ro##.9m'#o(ee 5he 'acka!e can be im'orted 4 in! the im'ort ke(word and the wi#d card 7O;. %or e<am'#e: im'ort 'a(ro##.OJ 5he c#a it e#, can be im'orted 4 in! the im'ort ke(word. %or e<am'#e: im'ort 'a(ro##.9m'#o(eeJ N te: ) c#a ,i#e can contain an( n4mber o, im'ort tatement . 5he im'ort tatement m4 t a''ear a,ter the 'acka!e tatement and be,ore the c#a dec#aration. *he Direct ry #tructure f Pac=a(e%: 5wo ma*or re 4#t occ4r when a c#a i '#aced in a 'acka!e: 5he name o, the 'acka!e become a 'art o, the name o, the c#a , a we *4 t di c4 ed in the 'revio4 ection. 5he name o, the 'acka!e m4 t match the director( tr4ct4re where the corre 'ondin! b(tecode re ide . Cere i im'#e wa( o, mana!in! (o4r ,i#e in *ava: P4t the o4rce code ,or a c#a , inter,ace, en4meration, or annotation t('e in a te<t ,i#e who e name i the im'#e name o, the t('e and who e e<ten ion i +ja)a. %or e<am'#e: GG %i#e Name : +ar.*ava 'acka!e vehic#eJ '4b#ic c#a +ar N GG +#a im'#ementation. Q Now '4t the o4rce ,i#e in a director( who e name re,#ect the name o, the 'acka!e to which the c#a be#on! : ....Ivehic#eI+ar.*ava Now the @4a#i,ied c#a name and 'athname wo4#d be a be#ow: +#a name -R vehic#e.+ar Path name -R vehic#eI+ar.*ava 7in window ; +2+ 3imited i a S4b idiar( o, 5+S 3imited 8.

$n !enera# a com'an( 4 e it rever ed $nternet domain name ,or it 'acka!e name . 9<am'#e: ) com'an(? $nternet domain name i a''#e.com, then a## it 'acka!e name wo4#d tart with com.a''#e. 9ach com'onent o, the 'acka!e name corre 'ond to a 4bdirector(. 9<am'#e: 5he com'an( had a com.a''#e.com'4ter 'acka!e that contained a De##.*ava o4rce ,i#e, it wo4#d be contained in a erie o, 4bdirectorie #ike thi : ....IcomIa''#eIcom'4ter IDe##.*ava )t the time o, com'i#ation, the com'i#er create a di,,erent o4t'4t ,i#e ,or each c#a , inter,ace and en4meration de,ined in it. 5he ba e name o, the o4t'4t ,i#e i the name o, the t('e, and it e<ten ion i +cla%% %or e<am'#e: GG %i#e Name: De##.*ava 'acka!e com.a''#e.com'4ter J '4b#ic c#a De##N Q c#a =' N

Q Now com'i#e thi ,i#e a ,o##ow 4 in! -d o'tion: M*avac -d . De##.*ava 5hi wo4#d '4t com'i#ed ,i#e a ,o##ow : .IcomIa''#eIcom'4ter IDe##.c#a .IcomIa''#eIcom'4ter I=' .c#a Fo4 can im'ort a## the c#a e or inter,ace de,ined in \com\apple\computers\ a ,o##ow : im'ort com.a''#e.com'4ter .OJ 3ike the .*ava o4rce ,i#e , the com'i#ed .c#a ,i#e ho4#d be in a erie o, directorie that re,#ect the 'acka!e name. Cowever, the 'ath to the .c#a ,i#e doe not have to be the ame a the 'ath to the .*ava o4rce ,i#e . Fo4 can arran!e (o4r o4rce and c#a directorie e'arate#(, a : V'ath-oneRI o4rce IcomIa''#eIcom'4ter IDe##.*ava V'ath-twoRIc#a e IcomIa''#eIcom'4ter IDe##.c#a E( doin! thi , it i 'o ib#e to !ive the c#a e director( to other 'ro!rammer witho4t revea#in! (o4r o4rce . Fo4 a# o need to mana!e o4rce and c#a ,i#e in thi manner o that the com'i#er and the 6ava Virt4a# 2achine 76V2; can ,ind a## the t('e (o4r 'ro!ram 4 e . 5he ,4## 'ath to the c#a e director(, V'ath-twoRIc#a e , i ca##ed the c#a 'ath, and i et with the +3)SSP)5C ( tem variab#e. Eoth the com'i#er and the 6V2 con tr4ct the 'ath to (o4r .c#a ,i#e b( addin! the 'acka!e name to the c#a 'ath. Sa( V'ath-twoRIc#a e i the c#a 'ath, and the 'acka!e name i com.a''#e.com'4ter , then the com'i#er and 6V2 wi## #ook ,or .c#a ,i#e in V'ath-twoRIc#a e IcomIa''#eIcom't4er . ) c#a 'ath ma( inc#4de evera# 'ath . 24#ti'#e 'ath ho4#d be e'arated b( a emico#on 7Window ; or co#on 7=ni<;. E( de,a4#t, the com'i#er and the 6V2 earch the c4rrent director( and the 6)" ,i#e containin! the 6ava '#at,orm c#a e o that the e directorie are a4tomatica##( in the c#a 'ath. #et 74A##PA*' #y%te! Variable: 5o di '#a( the c4rrent +3)SSP)5C variab#e, 4 e the ,o##owin! command in Window and =ni< 7Eo4rne he##;: $n Window -R +:IR et +3)SSP)5C $n =ni< -R L echo M+3)SSP)5C 5o de#ete the c4rrent content o, the +3)SSP)5C variab#e, 4 e : $n Window -R +:IR et +3)SSP)5CK $n =ni< -R L 4n et +3)SSP)5CJ e<'ort +3)SSP)5C 5o et the +3)SSP)5C variab#e: $n Window -R et +3)SSP)5CK+:I4 er I*ackI*avaIc#a e $n =ni< -R L +3)SSP)5CKGhomeG*ackG*avaGc#a e J e<'ort +3)SSP)5C Ja)a $ Data #tructure% 5he data tr4ct4re 'rovided b( the 6ava 4ti#it( 'acka!e are ver( 'ower,4# and 'er,orm a wide ran!e o, ,4nction . 5he e data tr4ct4re con i t o, the ,o##owin! inter,ace and c#a e : 9n4meration +2+ 3imited i a S4b idiar( o, 5+S 3imited 8/

EitSet Vector Stack Dictionar( Ca htab#e Pro'ertie )## the e c#a e are now #e!ac( and 6ava-2 ha introd4ced a new ,ramework ca##ed +o##ection %ramework which i di c4 ed in ne<t t4toria#: *he Enu!erati n: 5he 9n4meration inter,ace i n?t it e#, a data tr4ct4re, b4t it i ver( im'ortant within the conte<t o, other data tr4ct4re . 5he 9n4meration inter,ace de,ine a mean to retrieve 4cce ive e#ement ,rom a data tr4ct4re. %or e<am'#e, 9n4meration de,ine a method ca##ed ne<t9#ement that i 4 ed to !et the ne<t e#ement in a data tr4ct4re that contain m4#ti'#e e#ement . *he 6it#et 5he EitSet c#a im'#ement a !ro4' o, bit , or ,#a! , that can be et and c#eared individ4a##(. 5hi c#a i ver( 4 e,4# in ca e where (o4 need to kee' 4' with a et o, boo#ean va#4e J (o4 *4 t a i!n a bit to each va#4e and et or c#ear it a a''ro'riate. *he Vect r 5he Vector c#a i imi#ar to a traditiona# 6ava arra(, e<ce't that it can !row a nece ar( to accommodate new e#ement . 3ike an arra(, e#ement o, a Vector ob*ect can be acce ed via an inde< into the vector. 5he nice thin! abo4t 4 in! the Vector c#a i that (o4 don?t have to worr( abo4t ettin! it to a 'eci,ic iBe 4'on creationJ it hrink and !row a4tomatica##( when nece ar(. *he #tac= 5he Stack c#a im'#ement a #a t-in-,ir t-o4t 73$%&; tack o, e#ement . Fo4 can think o, a tack #itera##( a a vertica# tack o, ob*ect J when (o4 add a new e#ement, it !et tacked on to' o, the other . When (o4 '4## an e#ement o,, the tack, it come o,, the to'. $n other word , the #a t e#ement (o4 added to the tack i the ,ir t one to come back o,,. *he Dicti nary 5he Dictionar( c#a i an ab tract c#a that de,ine a data tr4ct4re ,or ma''in! ke( to va#4e . 5hi i 4 e,4# in ca e where (o4 want to be ab#e to acce data via a 'artic4#ar ke( rather than an inte!er inde<. Since the Dictionar( c#a i ab tract, it 'rovide on#( the ,ramework ,or a ke(-ma''ed data tr4ct4re rather than a 'eci,ic im'#ementation. *he 'a%htable 5he Ca htab#e c#a 'rovide a mean o, or!aniBin! data ba ed on ome 4 er-de,ined ke( tr4ct4re. %or e<am'#e, in an addre #i t ha h tab#e (o4 co4#d tore and ort data ba ed on a ke( 4ch a S$P code rather than on a 'er on? name. 5he 'eci,ic meanin! o, ke( in re!ard to ha h tab#e i tota##( de'endent on the 4 a!e o, the ha h tab#e and the data it contain . *he Pr "ertie% Pro'ertie i a 4bc#a o, Ca htab#e. $t i 4 ed to maintain #i t o, va#4e in which the ke( i a Strin! and the va#4e i a# o a Strin!. 5he Pro'ertie c#a i 4 ed b( man( other 6ava c#a e . %or e<am'#e, it i the t('e o, ob*ect ret4rned b( S( tem.!etPro'ertie 7 ; when obtainin! environmenta# va#4e . Ja)a 7 llecti n% Fra!ew r= Prior to 6ava 2, 6ava 'rovided ad hoc c#a e 4ch a Dicti nary, Vect r, #tac=, and Pr "ertie% to tore and mani'4#ate !ro4' o, ob*ect . )#tho4!h the e c#a e were @4ite 4 e,4#, the( #acked a centra#, 4ni,(in! theme. 5h4 , the wa( that (o4 4 ed Vector wa di,,erent ,rom the wa( that (o4 4 ed Pro'ertie . 5he co##ection ,ramework wa de i!ned to meet evera# !oa# . 5he ,ramework had to be hi!h-'er,ormance. 5he im'#ementation ,or the ,4ndamenta# co##ection 7d(namic arra( , #inked #i t , tree , and ha h tab#e ; are hi!h#( e,,icient. 5he ,ramework had to a##ow di,,erent t('e o, co##ection to work in a imi#ar manner and with a hi!h de!ree o, intero'erabi#it(. 9<tendin! andGor ada'tin! a co##ection had to be ea (. +2+ 3imited i a S4b idiar( o, 5+S 3imited 80

5oward thi end, the entire co##ection ,ramework i de i!ned aro4nd a et o, tandard inter,ace . Severa# tandard im'#ementation 4ch a 4in=ed4i%t, 'a%h#et, and *ree#et, o, the e inter,ace are 'rovided that (o4 ma( 4 e a -i and (o4 ma( a# o im'#ement (o4r own co##ection, i, (o4 choo e. ) co##ection ,ramework i a 4ni,ied architect4re ,or re're entin! and mani'4#atin! co##ection . )## co##ection ,ramework contain the ,o##owin!: Interface%: 5he e are ab tract data t('e that re're ent co##ection . $nter,ace a##ow co##ection to be mani'4#ated inde'endent#( o, the detai# o, their re're entation. $n ob*ect-oriented #an!4a!e , inter,ace !enera##( ,orm a hierarch(. I!"le!entati n% i+e+ 7la%%e%: 5he e are the concrete im'#ementation o, the co##ection inter,ace . $n e ence, the( are re4 ab#e data tr4ct4re . Al( rith!%: 5he e are the method that 'er,orm 4 e,4# com'4tation , 4ch a earchin! and ortin!, on ob*ect that im'#ement co##ection inter,ace . 5he a#!orithm are aid to be 'o#(mor'hic: that i , the ame method can be 4 ed on man( di,,erent im'#ementation o, the a''ro'riate co##ection inter,ace. $n addition to co##ection , the ,ramework de,ine evera# ma' inter,ace and c#a e . 2a' tore ke(Gva#4e 'air . )#tho4!h ma' are not collectio$s in the 'ro'er 4 e o, the term, b4t the( are ,4##( inte!rated with co##ection . *he 7 llecti n Interface%: 5he co##ection ,ramework de,ine evera# inter,ace . 5hi ection 'rovide an overview o, each inter,ace: # Interface% with De%cri"ti n N 5hi enab#e (o4 to work with !ro4' o, ob*ect J it i at the to' o, the co##ection 1 hierarch(. 2 5hi e<tend 7 llecti n and an in tance o, 3i t tore an ordered co##ection o, e#ement . 1 5hi e<tend +o##ection to hand#e et , which m4 t contain 4ni@4e e#ement D 5hi e<tend Set to hand#e orted et . 5hi ma' 4ni@4e ke( to va#4e . / 5hi de cribe an e#ement 7a ke(Gva#4e 'air; in a ma'. 5hi i an inner c#a o, 2a'. 0 5hi e<tend 2a' o that the ke( are maintained in a cendin! order. 5hi i #e!ac( inter,ace and de,ine the method b( which (o4 can en4merate 7obtain one 8 at a time; the e#ement in a co##ection o, ob*ect . 5hi #e!ac( inter,ace ha been 4'erceded b( $terator. *he 7 llecti n 7la%%e%: 6ava 'rovide a et o, tandard co##ection c#a e that im'#ement +o##ection inter,ace . Some o, the c#a e 'rovide ,4## im'#ementation that can be 4 ed a -i and other are ab tract c#a , 'rovidin! ke#eta# im'#ementation that are 4 ed a tartin! 'oint ,or creatin! concrete co##ection . 5he tandard co##ection c#a e are 4mmariBed in the ,o##owin! tab#e: # 7la%%e% with De%cri"ti n N Ab%tract7 llecti n 1 $m'#ement mo t o, the +o##ection inter,ace. Ab%tract4i%t 2 9<tend )b tract+o##ection and im'#ement mo t o, the 3i t inter,ace. Ab%tract#e@uential4i%t 1 9<tend )b tract3i t ,or 4 e b( a co##ection that 4 e e@4entia# rather than random acce o, it e#ement . D $m'#ement a #inked #i t b( e<tendin! )b tractSe@4entia#3i t. . $m'#ement a d(namic arra( b( e<tendin! )b tract3i t. Ab%tract#et / 9<tend )b tract+o##ection and im'#ement mo t o, the Set inter,ace. 0 9<tend )b tractSet ,or 4 e with a ha h tab#e. 8 9<tend Ca hSet to a##ow in ertion-order iteration . - $m'#ement a et tored in a tree. 9<tend )b tractSet. 10 Ab%tract&a" +2+ 3imited i a S4b idiar( o, 5+S 3imited 88

$m'#ement mo t o, the 2a' inter,ace. 11 9<tend )b tract2a' to 4 e a ha h tab#e. 12 9<tend )b tract2a' to 4 e a tree. 11 9<tend )b tract2a' to 4 e a ha h tab#e with weak ke( . 1D 9<tend Ca h2a' to a##ow in ertion-order iteration . 1. 9<tend )b tract2a' and 4 e re,erence e@4a#it( when com'arin! doc4ment . 5he *bstractCollectio$4 *bstract.et4 *bstract5ist4 *bstract.e6ue$tial5ist and *bstractMap c#a e 'rovide ke#eta# im'#ementation o, the core co##ection inter,ace , to minimiBe the e,,ort re@4ired to im'#ement them. 5he ,o##owin! #e!ac( c#a e de,ined b( *ava.4ti# ha been di c4 ed in 'revio4 t4toria#: # 7la%%e% with De%cri"ti n N 1 5hi im'#ement a d(namic arra(. $t i imi#ar to )rra(3i t, b4t with ome di,,erence . 2 Stack i a 4bc#a o, Vector that im'#ement a tandard #a t-in, ,ir t-o4t tack. Dictionar( i an ab tract c#a that re're ent a ke(Gva#4e tora!e re'o itor( and 1 o'erate m4ch #ike 2a'. Ca htab#e wa 'art o, the ori!ina# *ava.4ti# and i a concrete im'#ementation o, a D Dictionar(. Pro'ertie i a 4bc#a o, Ca htab#e. $t i 4 ed to maintain #i t o, va#4e in which the . ke( i a Strin! and the va#4e i a# o a Strin!. ) EitSet c#a create a 'ecia# t('e o, arra( that ho#d bit va#4e . 5hi arra( can increa e / in iBe a needed. *he 7 llecti n Al( rith!%: 5he co##ection ,ramework de,ine evera# a#!orithm that can be a''#ied to co##ection and ma' . 5he e a#!orithm are de,ined a tatic method within the +o##ection c#a . Severa# o, the method can throw a 7la%%7a%tE,ce"ti n, which occ4r when an attem't i made to com'are incom'atib#e t('e , or an 5n%u"" rtedO"erati nE,ce"ti n, which occ4r when an attem't i made to modi,( an 4nmodi,iab#e co##ection. +o##ection de,ine three tatic variab#e : 92P5FTS95, 92P5FT3$S5, and 92P5FT2)P. )## are imm4tab#e. # Al( rith!% with De%cri"ti n N Cere i a #i t o, a## the a#!orithm 1 im'#ementation. ' w t u%e an Iterat r ? &,ten, (o4 wi## want to c(c#e thro4!h the e#ement in a co##ection. %or e<am'#e, (o4 mi!ht want to di '#a( each e#ement. 5he ea ie t wa( to do thi i to em'#o( an iterator, which i an ob*ect that im'#ement either the $terator or the 3i t$terator inter,ace. $terator enab#e (o4 to c(c#e thro4!h a co##ection, obtainin! or removin! e#ement . 3i t$terator e<tend $terator to a##ow bidirectiona# traver a# o, a #i t, and the modi,ication o, e#ement . # Iterat r &eth d% with De%cri"ti n N Cere i a #i t o, a## the method with e<am'#e 'rovided b( $terator and 3i t$terator 1 inter,ace . ' w t u%e an 7 !"arat r ? Eoth 5reeSet and 5ree2a' tore e#ement in orted order. Cowever, it i the com'arator that de,ine 'reci e#( what sorted order mean . 5hi inter,ace #et 4 ort a !iven co##ection an( n4mber o, di,,erent wa( . )# o thi inter,ace can be 4 ed to ort an( in tance o, an( c#a .7even c#a e we cannot modi,(;. # Iterat r &eth d% with De%cri"ti n N Cere i a #i t o, a## the method with e<am'#e 'rovided b( +om'arator 1 $nter,ace. #u!!ary: +2+ 3imited i a S4b idiar( o, 5+S 3imited 8-

5he 6ava co##ection ,ramework !ive the 'ro!rammer acce to 're'acka!ed data tr4ct4re a we## a to a#!orithm ,or mani'4#atin! them. Ja)a $ Ceneric% $t wo4#d be nice i, we co4#d write a in!#e ort method that co4#d ort the e#ement in an $nte!er arra(, a Strin! arra( or an arra( o, an( t('e that 4''ort orderin!. 6ava Ceneric method and !eneric c#a e enab#e 'ro!rammer to 'eci,(, with a in!#e method dec#aration, a et o, re#ated method or, with a in!#e c#a dec#aration, a et o, re#ated t('e , re 'ective#(. Generic a# o 'rovide com'i#e-time t('e a,et( that a##ow 'ro!rammer to catch inva#id t('e at com'i#e time. = in! 6ava Generic conce't we mi!ht write a !eneric method ,or ortin! an arra( o, ob*ect , then invoke the !eneric method with $nte!er arra( , Do4b#e arra( , Strin! arra( and o on, to ort the arra( e#ement . Ceneric &eth d%: Fo4 can write a in!#e !eneric method dec#aration that can be ca##ed with ar!4ment o, di,,erent t('e . Ea ed on the t('e o, the ar!4ment 'a ed to the !eneric method, the com'i#er hand#e each method ca## a''ro'riate#(. %o##owin! are the r4#e to de,ine Generic 2ethod : )## !eneric method dec#aration have a t('e 'arameter ection de#imited b( an!#e bracket 7V and R; that 'recede the method? ret4rn t('e 7 V 9 R in the ne<t e<am'#e;. 9ach t('e 'arameter ection contain one or more t('e 'arameter e'arated b( comma . ) t('e 'arameter, a# o known a a t('e variab#e, i an identi,ier that 'eci,ie a !eneric t('e name. 5he t('e 'arameter can be 4 ed to dec#are the ret4rn t('e and act a '#aceho#der ,or the t('e o, the ar!4ment 'a ed to the !eneric method, which are known a act4a# t('e ar!4ment . ) !eneric method? bod( i dec#ared #ike that o, an( other method. Note that t('e 'arameter can re're ent on#( re,erence t('e not 'rimitive t('e 7#ike int, do4b#e and char;. E,a!"le: %o##owin! e<am'#e i##4 trate how we can 'rint arra( o, di,,erent t('e 4 in! a in!#e Generic method: '4b#ic c#a Generic2ethod5e t N GG !eneric method 'rint)rra( '4b#ic tatic V 9 R void 'rint)rra(7 98: in'4t)rra( ; N GG Di '#a( arra( e#ement ,or 7 9 e#ement : in'4t)rra( ;N S( tem.o4t.'rint,7 PL P, e#ement ;J Q S( tem.o4t.'rint#n7;J Q '4b#ic tatic void main7 Strin! ar! 8: ; N GG +reate arra( o, $nte!er, Do4b#e and +haracter $nte!er8: int)rra( K N 1, 2, 1, D, . QJ Do4b#e8: do4b#e)rra( K N 1.1, 2.2, 1.1, D.D QJ +haracter8: char)rra( K N ?C?, ?9?, ?3?, ?3?, ?&? QJ S( tem.o4t.'rint#n7 P)rra( inte!er)rra( contain :P ;J 'rint)rra(7 int)rra( ;J GG 'a an $nte!er arra( S( tem.o4t.'rint#n7 PIn)rra( do4b#e)rra( contain :P ;J 'rint)rra(7 do4b#e)rra( ;J GG 'a a Do4b#e arra( S( tem.o4t.'rint#n7 PIn)rra( character)rra( contain :P ;J 'rint)rra(7 char)rra( ;J GG 'a a +haracter arra( Q Q +2+ 3imited i a S4b idiar( o, 5+S 3imited -0

5hi wo4#d 'rod4ce ,o##owin! re 4#t: )rra( inte!er)rra( contain : 121D./ )rra( do4b#e)rra( contain : 1.1 2.2 1.1 D.D )rra( character)rra( contain : C933& 6 unded *y"e Para!eter%: 5here ma( be time when (o4?## want to re trict the kind o, t('e that are a##owed to be 'a ed to a t('e 'arameter. %or e<am'#e, a method that o'erate on n4mber mi!ht on#( want to acce't in tance o, N4mber or it 4bc#a e . 5hi i what bo4nded t('e 'arameter are ,or. 5o dec#are a bo4nded t('e 'arameter, #i t the t('e 'arameter? name, ,o##owed b( the e<tend ke(word, ,o##owed b( it 4''er bo4nd. E,a!"le: %o##owin! e<am'#e i##4 trate how e<tend i 4 ed in a !enera# en e to mean either Pe<tend P 7a in c#a e ; or Pim'#ement P 7a in inter,ace ;. 5hi e<am'#e i Generic method to ret4rn the #ar!e t o, three +om'arab#e ob*ect : '4b#ic c#a 2a<im4m5e t N GG determine the #ar!e t o, three +om'arab#e ob*ect '4b#ic tatic V5 e<tend +om'arab#eV5RR 5 ma<im4m75 <, 5 (, 5 B; N 5 ma< K <J GG a 4me < i initia##( the #ar!e t i, 7 (.com'are5o7 ma< ; R 0 ;N ma< K (J GG ( i the #ar!e t o ,ar Q i, 7 B.com'are5o7 ma< ; R 0 ;N ma< K BJ GG B i the #ar!e t now Q ret4rn ma<J GG ret4rn the #ar!e t ob*ect Q '4b#ic tatic void main7 Strin! ar! 8: ; N S( tem.o4t.'rint,7 P2a< o, Ld, Ld and Ld i LdInInP, 1, D, ., ma<im4m7 1, D, . ; ;J S( tem.o4t.'rint,7 P2a<m o, L.1,,L.1, and L.1, i L.1,InInP, /./, 8.8, 0.0, ma<im4m7 /./, 8.8, 0.0 ; ;J S( tem.o4t.'rint,7 P2a< o, L , L and L i L InP,P'earP, Pa''#eP, Poran!eP, ma<im4m7 P'earP, Pa''#eP, Poran!eP ; ;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 2a<im4m o, 1, D and . i . 2a<im4m o, /./, 8.8 and 0.0 i 8.8 2a<im4m o, 'ear, a''#e and oran!e i 'ear Ceneric 7la%%e%: ) !eneric c#a dec#aration #ook #ike a non-!eneric c#a dec#aration, e<ce't that the c#a name i ,o##owed b( a t('e 'arameter ection. ) with !eneric method , the t('e 'arameter ection o, a !eneric c#a can have one or more t('e 'arameter e'arated b( comma . 5he e c#a e are known a 'arameteriBed c#a e or 'arameteriBed t('e beca4 e the( acce't one or more 'arameter . E,a!"le: %o##owin! e<am'#e i##4 trate how we can de,ine a !eneric c#a : +2+ 3imited i a S4b idiar( o, 5+S 3imited

-1

'4b#ic c#a 'rivate 5 tJ

Eo<V5R N

'4b#ic void add75 t; N thi .t K tJ Q '4b#ic 5 !et7; N ret4rn tJ Q '4b#ic tatic void main7Strin!8: ar! ; N Eo<V$nte!erR inte!erEo< K new Eo<V$nte!erR7;J Eo<VStrin!R trin!Eo< K new Eo<VStrin!R7;J inte!erEo<.add7new $nte!er710;;J trin!Eo<.add7new Strin!7PCe##o Wor#dP;;J S( tem.o4t.'rint,7P$nte!er Va#4e :LdInInP, inte!erEo<.!et7;;J S( tem.o4t.'rint,7PStrin! Va#4e :L InP, trin!Eo<.!et7;;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: $nte!er Va#4e :10 Strin! Va#4e :Ce##o Wor#d Ja)a $ #eriali>ati n 6ava 'rovide a mechani m, ca##ed ob*ect eria#iBation where an ob*ect can be re're ented a a e@4ence o, b(te that inc#4de the ob*ect? data a we## a in,ormation abo4t the ob*ect? t('e and the t('e o, data tored in the ob*ect. ),ter a eria#iBed ob*ect ha been written into a ,i#e, it can be read ,rom the ,i#e and de eria#iBed that i , the t('e in,ormation and b(te that re're ent the ob*ect and it data can be 4 ed to recreate the ob*ect in memor(. 2o t im're ive i that the entire 'roce i 6V2 inde'endent, meanin! an ob*ect can be eria#iBed on one '#at,orm and de eria#iBed on an entire#( di,,erent '#at,orm. +#a e ObjectIn"ut#trea! and ObjectOut"ut#trea! are hi!h-#eve# tream that contain the method ,or eria#iBin! and de eria#iBin! an ob*ect. 5he &b*ect&4t'4tStream c#a contain man( write method ,or writin! vario4 data t('e , b4t one method in 'artic4#ar tand o4t: '4b#ic ,ina# void write&b*ect7&b*ect <; throw $&9<ce'tion 5he above method eria#iBe an &b*ect and end it to the o4t'4t tream. Simi#ar#(, the &b*ect$n'4tStream c#a contain the ,o##owin! method ,or de eria#iBin! an ob*ect: '4b#ic ,ina# &b*ect read&b*ect7; throw $&9<ce'tion, +#a Not%o4nd9<ce'tion 5hi method retrieve the ne<t &b*ect o4t o, the tream and de eria#iBe it. 5he ret4rn va#4e i &b*ect, o (o4 wi## need to ca t it to it a''ro'riate data t('e. 5o demon trate how eria#iBation work in 6ava, $ am !oin! to 4 e the 9m'#o(ee c#a that we di c4 ed ear#( on in the book. S4''o e that we have the ,o##owin! 9m'#o(ee c#a , which im'#ement the Seria#iBab#e inter,ace: '4b#ic c#a 9m'#o(ee im'#ement *ava.io.Seria#iBab#e N '4b#ic Strin! nameJ '4b#ic Strin! addre J '4b#ic tran ient int SSNJ '4b#ic int n4mberJ '4b#ic void mai#+heck7; N +2+ 3imited i a S4b idiar( o, 5+S 3imited -2

S( tem.o4t.'rint#n7P2ai#in! a check to P > name > P P > addre ;J Q Q Notice that ,or a c#a to be eria#iBed 4cce ,4##(, two condition m4 t be met: 5he c#a m4 t im'#ement the *ava.io.Seria#iBab#e inter,ace. )## o, the ,ie#d in the c#a m4 t be eria#iBab#e. $, a ,ie#d i not eria#iBab#e, it m4 t be marked tran ient. $, (o4 are c4rio4 to know i, a 6ava Satandard +#a i eria#iBab#e or not, check the doc4mentation ,or the c#a . 5he te t i im'#e: $, the c#a im'#ement *ava.io.Seria#iBab#e, then it i eria#iBab#eJ otherwi e, it? not. #eriali>in( an Object: 5he &b*ect&4t'4tStream c#a i 4 ed to eria#iBe an &b*ect. 5he ,o##owin! Seria#iBeDemo 'ro!ram in tantiate an 9m'#o(ee ob*ect and eria#iBe it to a ,i#e. When the 'ro!ram i done e<ec4tin!, a ,i#e named em'#o(ee. er i created. 5he 'ro!ram doe not !enerate an( o4t'4t, b4t t4d( the code and tr( to determine what the 'ro!ram i doin!. N te: When eria#iBin! an ob*ect to a ,i#e, the tandard convention in 6ava i to !ive the ,i#e a +%er e<ten ion. im'ort *ava.io.OJ '4b#ic c#a Seria#iBeDemo N '4b#ic tatic void main7Strin! 8: ar! ; N 9m'#o(ee e K new 9m'#o(ee7;J e.name K P"e(an )#iPJ e.addre K PPhokka H4an, )mbehta PeerPJ e.SSN K 11122111J e.n4mber K 101J tr( N %i#e&4t'4tStream ,i#e&4t K new %i#e&4t'4tStream7Pem'#o(ee. erP;J &b*ect&4t'4tStream o4t K new &b*ect&4t'4tStream7,i#e&4t;J o4t.write&b*ect7e;J o4t.c#o e7;J ,i#e&4t.c#o e7;J Qcatch7$&9<ce'tion i; N i.'rintStack5race7;J Q Q Q De%eriali>in( an Object: 5he ,o##owin! De eria#iBeDemo 'ro!ram de eria#iBe the 9m'#o(ee ob*ect created in the Seria#iBeDemo 'ro!ram. St4d( the 'ro!ram and tr( to determine it o4t'4t: im'ort *ava.io.OJ '4b#ic c#a De eria#iBeDemo N '4b#ic tatic void main7Strin! 8: ar! ; N 9m'#o(ee e K n4##J tr( N %i#e$n'4tStream ,i#e$n K new %i#e$n'4tStream7Pem'#o(ee. erP;J &b*ect$n'4tStream in K new &b*ect$n'4tStream7,i#e$n;J +2+ 3imited i a S4b idiar( o, 5+S 3imited -1

e K 79m'#o(ee; in.read&b*ect7;J in.c#o e7;J ,i#e$n.c#o e7;J Qcatch7$&9<ce'tion i; N i.'rintStack5race7;J ret4rnJ Qcatch7+#a Not%o4nd9<ce'tion c; N S( tem.o4t.'rint#n7P9m'#o(ee c#a not ,o4ndP;J c.'rintStack5race7;J ret4rnJ Q S( tem.o4t.'rint#n7PDe eria#iBed 9m'#o(ee...P;J S( tem.o4t.'rint#n7PName: P > e.name;J S( tem.o4t.'rint#n7P)ddre : P > e.addre ;J S( tem.o4t.'rint#n7PSSN: P > e.SSN;J S( tem.o4t.'rint#n7PN4mber: P > e.n4mber;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: De eria#iBed 9m'#o(ee... Name: "e(an )#i )ddre :Phokka H4an, )mbehta Peer SSN: 0 N4mber:101 Cere are ,o##owin! im'ortant 'oint to be noted: 5he tr(Gcatch b#ock trie to catch a +#a Not%o4nd9<ce'tion, which i dec#ared b( the read&b*ect7; method. %or a 6V2 to be ab#e to de eria#iBe an ob*ect, it m4 t be ab#e to ,ind the b(tecode ,or the c#a . $, the 6V2 can?t ,ind a c#a d4rin! the de eria#iBation o, an ob*ect, it throw a +#a Not%o4nd9<ce'tion. Notice that the ret4rn va#4e o, read&b*ect7; i ca t to an 9m'#o(ee re,erence. 5he va#4e o, the SSN ,ie#d wa 11122111 when the ob*ect wa eria#iBed, b4t beca4 e the ,ie#d i tran ient, thi va#4e wa not ent to the o4t'4t tream. 5he SSN ,ie#d o, the de eria#iBed 9m'#o(ee ob*ect i 0. Ja)a $ Netw r=in( 8# c=et Pr (ra!!in(; 5he term $et2ork programmi$g re,er to writin! 'ro!ram that e<ec4te acro m4#ti'#e device 7com'4ter ;, in which the device are a## connected to each other 4 in! a network. 5he *ava.net 'acka!e o, the 62S9 )P$ contain a co##ection o, c#a e and inter,ace that 'rovide the #ow-#eve# comm4nication detai# , a##owin! (o4 to write 'ro!ram that ,oc4 on o#vin! the 'rob#em at hand. 5he *ava.net 'acka!e 'rovide 4''ort ,or the two common network 'rotoco# : *7P: 5+P tand ,or 5ran mi ion +ontro# Protoco#, which a##ow ,or re#iab#e comm4nication between two a''#ication . 5+P i t('ica##( 4 ed over the $nternet Protoco#, which i re,erred to a 5+PG$P. 5DP: =DP tand ,or = er Data!ram Protoco#, a connection-#e 'rotoco# that a##ow ,or 'acket o, data to be tran mitted between a''#ication . 5hi t4toria# !ive !ood 4nder tandin! on the ,o##owin! two 4b*ect : # c=et Pr (ra!!in(: 5hi i mo t wide#( 4 ed conce't in Networkin! and it ha been e<'#ained in ver( detai#. 5R4 Pr ce%%in(: 5hi wo4#d be covered e'arate#(. +#ick here to #earn abo4t ="3 Proce in! in 6ava #an!4a!e. # c=et Pr (ra!!in(: Socket 'rovide the comm4nication mechani m between two com'4ter 4 in! 5+P. ) c#ient 'ro!ram create a ocket on it end o, the comm4nication and attem't to connect that ocket to a erver. When the connection i made, the erver create a ocket ob*ect on it end o, the comm4nication. 5he c#ient and erver can now comm4nicate b( writin! to and readin! ,rom the ocket. +2+ 3imited i a S4b idiar( o, 5+S 3imited -D

5he *ava.net.Socket c#a re're ent a ocket, and the *ava.net.ServerSocket c#a 'rovide a mechani m ,or the erver 'ro!ram to #i ten ,or c#ient and e tab#i h connection with them. 5he ,o##owin! te' occ4r when e tab#i hin! a 5+P connection between two com'4ter 4 in! ocket : 5he erver in tantiate a ServerSocket ob*ect, denotin! which 'ort n4mber comm4nication i to occ4r on. 5he erver invoke the acce't7; method o, the ServerSocket c#a . 5hi method wait 4nti# a c#ient connect to the erver on the !iven 'ort. ),ter the erver i waitin!, a c#ient in tantiate a Socket ob*ect, 'eci,(in! the erver name and 'ort n4mber to connect to. 5he con tr4ctor o, the Socket c#a attem't to connect the c#ient to the 'eci,ied erver and 'ort n4mber. $, comm4nication i e tab#i hed, the c#ient now ha a Socket ob*ect ca'ab#e o, comm4nicatin! with the erver. &n the erver ide, the acce't7; method ret4rn a re,erence to a new ocket on the erver that i connected to the c#ient? ocket. ),ter the connection are e tab#i hed, comm4nication can occ4r 4 in! $G& tream . 9ach ocket ha both an &4t'4tStream and an $n'4tStream. 5he c#ient? &4t'4tStream i connected to the erver? $n'4tStream, and the c#ient? $n'4tStream i connected to the erver? &4t'4tStream. 5+P i a twowa( comm4nication 'rotoco#, o data can be ent acro both tream at the ame time. 5here are ,o##owin! 4 e,4## c#a e 'rovidin! com'#ete et o, method to im'#ement ocket . #er)er# c=et 7la%% &eth d%: 5he ja)a+net+#er)er# c=et c#a i 4 ed b( erver a''#ication to obtain a 'ort and #i ten ,or c#ient re@4e t 5he ServerSocket c#a ha ,o4r con tr4ctor : # &eth d% with De%cri"ti n N "ublic #er)er# c=et8int " rt; thr w% IOE,ce"ti n 1 )ttem't to create a erver ocket bo4nd to the 'eci,ied 'ort. )n e<ce'tion occ4r i, the 'ort i a#read( bo4nd b( another a''#ication. "ublic #er)er# c=et8int " rt, int bac=l (; thr w% IOE,ce"ti n 2 Simi#ar to the 'revio4 con tr4ctor, the back#o! 'arameter 'eci,ie how man( incomin! c#ient to tore in a wait @4e4e. "ublic #er)er# c=et8int " rt, int bac=l (, InetAddre%% addre%%; thr w% IOE,ce"ti n Simi#ar to the 'revio4 con tr4ctor, the $net)ddre 'arameter 'eci,ie the #oca# $P 1 addre to bind to. 5he $net)ddre i 4 ed ,or erver that ma( have m4#ti'#e $P addre e , a##owin! the erver to 'eci,( which o, it $P addre e to acce't c#ient re@4e t on "ublic #er)er# c=et8; thr w% IOE,ce"ti n D +reate an 4nbo4nd erver ocket. When 4 in! thi con tr4ctor, 4 e the bind7; method when (o4 are read( to bind the erver ocket $, the ServerSocket con tr4ctor doe not throw an e<ce'tion, it mean that (o4r a''#ication ha 4cce ,4##( bo4nd to the 'eci,ied 'ort and i read( ,or c#ient re@4e t . Cere are ome o, the common method o, the ServerSocket c#a : # &eth d% with De%cri"ti n N "ublic int (et4 calP rt8; 1 "et4rn the 'ort that the erver ocket i #i tenin! on. 5hi method i 4 e,4# i, (o4 'a ed in 0 a the 'ort n4mber in a con tr4ctor and #et the erver ,ind a 'ort ,or (o4. "ublic # c=et acce"t8; thr w% IOE,ce"ti n Wait ,or an incomin! c#ient. 5hi method b#ock 4nti# either a c#ient connect to the erver 2 on the 'eci,ied 'ort or the ocket time o4t, a 4min! that the time-o4t va#4e ha been et 4 in! the etSo5imeo4t7; method. &therwi e, thi method b#ock inde,inite#( "ublic ) id %et# *i!e ut8int ti!e ut; 1 Set the time-o4t va#4e ,or how #on! the erver ocket wait ,or a c#ient d4rin! the acce't7;. +2+ 3imited i a S4b idiar( o, 5+S 3imited -.

"ublic ) id bind8# c=etAddre%% h %t, int bac=l (; D Eind the ocket to the 'eci,ied erver and 'ort in the Socket)ddre ob*ect. = e thi method i, (o4 in tantiated the ServerSocket 4 in! the no-ar!4ment con tr4ctor. When the ServerSocket invoke acce't7;, the method doe not ret4rn 4nti# a c#ient connect . ),ter a c#ient doe connect, the ServerSocket create a new Socket on an 4n 'eci,ied 'ort and ret4rn a re,erence to thi new Socket. ) 5+P connection now e<i t between the c#ient and erver, and comm4nication can be!in. # c=et 7la%% &eth d%: 5he ja)a+net+# c=et c#a re're ent the ocket that both the c#ient and erver 4 e to comm4nicate with each other. 5he c#ient obtain a Socket ob*ect b( in tantiatin! one, wherea the erver obtain a Socket ob*ect ,rom the ret4rn va#4e o, the acce't7; method. 5he Socket c#a ha ,ive con tr4ctor that a c#ient 4 e to connect to a erver: # &eth d% with De%cri"ti n N "ublic # c=et8#trin( h %t, int " rt; thr w% 5n=n wn' %tE,ce"ti n, IOE,ce"ti n+ 5hi method attem't to connect to the 'eci,ied erver at the 'eci,ied 'ort. $, thi 1 con tr4ctor doe not throw an e<ce'tion, the connection i 4cce ,4# and the c#ient i connected to the erver. "ublic # c=et8InetAddre%% h %t, int " rt; thr w% IOE,ce"ti n 2 5hi method i identica# to the 'revio4 con tr4ctor, e<ce't that the ho t i denoted b( an $net)ddre ob*ect. "ublic # c=et8#trin( h %t, int " rt, InetAddre%% l calAddre%%, int l calP rt; thr w% IOE,ce"ti n+ 1 +onnect to the 'eci,ied ho t and 'ort, creatin! a ocket on the #oca# ho t at the 'eci,ied addre and 'ort. "ublic # c=et8InetAddre%% h %t, int " rt, InetAddre%% l calAddre%%, int l calP rt; thr w% IOE,ce"ti n+ D 5hi method i identica# to the 'revio4 con tr4ctor, e<ce't that the ho t i denoted b( an $net)ddre ob*ect in tead o, a Strin! "ublic # c=et8; . +reate an 4nconnected ocket. = e the connect7; method to connect thi ocket to a erver. When the Socket con tr4ctor ret4rn , it doe not im'#( in tantiate a Socket ob*ect b4t it act4a##( attem't to connect to the 'eci,ied erver and 'ort. Some method o, intere t in the Socket c#a are #i ted here. Notice that both the c#ient and erver have a Socket ob*ect, o the e method can be invoked b( both the c#ient and erver. # &eth d% with De%cri"ti n N "ublic ) id c nnect8# c=etAddre%% h %t, int ti!e ut; thr w% IOE,ce"ti n 1 5hi method connect the ocket to the 'eci,ied ho t. 5hi method i needed on#( when (o4 in tantiated the Socket 4 in! the no-ar!4ment con tr4ctor. "ublic InetAddre%% (etInetAddre%%8; 2 5hi method ret4rn the addre o, the other com'4ter that thi ocket i connected to. "ublic int (etP rt8; 1 "et4rn the 'ort the ocket i bo4nd to on the remote machine. "ublic int (et4 calP rt8; D "et4rn the 'ort the ocket i bo4nd to on the #oca# machine. "ublic # c=etAddre%% (etRe! te# c=etAddre%%8; . "et4rn the addre o, the remote ocket. "ublic In"ut#trea! (etIn"ut#trea!8; thr w% IOE,ce"ti n / "et4rn the in'4t tream o, the ocket. 5he in'4t tream i connected to the o4t'4t tream o, the remote ocket. 0 "ublic Out"ut#trea! (etOut"ut#trea!8; thr w% IOE,ce"ti n +2+ 3imited i a S4b idiar( o, 5+S 3imited -/

"et4rn the o4t'4t tream o, the ocket. 5he o4t'4t tream i connected to the in'4t tream o, the remote ocket "ublic ) id cl %e8; thr w% IOE,ce"ti n 8 +#o e the ocket, which make thi Socket ob*ect no #on!er ca'ab#e o, connectin! a!ain to an( erver InetAddre%% 7la%% &eth d%: 5hi c#a re're ent an $nternet Protoco# 7$P; addre . Cere are ,o##owin! 4 e,4## method which (o4 wo4#d need whi#e doin! ocket 'ro!rammin!: # &eth d% with De%cri"ti n N %tatic InetAddre%% (et6yAddre%%8byte9: addr; 1 "et4rn an $net)ddre ob*ect !iven the raw $P addre . %tatic InetAddre%% (et6yAddre%%8#trin( h %t, byte9: addr; 2 +reate an $net)ddre ba ed on the 'rovided ho t name and $P addre . %tatic InetAddre%% (et6yNa!e8#trin( h %t; 1 Determine the $P addre o, a ho t, !iven the ho t? name. #trin( (et' %tAddre%%8; D "et4rn the $P addre trin! in te<t4a# 're entation. #trin( (et' %tNa!e8; . Get the ho t name ,or thi $P addre . %tatic InetAddre%% InetAddre%% (et4 cal' %t8; / "et4rn the #oca# ho t. #trin( t #trin(8; 0 +onvert thi $P addre to a Strin!. # c=et 7lient E,a!"le: 5he ,o##owin! Greetin!+#ient i a c#ient 'ro!ram that connect to a erver b( 4 in! a ocket and end a !reetin!, and then wait ,or a re 'on e. GG %i#e Name Greetin!+#ient.*ava im'ort *ava.net.OJ im'ort *ava.io.OJ '4b#ic c#a Greetin!+#ient N '4b#ic tatic void main7Strin! 8: ar! ; N Strin! erverName K ar! 80:J int 'ort K $nte!er.'ar e$nt7ar! 81:;J tr( N S( tem.o4t.'rint#n7P+onnectin! to P > erverName > P on 'ort P > 'ort;J Socket c#ient K new Socket7 erverName, 'ort;J S( tem.o4t.'rint#n7P64 t connected to P > c#ient.!et"emoteSocket)ddre 7;;J &4t'4tStream o4t5oServer K c#ient.!et&4t'4tStream7;J Data&4t'4tStream o4t K new Data&4t'4tStream7o4t5oServer;J o4t.write=5%7PCe##o ,rom P > c#ient.!et3oca#Socket)ddre 7;;J $n'4tStream in%romServer K c#ient.!et$n'4tStream7;J Data$n'4tStream in K new Data$n'4tStream7in%romServer;J S( tem.o4t.'rint#n7PServer a( P > in.read=5%7;;J +2+ 3imited i a S4b idiar( o, 5+S 3imited

-0

c#ient.c#o e7;J Qcatch7$&9<ce'tion e; N e.'rintStack5race7;J Q Q Q # c=et #er)er E,a!"le: 5he ,o##owin! Greetin!Server 'ro!ram i an e<am'#e o, a erver a''#ication that 4 e the Socket c#a to #i ten ,or c#ient on a 'ort n4mber 'eci,ied b( a command-#ine ar!4ment: GG %i#e Name Greetin!Server.*ava im'ort *ava.net.OJ im'ort *ava.io.OJ '4b#ic c#a Greetin!Server e<tend 5hread N 'rivate ServerSocket erverSocketJ '4b#ic Greetin!Server7int 'ort; throw $&9<ce'tion N erverSocket K new ServerSocket7'ort;J erverSocket. etSo5imeo4t710000;J Q '4b#ic void r4n7; N whi#e7tr4e; N tr( N S( tem.o4t.'rint#n7PWaitin! ,or c#ient on 'ort P > erverSocket.!et3oca#Port7; > P...P;J Socket erver K erverSocket.acce't7;J S( tem.o4t.'rint#n7P64 t connected to P > erver.!et"emoteSocket)ddre 7;;J Data$n'4tStream in K new Data$n'4tStream7 erver.!et$n'4tStream7;;J S( tem.o4t.'rint#n7in.read=5%7;;J Data&4t'4tStream o4t K new Data&4t'4tStream7 erver.!et&4t'4tStream7;;J o4t.write=5%7P5hank (o4 ,or connectin! to P > erver.!et3oca#Socket)ddre 7; > PInGoodb(eWP;J erver.c#o e7;J Qcatch7Socket5imeo4t9<ce'tion ; N S( tem.o4t.'rint#n7PSocket timed o4tWP;J breakJ Qcatch7$&9<ce'tion e; N e.'rintStack5race7;J breakJ Q Q Q '4b#ic tatic void main7Strin! 8: ar! ; N int 'ort K $nte!er.'ar e$nt7ar! 80:;J tr( +2+ 3imited i a S4b idiar( o, 5+S 3imited

-8

N 5hread t K new Greetin!Server7'ort;J t. tart7;J Qcatch7$&9<ce'tion e; N e.'rintStack5race7;J Q Q Q +om'i#e c#ient and erver and then tart erver a ,o##ow : M *ava Greetin!Server /0// Waitin! ,or c#ient on 'ort /0//... +heck c#ient 'ro!ram a ,o##ow : M *ava Greetin!+#ient #oca#ho t /0// +onnectin! to #oca#ho t on 'ort /0// 64 t connected to #oca#ho tG120.0.0.1:/0// Server a( 5hank (o4 ,or connectin! to G120.0.0.1:/0// Goodb(eW Ja)a $ #endin( E!ail 5o end an emai# 4 in! (o4r 6ava )''#ication i im'#e eno4!h b4t to tart with (o4 ho4#d have Ja)a&ail API and Ja)a Acti)ati n Fra!ew r= 8JAF; in ta##ed on (o4r machine. Fo4 can down#oad #ate t ver ion o, 6ava2ai# 7Ver ion 1.2; ,rom 6ava? tandard web ite. Fo4 can down#oad #ate t ver ion o, 6)% 7Ver ion 1.1.1; ,rom 6ava? tandard web ite. Down#oad and 4nBi' the e ,i#e , in the new#( created to' #eve# directorie (o4 wi## ,ind a n4mber o, *ar ,i#e ,or both the a''#ication . Fo4 need to add !ail+jar and acti)ati n+jar ,i#e in (o4r +3)SSP)5C. #end a #i!"le E!ail: Cere i an e<am'#e to end a im'#e emai# ,rom (o4r machine. Cere it i a 4med that (o4r l calh %t i connected to the internet and ca'ab#e eno4!h to end an emai#. GG %i#e Name Send9mai#.*ava im'ort *ava.4ti#.OJ im'ort *ava<.mai#.OJ im'ort *ava<.mai#.internet.OJ im'ort *ava<.activation.OJ '4b#ic c#a Send9mai# N '4b#ic tatic void main7Strin! 8: ar! ; N GG "eci'ient? emai# $D need to be mentioned. Strin! to K Pabcd^!mai#.comPJ GG Sender? emai# $D need to be mentioned Strin! ,rom K Pweb^!mai#.comPJ GG ) 4min! (o4 are endin! emai# ,rom #oca#ho t Strin! ho t K P#oca#ho tPJ GG Get ( tem 'ro'ertie Pro'ertie 'ro'ertie K S( tem.!etPro'ertie 7;J GG Set4' mai# erver 'ro'ertie . etPro'ert(7Pmai#. mt'.ho tP, ho t;J GG Get the de,a4#t Se ion ob*ect. Se ion e ion K Se ion.!etDe,a4#t$n tance7'ro'ertie ;J tr(N GG +reate a de,a4#t 2ime2e a!e ob*ect. +2+ 3imited i a S4b idiar( o, 5+S 3imited --

2ime2e a!e me a!e K new 2ime2e a!e7 e ion;J GG Set %rom: header ,ie#d o, the header. me a!e. et%rom7new $nternet)ddre 7,rom;;J GG Set 5o: header ,ie#d o, the header. me a!e.add"eci'ient72e a!e."eci'ient5('e.5&, new $nternet)ddre 7to;;J GG Set S4b*ect: header ,ie#d me a!e. etS4b*ect7P5hi i the S4b*ect 3ineWP;J GG Now et the act4a# me a!e me a!e. et5e<t7P5hi i act4a# me a!eP;J GG Send me a!e 5ran 'ort. end7me a!e;J S( tem.o4t.'rint#n7PSent me a!e 4cce ,4##(....P;J Qcatch 72e a!in!9<ce'tion me<; N me<.'rintStack5race7;J Q Q Q +om'i#e and r4n thi 'ro!ram to end a im'#e emai#: M *ava Send9mai# Sent me a!e 4cce ,4##(.... $, (o4 want to end an emai# to m4#ti'#e reci'ient then ,o##owin! method wo4#d be 4 ed to 'eci,( m4#ti'#e emai# $D : void add"eci'ient 72e a!e."eci'ient5('e t('e, )ddre 8: addre e ; throw 2e a!in!9<ce'tion Cere i the de cri'tion o, the 'arameter : ty"e: 5hi wo4#d be et to 5&, ++ or E++. Cere ++ re're ent +arbon +o'( and E++ re're ent E#ack +arbon +o'(. 9<am'#e Message#,ecipie$t(ype#(+ addre%%e%: 5hi i the arra( o, emai# $D. Fo4 wo4#d need to 4 e $nternet)ddre 7; method whi#e 'eci,(in! emai# $D #end an '*&4 E!ail: Cere i an e<am'#e to end an C523 emai# ,rom (o4r machine. Cere it i a 4med that (o4r l calh %t i connected to the internet and ca'ab#e eno4!h to end an emai#. 5hi e<am'#e i ver( imi#ar to 'revio4 one, e<ce't here we are 4 in! et+ontent7; method to et content who e econd ar!4ment i Pte<tGhtm#P to 'eci,( that the C523 content i inc#4ded in the me a!e. = in! thi e<am'#e, (o4 can end a bi! a C523 content (o4 #ike. GG %i#e Name SendC5239mai#.*ava im'ort *ava.4ti#.OJ im'ort *ava<.mai#.OJ im'ort *ava<.mai#.internet.OJ im'ort *ava<.activation.OJ '4b#ic c#a SendC5239mai# N '4b#ic tatic void main7Strin! 8: ar! ; N GG "eci'ient? emai# $D need to be mentioned. Strin! to K Pabcd^!mai#.comPJ GG Sender? emai# $D need to be mentioned +2+ 3imited i a S4b idiar( o, 5+S 3imited 100

Strin! ,rom K Pweb^!mai#.comPJ GG ) 4min! (o4 are endin! emai# ,rom #oca#ho t Strin! ho t K P#oca#ho tPJ GG Get ( tem 'ro'ertie Pro'ertie 'ro'ertie K S( tem.!etPro'ertie 7;J GG Set4' mai# erver 'ro'ertie . etPro'ert(7Pmai#. mt'.ho tP, ho t;J GG Get the de,a4#t Se ion ob*ect. Se ion e ion K Se ion.!etDe,a4#t$n tance7'ro'ertie ;J tr(N GG +reate a de,a4#t 2ime2e a!e ob*ect. 2ime2e a!e me a!e K new 2ime2e a!e7 e ion;J GG Set %rom: header ,ie#d o, the header. me a!e. et%rom7new $nternet)ddre 7,rom;;J GG Set 5o: header ,ie#d o, the header. me a!e.add"eci'ient72e a!e."eci'ient5('e.5&, new $nternet)ddre 7to;;J GG Set S4b*ect: header ,ie#d me a!e. etS4b*ect7P5hi i the S4b*ect 3ineWP;J GG Send the act4a# C523 me a!e, a bi! a (o4 #ike me a!e. et+ontent7PVh1R5hi i act4a# me a!eVGh1RP, Pte<tGhtm#P ;J GG Send me a!e 5ran 'ort. end7me a!e;J S( tem.o4t.'rint#n7PSent me a!e 4cce ,4##(....P;J Qcatch 72e a!in!9<ce'tion me<; N me<.'rintStack5race7;J Q Q Q +om'i#e and r4n thi 'ro!ram to end an C523 emai#: M *ava SendC5239mai# Sent me a!e 4cce ,4##(.... #end Attach!ent in E!ail: Cere i an e<am'#e to end an emai# with attachment ,rom (o4r machine. Cere it i a 4med that (o4r l calh %t i connected to the internet and ca'ab#e eno4!h to end an emai#. GG %i#e Name Send%i#e9mai#.*ava im'ort *ava.4ti#.OJ im'ort *ava<.mai#.OJ im'ort *ava<.mai#.internet.OJ im'ort *ava<.activation.OJ '4b#ic c#a Send%i#e9mai# N '4b#ic tatic void main7Strin! 8: ar! ; N GG "eci'ient? emai# $D need to be mentioned. +2+ 3imited i a S4b idiar( o, 5+S 3imited 101

Strin! to K Pabcd^!mai#.comPJ GG Sender? emai# $D need to be mentioned Strin! ,rom K Pweb^!mai#.comPJ GG ) 4min! (o4 are endin! emai# ,rom #oca#ho t Strin! ho t K P#oca#ho tPJ GG Get ( tem 'ro'ertie Pro'ertie 'ro'ertie K S( tem.!etPro'ertie 7;J GG Set4' mai# erver 'ro'ertie . etPro'ert(7Pmai#. mt'.ho tP, ho t;J GG Get the de,a4#t Se ion ob*ect. Se ion e ion K Se ion.!etDe,a4#t$n tance7'ro'ertie ;J tr(N GG +reate a de,a4#t 2ime2e a!e ob*ect. 2ime2e a!e me a!e K new 2ime2e a!e7 e ion;J GG Set %rom: header ,ie#d o, the header. me a!e. et%rom7new $nternet)ddre 7,rom;;J GG Set 5o: header ,ie#d o, the header. me a!e.add"eci'ient72e a!e."eci'ient5('e.5&, new $nternet)ddre 7to;;J GG Set S4b*ect: header ,ie#d me a!e. etS4b*ect7P5hi i the S4b*ect 3ineWP;J GG +reate the me a!e 'art Eod(Part me a!eEod(Part K new 2imeEod(Part7;J GG %i## the me a!e me a!eEod(Part. et5e<t7P5hi i me a!e bod(P;J GG +reate a m4#ti'ar me a!e 24#ti'art m4#ti'art K new 2ime24#ti'art7;J GG Set te<t me a!e 'art m4#ti'art.addEod(Part7me a!eEod(Part;J GG Part two i attachment me a!eEod(Part K new 2imeEod(Part7;J Strin! ,i#ename K P,i#e.t<tPJ DataSo4rce o4rce K new %i#eDataSo4rce7,i#ename;J me a!eEod(Part. etDataCand#er7new DataCand#er7 o4rce;;J me a!eEod(Part. et%i#eName7,i#ename;J m4#ti'art.addEod(Part7me a!eEod(Part;J GG Send the com'#ete me a!e 'art me a!e. et+ontent7m4#ti'art ;J GG Send me a!e 5ran 'ort. end7me a!e;J S( tem.o4t.'rint#n7PSent me a!e 4cce ,4##(....P;J Qcatch 72e a!in!9<ce'tion me<; N me<.'rintStack5race7;J +2+ 3imited i a S4b idiar( o, 5+S 3imited

102

Q Q Q +om'i#e and r4n thi 'ro!ram to end an C523 emai#: M *ava Send%i#e9mai# Sent me a!e 4cce ,4##(.... 5%er Authenticati n Part: $, it i re@4ired to 'rovide 4 er $D and Pa word to the emai# erver ,or a4thentication '4r'o e then (o4 can et the e 'ro'ertie a ,o##ow : 'ro' . etPro'ert(7Pmai#.4 erP, Pm(4 erP;J 'ro' . etPro'ert(7Pmai#.'a wordP, Pm('wdP;J "e t o, the emai# endin! mechani m wo4#d remain a e<'#ained above. Ja)a $ &ultithreadin( 6ava 'rovide b4i#t-in 4''ort ,or multithreaded programmi$g. ) m4#tithreaded 'ro!ram contain two or more 'art that can r4n conc4rrent#(. 9ach 'art o, 4ch a 'ro!ram i ca##ed a thread, and each thread de,ine a e'arate 'ath o, e<ec4tion. ) m4#tithreadin! i a 'ecia#iBed ,orm o, m4#tita kin!. 24#tita kin! thread re@4ire #e overhead than m4#tita kin! 'roce e . $ need to de,ine another term re#ated to thread : "r ce%%: ) 'roce con i t o, the memor( 'ace a##ocated b( the o'eratin! ( tem that can contain one or more thread . ) thread cannot e<i t on it ownJ it m4 t be a 'art o, a 'roce . ) 'roce remain r4nnin! 4nti# a## o, the non-daemon thread are done e<ec4tin!. 24#tithreadin! enab#e (o4 to write ver( e,,icient 'ro!ram that make ma<im4m 4 e o, the +P=, beca4 e id#e time can be ke't to a minim4m. 4ife 7ycle f a *hread: ) thread !oe thro4!h vario4 ta!e in it #i,e c(c#e. %or e<am'#e, a thread i born, tarted, r4n , and then die . %o##owin! dia!ram how com'#ete #i,e c(c#e o, a thread.

)bove mentioned ta!e are e<'#ained here: New: ) new thread be!in it #i,e c(c#e in the new tate. $t remain in thi tate 4nti# the 'ro!ram tart the thread. $t i a# o re,erred to a a born thread. Runnable: ),ter a new#( born thread i tarted, the thread become r4nnab#e. ) thread in thi tate i con idered to be e<ec4tin! it ta k. Waitin(: Sometime a thread tran ition to the waitin! tate whi#e the thread wait ,or another thread to 'er,orm a ta k.) thread tran ition back to the r4nnab#e tate on#( when another thread i!na# the waitin! thread to contin4e e<ec4tin!.

+2+ 3imited i a S4b idiar( o, 5+S 3imited

101

*i!ed waitin(: ) r4nnab#e thread can enter the timed waitin! tate ,or a 'eci,ied interva# o, time. ) thread in thi tate tran ition back to the r4nnab#e tate when that time interva# e<'ire or when the event it i waitin! ,or occ4r . *er!inated: ) r4nnab#e thread enter the terminated tate when it com'#ete it ta k or otherwi e terminate . *hread Pri ritie%: 9ver( 6ava thread ha a 'riorit( that he#' the o'eratin! ( tem determine the order in which thread are ched4#ed. 6ava 'rioritie are in the ran!e between 2$NTP"$&"$5F 7a con tant o, 1; and 2)ATP"$&"$5F 7a con tant o, 10;. E( de,a4#t, ever( thread i !iven 'riorit( N&"2TP"$&"$5F 7a con tant o, .;. 5hread with hi!her 'riorit( are more im'ortant to a 'ro!ram and ho4#d be a##ocated 'roce or time be,ore #ower-'riorit( thread . Cowever, thread 'rioritie cannot !4arantee the order in which thread e<ec4te and ver( m4ch '#at,orm de'endentant. 7reatin( a *hread: 6ava de,ine two wa( in which thi can be accom'#i hed: Fo4 can im'#ement the "4nnab#e inter,ace. Fo4 can e<tend the 5hread c#a , it e#,. 7reate *hread by I!"le!entin( Runnable: 5he ea ie t wa( to create a thread i to create a c#a that im'#ement the Runnable inter,ace. 5o im'#ement "4nnab#e, a c#a need on#( im'#ement a in!#e method ca##ed run8 ;, which i dec#ared #ike thi : '4b#ic void r4n7 ; Fo4 wi## de,ine the code that con tit4te the new thread in ide r4n7; method. $t i im'ortant to 4nder tand that r4n7; can ca## other method , 4 e other c#a e , and dec#are variab#e , *4 t #ike the main thread can. ),ter (o4 create a c#a that im'#ement "4nnab#e, (o4 wi## in tantiate an ob*ect o, t('e 5hread ,rom within that c#a . 5hread de,ine evera# con tr4ctor . 5he one that we wi## 4 e i hown here: 5hread7"4nnab#e thread&b, Strin! threadName;J Cere thread+b i an in tance o, a c#a that im'#ement the "4nnab#e inter,ace and the name o, the new thread i 'eci,ied b( threadName. ),ter the new thread i created, it wi## not tart r4nnin! 4nti# (o4 ca## it %tart8 ; method, which i dec#ared within 5hread. 5he tart7 ; method i hown here: void tart7 ;J E,a!"le: Cere i an e<am'#e that create a new thread and tart it r4nnin!: GG +reate a new thread. c#a New5hread im'#ement "4nnab#e N 5hread tJ New5hread7; N GG +reate a new, econd thread t K new 5hread7thi , PDemo 5hreadP;J S( tem.o4t.'rint#n7P+hi#d thread: P > t;J t. tart7;J GG Start the thread Q GG 5hi i the entr( 'oint ,or the econd thread. '4b#ic void r4n7; N tr( N ,or7int i K .J i R 0J i--; N S( tem.o4t.'rint#n7P+hi#d 5hread: P > i;J GG 3et the thread #ee' ,or a whi#e. 5hread. #ee'7.00;J Q Q catch 7$nterr4'ted9<ce'tion e; N S( tem.o4t.'rint#n7P+hi#d interr4'ted.P;J Q S( tem.o4t.'rint#n7P9<itin! chi#d thread.P;J Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 10D

Q '4b#ic c#a 5hreadDemo N '4b#ic tatic void main7Strin! ar! 8:; N new New5hread7;J GG create a new thread tr( N ,or7int i K .J i R 0J i--; N S( tem.o4t.'rint#n7P2ain 5hread: P > i;J 5hread. #ee'71000;J Q Q catch 7$nterr4'ted9<ce'tion e; N S( tem.o4t.'rint#n7P2ain thread interr4'ted.P;J Q S( tem.o4t.'rint#n7P2ain thread e<itin!.P;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: +hi#d thread: 5hread8Demo 5hread,.,main: 2ain 5hread: . +hi#d 5hread: . +hi#d 5hread: D 2ain 5hread: D +hi#d 5hread: 1 +hi#d 5hread: 2 2ain 5hread: 1 +hi#d 5hread: 1 9<itin! chi#d thread. 2ain 5hread: 2 2ain 5hread: 1 2ain thread e<itin!. 7reate *hread by E,tendin( *hread: 5he econd wa( to create a thread i to create a new c#a that e<tend *hread, and then to create an in tance o, that c#a . 5he e<tendin! c#a m4 t override the run8 ; method, which i the entr( 'oint ,or the new thread. $t m4 t a# o ca## %tart8 ; to be!in e<ec4tion o, the new thread. E,a!"le: Cere i the 'recedin! 'ro!ram rewritten to e<tend 5hread: GG +reate a econd thread b( e<tendin! 5hread c#a New5hread e<tend 5hread N New5hread7; N GG +reate a new, econd thread 4'er7PDemo 5hreadP;J S( tem.o4t.'rint#n7P+hi#d thread: P > thi ;J tart7;J GG Start the thread Q GG 5hi i the entr( 'oint ,or the econd thread. '4b#ic void r4n7; N tr( N ,or7int i K .J i R 0J i--; N S( tem.o4t.'rint#n7P+hi#d 5hread: P > i;J GG 3et the thread #ee' ,or a whi#e. 5hread. #ee'7.00;J Q Q catch 7$nterr4'ted9<ce'tion e; N S( tem.o4t.'rint#n7P+hi#d interr4'ted.P;J Q S( tem.o4t.'rint#n7P9<itin! chi#d thread.P;J Q +2+ 3imited i a S4b idiar( o, 5+S 3imited

10.

Q '4b#ic c#a 9<tend5hread N '4b#ic tatic void main7Strin! ar! 8:; N new New5hread7;J GG create a new thread tr( N ,or7int i K .J i R 0J i--; N S( tem.o4t.'rint#n7P2ain 5hread: P > i;J 5hread. #ee'71000;J Q Q catch 7$nterr4'ted9<ce'tion e; N S( tem.o4t.'rint#n7P2ain thread interr4'ted.P;J Q S( tem.o4t.'rint#n7P2ain thread e<itin!.P;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t: +hi#d thread: 5hread8Demo 5hread,.,main: 2ain 5hread: . +hi#d 5hread: . +hi#d 5hread: D 2ain 5hread: D +hi#d 5hread: 1 +hi#d 5hread: 2 2ain 5hread: 1 +hi#d 5hread: 1 9<itin! chi#d thread. 2ain 5hread: 2 2ain 5hread: 1 2ain thread e<itin!. *hread &eth d%: %o##owin! i the #i t o, im'ortant medthod avai#ab#e in the 5hread c#a . # &eth d% with De%cri"ti n N "ublic ) id %tart8; 1 Start the thread in a e'arate 'ath o, e<ec4tion, then invoke the r4n7; method on thi 5hread ob*ect. "ublic ) id run8; 2 $, thi 5hread ob*ect wa in tantiated 4 in! a e'arate "4nnab#e tar!et, the r4n7; method i invoked on that "4nnab#e ob*ect. "ublic final ) id %etNa!e8#trin( na!e; 1 +han!e the name o, the 5hread ob*ect. 5here i a# o a !etName7; method ,or retrievin! the name. "ublic final ) id %etPri rity8int "ri rity; D Set the 'riorit( o, thi 5hread ob*ect. 5he 'o ib#e va#4e are between 1 and 10. "ublic final ) id %etDae! n8b lean n; . ) 'arameter o, tr4e denote thi 5hread a a daemon thread. "ublic final ) id j in8l n( !illi%ec; / 5he c4rrent thread invoke thi method on a econd thread, ca4 in! the c4rrent thread to b#ock 4nti# the econd thread terminate or the 'eci,ied n4mber o, mi##i econd 'a e . "ublic ) id interru"t8; 0 $nterr4't thi thread, ca4 in! it to contin4e e<ec4tion i, it wa b#ocked ,or an( rea on. "ublic final b lean i%Ali)e8; 8 "et4rn tr4e i, the thread i a#ive, which i an( time a,ter the thread ha been tarted b4t be,ore it r4n to com'#etion. +2+ 3imited i a S4b idiar( o, 5+S 3imited 10/

5he 'revio4 method are invoked on a 'artic4#ar 5hread ob*ect. 5he ,o##owin! method in the 5hread c#a are tatic. $nvokin! one o, the tatic method 'er,orm the o'eration on the c4rrent#( r4nnin! thread # &eth d% with De%cri"ti n N "ublic %tatic ) id yield8; 1 +a4 e the c4rrent#( r4nnin! thread to (ie#d to an( other thread o, the ame 'riorit( that are waitin! to be ched4#ed "ublic %tatic ) id %lee"8l n( !illi%ec; 2 +a4 e the c4rrent#( r4nnin! thread to b#ock ,or at #ea t the 'eci,ied n4mber o, mi##i econd "ublic %tatic b lean h ld%4 c=8Object ,; 1 "et4rn tr4e i, the c4rrent thread ho#d the #ock on the !iven &b*ect. "ublic %tatic *hread current*hread8; D "et4rn a re,erence to the c4rrent#( r4nnin! thread, which i the thread that invoke thi method. "ublic %tatic ) id du!"#tac=8; . Print the tack trace ,or the c4rrent#( r4nnin! thread, which i 4 e,4# when deb4!!in! a m4#tithreaded a''#ication. E,a!"le: 5he ,o##owin! 5hread+#a Demo 'ro!ram demon trate ome o, the e method o, the 5hread c#a : GG %i#e Name : Di '#a(2e a!e.*ava GG +reate a thread to im'#ement "4nnab#e '4b#ic c#a Di '#a(2e a!e im'#ement "4nnab#e N 'rivate Strin! me a!eJ '4b#ic Di '#a(2e a!e7Strin! me a!e; N thi .me a!e K me a!eJ Q '4b#ic void r4n7; N whi#e7tr4e; N S( tem.o4t.'rint#n7me a!e;J Q Q Q GG %i#e Name : G4e )N4mber.*ava GG +reate a thread to e<tentd 5hread '4b#ic c#a G4e )N4mber e<tend 5hread N 'rivate int n4mberJ '4b#ic G4e )N4mber7int n4mber; N thi .n4mber K n4mberJ Q '4b#ic void r4n7; N int co4nter K 0J int !4e K 0J do N !4e K 7int; 72ath.random7; O 100 > 1;J S( tem.o4t.'rint#n7thi .!etName7; +2+ 3imited i a S4b idiar( o, 5+S 3imited 100

> P !4e e P > !4e ;J co4nter>>J Qwhi#e7!4e WK n4mber;J S( tem.o4t.'rint#n7POO +orrectW P > thi .!etName7; > P in P > co4nter > P !4e e .OOP;J Q Q GG %i#e Name : 5hread+#a Demo.*ava '4b#ic c#a 5hread+#a Demo N '4b#ic tatic void main7Strin! 8: ar! ; N "4nnab#e he##o K new Di '#a(2e a!e7PCe##oP;J 5hread thread1 K new 5hread7he##o;J thread1. etDaemon7tr4e;J thread1. etName7Phe##oP;J S( tem.o4t.'rint#n7PStartin! he##o thread...P;J thread1. tart7;J "4nnab#e b(e K new Di '#a(2e a!e7PGoodb(eP;J 5hread thread2 K new 5hread7he##o;J thread2. etPriorit(75hread.2$NTP"$&"$5F;J thread2. etDaemon7tr4e;J S( tem.o4t.'rint#n7PStartin! !oodb(e thread...P;J thread2. tart7;J S( tem.o4t.'rint#n7PStartin! thread1...P;J 5hread thread1 K new G4e )N4mber720;J thread1. tart7;J tr( N thread1.*oin7;J Qcatch7$nterr4'ted9<ce'tion e; N S( tem.o4t.'rint#n7P5hread interr4'ted.P;J Q S( tem.o4t.'rint#n7PStartin! threadD...P;J 5hread threadD K new G4e )N4mber70.;J threadD. tart7;J S( tem.o4t.'rint#n7Pmain7; i endin!...P;J Q Q 5hi wo4#d 'rod4ce ,o##owin! re 4#t. Fo4 can tr( thi e<am'#e a!ain and a!ain and (o4 wo4#d !et di,,erent re 4#t ever( time. Startin! he##o thread... Startin! !oodb(e thread... Ce##o Ce##o Ce##o Ce##o Ce##o Ce##o Ce##o Ce##o Ce##o 5hread-2 !4e e 20 Ce##o +2+ 3imited i a S4b idiar( o, 5+S 3imited

108

OO +orrectW 5hread-2 in 102 !4e e .OO Ce##o Startin! threadD... Ce##o Ce##o ..........remainin! re 4#t 'rod4ced. &aj r *hread 7 nce"t%: Whi#e doin! 24#tithreadin! 'ro!rammin!, (o4 wo4#d need to have ,o##owin! conce't ver( hand(: 5hread S(nchroniBation $nterthread +omm4nication 5hread Dead#ock 5hread +ontro#: S4 'end, Sto' and "e 4me 5%in( &ultithreadin(: 5he ke( to 4ti#iBin! m4#tithreadin! 4''ort e,,ective#( i to think conc4rrent#( rather than eria##(. %or e<am'#e, when (o4 have two 4b ( tem within a 'ro!ram that can e<ec4te conc4rrent#(, make them individ4a# thread . With the care,4# 4 e o, m4#tithreadin!, (o4 can create ver( e,,icient 'ro!ram . ) word o, ca4tion i in order, however: $, (o4 create too man( thread , (o4 can act4a##( de!rade the 'er,ormance o, (o4r 'ro!ram rather than enhance it. "emember, ome overhead i a ociated with conte<t witchin!. $, (o4 create too man( thread , more +P= time wi## be 'ent chan!in! conte<t than e<ec4tin! (o4r 'ro!ramW Ja)a $ A""let 6a%ic% )n a''#et i a 6ava 'ro!ram that r4n in a Web brow er. )n a''#et can be a ,4##( ,4nctiona# 6ava a''#ication beca4 e it ha the entire 6ava )P$ at it di 'o a#. 5here are ome im'ortant di,,erence between an a''#et and a tanda#one 6ava a''#ication, inc#4din! the ,o##owin!: )n a''#et i a 6ava c#a that e<tend the *ava.a''#et.)''#et c#a . ) main7; method i not invoked on an a''#et, and an a''#et c#a wi## not de,ine main7;. )''#et are de i!ned to be embedded within an C523 'a!e. When a 4 er view an C523 'a!e that contain an a''#et, the code ,or the a''#et i down#oaded to the 4 er? machine. ) 6V2 i re@4ired to view an a''#et. 5he 6V2 can be either a '#4!-in o, the Web brow er or a e'arate r4ntime environment. 5he 6V2 on the 4 er? machine create an in tance o, the a''#et c#a and invoke vario4 method d4rin! the a''#et? #i,etime. )''#et have trict ec4rit( r4#e that are en,orced b( the Web brow er. 5he ec4rit( o, an a''#et i o,ten re,erred to a andbo< ec4rit(, com'arin! the a''#et to a chi#d '#a(in! in a andbo< with vario4 r4#e that m4 t be ,o##owed. &ther c#a e that the a''#et need can be down#oaded in a in!#e 6ava )rchive 76)"; ,i#e. 4ife 7ycle f an A""let: %o4r method in the )''#et c#a !ive (o4 the ,ramework on which (o4 b4i#d an( erio4 a''#et: init: 5hi method i intended ,or whatever initia#iBation i needed ,or (o4r a''#et. $t i ca##ed a,ter the 'aram ta! in ide the a''#et ta! have been 'roce ed. %tart: 5hi method i a4tomatica##( ca##ed a,ter the brow er ca## the init method. $t i a# o ca##ed whenever the 4 er ret4rn to the 'a!e containin! the a''#et a,ter havin! !one o,, to other 'a!e . %t ": 5hi method i a4tomatica##( ca##ed when the 4 er move o,, the 'a!e on which the a''#et it . $t can, there,ore, be ca##ed re'eated#( in the ame a''#et. de%tr y: 5hi method i on#( ca##ed when the brow er h4t down norma##(. Eeca4 e a''#et are meant to #ive on an C523 'a!e, (o4 ho4#d not norma##( #eave re o4rce behind a,ter a 4 er #eave the 'a!e that contain the a''#et. "aint: $nvoked immediate#( a,ter the tart7; method, and a# o an( time the a''#et need to re'aint it e#, in the brow er. 5he 'aint7; method i act4a##( inherited ,rom the *ava.awt. A H'ell , W rldH A""let: 5he ,o##owin! i a im'#e a''#et named Ce##oWor#d)''#et.*ava: im'ort *ava.a''#et.OJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 10-

im'ort *ava.awt.OJ '4b#ic c#a Ce##oWor#d)''#et e<tend )''#et N '4b#ic void 'aint 7Gra'hic !; N !.drawStrin! 7PCe##o Wor#dP, 2., .0;J Q Q 5he e im'ort tatement brin! the c#a e into the co'e o, o4r a''#et c#a : *ava.a''#et.)''#et. *ava.awt.Gra'hic . Witho4t tho e im'ort tatement , the 6ava com'i#er wo4#d not reco!niBe the c#a e )''#et and Gra'hic , which the a''#et c#a re,er to. *he A""let 74A##: 9ver( a''#et i an e<ten ion o, the java#applet#*pplet class. 5he ba e )''#et c#a 'rovide method that a derived )''#et c#a ma( ca## to obtain in,ormation and ervice ,rom the brow er conte<t. 5he e inc#4de method that do the ,o##owin!: Get a''#et 'arameter Get the network #ocation o, the C523 ,i#e that contain the a''#et Get the network #ocation o, the a''#et c#a director( Print a tat4 me a!e in the brow er %etch an ima!e %etch an a4dio c#i' P#a( an a4dio c#i' "e iBe the a''#et )dditiona##(, the )''#et c#a 'rovide an inter,ace b( which the viewer or brow er obtain in,ormation abo4t the a''#et and contro# the a''#et? e<ec4tion. 5he viewer ma(: re@4e t in,ormation abo4t the a4thor, ver ion and co'(ri!ht o, the a''#et re@4e t a de cri'tion o, the 'arameter the a''#et reco!niBe initia#iBe the a''#et de tro( the a''#et tart the a''#et? e<ec4tion to' the a''#et? e<ec4tion 5he )''#et c#a 'rovide de,a4#t im'#ementation o, each o, the e method . 5ho e im'#ementation ma( be overridden a nece ar(. 5he PCe##o, Wor#dP a''#et i com'#ete a it tand . 5he on#( method overridden i the 'aint method. In) =in( an A""let: )n a''#et ma( be invoked b( embeddin! directive in an C523 ,i#e and viewin! the ,i#e thro4!h an a''#et viewer or 6ava-enab#ed brow er. 5he Va''#etR ta! i the ba i ,or embeddin! an a''#et in an C523 ,i#e. Ee#ow i an e<am'#e that invoke the PCe##o, Wor#dP a''#et: Vhtm#R Vtit#eR5he Ce##o, Wor#d )''#etVGtit#eR VhrR Va''#et codeKPCe##oWor#d)''#et.c#a P widthKP120P hei!htKP120PR $, (o4r brow er wa 6ava-enab#ed, a PCe##o, Wor#dP me a!e wo4#d a''ear here. VGa''#etR VhrR VGhtm#R Ea ed on the above e<am'#e , here i the #ive a''#et e<am'#e N te: Fo4 can re,er to C523 )''#et 5a! to 4nder tand more abo4t ca##in! a''#et ,rom C523. 5he code attrib4te o, the Va''#etR ta! i re@4ired. $t 'eci,ie the )''#et c#a to r4n. Width and hei!ht are a# o re@4ired to 'eci,( the initia# iBe o, the 'ane# in which an a''#et r4n . 5he a''#et directive m4 t be c#o ed with a VGa''#etR ta!. $, an a''#et take 'arameter , va#4e ma( be 'a ed ,or the 'arameter b( addin! V'aramR ta! between Va''#etR and VGa''#etR. 5he brow er i!nore te<t and other ta! between the a''#et ta! . +2+ 3imited i a S4b idiar( o, 5+S 3imited 110

Non-6ava-enab#ed brow er do not 'roce Va''#etR and VGa''#etR. 5here,ore, an(thin! that a''ear between the ta! , not re#ated to the a''#et, i vi ib#e in non-6ava-enab#ed brow er . 5he viewer or brow er #ook ,or the com'i#ed 6ava code at the #ocation o, the doc4ment. 5o 'eci,( otherwi e, 4 e the codeba e attrib4te o, the Va''#etR ta! a hown: Va''#et codeba eKPhtt':GGamrood.comGa''#et P codeKPCe##oWor#d)''#et.c#a P widthKP120P hei!htKP120PR $, an a''#et re ide in a 'acka!e other than the de,a4#t, the ho#din! 'acka!e m4 t be 'eci,ied in the code attrib4te 4 in! the 'eriod character 7.; to e'arate 'acka!eGc#a com'onent . %or e<am'#e: Va''#et codeKPm('acka!e. 4b'acka!e.5e t)''#et.c#a P widthKP120P hei!htKP120PR Cettin( A""let Para!eter%: 5he ,o##owin! e<am'#e demon trate how to make an a''#et re 'ond to et4' 'arameter 'eci,ied in the doc4ment. 5hi a''#et di '#a( a checkerboard 'attern o, b#ack and a econd co#or. 5he econd co#or and the iBe o, each @4are ma( be 'eci,ied a 'arameter to the a''#et within the doc4ment. +hecker)''#et !et it 'arameter in the init7; method. $t ma( a# o !et it 'arameter in the 'aint7; method. Cowever, !ettin! the va#4e and avin! the ettin! once at the tart o, the a''#et, in tead o, at ever( re,re h, i convenient and e,,icient. 5he a''#et viewer or brow er ca## the init7; method o, each a''#et it r4n . 5he viewer ca## init7; once, immediate#( a,ter #oadin! the a''#et. 7)''#et.init7; i im'#emented to do nothin!.; &verride the de,a4#t im'#ementation to in ert c4 tom initia#iBation code. 5he )''#et.!etParameter7; method ,etche a 'arameter !iven the 'arameter? name 7the va#4e o, a 'arameter i a#wa( a trin!;. $, the va#4e i n4meric or other non-character data, the trin! m4 t be 'ar ed. 5he ,o##owin! i a ke#eton o, +hecker)''#et.*ava: im'ort *ava.a''#et.OJ im'ort *ava.awt.OJ '4b#ic c#a +hecker)''#et e<tend )''#et N int @4areSiBe K .0JGG initia#iBed to de,a4#t iBe '4b#ic void init 7; NQ 'rivate void 'ar eS@4areSiBe 7Strin! 'aram; NQ 'rivate +o#or 'ar e+o#or 7Strin! 'aram; NQ '4b#ic void 'aint 7Gra'hic !; NQ Q Cere are +hecker)''#et? init7; and 'rivate 'ar eS@4areSiBe7; method : '4b#ic void init 7; N Strin! @4areSiBeParam K !etParameter 7P @4areSiBeP;J 'ar eS@4areSiBe 7 @4areSiBeParam;J Strin! co#orParam K !etParameter 7Pco#orP;J +o#or ,! K 'ar e+o#or 7co#orParam;J etEack!ro4nd 7+o#or.b#ack;J et%ore!ro4nd 7,!;J Q 'rivate void 'ar eS@4areSiBe 7Strin! 'aram; N i, 7'aram KK n4##; ret4rnJ tr( N @4areSiBe K $nte!er.'ar e$nt 7'aram;J Q catch 79<ce'tion e; N GG 3et de,a4#t va#4e remain Q Q 5he a''#et ca## 'ar eS@4areSiBe7; to 'ar e the @4areSiBe 'arameter. 'ar eS@4areSiBe7; ca## the #ibrar( method $nte!er.'ar e$nt7;, which 'ar e a trin! and ret4rn an inte!er. $nte!er.'ar e$nt7; throw an e<ce'tion whenever it ar!4ment i inva#id. 5here,ore, 'ar eS@4areSiBe7; catche e<ce'tion , rather than a##owin! the a''#et to ,ai# on bad in'4t. +2+ 3imited i a S4b idiar( o, 5+S 3imited 111

5he a''#et ca## 'ar e+o#or7; to 'ar e the co#or 'arameter into a +o#or va#4e. 'ar e+o#or7; doe a erie o, trin! com'ari on to match the 'arameter va#4e to the name o, a 'rede,ined co#or. Fo4 need to im'#ement the e method to make thi a''#et work . #"ecifyin( A""let Para!eter%: 5he ,o##owin! i an e<am'#e o, an C523 ,i#e with a +hecker)''#et embedded in it. 5he C523 ,i#e 'eci,ie both 'arameter to the a''#et b( mean o, the V'aramR ta!. Vhtm#R Vtit#eR+heckerboard )''#etVGtit#eR VhrR Va''#et codeKP+hecker)''#et.c#a P widthKPD80P hei!htKP120PR V'aram nameKPco#orP va#4eKPb#4ePR V'aram nameKP @4are iBeP va#4eKP10PR VGa''#etR VhrR VGhtm#R N te: Parameter name are not ca e en itive. A""licati n 7 n)er%i n t A""let%: $t i ea ( to convert a !ra'hica# 6ava a''#ication 7that i , an a''#ication that 4 e the )W5 and that (o4 can tart with the *ava 'ro!ram #a4ncher; into an a''#et that (o4 can embed in a web 'a!e. Cere are the 'eci,ic te' ,or convertin! an a''#ication to an a''#et. 2ake an C523 'a!e with the a''ro'riate ta! to #oad the a''#et code. S4''#( a 4bc#a o, the 6)''#et c#a . 2ake thi c#a '4b#ic. &therwi e, the a''#et cannot be #oaded. 9#iminate the main method in the a''#ication. Do not con tr4ct a ,rame window ,or the a''#ication. Fo4r a''#ication wi## be di '#a(ed in ide the brow er. 2ove an( initia#iBation code ,rom the ,rame window con tr4ctor to the init method o, the a''#et. Fo4 don?t need to e<'#icit#( con tr4ct the a''#et ob*ect.the brow er in tantiate it ,or (o4 and ca## the init method. "emove the ca## to etSiBeJ ,or a''#et , iBin! i done with the width and hei!ht 'arameter in the C523 ,i#e. "emove the ca## to etDe,a4#t+#o e&'eration. )n a''#et cannot be c#o edJ it terminate when the brow er e<it . $, the a''#ication ca## et5it#e, e#iminate the ca## to the method. )''#et cannot have tit#e bar . 7Fo4 can, o, co4r e, tit#e the web 'a!e it e#,, 4 in! the C523 tit#e ta!.; Don?t ca## etVi ib#e7tr4e;. 5he a''#et i di '#a(ed a4tomatica##(. E)ent 'andlin(: )''#et inherit a !ro4' o, event-hand#in! method ,rom the +ontainer c#a . 5he +ontainer c#a de,ine evera# method , 4ch a 'roce He(9vent and 'roce 2o4 e9vent, ,or hand#in! 'artic4#ar t('e o, event , and then one catch-a## method ca##ed 'roce 9vent. $norder to react an event, an a''#et m4 t override the a''ro'riate event- 'eci,ic method. im'ort *ava.awt.event.2o4 e3i tenerJ im'ort *ava.awt.event.2o4 e9ventJ im'ort *ava.a''#et.)''#etJ im'ort *ava.awt.Gra'hic J '4b#ic c#a 9<am'#e9ventCand#in! e<tend )''#et im'#ement 2o4 e3i tener N

Strin!E4,,er trE4,,erJ '4b#ic void init7; N add2o4 e3i tener7thi ;J trE4,,er K new Strin!E4,,er7;J add$tem7Pinitia#iBin! the a''#e P;J Q '4b#ic void tart7; N add$tem7P tartin! the a''#et P;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 112

Q '4b#ic void to'7; N add$tem7P to''in! the a''#et P;J Q '4b#ic void de tro(7; N add$tem7P4n#oadin! the a''#etP;J Q void add$tem7Strin! word; N S( tem.o4t.'rint#n7word;J trE4,,er.a''end7word;J re'aint7;J Q '4b#ic void 'aint7Gra'hic !; N GGDraw a "ectan!#e aro4nd the a''#et? di '#a( area. !.draw"ect70, 0, !etWidth7; - 1, !etCei!ht7; - 1;J GGdi '#a( the trin! in ide the rectan!#e. !.drawStrin!7 trE4,,er.toStrin!7;, 10, 20;J Q '4b#ic void mo4 Q '4b#ic void mo4 Q '4b#ic void mo4 Q '4b#ic void mo4 Q e9ntered72o4 e9vent event; N e9<ited72o4 e9vent event; N ePre ed72o4 e9vent event; N e"e#ea ed72o4 e9vent event; N

'4b#ic void mo4 e+#icked72o4 e9vent event; N add$tem7Pmo4 e c#ickedW P;J Q Q Now #et 4 ca## thi a''#et a ,o##ow : Vhtm#R Vtit#eR9vent Cand#in!VGtit#eR VhrR Va''#et codeKP9<am'#e9ventCand#in!.c#a P widthKP100P hei!htKP100PR VGa''#etR VhrR VGhtm#R $nitia##( the a''#et wi## di '#a( Pinitia#iBin! the a''#et. Startin! the a''#et.P 5hen once (o4 c#ick in ide the rectan!#e Pmo4 e c#ickedP wi## be di '#a(ed a we##. Ea ed on the above e<am'#e , here i the #ive a''#et e<am'#e: Di%"layin( I!a(e%: )n a''#et can di '#a( ima!e o, the ,ormat G$%, 6P9G, E2P, and other . 5o di '#a( an ima!e within the a''#et, (o4 4 e the draw$ma!e7; method ,o4nd in the *ava.awt.Gra'hic c#a . %o##owin! i the e<am'#e howin! a## the te' to how ima!e : im'ort *ava.a''#et.OJ im'ort *ava.awt.OJ im'ort *ava.net.OJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 111

'4b#ic c#a $ma!eDemo e<tend )''#et N 'rivate $ma!e ima!eJ 'rivate )''#et+onte<t conte<tJ '4b#ic void init7; N conte<t K thi .!et)''#et+onte<t7;J Strin! ima!e="3 K thi .!etParameter7Pima!eP;J i,7ima!e="3 KK n4##; N ima!e="3 K P*ava.*'!PJ Q tr( N ="3 4r# K new ="37thi .!etDoc4mentEa e7;, ima!e="3;J ima!e K conte<t.!et$ma!e74r#;J Qcatch72a#,ormed="39<ce'tion e; N e.'rintStack5race7;J GG Di '#a( in brow er tat4 bar conte<t. howStat4 7P+o4#d not #oad ima!eWP;J Q Q '4b#ic void 'aint7Gra'hic !; N conte<t. howStat4 7PDi '#a(in! ima!eP;J !.draw$ma!e7ima!e, 0, 0, 200, 8D, n4##;J !.drawStrin!7Pwww.*ava#icen e.comP, 1., 100;J Q Q Now #et 4 ca## thi a''#et a ,o##ow : Vhtm#R Vtit#eR5he $ma!eDemo a''#etVGtit#eR VhrR Va''#et codeKP$ma!eDemo.c#a P widthKP100P hei!htKP200PR V'aram nameKPima!eP va#4eKP*ava.*'!PR VGa''#etR VhrR VGhtm#R Ea ed on the above e<am'#e , here i the #ive a''#et e<am'#e: Playin( Audi : )n a''#et can '#a( an a4dio ,i#e re're ented b( the )4dio+#i' inter,ace in the *ava.a''#et 'acka!e. 5he )4dio+#i' inter,ace ha three method , inc#4din!: "ublic ) id "lay8;: P#a( the a4dio c#i' one time, ,rom the be!innin!. "ublic ) id l "8;: +a4 e the a4dio c#i' to re'#a( contin4a##(. "ublic ) id %t "8;: Sto' '#a(in! the a4dio c#i'. 5o obtain an )4dio+#i' ob*ect, (o4 m4 t invoke the !et)4dio+#i'7; method o, the )''#et c#a . 5he !et)4dio+#i'7; method ret4rn immediate#(, whether or not the ="3 re o#ve to an act4a# a4dio ,i#e. 5he a4dio ,i#e i not down#oaded 4nti# an attem't i made to '#a( the a4dio c#i'. %o##owin! i the e<am'#e howin! a## the te' to '#a( an a4dio: im'ort *ava.a''#et.OJ im'ort *ava.awt.OJ im'ort *ava.net.OJ '4b#ic c#a )4dioDemo e<tend )''#et N 'rivate )4dio+#i' c#i'J 'rivate )''#et+onte<t conte<tJ '4b#ic void init7; +2+ 3imited i a S4b idiar( o, 5+S 3imited 11D

N conte<t K thi .!et)''#et+onte<t7;J Strin! a4dio="3 K thi .!etParameter7Pa4dioP;J i,7a4dio="3 KK n4##; N a4dio="3 K Pde,a4#t.a4PJ Q tr( N ="3 4r# K new ="37thi .!etDoc4mentEa e7;, a4dio="3;J c#i' K conte<t.!et)4dio+#i'74r#;J Qcatch72a#,ormed="39<ce'tion e; N e.'rintStack5race7;J conte<t. howStat4 7P+o4#d not #oad a4dio ,i#eWP;J Q Q '4b#ic void tart7; N i,7c#i' WK n4##; N c#i'.#oo'7;J Q Q '4b#ic void to'7; N i,7c#i' WK n4##; N c#i'. to'7;J Q Q Q Now #et 4 ca## thi a''#et a ,o##ow : Vhtm#R Vtit#eR5he $ma!eDemo a''#etVGtit#eR VhrR Va''#et codeKP$ma!eDemo.c#a P widthKP0P hei!htKP0PR V'aram nameKPa4dioP va#4eKPte t.wavPR VGa''#etR VhrR VGhtm#R Fo4 can 4 e (o4r te t.wav at (o4r P+ to te t the above e<am'#e. Ja)a D cu!entati n 7 !!ent% 6ava 4''ort three t('e o, comment . 5he ,ir t two are the GG and the GO OG. 5he third t('e i ca##ed a doc4mentation comment. $t be!in with the character e@4ence GOO and it end with OG. Doc4mentation comment a##ow (o4 to embed in,ormation abo4t (o4r 'ro!ram into the 'ro!ram it e#,. Fo4 can then 4 e the *avadoc 4ti#it( 'ro!ram to e<tract the in,ormation and '4t it into an C523 ,i#e. Doc4mentation comment make it convenient to doc4ment (o4r 'ro!ram . *he ja)ad c *a(%: 5he *avadoc 4ti#it( reco!niBe the ,o##owin! ta! : *a( De%cri"ti n E,a!"le ^a4thor $denti,ie the a4thor o, a c#a . ^a4thor de cri'tion ^de'recate S'eci,ie that a c#a or member i ^de'recated de cri'tion d de'recated. S'eci,ie the 'ath to the root director( o, the N^doc"ootQ Director( Path c4rrent doc4mentation ^e<ce'tion $denti,ie an e<ce'tion thrown b( a method. ^e<ce'tion e<ce'tion-name +2+ 3imited i a S4b idiar( o, 5+S 3imited 11.

N^inheritDoc $nherit a comment ,rom the immediate Q 4'erc#a . N^#inkQ $n ert an in-#ine #ink to another to'ic. $n ert an in-#ine #ink to another to'ic, b4t N^#ink'#ainQ the #ink i di '#a(ed in a '#ain-te<t ,ont. ^'aram ^ret4rn ^ ee ^ eria# Doc4ment a method? 'arameter.

Doc4ment a method? ret4rn va#4e. S'eci,ie a #ink to another to'ic. Doc4ment a de,a4#t eria#iBab#e ,ie#d. Doc4ment the data written b( the ^ eria#Data write&b*ect7 ; or write9<terna#7 ; method Doc4ment an &b*ectStream%ie#d ^ eria#%ie#d com'onent. State the re#ea e when a 'eci,ic chan!e ^ ince wa introd4ced. ^throw Same a ^e<ce'tion.

e<'#anation $nherit a comment ,rom the immediate 4r'erc#a . N^#ink name te<tQ $n ert an in-#ine #ink to another to'ic. ^'aram 'arameter-name e<'#anation ^ret4rn e<'#anation ^ ee anchor ^ eria# de cri'tion ^ eria#Data de cri'tion ^ eria#%ie#d name t('e de cri'tion ^ ince re#ea e

5he ^throw ta! ha the ame meanin! a the ^e<ce'tion ta!. Di '#a( the va#4e o, a con tant, which m4 t Di '#a( the va#4e o, a con tant, N^va#4eQ be a tatic ,ie#d. which m4 t be a tatic ,ie#d. ^ver ion S'eci,ie the ver ion o, a c#a . ^ver ion in,o D cu!entati n 7 !!ent: ),ter the be!innin! GOO, the ,ir t #ine or #ine become the main de cri'tion o, (o4r c#a , variab#e, or method. ),ter that, (o4 can inc#4de one or more o, the vario4 ^ ta! . 9ach ^ ta! m4 t tart at the be!innin! o, a new #ine or ,o##ow an a teri k 7O; that i at the tart o, a #ine. 24#ti'#e ta! o, the ame t('e ho4#d be !ro4'ed to!ether. %or e<am'#e, i, (o4 have three ^ ee ta! , '4t them one a,ter the other. Cere i an e<am'#e o, a doc4mentation comment ,or a c#a : GOO O 5hi c#a draw a bar chart. O ^a4thor Sara )#i O ^ver ion 1.2 OG What ja)ad c Out"ut%? 5he *avadoc 'ro!ram take a in'4t (o4r 6ava 'ro!ram? o4rce ,i#e and o4t'4t evera# C523 ,i#e that contain the 'ro!ram? doc4mentation. $n,ormation abo4t each c#a wi## be in it own C523 ,i#e. 6ava 4ti#it( ja)ad c wi## a# o o4t'4t an inde< and a hierarch( tree. &ther C523 ,i#e can be !enerated. Since di,,erent im'#ementation o, *avadoc ma( work di,,erent#(, (o4 wi## need to check the in tr4ction that accom'an( (o4r 6ava deve#o'ment ( tem ,or detai# 'eci,ic to (o4r ver ion. E,a!"le: %o##owin! i a am'#e 'ro!ram that 4 e doc4mentation comment . Notice the wa( each comment immediate#( 'recede the item that it de cribe . ),ter bein! 'roce ed b( *avadoc, the doc4mentation abo4t the S@4areN4m c#a wi## be ,o4nd in S@4areN4m.htm#. im'ort *ava.io.OJ GOO O 5hi c#a demon trate doc4mentation comment . O ^a4thor )(an )mhed O ^ver ion 1.2 OG '4b#ic c#a S@4areN4m N GOO +2+ 3imited i a S4b idiar( o, 5+S 3imited

11/

O 5hi method ret4rn the @4are o, n4m. O 5hi i a m4#ti#ine de cri'tion. Fo4 can 4 e O a man( #ine a (o4 #ike. O ^'aram n4m 5he va#4e to be @4ared. O ^ret4rn n4m @4ared. OG '4b#ic do4b#e @4are7do4b#e n4m; N ret4rn n4m O n4mJ Q GOO O 5hi method in'4t a n4mber ,rom the 4 er. O ^ret4rn 5he va#4e in'4t a a do4b#e. O ^e<ce'tion $&9<ce'tion &n in'4t error. O ^ ee $&9<ce'tion OG '4b#ic do4b#e !etN4mber7; throw $&9<ce'tion N $n'4tStream"eader i r K new $n'4tStream"eader7S( tem.in;J E4,,ered"eader inData K new E4,,ered"eader7i r;J Strin! trJ tr K inData.read3ine7;J ret4rn 7new Do4b#e7 tr;;.do4b#eVa#4e7;J Q GOO O 5hi method demon trate @4are7;. O ^'aram ar! =n4 ed. O ^ret4rn Nothin!. O ^e<ce'tion $&9<ce'tion &n in'4t error. O ^ ee $&9<ce'tion OG '4b#ic tatic void main7Strin! ar! 8:; throw $&9<ce'tion N S@4areN4m ob K new S@4areN4m7;J do4b#e va#J S( tem.o4t.'rint#n7P9nter va#4e to be @4ared: P;J va# K ob.!etN4mber7;J va# K ob. @4are7va#;J S( tem.o4t.'rint#n7PS@4ared va#4e i P > va#;J Q Q Now 'roce above S@4areN4m.*ava ,i#e 4 in! *avadoc 4ti#it( a ,o##ow : M *avadoc S@4areN4m.*ava 3oadin! o4rce ,i#e S@4areN4m.*ava... +on tr4ctin! 6avadoc in,ormation... Standard Doc#et ver ion 1...0T11 E4i#din! tree ,or a## the 'acka!e and c#a e ... Generatin! S@4areN4m.htm#... S@4areN4m.*ava:1-: warnin! - ^ret4rn ta! cannot be 4 edI in method with void ret4rn t('e. Generatin! 'acka!e-,rame.htm#... Generatin! 'acka!e- 4mmar(.htm#... Generatin! 'acka!e-tree.htm#... Generatin! con tant-va#4e .htm#... E4i#din! inde< ,or a## the 'acka!e and c#a e ... Generatin! overview-tree.htm#... Generatin! inde<-a##.htm#... Generatin! de'recated-#i t.htm#... E4i#din! inde< ,or a## c#a e ... Generatin! a##c#a e -,rame.htm#... Generatin! a##c#a e -no,rame.htm#... +2+ 3imited i a S4b idiar( o, 5+S 3imited

110

Generatin! inde<.htm#... Generatin! he#'-doc.htm#... Generatin! t(#e heet.c ... 1 warnin! M Fo4 can check a## the !enerated doc4mentation here: Ja)a $ 4ibrary 7la%%e% 5hi t4toria# wo4#d cover 'acka!e ja)a+lan( which 'rovide c#a e that are ,4ndamenta# to the de i!n o, the 6ava 'ro!rammin! #an!4a!e. 5he mo t im'ortant c#a e are &b*ect, which i the root o, the c#a hierarch(, and +#a , in tance o, which re're ent c#a e at r4n time. Cere i the #i t o, c#a e o, acka!e ja)a+lan(. 5he e c#a e are ver( im'ortant to know ,or a 6ava 'ro!rammer. +#ick a c#a #ink to know more detai# abo4t that c#a . %or a ,4rther dri##, (o4 can re,er tandard 6ava doc4mentation. #N 1 2 1 D . / 0 8 10 11 Eoo#ean Eoo#ean E(te 5he E(te c#a wra' a va#4e o, 'rimitive t('e b(te in an ob*ect. wra' a va#4e o, the 'rimitive t('e char in an ob*ect. +#a re're ent c#a e and inter,ace in a r4nnin! 6ava &eth d% with De%cri"ti n

+haracter 5he +haracter c#a +#a $n tance o, the c#a a''#ication.

+#a 3oader ) c#a #oader i an ob*ect that i re 'on ib#e ,or #oadin! c#a e . +om'i#er 5he +om'i#er c#a ervice . Do4b#e 5he Do4b#e c#a %#oat 5he %#oat c#a $nte!er 5he $nte!er c#a 3on! 5he 3on! c#a i 'rovided to 4''ort 6ava-to-native-code com'i#er and re#ated

wra' a va#4e o, the 'rimitive t('e do4b#e in an ob*ect. wra' a va#4e o, 'rimitive t('e ,#oat in an ob*ect. wra' a va#4e o, the 'rimitive t('e int in an ob*ect. wra' a va#4e o, the 'rimitive t('e #on! in an ob*ect.

2ath 5he c#a 2ath contain method ,or 'er,ormin! ba ic n4meric o'eration 4ch a the e#ementar( e<'onentia#, #o!arithm, @4are root, and tri!onometric ,4nction . N4mber 5he ab tract c#a N4mber i the 4'erc#a Do4b#e, %#oat, $nte!er, 3on!, and Short. &b*ect +#a &b*ect i the root o, the c#a o, c#a e Ei!Decima#, Ei!$nte!er, E(te,

12 11 1D 1.

hierarch(.

Packa!e Packa!e ob*ect contain ver ion in,ormation abo4t the im'#ementation and 'eci,ication o, a 6ava 'acka!e. Proce 5he "4ntime.e<ec method create a native 'roce and ret4rn an in tance o, a 4bc#a 118

+2+ 3imited i a S4b idiar( o, 5+S 3imited

o, Proce 1/ 10 18 120 21 22 21 2D 2. 2/ 20 28 2-

that can be 4 ed to contro# the 'roce

and obtain in,ormation abo4t it.

"4ntime 9ver( 6ava a''#ication ha a in!#e in tance o, c#a "4ntime that a##ow the a''#ication to inter,ace with the environment in which the a''#ication i r4nnin!. "4ntimePermi ion 5hi c#a i ,or r4ntime 'ermi Sec4rit(2ana!er 5he ec4rit( mana!er i a c#a Short 5he Short c#a ion . that a##ow a''#ication to im'#ement a ec4rit( 'o#ic(.

wra' a va#4e o, 'rimitive t('e hort in an ob*ect.

Stack5race9#ement )n e#ement in a tack trace, a ret4rned b( 5hrowab#e.!etStack5race7;. Strict2ath 5he c#a Strict2ath contain method ,or 'er,ormin! ba ic n4meric o'eration 4ch a the e#ementar( e<'onentia#, #o!arithm, @4are root, and tri!onometric ,4nction . Strin! 5he Strin! c#a re're ent character trin! .

Strin!E4,,er ) trin! b4,,er im'#ement a m4tab#e e@4ence o, character . S( tem 5he S( tem c#a contain evera# 4 e,4# c#a ,ie#d and method .

5hread ) thread i a thread o, e<ec4tion in a 'ro!ram. 5hreadGro4' ) thread !ro4' re're ent a et o, thread . 5hread3oca# 5hi c#a 'rovide thread-#oca# variab#e . 5hrowab#e 5he 5hrowab#e c#a i the 4'erc#a o, a## error and e<ce'tion in the 6ava #an!4a!e. to ho#d a re,erence to the +#a

Void 5he Void c#a i an 4nin tantiab#e '#aceho#der c#a ob*ect re're entin! the 6ava ke(word void.

Ja)a #win( *ut rial

What i Swin! in *ava [ ) 'art o, 5he 6%+ Swin! 6ava con i t o,: 3ook and ,ee# )cce ibi#it( 6ava 2D Dra! and Dro', etc +om'i#in! X r4nnin! 'ro!ram $, (o4 do not e<'#icit#( add a G=$ com'onent to a container, the G=$ com'onent wi## not be di '#a(ed when the container a''ear on the creen. Swin!, which i an e<ten ion #ibrar( to the )W5, inc#4de new and im'roved com'onent that enhance the #ook and ,4nctiona#it( o, G=$ . Swin! can be 4 ed to b4i#d Standa#one win! !4i )'' a we## a Serv#et and )''#et . $t em'#o( a mode#Gview de i!n architect4re. Swin! i more 'ortab#e and more ,#e<ib#e than )W5. Swin! 2ode#Gview de i!n: 5he Pview 'artP o, the 2V de i!n i im'#emented with a com'onent ob*ect and the =$ ob*ect 5he Pmode# 'artP o, the 2V de i!n i im'#emented b( a mode# ob*ect and a chan!e #i tener ob*ect. +2+ 3imited i a S4b idiar( o, 5+S 3imited 11-

Swin! i b4i#t on to' o, )W5 and i entire#( written in 6ava, 4 in! )W5? #i!htwei!ht com'onent 4''ort. $n 'artic4#ar, 4n#ike )W5, t he architect4re o, Swin! com'onent make it ea ( to c4 tomiBe both their a''earance and behavior. +om'onent ,rom )W5 and Swin! can be mi<ed, a##owin! (o4 to add Swin! 4''ort to e<i tin! )W5-ba ed 'ro!ram . %or e<am'#e, win! com'onent 4ch a 6S#ider, 6E4tton and 6+heckbo< co4#d be 4 ed in the ame 'ro!ram with tandard )W5 #abe# , te<t,ie#d and cro##bar . Fo4 co4#d 4bc#a the e<i tin! Swin! =$, mode#, or chan!e #i tener c#a e witho4t havin! to reinvent the entire im'#ementation. Swin! a# o ha the abi#it( to re'#ace the e ob*ect on-the,#(. 100L 6ava im'#ementation o, com'onent P#4!!ab#e 3ook X %ee# 3i!htwei!ht com'onent = e 2V+ )rchitect4re 2ode# re're ent the data View a a vi 4a# re're entation o, the data +ontro##er take in'4t and tran #ate it to chan!e in data 5hree 'art +om'onent et 7 4bc#a e o, 6+om'onent; S4''ort c#a e $nter,ace $n Swin!, c#a e that re're ent G=$ com'onent have name be!innin! with the #etter 6. Some e<am'#e are 6E4tton, 63abe#, and 6S#ider. )#to!ether there are more than 2.0 new c#a e and 0. inter,ace in Swin! _ twice a man( a in )W5. 6ava Swin! c#a hierarch( 5he c#a 6+om'onent, de cended direct#( ,rom +ontainer, i the root c#a ,or mo t o, Swin!? 4 er inter,ace com'onent .

Swin! contain com'onent that (o4?## 4 e to b4i#d a G=$. $ am #i tin! (o4 ome o, the common#( 4 ed Swin! com'onent . 5o #earn and 4nder tand the e win! 'ro!ram , )W5 Pro!rammin! know#ed!e i not re@4ired. Ja)a #win( E,a!"le% Ee#ow i a *ava win! code ,or the traditiona# Ce##o Wor#d 'ro!ram. Ea ica##(, the idea behind thi Ce##o Wor#d 'ro!ram i to #earn how to create a *ava 'ro!ram, com'i#e and r4n it. 5o create (o4r *ava o4rce code (o4 can 4 e an( editor7 5e<t 'adG9dit '#4 are m( ,avorite ; or (o4 can 4 e an $D9 #ike 9c#i' e. im'ort *ava<. win!.6%rameJ im'ort *ava<. win!.63abe#J GGim'ort tatement GG+heck i, window c#o e a4tomatica##(. &therwi e add 4itab#e code +2+ 3imited i a S4b idiar( o, 5+S 3imited 120

'4b#ic c#a

Ce##oWor#d%rame e<tend 6%rame N

'4b#ic tatic void main7Strin! ar! 8:; N new Ce##oWor#d%rame7;J Q Ce##oWor#d%rame7; N 63abe# *#bCe##oWor#d K new 63abe#7PCe##o Wor#dP;J add7*#bCe##oWor#d;J thi . etSiBe7100, 100;J GG 'ack7;J etVi ib#e7tr4e;J Q Q Out"ut

6Pane# i Swin!? ver ion o, the )W5 c#a Pane# and 4 e the ame de,a4#t #a(o4t, %#ow3a(o4t. 6Pane# i de cended direct#( ,rom 6+om'onent. 6%rame i Swin!? ver ion o, %rame and i de cended direct#( ,rom that c#a . 5he com'onent added to the ,rame are re,erred to a it content J the e are mana!ed b( the contentPane. 5o add a com'onent to a 6%rame, we m4 t 4 e it contentPane in tead. 6$nterna#%rame i con,ined to a vi ib#e area o, a container it i '#aced in. $t can be iconi,ied , ma<imiBed and #a(ered. 6Window i Swin!? ver ion o, Window and i de cended direct#( ,rom that c#a . 3ike Window, it 4 e Eorder3a(o4t b( de,a4#t. 6Dia#o! i Swin!? ver ion o, Dia#o! and i de cended direct#( ,rom that c#a . 3ike Dia#o!, it 4 e Eorder3a(o4t b( de,a4#t. 3ike 6%rame and 6Window, 6Dia#o! contain a rootPane hierarch( inc#4din! a contentPane, and it a##ow #a(ered and !#a 'ane . )## dia#o! are moda#, which mean the c4rrent thread i b#ocked 4nti# 4 er interaction with it ha been com'#eted. 6Dia#o! c#a i intended a the ba i ,or creatin! c4 tom dia#o! J however, ome o, the mo t common dia#o! are 'rovided thro4!h tatic method in the c#a 6&'tionPane. 63abe#, de cended ,rom 6+om'onent, i 4 ed to create te<t #abe# . 5he ab tract c#a )b tractE4tton e<tend c#a 6+om'onent and 'rovide a ,o4ndation ,or a ,ami#( o, b4tton c#a e , inc#4din! 6E4tton. 65e<t%ie#d a##ow editin! o, a in!#e #ine o, te<t. New ,eat4re inc#4de the abi#it( to *4 ti,( the te<t #e,t, ri!ht, or center, and to et the te<t? ,ont. 6Pa word%ie#d 7a direct 4bc#a o, 65e<t%ie#d; (o4 can 4''re the di '#a( o, in'4t. 9ach character entered can be re'#aced b( an echo character. 5hi a##ow con,identia# in'4t ,or 'a word , ,or e<am'#e. E( de,a4#t, the echo character i the a teri k, O. 65e<t)rea a##ow editin! o, m4#ti'#e #ine o, te<t. 65e<t)rea can be 4 ed in con*4nction with c#a 6Scro##Pane to achieve cro##in!. 5he 4nder#(in! 6Scro##Pane can be ,orced to a#wa( or never have either the vertica# or horiBonta# cro##barJ 6E4tton i a com'onent the 4 er c#ick to tri!!er a 'eci,ic action. 6"adioE4tton i imi#ar to 6+heckbo<, e<ce't ,or the de,a4#t icon ,or each c#a . ) et o, radio b4tton can be a ociated a a !ro4' in which on#( one b4tton at a time can be e#ected. 6+heckEo< i not a member o, a checkbo< !ro4'. ) checkbo< can be e#ected and de e#ected, and it a# o di '#a( it c4rrent tate. 6+omboEo< i #ike a dro' down bo<. Fo4 can c#ick a dro'-down arrow and e#ect an o'tion ,rom a #i t. %or e<am'#e, when the com'onent ha ,oc4 , +2+ 3imited i a S4b idiar( o, 5+S 3imited 121

're in! a ke( that corre 'ond to the ,ir t character in ome entr(? name e#ect that entr(. ) vertica# cro##bar i 4 ed ,or #on!er #i t . 63i t 'rovide a cro##ab#e et o, item ,rom which one or more ma( be e#ected. 63i t can be 'o'4#ated ,rom an )rra( or Vector. 63i t doe not 4''ort cro##in! direct#(, in tead, the #i t m4 t be a ociated with a cro##'ane. 5he view 'ort 4 ed b( the cro## 'ane can a# o have a 4 er-de,ined border. 63i t action are hand#ed 4 in! 3i tSe#ection3i tener. 65abbedPane contain a tab that can have a too# ti' and a mnemonic, and it can di '#a( both te<t and an ima!e. 65oo#bar contain a n4mber o, com'onent who e t('e i 4 4a##( ome kind o, b4tton which can a# o inc#4de e'arator to !ro4' re#ated com'onent within the too#bar. %#ow3a(o4t when 4 ed arran!e win! com'onent ,rom #e,t to ri!ht 4nti# there? no more 'ace avai#ab#e. 5hen it be!in a new row be#ow it and move ,rom #e,t to ri!ht a!ain. 9ach com'onent in a %#ow3a(o4t !et a m4ch 'ace a it need and no more. Eorder3a(o4t '#ace win! com'onent in the North, So4th, 9a t, We t and center o, a container. Fo4 can add horiBonta# and vertica# !a' between the area . Grid3a(o4t i a #a(o4t mana!er that #a( o4t a container? com'onent in a rectan!4#ar !rid. 5he container i divided into e@4a#- iBed rectan!#e , and one com'onent i '#aced in each rectan!#e. GridEa!3a(o4t i a #a(o4t mana!er that #a( o4t a container? com'onent in a !rid o, ce## with each com'onent occ4'(in! one or more ce## , ca##ed it di '#a( area. 5he di '#a( area a#i!n com'onent vertica##( and horiBonta##(, witho4t re@4irin! that the com'onent be o, the ame iBe. 62en4bar can contain evera# 62en4? . 9ach o, the 62en4? can contain a erie o, 62en4$tem ? that (o4 can e#ect. Swin! 'rovide 4''ort ,or '4##-down and 'o'4' men4 . Scro##ab#e 6Po'4'2en4 i a cro##ab#e 'o'4' men4 that can be 4 ed whenever we have o man( item in a 'o'4' men4 that e<ceed the creen vi ib#e hei!ht.

B+ Ja)a JFra!e cla%% e,a!"le JFra!e 5he com'onent added to the ,rame are re,erred to a it content J the e are mana!ed b( the contentPane. 5o add a com'onent to a 6%rame, we m4 t 4 e it contentPane in tead.6%rame i a Window with border, tit#e and b4tton . When 6%rame i et vi ib#e, an event di 'atchin! thread i tarted. 6%rame ob*ect tore evera# ob*ect inc#4din! a +ontainer ob*ect known a the content 'ane. 5o add a com'onent to a 6%rame, add it to the content 'ane. JFra!e Feature% $t` a window with tit#e, border, 7o'tiona#; men4 bar and 4 er- 'eci,ied com'onent . $t can be moved, re iBed, iconi,ied. $t i not a 4bc#a o, 6+om'onent. De#e!ate re 'on ibi#it( o, mana!in! 4 er- 'eci,ied com'onent to a content 'ane, an in tance o, 6Pane#. 7enterin( JFra!eI% E( de,a4#t, a 6,rame i di '#a(ed in the 4''er-#e,t corner o, the creen. 5o di '#a( a ,rame at a 'eci,ied #ocation, (o4 can 4 e the et3ocation7<, (; method in the 6%rame c#a . 5hi method '#ace the 4''er-#e,t corner o, a ,rame at #ocation 7<, (;. 5he Swin! )P$ kee' im'rovin! with ab traction 4ch a the etDe,a4#t+#o e&'eration method ,or the 6%rame 7ratin( a JFra!e Wind w Ste' 1: +on tr4ct an ob*ect o, the 6%rame c#a . Ste' 2: Set the iBe o, the 6,rame. Ste' 1: Set the tit#e o, the 6,rame to a''ear in the tit#e bar 7tit#e bar wi## be b#ank i, no tit#e i et;. +2+ 3imited i a S4b idiar( o, 5+S 3imited 122

Ste' D: Set the de,a4#t c#o e o'eration. When the 4 er c#ick the c#o e b4tton, the 'ro!ram to' r4nnin!. Ste' .: 2ake the 6,rame vi ib#e. Cow to 'o ition 6%rame on Screen[ ,rame. et3ocation"e#ative5o7 n4## ;J JFra!e # urce 7 de im'ort *ava.awt.OJ im'ort *ava.awt.event.OJ im'ort *ava<. win!.OJ '4b#ic c#a 6%rameDemo N

'4b#ic tatic void main7Strin! 8:; N 6%rame ,rame K new 6%rame7P6%rame So4rce DemoP;J GG )dd a window #i tner ,or c#o e b4tton ,rame.addWindow3i tener7new Window)da'ter7; N '4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J Q Q;J GG 5hi i an em't( content area in the ,rame 63abe# *#bem't( K new 63abe#7PP;J *#bem't(. etPre,erredSiBe7new Dimen ion710., 100;;J ,rame.!et+ontentPane7;.add7*#bem't(, Eorder3a(o4t.+9N59";J ,rame.'ack7;J ,rame. etVi ib#e7tr4e;J Q Out"ut

Ja)a JFra!e 'ierarchy *ava<. win! +#a 6%rame *ava.#an!.&b*ect *ava.awt.+om'onent *ava.awt.+ontainer *ava.awt.Window *ava.awt.%rame *ava<. win!.6%rame )## $m'#emented $nter,ace : )cce ib#e, $ma!e&b erver, 2en4+ontainer, "ootPane+ontainer, Seria#iBab#e, Window+on tant JFra!e 7 n%truct r 6%rame7;: +on tr4ct a new ,rame that i initia##( invi ib#e. 6%rame7Gra'hic +on,i!4ration !c; +reate a %rame in the 'eci,ied Gra'hic +on,i!4ration o, a creen device and a b#ank tit#e. 6%rame7Strin! tit#e;: +reate a new, initia##( invi ib#e %rame with the 'eci,ied tit#e. 6%rame7Strin! tit#e, Gra'hic +on,i!4ration !c; +reate a 6%rame with the 'eci,ied tit#e and the 'eci,ied Gra'hic +on,i!4ration o, a creen device. +2+ 3imited i a S4b idiar( o, 5+S 3imited 121

-+ Ja)a JInternalFra!e cla%% e,a!"le JInternalFra!e 6ava Swin! 54toria# 9<'#ainin! the 6$nterna#%rame c#a . ) 6$nterna#%rame i con,ined to a vi ib#e area o, a container it i '#aced in. 6$nterna#%rame a to' #eve# win! com'onent that ha a content'ane. a $t can be iconi,ied -- in thi ca e the icon remain in the main a''#ication container. a $t can be ma<imiBed -- %rame con 4me the main a''#ication a $t can be c#o ed 4 in! tandard 'o'4' window contro# a $t can be #a(ered JInternalFra!e # urce 7 de im'ort *ava<. win!.6$nterna#%rameJ im'ort *ava<. win!.6De kto'PaneJ im'ort *ava<. win!.62en4J im'ort *ava<. win!.62en4$temJ im'ort *ava<. win!.62en4EarJ im'ort *ava<. win!.6%rameJ im'ort *ava.awt.event.OJ im'ort *ava.awt.OJ '4b#ic c#a 6$nterna#%rameDemo e<tend 6%rame N

6De kto'Pane *d'De kto'J tatic int o'en%rame+o4nt K 0J '4b#ic 6$nterna#%rameDemo7; N 4'er7P6$nterna#%rame = a!e DemoP;J GG 2ake the main window 'o itioned a .0 'i<e# ,rom each ed!e o, the GG creen. int in et K .0J Dimen ion creenSiBe K 5oo#kit.!etDe,a4#t5oo#kit7;.!etScreenSiBe7;J etEo4nd 7in et, in et, creenSiBe.width - in et O 2, creenSiBe.hei!ht - in et O 2;J GG )dd a Window 9<it 3i tener addWindow3i tener7new Window)da'ter7; N '4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J Q Q;J GG +reate and Set 4' the G=$. *d'De kto' K new 6De kto'Pane7;J GG ) 'ecia#iBed #a(ered 'ane to be 4 ed with 6$nterna#%rame create%rame7;J GG +reate ,ir t window et+ontentPane7*d'De kto';J et62en4Ear7create2en4Ear7;;J GG 2ake dra!!in! ,a ter b( ettin! dra! mode to &4t#ine *d'De kto'.'4t+#ientPro'ert(7P6De kto'Pane.dra!2odeP, Po4t#ineP;J Q 'rotected 62en4Ear create2en4Ear7; N 62en4Ear men4Ear K new 62en4Ear7;J 62en4 men4 K new 62en47P%rameP;J men4. et2nemonic7He(9vent.VHTN;J 62en4$tem men4$tem K new 62en4$tem7PNew $%rameP;J men4$tem. et2nemonic7He(9vent.VHTN;J men4$tem.add)ction3i tener7new )ction3i tener7; N '4b#ic void actionPer,ormed7)ction9vent e; N create%rame7;J Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 12D

Q;J men4.add7men4$tem;J men4Ear.add7men4;J ret4rn men4EarJ Q 'rotected void create%rame7; N 2($nterna#%rame ,rame K new 2($nterna#%rame7;J ,rame. etVi ib#e7tr4e;J GG 9ver( 6$nterna#%rame m4 t be added to content 'ane 4 in! 6De kto'Pane *d'De kto'.add7,rame;J tr( N ,rame. etSe#ected7tr4e;J Q catch 7*ava.bean .Pro'ert(Veto9<ce'tion e; N Q Q '4b#ic tatic void main7Strin!8: ar! ; N 6$nterna#%rameDemo ,rame K new 6$nterna#%rameDemo7;J ,rame. etVi ib#e7tr4e;J Q c#a 2($nterna#%rame e<tend 6$nterna#%rame N tatic ,ina# int <Po ition K 10, (Po ition K 10J '4b#ic 2($nterna#%rame7; N 4'er7P$%rame bP > 7>>o'en%rame+o4nt;, tr4e, GG re iBab#e tr4e, GG c#o ab#e tr4e, GG ma<imiBab#e tr4e;JGG iconi,iab#e etSiBe7100, 100;J GG Set the window? #ocation. et3ocation7<Po ition O o'en%rame+o4nt, (Po ition O o'en%rame+o4nt;J Q Q
}

Out"ut

+2+ 3imited i a S4b idiar( o, 5+S 3imited

12.

Ja)a JInternalFra!e 'ierarchy *ava<. win! +#a 6$nterna#%rame *ava.#an!.&b*ect Y >--*ava.awt.+om'onent Y >--*ava.awt.+ontainer Y >--*ava<. win!.6+om'onent Y >--*ava<. win!.6$nterna#%rame )## $m'#emented $nter,ace : )cce ib#e, $ma!e&b erver, 2en4+ontainer, "ootPane+ontainer, Seria#iBab#e, Window+on tant JInternalFra!e 7 n%truct r 6$nterna#%rame7; +reate a non-re iBab#e, non-c#o ab#e, non-ma<imiBab#e, non-iconi,iab#e 6$nterna#%rame with no tit#e. 6$nterna#%rame7Strin! tit#e; +reate a non-re iBab#e, non-c#o ab#e, non-ma<imiBab#e, non-iconi,iab#e 6$nterna#%rame with the 'eci,ied tit#e. 6$nterna#%rame7Strin! tit#e, boo#ean re iBab#e; +reate a non-c#o ab#e, non-ma<imiBab#e, non-iconi,iab#e 6$nterna#%rame with the 'eci,ied tit#e and re iBabi#it(. 6$nterna#%rame7Strin! tit#e, boo#ean re iBab#e, boo#ean c#o ab#e; +reate a non-ma<imiBab#e, non-iconi,iab#e 6$nterna#%rame with the 'eci,ied tit#e, re iBabi#it(, and c#o abi#it(. 6$nterna#%rame7Strin! tit#e, boo#ean re iBab#e, boo#ean c#o ab#e, boo#ean ma<imiBab#e; +reate a non-iconi,iab#e 6$nterna#%rame with the 'eci,ied tit#e, re iBabi#it(, c#o abi#it(, and ma<imiBabi#it(. 6$nterna#%rame7Strin! tit#e, boo#ean re iBab#e, boo#ean c#o ab#e, boo#ean ma<imiBab#e, boo#ean iconi,iab#e; +reate a 6$nterna#%rame with the 'eci,ied tit#e, re iBabi#it(, c#o abi#it(, ma<imiBabi#it( J+ Ja)a JWind w cla%% e,a!"le JWind w 6ava Swin! 54toria# 9<'#ainin! the 6Window +om'onent. 6Window i Swin!? ver ion o, Window and i de cended direct#( ,rom that c#a . 3ike Window, it 4 e Eorder3a(o4t b( de,a4#t. )#mo t a## Swin! com'onent are #i!htwei!ht e<ce't 6)''#et, 6%rame, 6Dia#o!, and 6Window.

+2+ 3imited i a S4b idiar( o, 5+S 3imited

12/

JWind w # urce 7 de '4b#ic c#a 6WindowDemo e<tend 6Window N 'rivate int A K 0J 'rivate int F K 0J '4b#ic 6WindowDemo7; N etEo4nd 7/0, /0, 100, 100;J addWindow3i tener7new Window)da'ter7; N '4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J GG )n 9<it 3i tener Q Q;J GG Print 7A,F; coordinate on 2o4 e +#ick add2o4 e3i tener7new 2o4 e)da'ter7; N '4b#ic void mo4 ePre ed72o4 e9vent e; N A K e.!etA7;J F K e.!etF7;J S( tem.o4t.'rint#n7P5he 7A,F; coordinate o, window i 7P > A > P,P > F > P;P;J Q Q;J add2o4 e2otion3i tener7new 2o4 e2otion)da'ter7; N '4b#ic void mo4 eDra!!ed72o4 e9vent e; N et3ocation7!et3ocation7;.< > 7e.!etA7; - A;, !et3ocation7;.( > 7e.!etF7; - F;;J Q Q;J etVi ib#e7tr4e;J Q '4b#ic tatic void main7Strin!8: ar! ; N new 6WindowDemo7;J Q Q &4t'4t Ja)a JWind w 'ierarchy *ava<. win! +#a 6Window *ava.#an!.&b*ect *ava.awt.+om'onent *ava.awt.+ontainer *ava.awt.Window )## $m'#emented $nter,ace : )cce ib#e, $ma!e&b erver, 2en4+ontainer, Seria#iBab#e Direct Hnown S4bc#a e : Ea ic5oo#Ear=$.Dra!Window, Dia#o!, %rame, 6Window JWind w 7 n%truct r Window7%rame owner; +on tr4ct a new invi ib#e window with the 'eci,ied %rame a it owner. Window7Window owner; +on tr4ct a new invi ib#e window with the 'eci,ied Window a it owner. Window7Window owner, Gra'hic +on,i!4ration !c; +on tr4ct a new invi ib#e window with the 'eci,ied window a it owner and a Gra'hic +on,i!4ration o, a creen device. +2+ 3imited i a S4b idiar( o, 5+S 3imited 120

K+ Ja)a J4abel cla%% e,a!"le J4abel% 6ava Swin! 54toria# 9<'#ainin! the 63abe# +om'onent. 63abe#, de cended ,rom 6+om'onent, i 4 ed to create te<t #abe# . ) 63abe# ob*ect 'rovide te<t in tr4ction or in,ormation on a G=$ _ di '#a( a in!#e #ine o, read-on#( te<t, an ima!e or both te<t and ima!e. We 4 e a Swin! 63abe# when we need a 4 er inter,ace com'onent that di '#a( a me a!e or an ima!e. 6c3abe# a Provide te<t in tr4ction on a G=$ a #"ead-on#( te<t a #Pro!ram rare#( chan!e a #abe#? content a #+#a 63abe# 7 4bc#a o, 6+om'onent; 63abe# So4rce +ode im'ort *ava.awt.Grid3a(o4tJ $m'ort *ava.awt.event.Window)da'terJ im'ort *ava.awt.event.Window9ventJ im'ort *ava<. win!.63abe#J im'ort *ava<. win!.6Pane#J im'ort *ava<. win!.6%rameJ im'ort *ava<. win!.$ma!e$conJ '4b#ic c#a 6#abe#Demo e<tend 6Pane# N

63abe# *#b3abe#1, *#b3abe#2, *#b3abe#1J '4b#ic 6#abe#Demo7; N $ma!e$con icon K new $ma!e$con7P*ava- win!-t4toria#.6PGP, P2( Web iteP;J GG +reatin! an $con et3a(o4t7new Grid3a(o4t71, 1;;J GG 1 row , 1 co#4mn Pane# havin! Grid 3a(o4t *#b3abe#1 K new 63abe#7P$ma!e with 5e<tP, icon, 63abe#.+9N59";J GG We can 'o ition o, the te<t, re#ative to the icon: *#b3abe#1. etVertica#5e<tPo ition763abe#.E&55&2;J *#b3abe#1. etCoriBonta#5e<tPo ition763abe#.+9N59";J *#b3abe#2 K new 63abe#7P5e<t &n#( 3abe#P;J *#b3abe#1 K new 63abe#7icon;J GG 3abe# o, $con &n#( GG )dd #abe# to the Pane# add7*#b3abe#1;J add7*#b3abe#2;J add7*#b3abe#1;J Q '4b#ic tatic void main7Strin!8: ar! ; N 6%rame ,rame K new 6%rame7P*3abe# = a!e DemoP;J ,rame.addWindow3i tener7new Window)da'ter7; N GG Show code to )dd Window 3i tener '4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J Q Q;J ,rame. et+ontentPane7new 6#abe#Demo7;;J ,rame.'ack7;J ,rame. etVi ib#e7tr4e;J Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 128

Q Out"ut

Ja)a J4abel 'ierarchy *ava<. win! +#a 63abe# *ava.#an!.&b*ect *ava.awt.+om'onent *ava.awt.+ontainer *ava<. win!.6+om'onent *ava<. win!.63abe# )## $m'#emented $nter,ace : )cce ib#e, $ma!e&b erver, 2en4+ontainer, Seria#iBab#e, Swin!+on tant Direct Hnown S4bc#a e : Ea ic+omboEo<"enderer, De,a4#t3i t+e##"enderer, De,a4#t5ab#e+e##"enderer, De,a4#t5ree+e##"enderer J4abel 7 n%truct r 63abe#7;: +reate a 63abe# in tance with no ima!e and with an em't( trin! ,or the tit#e. 63abe#7$con ima!e;: +reate a 63abe# in tance with the 'eci,ied ima!e. 63abe#7$con ima!e, int horiBonta#)#i!nment; +reate a 63abe# in tance with the 'eci,ied ima!e and horiBonta# a#i!nment. 63abe#7Strin! te<t;: +reate a 63abe# in tance with the 'eci,ied te<t. 63abe#7Strin! te<t, $con icon, int horiBonta#)#i!nment; +reate a 63abe# in tance with the 'eci,ied te<t, ima!e, and horiBonta# a#i!nment. 63abe#7Strin! te<t, int horiBonta#)#i!nment; +reate a 63abe# in tance with the 'eci,ied te<t and horiBonta# a#i!nment. 2+ Ja)a J6utt n cla%% e,a!"le J6utt n 6ava Swin! 54toria# 9<'#ainin! the 6E4tton +om'onent. 5he ab tract c#a )b tractE4tton e<tend c#a 6+om'onent and 'rovide a ,o4ndation ,or a ,ami#( o, b4tton c#a e , inc#4din! 6E4tton. ) b4tton i a com'onent the 4 er c#ick to tri!!er a 'eci,ic action. 5here are evera# t('e o, b4tton in 6ava, a## are 4bc#a e o, )b tractE4tton. a command b4tton : i created with c#a 6E4tton. $t !enerate )ction9vent. a to!!#e b4tton : have onGo,, or tr4eG,a# e va#4e . a check bo<e : a !ro4' o, b4tton . $t !enerate $tem9vent. a radio b4tton : a !ro4' o, b4tton in which on#( one can be e#ected. $t !enerate $tem9vent. J6utt n # urce 7 de im'ort *ava.awt.event.)ction9ventJ im'ort *ava.awt.event.)ction3i tenerJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 12-

im'ort *ava.awt.event.He(9ventJ im'ort *ava.net.="3J im'ort *ava<. win!.)b tractE4ttonJ im'ort *ava<. win!.$ma!e$conJ im'ort *ava<. win!.6E4ttonJ im'ort *ava<. win!.6%rameJ im'ort *ava<. win!.6Pane#J '4b#ic c#a 6E4ttonDemo e<tend 6Pane# im'#ement )ction3i tener N

'rotected tatic 6E4tton *bn3e,t, *bn2idd#e, *bn"i!htJ '4b#ic 6E4ttonDemo7; N GG +reate $con that can be 4 ed with the *E4tton $ma!e$con #e,tE4tton$con K create$ma!e$con7Pri!htarrow.6PGP;J $ma!e$con midd#eE4tton$con K create$ma!e$con7P*ava- win!-t4toria#.6PGP;J $ma!e$con ri!htE4tton$con K create$ma!e$con7P#e,tarrow.6PGP;J *bn3e,t K new 6E4tton7PDi ab#e centre b4ttonP, #e,tE4tton$con;J *bn3e,t. etVertica#5e<tPo ition7)b tractE4tton.+9N59";J *bn3e,t. etCoriBonta#5e<tPo ition7)b tractE4tton.39)D$NG;J *bn3e,t. et2nemonic7He(9vent.VHTD;J GG )#t-D c#ick the b4tton *bn3e,t. et)ction+ommand7Pdi ab#eP;J *bn3e,t. et5oo#5i'5e<t7Pdi ab#e the +entre b4tton.P;J GG )ddin! 5oo# GG ti' *bn2idd#e K new 6E4tton7P+entre b4ttonP, midd#eE4tton$con;J *bn2idd#e. etVertica#5e<tPo ition7)b tractE4tton.E&55&2;J *bn2idd#e. etCoriBonta#5e<tPo ition7)b tractE4tton.+9N59";J *bn2idd#e. et2nemonic7He(9vent.VHT2;J GG )#t-2 c#ick the b4tton *bn2idd#e. et5oo#5i'5e<t7P+entre b4ttonP;J *bn"i!ht K new 6E4tton7P9nab#e centre b4ttonP, ri!htE4tton$con;J GG = e the de,a4#t te<t 'o ition o, +9N59", 5")$3$NG 7"$GC5;. *bn"i!ht. et2nemonic7He(9vent.VHT9;J GG )#t-9 c#ick the b4tton *bn"i!ht. et)ction+ommand7Penab#eP;J *bn"i!ht. et9nab#ed7,a# e;J GG Di ab#e the E4tton at creation time GG 3i ten ,or action on 3e,t and "o!ht E4tton *bn3e,t.add)ction3i tener7thi ;J *bn"i!ht.add)ction3i tener7thi ;J *bn"i!ht. et5oo#5i'5e<t7P9nab#e the +entre b4tton.P;J GG )dd +om'onent to the ,rame, 4 in! the de,a4#t %#ow3a(o4t. add7*bn3e,t;J add7*bn2idd#e;J add7*bn"i!ht;J Q '4b#ic void actionPer,ormed7)ction9vent e; N i, 7Pdi ab#eP.e@4a# 7e.!et)ction+ommand7;;; N *bn2idd#e. et9nab#ed7,a# e;J *bn3e,t. et9nab#ed7,a# e;J *bn"i!ht. et9nab#ed7tr4e;J Q e# e N *bn2idd#e. et9nab#ed7tr4e;J *bn3e,t. et9nab#ed7tr4e;J *bn"i!ht. et9nab#ed7,a# e;J Q Q GG "et4rn an $ma!e$con, or n4## i, the 'ath wa inva#id. +2+ 3imited i a S4b idiar( o, 5+S 3imited 110

'rotected tatic $ma!e$con create$ma!e$con7Strin! 'ath; N ="3 im!="3 K 6E4ttonDemo.c#a .!et"e o4rce7'ath;J i, 7im!="3 WK n4##; N ret4rn new $ma!e$con7im!="3;J Q e# e N S( tem.err.'rint#n7P+o4#dn?t ,ind ima!e in ( tem: P > 'ath;J ret4rn n4##J Q Q GG +reate the G=$ and how it. 'rivate tatic void createG=$7; N 6%rame. etDe,a4#t3ook)nd%ee#Decorated7tr4e;J GG +reate and et 4' the ,rame. 6%rame ,rame K new 6%rame7P*E4tton 4 a!e demoP;J ,rame. etDe,a4#t+#o e&'eration76%rame.9A$5T&NT+3&S9;J GG +reate and et 4' the content 'ane. 6E4ttonDemo b4tton+ontentPane K new 6E4ttonDemo7;J b4tton+ontentPane. et&'a@4e7tr4e;J GG content 'ane m4 t be o'a@4e ,rame.!et"ootPane7;. etDe,a4#tE4tton7*bn3e,t;J ,rame. et+ontentPane7b4tton+ontentPane;J GG Di '#a( the window. ,rame.'ack7;J ,rame. etVi ib#e7tr4e;J Q '4b#ic tatic void main7Strin!8: ar! ; N *ava<. win!.Swin!=ti#itie .invoke3ater7new "4nnab#e7; N '4b#ic void r4n7; N createG=$7;J Q Q;J Q Q &4t'4t

An ther E,a!"le: J6utt n # urce 7 de im'ort *ava.awt.OJ im'ort *ava.awt.event.OJ im'ort *ava<. win!.OJ '4b#ic c#a 6E4ttonDemo2 N

6%rame *t,2ain%rameJ 6E4tton *bnE4tton1, *bnE4tton2J 65e<t%ie#d *t,$n'4tJ 6Pane# *'#Pane#J '4b#ic 6E4ttonDemo27; N *t,2ain%rame K new 6%rame7PWhich E4tton DemoP;J *t,2ain%rame. etSiBe7.0, .0;J *bnE4tton1 K new 6E4tton7PE4tton 1P;J *bnE4tton2 K new 6E4tton7PE4tton 2P;J *t,$n'4t K new 65e<t%ie#d720;J *'#Pane# K new 6Pane#7;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 111

*bnE4tton1. et2nemonic7He(9vent.VHT$;J GGSet Short+4t He( *bnE4tton1.add)ction3i tener7new )ction3i tener7; N '4b#ic void actionPer,ormed7)ction9vent e; N *t,$n'4t. et5e<t7PE4tton 1WP;J Q Q;J *bnE4tton2. et2nemonic7He(9vent.VHT$;J *bnE4tton2.add)ction3i tener7new )ction3i tener7; N '4b#ic void actionPer,ormed7)ction9vent e; N *t,$n'4t. et5e<t7PE4tton 2WP;J Q Q;J *'#Pane#. et3a(o4t7new %#ow3a(o4t7;;J *'#Pane#.add7*t,$n'4t;J *'#Pane#.add7*bnE4tton1;J *'#Pane#.add7*bnE4tton2;J *t,2ain%rame.!et+ontentPane7;.add7*'#Pane#, Eorder3a(o4t.+9N59";J *t,2ain%rame. etDe,a4#t+#o e&'eration76%rame.9A$5T&NT+3&S9;J *t,2ain%rame.'ack7;J *t,2ain%rame. etVi ib#e7tr4e;J Q '4b#ic tatic void main7Strin!8: ar! ; N GG Set the #ook and ,ee# to 6ava Swin! 3ook tr( N =$2ana!er. et3ook)nd%ee#7=$2ana!er .!et+ro P#at,orm3ook)nd%ee#+#a Q catch 79<ce'tion e; N Q 6E4ttonDemo2 a''#ication K new 6E4ttonDemo27;J Q Q &4t'4t

Name7;;J

Ja)a J6utt n 'ierarchy *ava<. win! +#a 6E4tton *ava.#an!.&b*ect *ava.awt.+om'onent *ava.awt.+ontainer *ava<. win!.6+om'onent *ava<. win!.)b tractE4tton *ava<. win!.6E4tton )## $m'#emented $nter,ace : )cce ib#e, $ma!e&b erver, $temSe#ectab#e, 2en4+ontainer, Seria#iBab#e, Swin!+on tant Direct Hnown S4bc#a e : Ea ic)rrowE4tton, 2eta#+omboEo<E4tton J6utt n 7 n%truct r 6E4tton7;: +reate a b4tton with no et te<t or icon. 6E4tton7)ction a;: +reate a b4tton where 'ro'ertie are taken ,rom the )ction 4''#ied. 6E4tton7$con icon;: +reate a b4tton with an icon. 6E4tton7Strin! te<t;: +reate a b4tton with te<t. 6E4tton7Strin! te<t, $con icon;: +reate a b4tton with initia# te<t and an icon. +2+ 3imited i a S4b idiar( o, 5+S 3imited

112

L+ Ja)a J*e,tField cla%% e,a!"le J*e,tField 6ava Swin! 54toria# 9<'#ainin! the 65e<t%ie#d +om'onent. 65e<t%ie#d a##ow editin!Gdi '#a(in! o, a in!#e #ine o, te<t. New ,eat4re inc#4de the abi#it( to *4 ti,( the te<t #e,t, ri!ht, or center, and to et the te<t? ,ont. When the 4 er t('e data into them and 're e the 9nter ke(, an action event occ4r . $, the 'ro!ram re!i ter an event #i tener, the #i tener 'roce e the event and can 4 e the data in the te<t ,ie#d at the time o, the event in the 'ro!ram. 65e<t%ie#d i an in'4t area where the 4 er can t('e in character . $, (o4 want to #et the 4 er enter m4#ti'#e #ine o, te<t, (o4 cannot 4 e 6te<t,ie#d? 4n#e (o4 create evera# o, them. 5he o#4tion i to 4 e 65e<t)rea, which enab#e the 4 er to enter m4#ti'#e #ine o, te<t. J*e,tField # urce 7 de GG ) 'ro!ram to demon trate the 4 e o, 65e<t%ie#d ? GG$m'ort Statement im'ort *ava<. win!.OJ im'ort *ava.awt.OJ im'ort *ava.awt.event.OJ '4b#ic c#a 65e<t%ie#dDemo e<tend 6%rame N

GG+#a Dec#aration 65e<t%ie#d *t,5e<t1, *t,=neditab#e5e<tJ Strin! di ' K PPJ 5e<tCand#er hand#er K n4##J GG+on tr4ctor '4b#ic 65e<t%ie#dDemo7; N 4'er7P5e<t%ie#d 5e t DemoP;J +ontainer container K !et+ontentPane7;J container. et3a(o4t7new %#ow3a(o4t7;;J *t,5e<t1 K new 65e<t%ie#d710;J *t,=neditab#e5e<t K new 65e<t%ie#d7P=neditab#e te<t ,ie#dP, 20;J *t,=neditab#e5e<t. et9ditab#e7,a# e;J container.add7*t,5e<t1;J container.add7*t,=neditab#e5e<t;J hand#er K new 5e<tCand#er7;J *t,5e<t1.add)ction3i tener7hand#er;J *t,=neditab#e5e<t.add)ction3i tener7hand#er;J etSiBe712., 100;J etVi ib#e7tr4e;J Q GG$nner +#a 5e<tCand#er 'rivate c#a 5e<tCand#er im'#ement )ction3i tener N '4b#ic void actionPer,ormed7)ction9vent e; N i, 7e.!etSo4rce7; KK *t,5e<t1; N di ' K Pte<t1 : P > e.!et)ction+ommand7;J Q e# e i, 7e.!etSo4rce7; KK *t,=neditab#e5e<t; N di ' K Pte<t1 : P > e.!et)ction+ommand7;J Q 6&'tionPane. how2e a!eDia#o!7n4##, di ';J Q Q GG2ain Pro!ram that tart 9<ec4tion '4b#ic tatic void main7Strin! ar! 8:; N 65e<t%ie#dDemo te t K new 65e<t%ie#dDemo7;J te t. etDe,a4#t+#o e&'eration76%rame.9A$5T&NT+3&S9;J Q QGG 9nd o, c#a 5e<t%ie#d5e t +2+ 3imited i a S4b idiar( o, 5+S 3imited 111

&4t'4t

An ther E,a!"le: J*e,tField # urce 7 de '4b#ic c#a 65e<t%ie#dDemo2 e<tend 6%rame im'#ement )ction3i tener N 65e<t%ie#d *t,$n'4tJ 65e<t)rea *t)rea&4t'4tJ Strin! new#ine K PInPJ '4b#ic 65e<t%ie#dDemo27; N createG4i7;J Q '4b#ic void createG4i7; N *t,$n'4t K new 65e<t%ie#d720;J *t,$n'4t.add)ction3i tener7thi ;J *t)rea&4t'4t K new 65e<t)rea7., 20;J *t)rea&4t'4t. et9ditab#e7,a# e;J 6Scro##Pane cro##Pane K new 6Scro##Pane7*t)rea&4t'4t, 6Scro##Pane.V9"5$+)3TS+"&33E)"T)3W)FS, 6Scro##Pane.C&"$S&N5)3TS+"&33E)"T)3W)FS;J GridEa!3a(o4t !ridEa! K new GridEa!3a(o4t7;J +ontainer contentPane K !et+ontentPane7;J contentPane. et3a(o4t7!ridEa!;J GridEa!+on traint !rid+on 1 K new GridEa!+on traint 7;J !rid+on 1.!ridwidth K GridEa!+on traint ."92)$ND9"J !rid+on 1.,i## K GridEa!+on traint .C&"$S&N5)3J contentPane.add7*t,$n'4t, !rid+on 1;J GridEa!+on traint !rid+on 2 K new GridEa!+on traint 7;J !rid+on 2.wei!ht< K 1.0J !rid+on 2.wei!ht( K 1.0J contentPane.add7 cro##Pane, !rid+on 2;J Q '4b#ic void actionPer,ormed7)ction9vent evt; N Strin! te<t K *t,$n'4t.!et5e<t7;J *t)rea&4t'4t.a''end7te<t > new#ine;J *t,$n'4t. e#ect)##7;J Q '4b#ic tatic void main7Strin!8: ar! ; N 65e<t%ie#dDemo2 *t,5,Demo K new 65e<t%ie#dDemo27;J *t,5,Demo.'ack7;J *t,5,Demo.addWindow3i tener7new Window)da'ter7; N '4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J Q Q;J *t,5,Demo. etVi ib#e7tr4e;J Q Q &4t'4t

+2+ 3imited i a S4b idiar( o, 5+S 3imited

11D

Ja)a J*e,tField 'ierarchy *ava<. win! *ava.#an!.&b*ect *ava.awt.+om'onent *ava.awt.+ontainer *ava<. win!.6+om'onent *ava<. win!.te<t.65e<t+om'onent *ava<. win!.65e<t%ie#d )## $m'#emented $nter,ace : )cce ib#e, $ma!e&b erver, 2en4+ontainer, Scro##ab#e, Seria#iBab#e, Swin!+on tant Direct Hnown S4bc#a e : De,a4#t5ree+e##9ditor.De,a4#t5e<t%ie#d, 6%ormatted5e<t%ie#d, 6Pa word%ie#d J*e,tField 7 n%truct r 65e<t%ie#d7;: +on tr4ct a new 5e<t%ie#d. 65e<t%ie#d7Doc4ment doc, Strin! te<t, int co#4mn ; +on tr4ct a new 65e<t%ie#d that 4 e the !iven te<t tora!e mode# and the !iven n4mber o, co#4mn . 65e<t%ie#d7int co#4mn ;: +on tr4ct a new em't( 5e<t%ie#d with the 'eci,ied n 4mber o, co#4mn . 65e<t%ie#d7Strin! te<t;: +on tr4ct a new 5e<t%ie#d initia#iBed with the 'eci,ied te<t. 65e<t%ie#d7Strin! te<t, int co#4mn ;: +on tr4ct a new 5e<t%ie#d initia#iBed with the 'eci,ied te<t and co#4mn . M+ Ja)a JPa%%w rdField cla%% e,a!"le JPa%%w rdField 6ava Swin! 54toria# 9<'#ainin! the 6Pa word%ie#d +om'onent. 6Pa word%ie#d 7a direct 4bc#a o, 65e<t%ie#d; (o4 can 4''re the di '#a( o, in'4t. 9ach character entered can be re'#aced b( an echo character. 5hi a##ow con,identia# in'4t ,or 'a word , ,or e<am'#e. E( de,a4#t, the echo character i the a teri k, O. When the 4 er t('e data into them and 're e the 9nter ke(, an action event occ4r . $, the 'ro!ram re!i ter an event #i tener, the #i tener 'roce e the event and can 4 e the data in the te<t ,ie#d at the time o, the event in the 'ro!ram. $, (o4 need to 'rovide an editab#e te<t ,ie#d that doe n`t how the character the 4 er t('e d 4 e the 6Pa word%ie#d c#a . JPa%%w rdField # urce 7 de '4b#ic c#a 6Pa word%ie#dDemo N

'4b#ic tatic void main7Strin!8: ar!v; N ,ina# 6%rame ,rame K new 6%rame7P6Pa word = a!e DemoP;J 63abe# *#bPa word K new 63abe#7P9nter the 'a word: P;J 6Pa word%ie#d *'wName K new 6Pa word%ie#d710;J *'wName. et9cho+har7?O?;J *'wName.add)ction3i tener7new )ction3i tener7; N '4b#ic void actionPer,ormed7)ction9vent e; N 6Pa word%ie#d in'4t K 76Pa word%ie#d; e.!etSo4rce7;J char8: 'a word K in'4t.!etPa word7;J i, 7i Pa word+orrect7'a word;; N 6&'tionPane. how2e a!eDia#o!7,rame, +2+ 3imited i a S4b idiar( o, 5+S 3imited 11.

P+orrect 'a Q e# e N

word.P;J

6&'tionPane. how2e a!eDia#o!7,rame, PSorr(. 5r( a!ain.P, P9rror 2e a!eP, 6&'tionPane.9""&"T29SS)G9;J Q Q Q;J 6Pane# *'#+ontentPane K new 6Pane#7new Eorder3a(o4t7;;J *'#+ontentPane. etEorder7Eorder%actor(.create9m't(Eorder720, 20, 20, 20;;J *'#+ontentPane.add7*#bPa word, Eorder3a(o4t.W9S5;J *'#+ontentPane.add7*'wName, Eorder3a(o4t.+9N59";J ,rame. et+ontentPane7*'#+ontentPane;J ,rame.addWindow3i tener7new Window)da'ter7; N '4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J Q Q;J ,rame.'ack7;J ,rame. etVi ib#e7tr4e;J Q 'rivate tatic boo#ean i Pa word+orrect7char8: in'4tPa word; N char8: act4a#Pa word K N ?h?, ?e?, ?m?, ?a?, ?n?, ?t?, ?h? QJ i, 7in'4tPa word.#en!th WK act4a#Pa word.#en!th; ret4rn ,a# eJ GG "et4rn ,a# e i, #en!th are 4ne@4a# ,or 7int i K 0J i V in'4tPa word.#en!thJ i>>; i, 7in'4tPa word8i: WK act4a#Pa word8i:; ret4rn ,a# eJ ret4rn tr4eJ Q Q &4t'4t

Ja)a JPa%%w rdField 'ierarchy *ava<. win! +#a 6Pa word%ie#d *ava.#an!.&b*ect *ava.awt.+om'onent *ava.awt.+ontainer *ava<. win!.6+om'onent *ava<. win!.te<t.65e<t+om'onent *ava<. win!.65e<t%ie#d *ava<. win!.6Pa word%ie#d )## $m'#emented $nter,ace : $ma!e&b erver, 2en4+ontainer, Seria#iBab#e, )cce ib#e, Scro##ab#e, Swin!+on tant JPa%%w rdField 7 n%truct r

+2+ 3imited i a S4b idiar( o, 5+S 3imited

11/

6Pa word%ie#d7; +on tr4ct a new 6Pa word%ie#d, with a de,a4#t doc4ment, n4## tartin! te<t trin!, and 0 co#4mn width. 6Pa word%ie#d7Doc4ment doc, Strin! t<t, int co#4mn ; +on tr4ct a new 6Pa word%ie#d that 4 e the !iven te<t tora!e mode# and the !iven n4mber o, co#4mn . 6Pa word%ie#d7int co#4mn ;: +on tr4ct a new em't( 6Pa word%ie#d with the 'eci,ied n4mber o, co# . 6Pa word%ie#d7Strin! te<t; +on tr4ct a new 6Pa word%ie#d initia#iBed with the 'eci,ied te<t. 6Pa word%ie#d7Strin! te<t, int co#4mn ; +on tr4ct a new 6Pa word%ie#d initia#iBed with the 'eci,ied te<t and co#4mn . 3+ Ja)a J*e,tArea cla%% e,a!"le J*e,tArea 6ava Swin! 54toria# 9<'#ainin! the 65e<t)rea +om'onent. 65e<t)rea a##ow editin! o, m4#ti'#e #ine o, te<t. 65e<t)rea can be 4 ed in con*4nction with c#a 6Scro##Pane to achieve cro##in!. 5he 4nder#(in! 6Scro##Pane can be ,orced to a#wa( or never have either the vertica# or horiBonta# cro##bar. J*e,tArea # urce 7 de im'ort *ava.awt.OJ im'ort *ava.awt.event.OJ im'ort *ava<. win!.OJ '4b#ic c#a 65e<t)reaDemo e<tend 6%rame im'#ement )ction3i tener N

65e<t%ie#d *t,$n'4tJ 65e<t)rea *t)rea&4t'4tJ Strin! new#ine K PInPJ '4b#ic 65e<t)reaDemo7; N createG4i7;J Q '4b#ic void createG4i7; N *t,$n'4t K new 65e<t%ie#d720;J *t,$n'4t.add)ction3i tener7thi ;J *t)rea&4t'4t K new 65e<t)rea7., 20;J *t)rea&4t'4t. et+aretPo ition7*t)rea&4t'4t.!etDoc4ment7; .!et3en!th7;;J *t)rea&4t'4t. et9ditab#e7,a# e;J 6Scro##Pane cro##Pane K new 6Scro##Pane7*t)rea&4t'4t, 6Scro##Pane.V9"5$+)3TS+"&33E)"T)3W)FS, 6Scro##Pane.C&"$S&N5)3TS+"&33E)"T)3W)FS;J GridEa!3a(o4t !ridEa! K new GridEa!3a(o4t7;J +ontainer contentPane K !et+ontentPane7;J contentPane. et3a(o4t7!ridEa!;J GridEa!+on traint !rid+on 1 K new GridEa!+on traint 7;J !rid+on 1.!ridwidth K GridEa!+on traint ."92)$ND9"J !rid+on 1.,i## K GridEa!+on traint .C&"$S&N5)3J contentPane.add7*t,$n'4t, !rid+on 1;J GridEa!+on traint !rid+on 2 K new GridEa!+on traint 7;J !rid+on 2.wei!ht< K 1.0J !rid+on 2.wei!ht( K 1.0J contentPane.add7 cro##Pane, !rid+on 2;J Q '4b#ic void actionPer,ormed7)ction9vent evt; N Strin! te<t K *t,$n'4t.!et5e<t7;J *t)rea&4t'4t.a''end7te<t > new#ine;J *t,$n'4t. e#ect)##7;J Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 110

'4b#ic tatic void main7Strin!8: ar! ; N 65e<t)reaDemo *t,5,Demo K new 65e<t)reaDemo7;J *t,5,Demo.'ack7;J *t,5,Demo.addWindow3i tener7new Window)da'ter7; N '4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J Q Q;J *t,5,Demo. etVi ib#e7tr4e;J Q Q &4t'4t

Ja)a J*e,tArea 'ierarchy *ava<. win! +#a 65e<t)rea *ava.#an!.&b*ect *ava.awt.+om'onent *ava.awt.+ontainer *ava<. win!.6+om'onent *ava<. win!.te<t.65e<t+om'onent *ava<. win!.65e<t%ie#d )## $m'#emented $nter,ace : )cce ib#e, $ma!e&b erver, 2en4+ontainer, Scro##ab#e, Seria#iBab#e, Swin!+on tant Direct Hnown S4bc#a e : De,a4#t5ree+e##9ditor.De,a4#t5e<t%ie#d, 6%ormatted5e<t%ie#d, 6Pa word%ie#d J*e,tArea 7 n%truct r 65e<t)rea7;: +on tr4ct a new 5e<t)rea. 65e<t)rea7Doc4ment doc;: +on tr4ct a new 65e<t)rea with the !iven doc4ment mode#, and de,a4#t ,or a## o, the other ar!4ment 7n4##, 0, 0;. 65e<t)rea7Doc4ment doc, Strin! te<t, int row , int co#4mn ; +on tr4ct a new 65e<t)rea with the 'eci,ied n4mber o, row and co#4mn , and the !iven mode#. 65e<t)rea7int row , int co#4mn ;: +on tr4ct a new em't( 5e<t)rea with the 'eci,ied n4mber o, row and co#4mn . 65e<t)rea7Strin! te<t;: +on tr4ct a new 5e<t)rea with the 'eci,ied te<t di '#a(ed. 65e<t)rea7Strin! te<t, int row , int co#4mn ; +on tr4ct a new 5e<t)rea with the 'eci,ied te<t and n4mber o, row and co#4mn . 1+ Ja)a JRadi 6utt n cla%% e,a!"le JRadi 6utt n 6ava Swin! 54toria# 9<'#ainin! the 6"adioE4tton +om'onent. 6"adioE4tton i imi#ar to 6+heckbo<, e<ce't ,or the de,a4#t icon ,or each c#a . ) et o, radio b4tton can be a ociated a a !ro4' in which on#( one b4tton at a time can be e#ected. JRadi 6utt n # urce 7 de im'ort *ava.awt.OJ im'ort *ava.awt.event.OJ im'ort *ava<. win!.OJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 118

'4b#ic c#a

6"adioE4ttonDemo e<tend 6Pane# N

tatic 6%rame ,rameJ 63abe# *#bPict4reJ "adio3i tener m(3i tener K n4##J '4b#ic 6"adioE4ttonDemo7; N GG +reate the radio b4tton and a i!n He(board hortc4t 4 in! GG 2nemonic 6"adioE4tton *rbN4mber K new 6"adioE4tton7PN4mber P;J *rbN4mber . et2nemonic7He(9vent.VHTN;J *rbN4mber . et)ction+ommand7Pn4mber P;J *rbN4mber . etSe#ected7tr4e;J 6"adioE4tton *rb)#'habet K new 6"adioE4tton7P)#'habet P;J *rb)#'habet . et2nemonic7He(9vent.VHT);J *rb)#'habet . et)ction+ommand7Pa#'habet P;J 6"adioE4tton *rbS(mbo# K new 6"adioE4tton7PS(mbo# P;J *rbS(mbo# . et2nemonic7He(9vent.VHTS;J *rbS(mbo# . et)ction+ommand7P (mbo# P;J GG Gro4' the radio b4tton . E4ttonGro4' !ro4' K new E4ttonGro4'7;J !ro4'.add7*rbN4mber ;J !ro4'.add7*rb)#'habet ;J !ro4'.add7*rbS(mbo# ;J GG "e!i ter an action #i tener ,or the radio b4tton . m(3i tener K new "adio3i tener7;J *rbN4mber .add)ction3i tener7m(3i tener;J *rb)#'habet .add)ction3i tener7m(3i tener;J *rbS(mbo# .add)ction3i tener7m(3i tener;J GG Set 4' the 'ict4re #abe# *#bPict4re K new 63abe#7new $ma!e$con7PP > Pn4mber P > P.*'!P;;J GG Set the De,a4#t $ma!e *#bPict4re. etPre,erredSiBe7new Dimen ion7100, 122;;J GG P4t the radio b4tton in a co#4mn in a 'ane# 6Pane# *'#"adio K new 6Pane#7;J *'#"adio. et3a(o4t7new Grid3a(o4t70, 1;;J *'#"adio.add7*rbN4mber ;J *'#"adio.add7*rb)#'habet ;J *'#"adio.add7*rbS(mbo# ;J et3a(o4t7new Eorder3a(o4t7;;J add7*'#"adio, Eorder3a(o4t.W9S5;J add7*#bPict4re, Eorder3a(o4t.+9N59";J etEorder7Eorder%actor(.create9m't(Eorder720, 20, 20, 20;;J Q GOO 3i ten to the radio b4tton . OG c#a "adio3i tener im'#ement )ction3i tener N '4b#ic void actionPer,ormed7)ction9vent e; N *#bPict4re. et$con7new $ma!e$con7PP > e.!et)ction+ommand7; > P.*'!P;;J Q Q '4b#ic tatic void main7Strin! 8:; N ,rame K new 6%rame7P6"adioE4tton = a!e DemoP;J ,rame.addWindow3i tener7new Window)da'ter7; N '4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 11-

Q;J ,rame.!et+ontentPane7;.add7new 6"adioE4ttonDemo7;, Eorder3a(o4t.+9N59";J ,rame.'ack7;J ,rame. etVi ib#e7tr4e;J Q Q &4t'4t

Ja)a JRadi 6utt n 'ierarchy *ava<. win! +#a 6"adioE4tton *ava.#an!.&b*ect *ava.awt.+om'onent *ava.awt.+ontainer *ava<. win!.6+om'onent *ava<. win!.)b tractE4tton *ava<. win!.65o!!#eE4tton *ava<. win!.6"adioE4tton )## $m'#emented $nter,ace : )cce ib#e, $ma!e&b erver, $temSe#ectab#e, 2en4+ontainer, Seria#iBab#e, Swin!+on tant JRadi 6utt n 7 n%truct r 6"adioE4tton7;: +reate an initia##( 4n e#ected radio b4tton with no et te<t. 6"adioE4tton7)ction a;: +reate a radiob4tton where 'ro'ertie are taken ,rom the )ction 4''#ied. 6"adioE4tton7$con icon;: +reate an initia##( 4n e#ected radio b4tton with the 'eci,ied ima!e b4t no te<t. 6"adioE4tton7$con icon, boo#ean e#ected;: +reate a radio b4tton with the 'eci,ied ima!e and e#ection tate, b4t no te<t. 6"adioE4tton7Strin! te<t;: +reate an 4n e#ected radio b4tton with the 'eci,ied te<t. 6"adioE4tton7Strin! te<t, boo#ean e#ected;: +reate a radio b4tton with the 'eci,ied te<t and e#ection tate. 6"adioE4tton7Strin! te<t, $con icon; +reate a radio b4tton that ha the 'eci,ied te<t and ima!e, and that i initia##( 4n e#ected. 6"adioE4tton7Strin! te<t, $con icon, boo#ean e#ected; +reate a radio b4tton that ha the 'eci,ied te<t, ima!e, and e#ection tate. B.+ Ja)a J7hec=6 , cla%% e,a!"le J7hec=6 , 6ava Swin! 54toria# 9<'#ainin! the 6+heckEo< +om'onent. 6+heckEo< i not a member o, a checkbo< !ro4'. ) checkbo< can be e#ected and de e#ected, and it a# o di '#a( it c4rrent tate. J7hec=6 , # urce 7 de im'ort *ava.awt.OJ im'ort *ava.awt.event.OJ im'ort *ava<. win!.OJ '4b#ic c#a 6+heckEo<Demo e<tend 6Pane# N +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D0

GG%o4r acce or( choice 'rovide ,or 1/ di,,erent combination 6+heckEo< *cb+hinJ 6+heckEo< *cbG#a e J 6+heckEo< *cbCairJ 6+heckEo< *cb5eethJ GO 5he ima!e ,or each combination i contained in a e'arate ima!e ,i#e who e name indicate the acce orie . 5he ,i#ename are P!eek-AAAA.!i,P where AAAA can be one O o, the ,o##owin! 1/ choice . OG Strin!E4,,er choice J 63abe# *#bPict4reJ +heckEo<3i tener m(3i tener K n4##J '4b#ic 6+heckEo<Demo7; N GG )dd an item #i tener ,or each o, the check bo<e . GG 5hi i the #i tener c#a which contain b4 ine #o!ic m(3i tener K new +heckEo<3i tener7;J GG +reate check bo<e with de,a4#t e#ection tr4e *cb+hin K new 6+heckEo<7P+hinP;J *cb+hin. et2nemonic7He(9vent.VHT+;J GG)#t>+ +heck G=ncheck the check Eo< *cb+hin. etSe#ected7tr4e;J *cb+hin.add$tem3i tener7m(3i tener;J *cbG#a e K new 6+heckEo<7PG#a e P;J *cbG#a e . et2nemonic7He(9vent.VHTG;J GG)#t>G +heck G=ncheck the check Eo< *cbG#a e . etSe#ected7tr4e;J *cbG#a e .add$tem3i tener7m(3i tener;J *cbCair K new 6+heckEo<7PCairP;J *cbCair. et2nemonic7He(9vent.VHTC;J GG)#t>C +heck G=ncheck the check Eo< *cbCair. etSe#ected7tr4e;J *cbCair.add$tem3i tener7m(3i tener;J *cb5eeth K new 6+heckEo<7P5eethP;J *cb5eeth. et2nemonic7He(9vent.VHT5;J GG)#t>5 +heck G=ncheck the check Eo< *cb5eeth. etSe#ected7tr4e;J *cb5eeth.add$tem3i tener7m(3i tener;J

GG $ndicate what? on the !eek. choice K new Strin!E4,,er7Pc!htP;JGGDe,a4#t $ma!e ha a## the 'art . GG Set 4' the 'ict4re #abe# *#bPict4re K new 63abe#7new $ma!e$con7P!eek-P > choice .toStrin!7;.trim7; > P.!i,P;;J *#bPict4re. et5oo#5i'5e<t7choice .toStrin!7;.trim7;;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D1

GG P4t the check bo<e in a co#4mn in a 'ane# 6Pane# *'#+heckEo< K new 6Pane#7;J *'#+heckEo<. et3a(o4t7new Grid3a(o4t70, 1;;J *'#+heckEo<.add7*cb+hin;J *'#+heckEo<.add7*cbG#a e ;J *'#+heckEo<.add7*cbCair;J *'#+heckEo<.add7*cb5eeth;J

GG0 row , 1 +o#4mn

et3a(o4t7new Eorder3a(o4t7;;J add7*'#+heckEo<, Eorder3a(o4t.W9S5;J add7*#bPict4re, Eorder3a(o4t.+9N59";J etEorder7Eorder%actor(.create9m't(Eorder720,20,20,20;;J Q GG3i ten to the check bo<e event c#a +heckEo<3i tener im'#ement $tem3i tener N '4b#ic void itemState+han!ed7$tem9vent e; N int inde< K 0J char c K ?-?J &b*ect o4rce K e.!etSo4rce7;J i, 7 o4rce KK *cb+hin; N inde< K 0J c K ?c?J Q e# e i, 7 o4rce KK *cbG#a e ; N inde< K 1J c K ?!?J Q e# e i, 7 o4rce KK *cbCair; N inde< K 2J c K ?h?J Q e# e i, 7 o4rce KK *cb5eeth; N inde< K 1J c K ?t?J Q i, 7e.!etState+han!e7; KK $tem9vent.D9S939+59D; c K ?-?J choice . et+har)t7inde<, c;J *#bPict4re. et$con7new $ma!e$con7P!eek-P > choice .toStrin!7;.trim7; > P.!i,P;;J *#bPict4re. et5oo#5i'5e<t7choice .toStrin!7;;J Q Q '4b#ic tatic void main7Strin! 8:; N 6%rame ,rame K new 6%rame7P6+heckEo< = a!e DemoP;J ,rame.addWindow3i tener7new Window)da'ter7; N '4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J Q Q;J ,rame. et+ontentPane7new 6+heckEo<Demo7;;J ,rame.'ack7;J ,rame. etVi ib#e7tr4e;J Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D2

Q &4t'4t

BB+ Ja)a J7 !b 6 , cla%% e,a!"le J7 !b 6 , 6ava Swin! 54toria# 9<'#ainin! the 6+omboEo< +om'onent. 6+omboEo< i #ike a dro' down bo< _ (o4 can c#ick a dro'-down arrow and e#ect an o'tion ,rom a #i t. $t !enerate $tem9vent. %or e<am'#e, when the com'onent ha ,oc4 , 're in! a ke( that corre 'ond to the ,ir t character in ome entr(? name e#ect that entr(. ) vertica# cro##bar i 4 ed ,or #on!er #i t . J7 !b 6 , # urce 7 de im'ort *ava.awt.OJ im'ort *ava.awt.event.OJ im'ort *ava<. win!.OJ '4b#ic c#a 6+omboEo<Demo e<tend 6Pane# N

63abe# *#bPict4reJ '4b#ic 6+omboEo<Demo7; N Strin!8: combo5('e K N PN4mber P, P)#'habet P, PS(mbo# P QJ GG +reate the combo bo<, and et 2nd item a De,a4#t 6+omboEo< combo5('e 3i t K new 6+omboEo<7combo5('e ;J combo5('e 3i t. etSe#ected$nde<72;J combo5('e 3i t.add)ction3i tener7new )ction3i tener7; N '4b#ic void actionPer,ormed7)ction9vent e; N 6+omboEo< *cmb5('e K 76+omboEo<; e.!etSo4rce7;J Strin! cmb5('e K 7Strin!; *cmb5('e.!etSe#ected$tem7;J *#bPict4re. et$con7new $ma!e$con7PP > cmb5('e.trim7;.to3ower+a e7; > P.*'!P;;J Q Q;J GG Set 4' the 'ict4re *#bPict4re K new 63abe#7new $ma!e$con7PP > combo5('e 8combo5('e 3i t.!etSe#ected$nde<7;: > P.*'!P;;J *#bPict4re. etEorder7Eorder%actor(.create9m't(Eorder710, 0, 0, 0;;J *#bPict4re. etPre,erredSiBe7new Dimen ion7100, 122 > 10;;J GG 3a(o4t the demo et3a(o4t7new Eorder3a(o4t7;;J add7combo5('e 3i t, Eorder3a(o4t.N&"5C;J add7*#bPict4re, Eorder3a(o4t.S&=5C;J etEorder7Eorder%actor(.create9m't(Eorder720, 20, 20, 20;;J Q '4b#ic tatic void main7Strin! 8:; N 6%rame ,rame K new 6%rame7P6+omboEo< = a!e DemoP;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D1

,rame.addWindow3i tener7new Window)da'ter7; N '4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J Q Q;J ,rame. et+ontentPane7new 6+omboEo<Demo7;;J ,rame.'ack7;J ,rame. etVi ib#e7tr4e;J Q Q &4t'4t )nother 9<am'#e: 6+omboEo< So4rce +ode im'ort *ava.awt.OJ im'ort *ava.awt.event.OJ im'ort *ava<. win!.OJ im'ort *ava<. win!.border.OJ im'ort *ava.4ti#.OJ im'ort *ava.te<t.OJ '4b#ic c#a Date+omboEo<Demo e<tend 6Pane# N

tatic 6%rame ,rameJ 63abe# *#b"e 4#tJ Strin! datePatternT+4rrentJ '4b#ic Date+omboEo<Demo7; N Strin!8: datePattern K N Pdd 22222 ((((P, Pdd.22.((P, P22GddG((P, P((((.22.dd G ?at? hh:mm: BP, P999, 222 d, ??((P, Ph:mm aP, PC:mm: :SSSP, PH:mm a,BP, P((((.22222.dd GGG hh:mm aaaP QJ datePatternT+4rrent K datePattern 80:J GG Set 4' the =$ ,or e#ectin! a 'attern. 63abe# *#bCeadin! K new 63abe#7 P9nter Date 'attern GSe#ect ,rom #i t:P;J 6+omboEo< 'attern3i t K new 6+omboEo<7datePattern ;J 'attern3i t. et9ditab#e7tr4e;J 'attern3i t. et)#i!nmentA7+om'onent.39%5T)3$GN29N5;J 'attern3i t.add)ction3i tener7new )ction3i tener7; N '4b#ic void actionPer,ormed7)ction9vent e; N 6+omboEo< *cmbDate K 76+omboEo<; e.!etSo4rce7;J Strin! e#etedDate K 7Strin!; *cmbDate .!etSe#ected$tem7;J datePatternT+4rrent K e#etedDateJ howDatein3abe#7;J Q Q;J GG +reate the =$ ,or di '#a(in! re 4#t 63abe# *#b"e 4#tCeadin! K new 63abe#7P+4rrent DateG5imeP, 63abe#.39%5;J *#b"e 4#t K new 63abe#7P P;J *#b"e 4#t. et%ore!ro4nd7+o#or.b#ack;J *#b"e 4#t. etEorder7Eorder%actor(.create+om'o4ndEorder7 Eorder%actor(.create3ineEorder7+o#or.b#ack;, Eorder%actor( .create9m't(Eorder7., ., ., .;;;J GG 3a( o4t ever(thin! 6Pane# *'nDate K new 6Pane#7;J *'nDate. et3a(o4t7new Eo<3a(o4t7*'nDate, Eo<3a(o4t.FT)A$S;;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 1DD

*'nDate.add7*#bCeadin!;J *'nDate.add7'attern3i t;J 6Pane# *'n"e 4#t K new 6Pane#7;J *'n"e 4#t . et3a(o4t7new Grid3a(o4t70, 1;;J *'n"e 4#t .add7*#b"e 4#tCeadin!;J *'n"e 4#t .add7*#b"e 4#t;J et3a(o4t7new Eo<3a(o4t7thi , Eo<3a(o4t.FT)A$S;;J *'nDate. et)#i!nmentA7+om'onent.39%5T)3$GN29N5;J *'n"e 4#t . et)#i!nmentA7+om'onent.39%5T)3$GN29N5;J add7*'nDate;J add7Eo<.create"i!id)rea7new Dimen ion70, 10;;;J add7*'n"e 4#t ;J etEorder7Eorder%actor(.create9m't(Eorder710, 10, 10, 10;;J howDatein3abe#7;J Q GG con tr4ctor GOO %ormat and di '#a( toda(? date. OG '4b#ic void howDatein3abe#7; N Date toda( K new Date7;J Sim'#eDate%ormat ,ormatter K new Sim'#eDate%ormat7 datePatternT+4rrent;J tr( N Strin! dateStrin! K ,ormatter.,ormat7toda(;J *#b"e 4#t. et%ore!ro4nd7+o#or.b#ack;J *#b"e 4#t. et5e<t7dateStrin!;J Q catch 7$##e!a#)r!4ment9<ce'tion e; N *#b"e 4#t. et%ore!ro4nd7+o#or.red;J *#b"e 4#t. et5e<t7P9rror: P > e.!et2e a!e7;;J Q Q '4b#ic tatic void main7Strin! 8:; N ,rame K new 6%rame7P6+omboEo< = a!e DemoP;J ,rame.addWindow3i tener7new Window)da'ter7; N '4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J Q Q;J ,rame. et+ontentPane7new Date+omboEo<Demo7;;J ,rame.'ack7;J ,rame. etVi ib#e7tr4e;J Q Q &4t'4t Ja)a J7 !b 6 , 'ierarchy *ava<. win! +#a 6+omboEo< *ava.#an!.&b*ect *ava.awt.+om'onent *ava.awt.+ontainer *ava<. win!.6+om'onent *ava<. win!.6+omboEo< )## $m'#emented $nter,ace : )cce ib#e, )ction3i tener, 9vent3i tener, $ma!e&b erver, $temSe#ectab#e, 3i tData3i tener, 2en4+ontainer, Seria#iBab#e J7 !b 6 , 7 n%truct r 6+omboEo<7;: +reate a 6+omboEo< with a de,a4#t data mode#. +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D.

6+omboEo<7+omboEo<2ode# a2ode#; +reate a 6+omboEo< that take it? item ,rom an e<i tin! +omboEo<2ode#. 6+omboEo<7&b*ect8: item ;: +reate a 6+omboEo< that contain the e#ement in the 'eci,ied arra(. 6+omboEo<7Vector item ;: +reate a 6+omboEo< that contain the e#ement in the 'eci,ied Vector. B-+Ja)a J4i%t cla%% e,a!"le J4i%t 6ava Swin! 54toria# 9<'#ainin! the 63i t +om'onent. 63i t 'rovide a cro##ab#e et o, item ,rom which one or more ma( be e#ected. 63i t can be 'o'4#ated ,rom an )rra( or Vector. 63i t doe not 4''ort cro##in! direct#(_in tead, the #i t m4 t be a ociated with a cro##'ane. 5he view 'ort 4 ed b( the cro##'ane can a# o have a 4 er-de,ined border. 63i t action are hand#ed 4 in! 3i tSe#ection3i tener. J4i%t # urce 7 de im'ort *ava<. win!.OJ im'ort *ava<. win!.event.3i tSe#ection9ventJ im'ort *ava<. win!.event.3i tSe#ection3i tenerJ im'ort *ava.awt.OJ im'ort *ava.awt.event.OJ '4 '4b#ic c#a 63i tDemo e<tend 6%rame N

63i t #i tJ Strin!8: #i t+o#orName K N Pb#ackP, Pb#4eP, P!reenP, P(e##owP, PwhiteP QJ +o#or8: #i t+o#orVa#4e K N +o#or.E3)+H, +o#or.E3=9, +o#or.G"99N, +o#or.F933&W, +o#or.WC$59 QJ +ontainer content'aneJ '4b#ic 63i tDemo7; N 4'er7P3i t So4rce DemoP;J content'ane K !et+ontentPane7;J content'ane. et3a(o4t7new %#ow3a(o4t7;;J #i t K new 63i t7#i t+o#orName ;J #i t. etSe#ected$nde<70;J #i t. etSe#ection2ode73i tSe#ection2ode#.S$NG39TS939+5$&N;J content'ane.add7new 6Scro##Pane7#i t;;J #i t.add3i tSe#ection3i tener7new 3i tSe#ection3i tener7; N '4b#ic void va#4e+han!ed73i tSe#ection9vent e; N content'ane. etEack!ro4nd7#i t+o#orVa#4e 8#i t .!etSe#ected$nde<7;:;J Q Q;J etSiBe7200, 200;J etVi ib#e7tr4e;J Q '4b#ic tatic void main7Strin!8: ar! ; N 63i tDemo te t K new 63i tDemo7;J te t. etDe,a4#t+#o e&'eration76%rame.9A$5T&NT+3&S9;J Q Q

+2+ 3imited i a S4b idiar( o, 5+S 3imited

1D/

&4t'4t Ja)a J4i%t 7la%% *ava<. win! +#a +#a 63i t *ava.#an!.&b*ect *ava.awt.+om'onent *ava.awt.+ontainer *ava<. win!.6+om'onent *ava<. win!.63i t )## $m'#emented $nter,ace : )cce ib#e, $ma!e&b erver, 2en4+ontainer, Scro##ab#e, Seria#iBab#e 63i t +on tr4ctor 63i t7;: +on tr4ct a 63i t with an em't( mode#. 63i t73i t2ode# data2ode#;: +on tr4ct a 63i t that di '#a( the e#ement in the 'eci,ied, non-n4## mode#. 63i t7&b*ect8: #i tData;: +on tr4ct a 63i t that di '#a( the e#ement in the 'eci,ied arra(. 63i t7Vector #i tData;: +on tr4ct a 63i t that di '#a( the e#ement in the 'eci,ied Vector. BJ+ Ja)a J*abbedPane cla%% e,a!"le J*abbedPane 6ava Swin! 54toria# 9<'#ainin! the 65abbedPane +om'onent. ) 65abbedPane contain a tab that can have a too# ti' and a mnemonic, and it can di '#a( both te<t and an ima!e. 5he ha'e o, a tab and the wa( in which the e#ected tab i di '#a(ed varie b( 3ook and %ee#. J*abbedPane # urce 7 de im'ort *ava<. win!.65abbedPaneJ im'ort *ava<. win!.$ma!e$conJ im'ort *ava<. win!.63abe#J im'ort *ava<. win!.6Pane#J im'ort *ava<. win!.6%rameJ im'ort *ava.awt.OJ im'ort *ava.awt.event.OJ '4b#ic c#a 65abbedPaneDemo e<tend 6Pane# N

'4b#ic 65abbedPaneDemo7; N $ma!e$con icon K new $ma!e$con7P*ava- win!-t4toria#.6PGP;J 65abbedPane *tb9<am'#e K new 65abbedPane7;J 6Pane# *'#$nnerPane#1 K create$nnerPane#7P5ab 1 +ontain 5oo#ti' and $conP;J *tb9<am'#e.add5ab7P&neP, icon, *'#$nnerPane#1, P5ab 1P;J *tb9<am'#e. etSe#ected$nde<70;J 6Pane# *'#$nnerPane#2 K create$nnerPane#7P5ab 2 +ontain $con on#(P;J *tb9<am'#e.add5ab7P5woP, icon, *'#$nnerPane#2;J 6Pane# *'#$nnerPane#1 K create$nnerPane#7P5ab 1 +ontain 5oo#ti' and $conP;J *tb9<am'#e.add5ab7P5hreeP, icon, *'#$nnerPane#1, P5ab 1P;J 6Pane# *'#$nnerPane#D K create$nnerPane#7P5ab D +ontain 5e<t on#(P;J *tb9<am'#e.add5ab7P%o4rP, *'#$nnerPane#D;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D0

GG )dd the tabbed 'ane to thi 'ane#. et3a(o4t7new Grid3a(o4t71, 1;;J add7*tb9<am'#e;J Q 'rotected 6Pane# create$nnerPane#7Strin! te<t; N 6Pane# *'#Pane# K new 6Pane#7;J 63abe# *#bDi '#a( K new 63abe#7te<t;J *#bDi '#a(. etCoriBonta#)#i!nment763abe#.+9N59";J *'#Pane#. et3a(o4t7new Grid3a(o4t71, 1;;J *'#Pane#.add7*#bDi '#a(;J ret4rn *'#Pane#J Q '4b#ic tatic void main7Strin!8: ar! ; N 6%rame ,rame K new 6%rame7P5abbedPane So4rce DemoP;J ,rame.addWindow3i tener7new Window)da'ter7; N '4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J Q Q;J ,rame.!et+ontentPane7;.add7new 65abbedPaneDemo7;, Eorder3a(o4t.+9N59";J ,rame. etSiBe7D00, 12.;J ,rame. etVi ib#e7tr4e;J Q Q &4t'4t

65abbedPane @4e tion When $ 4 e a 65abbedPane and want to #i ten to which tab i bein! c#icked, which #i terner ho4#d $ 4 e[ )n wer: +han!e3i tener Ja)a J*abbedPane 'ierarchy *ava<. win! +#a 65abbedPane *ava.#an!.&b*ect *ava.awt.+om'onent *ava.awt.+ontainer *ava<. win!.6+om'onent *ava<. win!.65abbedPane )## $m'#emented $nter,ace : )cce ib#e, $ma!e&b erver, 2en4+ontainer, Seria#iBab#e, Swin!+on tant J*abbedPane 7 n%truct r 65abbedPane7; +reate an em't( 5abbedPane with a de,a4#t tab '#acement o, 65abbedPane.5&P. 65abbedPane7int tabP#acement; +reate an em't( 5abbedPane with the 'eci,ied tab '#acement o, either: 65abbedPane.5&P, 65abbedPane.E&55&2, 65abbedPane.39%5, or 65abbedPane."$GC5. 65abbedPane7int tabP#acement, int tab3a(o4tPo#ic(; +reate an em't( 5abbedPane with the 'eci,ied tab '#acement and tab #a(o4t 'o#ic(. +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D8

BK+ Ja)a J* l6ar cla%% e,a!"le J* lbar 6ava Swin! 54toria# 9<'#ainin! the 65oo#Ear +om'onent. ) 65oo#bar contain a n4mber o, com'onent who e t('e i 4 4a##( ome kind o, b4tton which can a# o inc#4de e'arator to !ro4' re#ated com'onent within the too#bar. 5he too#bar can be docked a!ain t an( o, the ,o4r ed!e o, a container 7'ane# or a ,rame;. ) too#bar can a# o be made to ,#oat. 5oo#bar 4 e Eo<3a(o4t, which arran!e com'onent in one horiBonta# rowG vertica# co#4mn. 5hi #a(o4t mana!er doe not ,orce each com'onent to have the ame hei!ht or widthJ in tead, it 4 e their 're,erred hei!ht or width, and attem't to a#i!n them. Fo4 can ad*4 t the re 4#tin! a#i!nment b( ca##in! c#a +om'onent? method et)#i!nmentA7; andGor et)#i!nmentF7; on each com'onent. im'ort *ava<. win!.65oo#EarJ im'ort *ava<. win!.6E4ttonJ im'ort *ava<. win!.$ma!e$conJ im'ort *ava<. win!.6%rameJ im'ort *ava<. win!.65e<t)reaJ im'ort *ava<. win!.6Scro##PaneJ im'ort *ava<. win!.6Pane#J im'ort *ava<. win!.65e<t%ie#dJ im'or t *ava.awt.OJ im'ort *ava.awt.event.OJ '4b#ic c#a 65oo#EarDemo e<tend 6%rame N

'rotected 65e<t)rea te<t)reaJ 'rotected Strin! new#ine K PInPJ '4b#ic 65oo#EarDemo7; N 4'er7P5oo#EarDemoP;J GG +reate the too#bar. 65oo#Ear *tb2ain5oo#bar K new 65oo#Ear7;J GG et%#oatab#e7,a# e; to make the too#bar non movab#e addE4tton 7*tb2ain5oo#bar;J GG +reate the te<t area te<t)rea K new 65e<t)rea7., 10;J 6Scro##Pane * Pane K new 6Scro##Pane7te<t)rea;J GG 3a( o4t the content 'ane. 6Pane# *'#+ontentPane K new 6Pane#7;J *'#+ontentPane. et3a(o4t7new Eorder3a(o4t7;;J *'#+ontentPane. etPre,erredSiBe7new Dimen ion7D00, 100;;J *'#+ontentPane.add7*tb2ain5oo#bar, Eorder3a(o4t.N&"5C;J *'#+ontentPane.add7* Pane, Eorder3a(o4t.+9N59";J et+ontentPane7*'#+ontentPane;J Q '4b#ic void addE4tton 765oo#Ear *tb5oo#Ear; N 6E4tton *bn5oo#barE4tton K n4##J GG ,ir t b4tton *bn5oo#barE4tton K new 6E4tton7new $ma!e$con7P#e,t.!i,P;;J *bn5oo#barE4tton . et5oo#5i'5e<t7P#e,tP;J *bn5oo#barE4tton .add)ction3i tener7new )ction3i tener7; N '4b#ic void actionPer,ormed7)ction9vent e; N di '#a($n5e<t)rea7P5hi i 3e,t 5oo#bar E4tton "e'ortin!P;J Q Q;J *tb5oo#Ear.add7*bn5oo#barE4tton ;J GG 2nd b4tton *bn5oo#barE4tton K new 6E4tton7new $ma!e$con7Pri!ht.!i,P;;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D-

*bn5oo#barE4tton . et5oo#5i'5e<t7Pri!htP;J *bn5oo#barE4tton .add)ction3i tener7new )ction3i tener7; N '4b#ic void actionPer,ormed7)ction9vent e; N di '#a($n5e<t)rea7P5hi i ri!ht 5oo#bar E4tton "e'ortin!P;J Q Q;J *tb5oo#Ear.add7*bn5oo#barE4tton ;J *tb5oo#Ear.addSe'arator7;J GG 1rd b4tton *bn5oo#barE4tton K new 6E4tton7new $ma!e$con7Po'en.!i,P;;J *bn5oo#barE4tton . et5oo#5i'5e<t7Po'enP;J *bn5oo#barE4tton .add)ction3i tener7new )ction3i tener7; N '4b#ic void actionPer,ormed7)ction9vent e; N di '#a($n5e<t)rea7P5hi i o'en 5oo#bar E4tton "e'ortin!P;J Q Q;J *tb5oo#Ear.add7*bn5oo#barE4tton ;J GG Dth b4tton *bn5oo#barE4tton K new 6E4tton7new $ma!e$con7P ave.!i,P;;J *bn5oo#barE4tton . et5oo#5i'5e<t7P aveP;J *bn5oo#barE4tton .add)ction3i tener7new )ction3i tener7; N '4b#ic void actionPer,ormed7)ction9vent e; N di '#a($n5e<t)rea7P5hi i ave 5oo#bar E4tton "e'ortin!P;J Q Q;J *tb5oo#Ear.add7*bn5oo#barE4tton ;J GG We can add e'arator to !ro4' imi#ar com'onent *tb5oo#Ear.addSe'arator7;J GG ,o4rth b4tton *bn5oo#barE4tton K new 6E4tton7P5e<t b4ttonP;J *bn5oo#barE4tton .add)ction3i tener7new )ction3i tener7; N '4b#ic void actionPer,ormed7)ction9vent e; N di '#a($n5e<t)rea7P5e<t b4ttonP;J Q Q;J *tb5oo#Ear.add7*bn5oo#barE4tton ;J GG ,i,th com'onent i N&5 a b4ttonW 65e<t%ie#d *t,E4tton K new 65e<t%ie#d7P5e<t ,ie#dP;J *t,E4tton. et9ditab#e7,a# e;J *t,E4tton.add)ction3i tener7new )ction3i tener7; N '4b#ic void actionPer,ormed7)ction9vent e; N di '#a($n5e<t)rea7P5e<t%ie#d com'onent can a# o be '#acedP;J Q Q;J *tb5oo#Ear.add7*t,E4tton;J Q 'rotected void di '#a($n5e<t)rea7Strin! actionDe cri'tion; N te<t)rea.a''end7actionDe cri'tion > new#ine;J Q '4b#ic tatic void main7Strin!8: ar! ; N 65oo#EarDemo *t,5oo#bar K new 65oo#EarDemo7;J GG 9<tend %rame. *t,5oo#bar.'ack7;J *t,5oo#bar.addWindow3i tener7new Window)da'ter7; N +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.0

'4b#ic void window+#o in!7Window9vent e; N S( tem.e<it70;J Q Q;J *t,5oo#bar. etVi ib#e7tr4e;J Q Q &4t'4t

Ja)a J* l6ar 'ierarchy *ava<. win! +#a 65oo#Ear *ava.#an!.&b*ect *ava.awt.Eorder3a(o4t )## $m'#emented $nter,ace : 3a(o4t2ana!er, 3a(o4t2ana!er2, Seria#iBab#e J* l6ar 7 n%truct r 65oo#Ear7;: +reate a new too# barJ orientation de,a4#t to C&"$S&N5)3. 65oo#Ear7int orientation;: +reate a new too# bar with the 'eci,ied orientation. 65oo#Ear7Strin! name;: +reate a new too# bar with the 'eci,ied name. 65oo#Ear7Strin! name, int orientation;: +reate a new too# bar with a 'eci,ied name and orientation. B2+ Ja)a Fl w4ay ut cla%% e,a!"le Fl w4ay ut 6ava Swin! 54toria# 9<'#ainin! the %#ow3a(o4t. %#ow3a(o4t when 4 ed arran!e win! com'onent ,rom #e,t to ri!ht 4nti# there? no more 'ace avai#ab#e. 5hen it be!in a new row be#ow it and move ,rom #e,t to ri!ht a!ain. 9ach com'onent in a %#ow3a(o4t !et a m4ch 'ace a it need and no more. %#ow3a(o4t So4rce +ode im'ort *ava.awt.+om'onent&rientationJ im'ort *ava.awt.+ontainerJ im'ort *ava.awt.Dimen ionJ im'ort *ava.awt.%#ow3a(o4tJ im'ort *ava<. im'ort *ava<. im'ort *ava<. im'ort *ava<. im'ort *ava<. win!.6E4ttonJ win!.6+heckEo<J win!.6%rameJ win!.63abe#J win!.65e<t%ie#dJ

'4b#ic c#a %#ow3a(o4tDemo N '4b#ic tatic boo#ean "$GC5T5&T39%5 K ,a# eJ '4b#ic tatic void add+om'onent 7+ontainer contentPane; N i, 7"$GC5T5&T39%5; N contentPane. et+om'onent&rientation7 +om'onent&rientation."$GC5T5&T39%5;J Q contentPane. et3a(o4t7new %#ow3a(o4t7;;J contentPane.add7new 63abe#7P63abe# 1P;;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.1

contentPane.add7new 6E4tton7P6E4tton 2P;;J contentPane.add7new 6+heckEo<7P6+heckEo< 1P;;J contentPane.add7new 65e<t%ie#d7P3on!-Named 65e<t%ie#d DP;;J contentPane.add7new 6E4tton7P6E4tton .P;;J Q 'rivate tatic void create)ndShowG=$7; N 6%rame. etDe,a4#t3ook)nd%ee#Decorated7tr4e;J 6%rame ,rame K new 6%rame7P%#ow3a(o4t So4rce DemoP; N '4b#ic Dimen ion !et2inim4mSiBe7; N Dimen ion 're,SiBe K !etPre,erredSiBe7;J ret4rn new Dimen ion7100, 're,SiBe.hei!ht;J Q QJ ,rame. etDe,a4#t+#o e&'eration76%rame.9A$5T&NT+3&S9;J GGSet 4' the content 'ane and com'onent in %#ow3a(o4t add+om'onent 7,rame.!et+ontentPane7;;J ,rame.'ack7;J ,rame. etVi ib#e7tr4e;J Q '4b#ic tatic void main7Strin!8: ar! ; N *ava<. win!.Swin!=ti#itie .invoke3ater7new "4nnab#e7; N '4b#ic void r4n7; N create)ndShowG=$7;J Q Q;J Q Q

&4t'4t BL+ Ja)a 6 rder4ay ut cla%% e,a!"le 6 rder4ay ut 6ava Swin! 54toria# 9<'#ainin! the Eorder3a(o4t . Eorder3a(o4t '#ace win! com'onent in the North, So4th, 9a t, We t and center o, a container. )## e<tra 'ace i '#aced in the center area. Fo4 can add horiBonta# and vertica# !a' between the area . 9ver( content 'ane i initia#iBed to 4 e a Eorder3a(o4t. +om'onent are added to a Eorder3a(o4t b( 4 in! the add method. 6%rame` content 'ane de,a4#t #a(o4t mana!er: Eorder3a(o4t. $n Eorder3a(o4t, a com'onent` 'o ition i 'eci,ied b( a econd ar!4ment to add. 6 rder4ay ut # urce 7 de GO O Eorder3a(o4tDemo.*ava i a 1.D a''#ication that re@4ire no other ,i#e . OG im'ort *ava.awt.Eorder3a(o4tJ im'ort *ava.awt.+ontainerJ im'ort *ava.awt.Dimen ionJ im'ort *ava<. win!.6E4ttonJ im'ort *ava<. win!.6%rameJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.2

im'ort *ava<. win!.63abe#J '4b#ic c#a Eorder3a(o4tDemo N '4b#ic tatic boo#ean "$GC5T5&T39%5 K ,a# eJ '4b#ic tatic void add+om'onent 5oPane7+ontainer contentPane; N = e Eorder3a(o4t. De,a4#t em't( con tr4ctor with no horiBonta# and vertica# !a' contentPane. et3a(o4t7new Eorder3a(o4t7.,.;;J i, 7W7contentPane.!et3a(o4t7; in tanceo, Eorder3a(o4t;; N contentPane.add7new 63abe#7P+ontainer doe n?t 4 e Eorder3a(o4tWP;;J ret4rnJ Q i, 7"$GC5T5&T39%5; N contentPane. et+om'onent&rientation7 *ava.awt.+om'onent&rientation."$GC5T5&T39%5;J Q 6E4tton *bnSam'#eE4tton K new 6E4tton7PE4tton 1 7P)G9TS5)"5;P;J contentPane.add7*bnSam'#eE4tton , Eorder3a(o4t.P)G9TS5)"5;J *bnSam'#eE4tton K new 6E4tton7PE4tton 2 7+9N59";P;J *bnSam'#eE4tton . etPre,erredSiBe7new Dimen ion7200, 100;;J contentPane.add7*bnSam'#eE4tton , Eorder3a(o4t.+9N59";J *bnSam'#eE4tton K new 6E4tton7PE4tton 1 73$N9TS5)"5;P;J contentPane.add7*bnSam'#eE4tton , Eorder3a(o4t.3$N9TS5)"5;J *bnSam'#eE4tton K new 6E4tton7P3on!-Named E4tton D 7P)G9T9ND;P;J contentPane.add7*bnSam'#eE4tton , Eorder3a(o4t.P)G9T9ND;J *bnSam'#eE4tton K new 6E4tton7P. 73$N9T9ND;P;J contentPane.add7*bnSam'#eE4tton , Eorder3a(o4t.3$N9T9ND;J Q 'rivate tatic void create)ndShowG=$7; N 6%rame. etDe,a4#t3ook)nd%ee#Decorated7tr4e;J 6%rame ,rame K new 6%rame7PEorder3a(o4t So4rce DemoP;J ,rame. etDe,a4#t+#o e&'eration76%rame.9A$5T&NT+3&S9;J GGSet 4' the content 'ane and add win! com'onent to it add+om'onent 5oPane7,rame.!et+ontentPane7;;J ,rame.'ack7;J ,rame. etVi ib#e7tr4e;J Q '4b#ic tatic void main7Strin!8: ar! ; N *ava<. win!.Swin!=ti#itie .invoke3ater7new "4nnab#e7; N +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.1

GG GG

'4b#ic void r4n7; N create)ndShowG=$7;J Q Q;J Q Q ut"ut

BM+ Ja)a Crid4ay ut cla%% e,a!"le Crid4ay ut 6ava Swin! 54toria# 9<'#ainin! the Grid3a(o4t. Grid3a(o4t i a #a(o4t mana!er that #a( o4t a container? com'onent in a rectan!4#ar !rid. 5he container i divided into e@4a#- iBed rectan!#e , and one com'onent i '#aced in each rectan!#e. Crid4ay ut # urce 7 de im'ort *ava.awt.OJ im'ort *ava<. win!.OJ '4b#ic c#a Grid3a(o4tDemo N '4b#ic ,ina# tatic boo#ean "$GC5T5&T39%5 K ,a# eJ '4b#ic tatic void add+om'onent 5oPane7+ontainer contentPane; N i, 7"$GC5T5&T39%5; N contentPane. et+om'onent&rientation7 +om'onent&rientation."$GC5T5&T39%5;J Q )n( n4mber o, row and 2 co#4mn contentPane. et3a(o4t7new Grid3a(o4t70,2;;J contentPane.add7new 63abe#7P63abe# 1P;;J contentPane.add7new 6E4tton7P6E4tton 2P;;J contentPane.add7new 6+heckEo<7P6+heckEo< 1P;;J contentPane.add7new 65e<t%ie#d7P3on!-Named 65e<t%ie#d DP;;J contentPane.add7new 6E4tton7P6E4tton .P;;J Q 'rivate tatic void create)ndShowG=$7; N 6%rame. etDe,a4#t3ook)nd%ee#Decorated7tr4e;J 6%rame ,rame K new 6%rame7PGrid3a(o4t So4rce DemoP;J ,rame. etDe,a4#t+#o e&'eration76%rame.9A$5T&NT+3&S9;J GGSet 4' the content 'ane and com'onent in Grid3a(o4t add+om'onent 5oPane7,rame.!et+ontentPane7;;J ,rame.'ack7;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.D

GG

,rame. etVi ib#e7tr4e;J Q '4b#ic tatic void main7Strin!8: ar! ; N *ava<. win!.Swin!=ti#itie .invoke3ater7new "4nnab#e7; N '4b#ic void r4n7; N create)ndShowG=$7;J Q Q;J Q Q &4t'4t

Ja)a Crid4ay ut 'ierarchy *ava<. win! +#a Grid3a(o4t *ava.#an!.&b*ect *ava.awt.Grid3a(o4t )## $m'#emented $nter,ace : 3a(o4t2ana!er, Seria#iBab#e Crid4ay ut 7 n%truct r Grid3a(o4t7;: +reate a !rid #a(o4t with a de,a4#t o, one co#4mn 'er com'onent, in a in!#e row. Grid3a(o4t7int row , int co# ;: +reate a !rid #a(o4t with the 'eci,ied n4mber o, row and co#4mn . Grid3a(o4t7int row , int co# , int h!a', int v!a';: +reate a !rid #a(o4t with the 'eci,ied n4mber o, row and co#4mn . B3+ Ja)a Crid6a(4ay ut cla%% e,a!"le Crid6a(4ay ut 6ava Swin! 54toria# 9<'#ainin! the GridEa!3a(o4t. GridEa!3a(o4t i a #a(o4t mana!er that #a( o4t a container? com'onent in a !rid o, ce## with each com'onent occ4'(in! one or more ce## , ca##ed it di '#a( area. 5he di '#a( area a#i!n com'onent vertica##( and horiBonta##(, witho4t re@4irin! that the com'onent be o, the ame iBe. Crid6a(4ay ut # urce 7 de im'ort *ava.awt.OJ im'ort *ava<. win!.6E4ttonJ im'ort *ava<. win!.6+omboEo<J im'ort *ava<. win!.6%rameJ im'ort *ava<. win!.65e<t%ie#dJ '4b#ic c#a GridEa!3a(o4tDemo N

'4b#ic tatic void add+om'onent 5oPane7+ontainer 'ane; N 6E4tton *bnE4ttonJ 'ane. et3a(o4t7new GridEa!3a(o4t7;;J GridEa!+on traint !E+ K new GridEa!+on traint 7;J !E+.,i## K GridEa!+on traint .C&"$S&N5)3J *bnE4tton K new 6E4tton7PE4tton 1P;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 1..

!E+.wei!ht< K 0..J !E+.!rid< K 0J !E+.!rid( K 0J 'ane.add7*bnE4tton, !E+;J 65e<t%ie#d *t, K new 65e<t%ie#d7P5e<t%ie#d 1P;J !E+.!rid< K 2J !E+.!rid( K 0J *t,. et9ditab#e7,a# e;J 'ane.add7*t,, !E+;J *bnE4tton K new 6E4tton7PE4tton 1P;J !E+.!rid< K 2J !E+.!rid( K 0J 'ane.add7*bnE4tton, !E+;J *bnE4tton K new 6E4tton7PE4tton DP;J !E+.i'ad( K D0J GG5hi com'onent ha more breadth com'ared to other b4tton !E+.wei!ht< K 0.0J !E+.!ridwidth K 1J !E+.!rid< K 0J !E+.!rid( K 1J 'ane.add7*bnE4tton, !E+;J 6+omboEo< *cmbSam'#e K new 6+omboEo<7new Strin!8:NP+omboEo< 1P, PhiP, Phe##oPQ;J !E+.i'ad( K 0J !E+.wei!ht( K 1.0J !E+.anchor K GridEa!+on traint .P)G9T9NDJ !E+.in et K new $n et 710,0,0,0;J GGPaddin! !E+.!rid< K 1J !E+.!ridwidth K 2J !E+.!rid( K 2J 'ane.add7*cmbSam'#e, !E+;J Q 'rivate tatic void create)ndShowG=$7; N 6%rame. etDe,a4#t3ook)nd%ee#Decorated7tr4e;J 6%rame ,rame K new 6%rame7PGridEa!3a(o4t So4rce DemoP;J ,rame. etDe,a4#t+#o e&'eration76%rame.9A$5T&NT+3&S9;J GGSet 4' the content 'ane. add+om'onent 5oPane7,rame.!et+ontentPane7;;J ,rame.'ack7;J ,rame. etVi ib#e7tr4e;J Q '4b#ic tatic void main7Strin!8: ar! ; N *ava<. win!.Swin!=ti#itie .invoke3ater7new "4nnab#e7; N '4b#ic void r4n7; N create)ndShowG=$7;J Q Q;J Q Q &4t'4t +2+ 3imited i a S4b idiar( o, 5+S 3imited 1./

),ter 9<'andin! the %rame

Crid6a(4ay ut 7la%% *ava<. win! +#a GridEa!3a(o4t *ava.#an!.&b*ect *ava.awt.GridEa!3a(o4t )## $m'#emented $nter,ace : 3a(o4t2ana!er, 3a(o4t2ana!er2, Seria#iBab#e Crid6a(4ay ut 7 n%truct r GridEa!3a(o4t7;: +reate a !rid ba! #a(o4t mana!er.. B1+ Ja)a J&enu cla%% e,a!"le J&enu 6ava Swin! 54toria# 9<'#ainin! the 62en4Ear +om'onent. Swin! 'rovide 4''ort ,or '4##-down and 'o'4' men4 . ) 62en4bar can contain evera# 62en4 ? . 9ach o, the 62en4 ? can contain a erie o, 62en4$tem ? that (o4 can e#ect. Cow 2en4? )re +reated[ 1. %ir t, ) 62en4bar i created 2. 5hen, we attach a## o, the men4 to thi 62en4bar. 1. 5hen we add 62en4$tem ? to the 62en4 ? . D. 5he 62en4bar i then added to the ,rame. E( de,a4#t, each 62en4$tem added to a 62en4 i enab#ed _that i , it can be e#ected. $n certain it4ation , we ma( need to di ab#e a 62en4$tem. 5hi i done b( ca##in! et9nab#ed7;. 5he et9nab#ed7; method a# o a##ow com'onent to be enab#ed. +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.0

J&enu # urce 7 de im'ort *ava.awt.OJ im'ort *ava.awt.event.OJ im'ort *ava<. win!.62en4J im'ort *ava<. win!.62en4$temJ im'ort *ava<. win!.6+heckEo<2en4$temJ im'ort *ava<. win!.6"adioE4tton2en4$temJ im'ort *ava<. win!.E4ttonGro4'J im'ort *ava<. win!.62en4EarJ im'ort *ava<. win!.He(StrokeJ im'ort *ava<. win!.$ma!e$conJ im'ort *ava<. win!.6Pane#J im'ort *ava<. win!.65e<t)reaJ im'ort *ava<. win!.6Scro##PaneJ im'ort *ava<. win!.6%rameJ GG= ed )ction 3i tner ,or 62en4$tem X 6"adioE4tton2en4$tem GG= ed $tem 3i tner ,or 6+heckEo<2en4$tem '4b#ic c#a 62en4Demo im'#ement )ction3i tener, $tem3i tener N 65e<t)rea *t)rea&4t'4tJ 6Scro##Pane * 'PaneJ '4b#ic 62en4Ear create62en4Ear7; N 62en4Ear main2en4EarJ 62en4 men41, men42, 4bmen4J 62en4$tem '#ain5e<t2en4$tem, te<t$con2en4$tem, icon2en4$tem, 4b2en4$temJ 6"adioE4tton2en4$tem rb2en4$temJ 6+heckEo<2en4$tem cb2en4$temJ $ma!e$con icon K create$ma!e$con7P*men4.*'!P;J main2en4Ear K new 62en4Ear7;J men41 K new 62en47P2en4 1P;J men41. et2nemonic7He(9vent.VHT2;J main2en4Ear.add7men41;J GG +reatin! the 2en4$tem '#ain5e<t2en4$tem K new 62en4$tem7P2en4 item with P#ain 5e<tP, He(9vent.VHT5;J GG can be done either wa( ,or a i!nin! hortc4t GG men4$tem. et2nemonic7He(9vent.VHT5;J GG )cce#erator , o,,er ke(board hortc4t to b('a navi!atin! the men4 GG hierarch(. '#ain5e<t2en4$tem. et)cce#erator7He(Stroke.!etHe(Stroke7 He(9vent.VHT1, )ction9vent.)35T2)SH;;J '#ain5e<t2en4$tem.add)ction3i tener7thi ;J men41.add7'#ain5e<t2en4$tem;J te<t$con2en4$tem K new 62en4$tem7P2en4 $tem with 5e<t X $ma!eP, icon;J te<t$con2en4$tem. et2nemonic7He(9vent.VHTE;J te<t$con2en4$tem.add)ction3i tener7thi ;J men41.add7te<t$con2en4$tem;J GG 2en4 $tem with *4 t an $ma!e icon2en4$tem K new 62en4$tem7icon;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.8

icon2en4$tem. et2nemonic7He(9vent.VHTD;J icon2en4$tem.add)ction3i tener7thi ;J men41.add7icon2en4$tem;J men41.addSe'arator7;J GG "adio E4tton 2en4 item ,o##ow a e'erator E4ttonGro4' itemGro4' K new E4ttonGro4'7;J rb2en4$tem K new 6"adioE4tton2en4$tem7 P2en4 $tem with "adio E4ttonP;J rb2en4$tem. etSe#ected7tr4e;J rb2en4$tem. et2nemonic7He(9vent.VHT";J itemGro4'.add7rb2en4$tem;J rb2en4$tem.add)ction3i tener7thi ;J men41.add7rb2en4$tem;J rb2en4$tem K new 6"adioE4tton2en4$tem7 P2en4 $tem 2 with "adio E4ttonP;J itemGro4'.add7rb2en4$tem;J rb2en4$tem.add)ction3i tener7thi ;J men41.add7rb2en4$tem;J men41.addSe'arator7;J GG "adio E4tton 2en4 item ,o##ow a e'erator cb2en4$tem K new 6+heckEo<2en4$tem7P2en4 $tem with check bo<P;J cb2en4$tem. et2nemonic7He(9vent.VHT+;J cb2en4$tem.add$tem3i tener7thi ;J men41.add7cb2en4$tem;J cb2en4$tem K new 6+heckEo<2en4$tem7P2en4 $tem 2 with check bo<P;J cb2en4$tem.add$tem3i tener7thi ;J men41.add7cb2en4$tem;J men41.addSe'arator7;J GG S4b 2en4 ,o##ow a e'erator 4bmen4 K new 62en47PS4b 2en4P;J 4bmen4. et2nemonic7He(9vent.VHTS;J 4b2en4$tem K new 62en4$tem7PS4b 2en4$tem 1P;J 4b2en4$tem. et)cce#erator7He(Stroke.!etHe(Stroke7He(9vent.VHT2, )ction9vent.+5"3T2)SH;;J 4b2en4$tem.add)ction3i tener7thi ;J 4bmen4.add7 4b2en4$tem;J 4b2en4$tem K new 62en4$tem7PS4b 2en4$tem 2P;J 4bmen4.add7 4b2en4$tem;J 4b2en4$tem.add)ction3i tener7thi ;J men41.add7 4bmen4;J GG E4i#d econd men4 in the men4 bar. men42 K new 62en47P2en4 2P;J men42. et2nemonic7He(9vent.VHTN;J main2en4Ear.add7men42;J ret4rn main2en4EarJ Q '4b#ic +ontainer create+ontentPane7; N GG +reate the content-'ane-to-be. 6Pane# *'#+ontentPane K new 6Pane#7new Eorder3a(o4t7;;J *'#+ontentPane. et3a(o4t7new Eorder3a(o4t7;;JGG +an do it either wa( GG to et #a(o4t +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.-

*'#+ontentPane. et&'a@4e7tr4e;J GG +reate a cro##ed te<t area. *t)rea&4t'4t K new 65e<t)rea7., 10;J *t)rea&4t'4t. et9ditab#e7,a# e;J * 'Pane K new 6Scro##Pane7*t)rea&4t'4t;J GG )dd the te<t area to the content 'ane. *'#+ontentPane.add7* 'Pane, Eorder3a(o4t.+9N59";J ret4rn *'#+ontentPaneJ Q GOO "et4rn an $ma!e$con, or n4## i, the 'ath wa inva#id. OG 'rotected tatic $ma!e$con create$ma!e$con7Strin! 'ath; N *ava.net.="3 im!="3 K 62en4Demo.c#a .!et"e o4rce7'ath;J i, 7im!="3 WK n4##; N ret4rn new $ma!e$con7im!="3;J Q e# e N S( tem.err.'rint#n7P+o4#dn?t ,ind ima!e ,i#e: P > 'ath;J ret4rn n4##J Q Q 'rivate tatic void createG=$7; N 6%rame. etDe,a4#t3ook)nd%ee#Decorated7tr4e;J GG +reate and et 4' the window. 6%rame ,rame K new 6%rame7P62en4 = a!e DemoP;J ,rame. etDe,a4#t+#o e&'eration76%rame.9A$5T&NT+3&S9;J 62en4Demo a'' K new 62en4Demo7;J ,rame. et62en4Ear7a''.create62en4Ear7;;J ,rame. et+ontentPane7a''.create+ontentPane7;;J ,rame. etSiBe7.00, 100;J ,rame. etVi ib#e7tr4e;J Q '4b#ic void actionPer,ormed7)ction9vent e; N 62en4$tem o4rce K 762en4$tem; 7e.!etSo4rce7;;J Strin! K P2en4 $tem o4rce: P > o4rce.!et5e<t7; > P 7an in tance o, P > !et+#a Name7 o4rce; > P;PJ *t)rea&4t'4t.a''end7 > PInP;J *t)rea&4t'4t. et+aretPo ition7*t)rea&4t'4t.!etDoc4ment7; .!et3en!th7;;J Q '4b#ic void itemState+han!ed7$tem9vent e; N 62en4$tem o4rce K 762en4$tem; 7e.!etSo4rce7;;J Strin! K P2en4 $tem o4rce: P > o4rce.!et5e<t7; > P 7an in tance o, P > !et+#a Name7 o4rce; > P;P > PInP > P State o, check Eo<: P > 77e.!etState+han!e7; KK $tem9vent.S939+59D; [ P e#ectedP : P4n e#ectedP;J *t)rea&4t'4t.a''end7 > PInP;J *t)rea&4t'4t. et+aretPo ition7*t)rea&4t'4t.!etDoc4ment7; +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/0

.!et3en!th7;;J Q GG "et4rn the c#a name, no 'acka!e in,o 'rotected Strin! !et+#a Name7&b*ect o; N Strin! c#a Strin! K o.!et+#a 7;.!etName7;J int dot$nde< K c#a Strin!.#a t$nde<&,7P.P;J ret4rn c#a Strin!. 4b trin!7dot$nde< > 1;J GG "et4rn on#( +#a Q '4b#ic tatic void main7Strin!8: ar! ; N *ava<. win!.Swin!=ti#itie .invoke3ater7new "4nnab#e7; N '4b#ic void r4n7; N createG=$7;J Q Q;J Q Q Out"ut

name

JD67 Intr ducti n What i% JD67? 6DE+ tand ,or Java Databa e 7onnectivit(, which i a tandard 6ava )P$ ,or databa e-inde'endent connectivit( between the 6ava 'ro!rammin! #an!4a!e and a wide ran!e o, databa e . 5he 6DE+ #ibrar( inc#4de )P$ ,or each o, the ta k common#( a ociated with databa e 4 a!e: 2akin! a connection to a databa e +reatin! S]3 or 2(S]3 tatement 9<ec4tin! that S]3 or 2(S]3 @4erie in the databa e Viewin! X 2odi,(in! the re 4#tin! record %4ndamenta##(, 6DE+ i a 'eci,ication that 'rovide a com'#ete et o, inter,ace that a##ow ,or 'ortab#e acce to an 4nder#(in! databa e. 6ava can be 4 ed to write di,,erent t('e o, e<ec4tab#e , 4ch a : 6ava )''#ication 6ava )''#et 6ava Serv#et 6ava ServerPa!e 76SP ; 9nter'ri e 6avaEean 796E ; )## o, the e di,,erent e<ec4tab#e are ab#e to 4 e a 6DE+ driver to acce a databa e and take advanta!e o, the tored data. +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/1

6DE+ 'rovide the ame ca'abi#itie a &DE+, a##owin! 6ava 'ro!ram to contain databa einde'endent code. Pre$Re@ui%ite: Ee,ore 'ro!re in! on thi t4toria# (o4 need to have !ood 4nder tandin! on the ,o##owin! two 4b*ect : +ore 6)V) Pro!rammin! S]3 or 2(S]3 Databa e JD67 Architecture: 5he 6DE+ )P$ 4''ort both two-tier and three-tier 'roce in! mode# ,or databa e acce b4t in !enera# 6DE+ )rchitect4re con i t o, two #a(er : JD67 API: 5hi 'rovide the a''#ication-to-6DE+ 2ana!er connection. JD67 Dri)er API: 5hi 4''ort the 6DE+ 2ana!er-to-Driver +onnection. 5he 6DE+ )P$ 4 e a driver mana!er and databa e- 'eci,ic driver to 'rovide tran 'arent connectivit( to hetero!eneo4 databa e . 5he 6DE+ driver mana!er en 4re that the correct driver i 4 ed to acce each data o4rce. 5he driver mana!er i ca'ab#e o, 4''ortin! m4#ti'#e conc4rrent driver connected to m4#ti'#e hetero!eneo4 databa e . %o##owin! i the architect4ra# dia!ram, which how the #ocation o, the driver mana!er with re 'ect to the 6DE+ driver and the 6ava a''#ication:

7 !! n JD67 7 !" nent%: 5he 6DE+ )P$ 'rovide the ,o##owin! inter,ace and c#a e : Dri)er&ana(er: 5hi c#a mana!e a #i t o, databa e driver . 2atche connection re@4e t ,rom the *ava a''#ication with the 'ro'er databa e driver 4 in! comm4nication 4b'rotoco#. 5he ,ir t driver that reco!niBe a certain 4b'rotoco# 4nder 6DE+ wi## be 4 ed to e tab#i h a databa e +onnection. Dri)er: 5hi inter,ace hand#e the comm4nication with the databa e erver. Fo4 wi## interact direct#( with Driver ob*ect ver( rare#(. $n tead, (o4 4 e Driver2ana!er ob*ect , which mana!e ob*ect o, thi t('e. $t a# o ab tract the detai# a ociated with workin! with Driver ob*ect 7 nnecti n : 5hi inter,ace with a## method ,or contactin! a databa e. 5he connection ob*ect re're ent comm4nication conte<t, i.e., a## comm4nication with databa e i thro4!h connection ob*ect on#(. #tate!ent : Fo4 4 e ob*ect created ,rom thi inter,ace to 4bmit the S]3 tatement to the databa e. Some derived inter,ace acce't 'arameter in addition to e<ec4tin! tored 'roced4re . Re%ult#et: 5he e ob*ect ho#d data retrieved ,rom a databa e a,ter (o4 e<ec4te an S]3 @4er( 4 in! Statement ob*ect . $t act a an iterator to a##ow (o4 to move thro4!h it data. #N4E,ce"ti n: 5hi c#a hand#e an( error that occ4r in a databa e a''#ication. *he JD67 K+. Pac=a(e% 5he *ava. @# and *ava<. @# are the 'rimar( 'acka!e ,or 6DE+ D.0. 5hi i the #ate t 6DE+ ver ion at the time o, writin! thi t4toria#. $t o,,er the main c#a e ,or interactin! with (o4r data o4rce . +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/2

5he new ,eat4re in the e 'acka!e inc#4de chan!e in the ,o##owin! area : )4tomatic databa e driver #oadin! 9<ce'tion hand#in! im'rovement 9nhanced E3&EG+3&E ,4nctiona#it( +onnection and tatement inter,ace enhancement Nationa# character et 4''ort S]3 "&W$D acce S]3 2001 A23 data t('e 4''ort )nnotation JD67 $ #N4 #ynta, #tr4ct4red N4er( 4an!4a!e 7S]3; i a tandardiBed #an!4a!e that a##ow (o4 to 'er,orm o'eration on a databa e, 4ch a creatin! entrie , readin! content, 4'datin! content, and de#etin! entrie . S]3 i 4''orted b( a## mo t an( databa e (o4 wi## #ike#( 4 e, and it a##ow (o4 to write databa e code inde'endent#( o, the 4nder#(in! databa e. 5hi t4toria# !ive an overview o, S]3, which i a 're-re@4i ite to 4nder tand 6DE+ conce't . 5hi t4toria# !ive (o4 eno4!h S]3 to be ab#e to 7reate, Read, 5'date, and De#ete 7o,ten re,erred to a 7R5D o'eration ; data ,rom a databa e. %or a detai#ed 4nder tandin! on S]3, (o4 can read o4r 2(S]3 54toria#. 7reate Databa%e: 5he +"9)59 D)5)E)S9 tatement i 4 ed ,or creatin! a new databa e. 5he (nta< i : S]3R +"9)59 D)5)E)S9 D)5)E)S9TN)29J E,a!"le: 5he ,o##owin! S]3 tatement create a Databa e named 92P: S]3R +"9)59 D)5)E)S9 92PJ Dr " Databa%e: 5he D"&P D)5)E)S9 tatement i 4 ed ,or de#etin! an e<i tin! databa e. 5he (nta< i : S]3R D"&P D)5)E)S9 D)5)E)S9TN)29J N te: 5o create or dro' a databa e (o4 ho4#d have admini trator 'rivi#e!e on (o4r databa e erver. Ee care,4#, de#etin! a databa e wo4#d #o a## the data tored in databa e. 7reate *able: 5he +"9)59 5)E39 tatement i 4 ed ,or creatin! a new tab#e. 5he (nta< i : S]3R +"9)59 5)E39 tab#eTname 7 co#4mnTname co#4mnTdataTt('e, co#4mnTname co#4mnTdataTt('e, co#4mnTname co#4mnTdataTt('e ... ;J E,a!"le: 5he ,o##owin! S]3 tatement create a tab#e named 9m'#o(ee with ,o4r co#4mn : S]3R +"9)59 5)E39 9m'#o(ee 7 id $N5 N&5 N=33, a!e $N5 N&5 N=33, ,ir t V)"+C)"72..;, #a t V)"+C)"72..;, P"$2)"F H9F 7 id ; ;J Dr " *able: 5he D"&P 5)E39 tatement i 4 ed ,or de#etin! an e<i tin! tab#e. 5he (nta< i : S]3R D"&P 5)E39 tab#eTnameJ E,a!"le: 5he ,o##owin! S]3 tatement de#ete a tab#e named 9m'#o(ee : S]3R D"&P 5)E39 9m'#o(ee J IN#ER* Data: 5he (nta< ,or $NS9"5 #ook imi#ar to the ,o##owin!, where co#4mn1, co#4mn2, and o on re're ent the new data to a''ear in the re 'ective co#4mn : S]3R $NS9"5 $N5& tab#eTname V)3=9S 7co#4mn1, co#4mn2, ...;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/1

E,a!"le: 5he ,o##owin! S]3 $NS9"5 tatement in ert a new row in the 9m'#o(ee databa e created ear#ier: S]3R $NS9"5 $N5& 9m'#o(ee V)3=9S 7100, 18, ?Sara?, ?)#i?;J #E4E7* Data: 5he S939+5 tatement i 4 ed to retrieve data ,rom a databa e. 5he (nta< ,or S939+5 i : S]3R S939+5 co#4mnTname, co#4mnTname, ... %"&2 tab#eTname WC9"9 condition J 5he WC9"9 c#a4 e can 4 e the com'ari on o'erator 4ch a K, WK, V, R, VK,and RK, a we## a the E95W99N and 3$H9 o'erator . E,a!"le: 5he ,o##owin! S]3 tatement e#ect the a!e, ,ir t and #a t co#4mn ,rom the 9m'#o(ee tab#e where id co#4mn i 100: S]3R S939+5 ,ir t, #a t, a!e %"&2 9m'#o(ee WC9"9 id K 100J 5he ,o##owin! S]3 tatement e#ect the a!e, ,ir t and #a t co#4mn ,rom the 9m'#o(ee tab#e where )irst co#4mn contain 7ara: S]3R S939+5 ,ir t, #a t, a!e %"&2 9m'#o(ee WC9"9 ,ir t 3$H9 ?LSaraL?J 5PDA*E Data: 5he =PD)59 tatement i 4 ed to 4'date data. 5he (nta< ,or =PD)59 i : S]3R =PD)59 tab#eTname S95 co#4mnTname K va#4e, co#4mnTname K va#4e, ... WC9"9 condition J 5he WC9"9 c#a4 e can 4 e the com'ari on o'erator 4ch a K, WK, V, R, VK,and RK, a we## a the E95W99N and 3$H9 o'erator . E,a!"le: 5he ,o##owin! S]3 =PD)59 tatement chan!e the a!e co#4mn o, the em'#o(ee who e id i 100: S]3R =PD)59 9m'#o(ee S95 a!eK20 WC9"9 idK100J DE4E*E Data: 5he D93959 tatement i 4 ed to de#ete data ,rom tab#e . 5he (nta< ,or D93959 i : S]3R D93959 %"&2 tab#eTname WC9"9 condition J 5he WC9"9 c#a4 e can 4 e the com'ari on o'erator 4ch a K, WK, V, R, VK,and RK, a we## a the E95W99N and 3$H9 o'erator . E,a!"le: 5he ,o##owin! S]3 D93959 tatement de#ete the record o, the em'#o(ee who e id i 100: S]3R D93959 %"&2 9m'#o(ee WC9"9 idK100J JD67 $ #a!"le, E,a!"le 7 de 5hi t4toria# 'rovide an e<am'#e o, how to create a im'#e 6DE+ a''#ication. 5hi wi## how (o4 how to o'en a databa e connection, e<ec4te a S]3 @4er(, and di '#a( the re 4#t . )## the te' mentioned in thi tem'#ate e<am'#e, wo4#d be e<'#ained in 4b e@4ent cha'ter o, thi t4toria#. 7reatin( JD67 A""licati n: 5here are ,o##owin! i< te' invo#ved in b4i#din! a 6DE+ a''#ication: I!" rt the "ac=a(e% + "e@4ire that (o4 inc#4de the 'acka!e containin! the 6DE+ c#a e needed ,or databa e 'ro!rammin!. 2o t o,ten, 4 in! import java#s6l#8 wi## 4,,ice. Re(i%ter the JD67 dri)er + "e@4ire that (o4 initia#iBe a driver o (o4 can o'en a comm4nication channe# with the databa e. O"en a c nnecti n + "e@4ire 4 in! the 9riverMa$ager#getCo$$ectio$ ! method to create a +onnection ob*ect, which re're ent a 'h( ica# connection with the databa e. E,ecute a @uery + "e@4ire 4 in! an ob*ect o, t('e Statement ,or b4i#din! and 4bmittin! an S]3 tatement to the databa e. E,tract data fr ! re%ult %et + "e@4ire that (o4 4 e the a''ro'riate ,esult.et#get::: ! method to retrieve the data ,rom the re 4#t et. 7lean u" the en)ir n!ent + "e@4ire e<'#icit#( c#o in! a## databa e re o4rce ver 4 re#(in! on the 6V2? !arba!e co##ection. +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/D

#a!"le 7 de: 5hi am'#e e<am'#e can erve a a te!"late when (o4 need to create (o4r own 6DE+ a''#ication in the ,4t4re. 5hi am'#e code ha been written ba ed on the environment and databa e et4' done in 'revio4 cha'ter. +o'( and 'a t ,o##owin! e<am'#e in %ir t9<am'#e.*ava, com'i#e and r4n a ,o##ow : GGS59P 1. $m'ort re@4ired 'acka!e im'ort *ava. @#.OJ '4b#ic c#a %ir t9<am'#e N GG 6DE+ driver name and databa e ="3 tatic ,ina# Strin! 6DE+TD"$V9" K Pcom.m( @#.*dbc.DriverPJ tatic ,ina# Strin! DET="3 K P*dbc:m( @#:GG#oca#ho tG92PPJ GG Databa e credentia# tatic ,ina# Strin! =S9" K P4 ernamePJ tatic ,ina# Strin! P)SS K P'a wordPJ '4b#ic tatic void main7Strin!8: ar! ; N +onnection conn K n4##J Statement tmt K n4##J tr(N GGS59P 2: "e!i ter 6DE+ driver +#a .,orName7Pcom.m( @#.*dbc.DriverP;J GGS59P 1: &'en a connection S( tem.o4t.'rint#n7P+onnectin! to databa e...P;J conn K Driver2ana!er.!et+onnection7DET="3,=S9",P)SS;J GGS59P D: 9<ec4te a @4er( S( tem.o4t.'rint#n7P+reatin! tatement...P;J tmt K conn.createStatement7;J Strin! @#J @# K PS939+5 id, ,ir t, #a t, a!e %"&2 9m'#o(ee PJ "e 4#tSet r K tmt.e<ec4te]4er(7 @#;J GGS59P .: 9<tract data ,rom re 4#t et whi#e7r .ne<t7;;N GG"etrieve b( co#4mn name int id K r .!et$nt7PidP;J int a!e K r .!et$nt7Pa!eP;J Strin! ,ir t K r .!etStrin!7P,ir tP;J Strin! #a t K r .!etStrin!7P#a tP;J GGDi '#a( va#4e S( tem.o4t.'rint7P$D: P > id;J S( tem.o4t.'rint7P, )!e: P > a!e;J S( tem.o4t.'rint7P, %ir t: P > ,ir t;J S( tem.o4t.'rint#n7P, 3a t: P > #a t;J Q GGS59P /: +#ean-4' environment r .c#o e7;J tmt.c#o e7;J conn.c#o e7;J Qcatch7S]39<ce'tion e;N GGCand#e error ,or 6DE+ e.'rintStack5race7;J Qcatch79<ce'tion e;N GGCand#e error ,or +#a .,orName +2+ 3imited i a S4b idiar( o, 5+S 3imited

1/.

e.'rintStack5race7;J Q,ina##(N GG,ina##( b#ock 4 ed to c#o e re o4rce tr(N i,7 tmtWKn4##; tmt.c#o e7;J Qcatch7S]39<ce'tion e2;N QGG nothin! we can do tr(N i,7connWKn4##; conn.c#o e7;J Qcatch7S]39<ce'tion e;N e.'rintStack5race7;J QGGend ,ina##( tr( QGGend tr( S( tem.o4t.'rint#n7PGoodb(eWP;J QGGend main QGGend %ir t9<am'#e Now #et 4 com'i#e above e<am'#e a ,o##ow : +:IR*avac %ir t9<am'#e.*ava +:IR When (o4 r4n Fir%tE,a!"le, it 'rod4ce ,o##owin! re 4#t: +:IR*ava %ir t9<am'#e +onnectin! to databa e... +reatin! tatement... $D: 100, )!e: 18, %ir t: Sara, 3a t: )#i $D: 101, )!e: 2., %ir t: 2ahnaB, 3a t: %atma $D: 102, )!e: 10, %ir t: Said, 3a t: Hhan $D: 101, )!e: 28, %ir t: S4mit, 3a t: 2itta# +:IR JD67 $ Dri)er *y"e% What i% JD67 Dri)er ? 6DE+ driver im'#ement the de,ined inter,ace in the 6DE+ )P$ ,or interactin! with (o4r databa e erver. %or e<am'#e, 4 in! 6DE+ driver enab#e (o4 to o'en databa e connection and to interact with it b( endin! S]3 or databa e command then receivin! re 4#t with 6ava. 5he Java#s6l 'acka!e that hi' with 6DH contain vario4 c#a e with their behavio4r de,ined and their act4a# im'#ementaion are done in third-'art( driver . 5hird 'art( vendor im'#ement the java#s6l#9river inter,ace in their databa e driver. JD67 Dri)er% *y"e%: 6DE+ driver im'#ementation var( beca4 e o, the wide variet( o, o'eratin! ( tem and hardware '#at,orm in which 6ava o'erate . S4n ha divided the im'#ementation t('e into ,o4r cate!orie , 5('e 1, 2, 1, and D, which i e<'#ained be#ow: *y"e B: JD67$OD67 6rid(e Dri)er: $n a 5('e 1 driver, a 6DE+ brid!e i 4 ed to acce &DE+ driver in ta##ed on each c#ient machine. = in! &DE+ re@4ire con,i!4rin! on (o4r ( tem a Data So4rce Name 7DSN; that re're ent the tar!et databa e. When 6ava ,ir t came o4t, thi wa a 4 e,4# driver beca4 e mo t databa e on#( 4''orted &DE+ acce b4t now thi t('e o, driver i recommended on#( ,or e<'erimenta# 4 e or when no other a#ternative i avai#ab#e.

+2+ 3imited i a S4b idiar( o, 5+S 3imited

1//

5he 6DE+-&DE+ brid!e that come with 6DH 1.2 i a !ood e<am'#e o, thi kind o, driver. *y"e -: JD67$Nati)e API: $n a 5('e 2 driver, 6DE+ )P$ ca## are converted into native +G+>> )P$ ca## which are 4ni@4e to the databa e. 5he e driver t('ica##( 'rovided b( the databa e vendor and 4 ed in the ame manner a the 6DE+-&DE+ Erid!e, the vendor- 'eci,ic driver m4 t be in ta##ed on each c#ient machine. $, we chan!e the Databa e we have to chan!e the native )P$ a it i 'eci,ic to a databa e and the( are mo t#( ob o#ete now b4t (o4 ma( rea#iBe ome 'eed increa e with a 5('e 2 driver, beca4 e it e#iminate &DE+? overhead.

5he &rac#e +a## $nter,ace 7&+$; driver i an e<am'#e o, a 5('e 2 driver. *y"e J: JD67$Net "ure Ja)a: $n a 5('e 1 driver, a three-tier a''roach i 4 ed to acce in! databa e . 5he 6DE+ c#ient 4 e tandard network ocket to comm4nicate with an midd#eware a''#ication erver. 5he ocket in,ormation i then tran #ated b( the midd#eware a''#ication erver into the ca## ,ormat re@4ired b( the DE2S, and ,orwarded to the databa e erver. 5hi kind o, driver i e<treme#( ,#e<ib#e, ince it re@4ire no code in ta##ed on the c#ient and a in!#e driver can act4a##( 'rovide acce to m4#ti'#e databa e .

+2+ 3imited i a S4b idiar( o, 5+S 3imited

1/0

Fo4 can think o, the a''#ication erver a a 6DE+ P'ro<(,P meanin! that it make ca## ,or the c#ient a''#ication. ) a re 4#t, (o4 need ome know#ed!e o, the a''#ication erver? con,i!4ration in order to e,,ective#( 4 e thi driver t('e. Fo4r a''#ication erver mi!ht 4 e a 5('e 1, 2, or D driver to comm4nicate with the databa e, 4nder tandin! the n4ance wi## 'rove he#',4#. *y"e K: B..O "ure Ja)a: $n a 5('e D driver, a '4re 6ava-ba ed driver that comm4nicate direct#( with vendor? databa e thro4!h ocket connection. 5hi i the hi!he t 'er,ormance driver avai#ab#e ,or the databa e and i 4 4a##( 'rovided b( the vendor it e#,. 5hi kind o, driver i e<treme#( ,#e<ib#e, (o4 don?t need to in ta## 'ecia# o,tware on the c#ient or erver. %4rther, the e driver can be down#oaded d(namica##(.

2(S]3? +onnectorG6 driver i a 5('e D driver. Eeca4 e o, the 'ro'rietar( nat4re o, their network 'rotoco# , databa e vendor 4 4a##( 4''#( t('e D driver . Which Dri)er %h uld be u%ed? $, (o4 are acce in! one t('e o, databa e, 4ch a &rac#e, S(ba e, or $E2, the 're,erred driver t('e i D. $, (o4r 6ava a''#ication i acce in! m4#ti'#e t('e o, databa e at the ame time, t('e 1 i the 're,erred driver. 5('e 2 driver are 4 e,4# in it4ation where a t('e 1 or t('e D driver i not avai#ab#e (et ,or (o4r databa e. 5he t('e 1 driver i not con idered a de'#o(ment-#eve# driver and i t('ica##( 4 ed ,or deve#o'ment and te tin! '4r'o e on#(. +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/8

JD67 Databa%e 7 nnecti n% ),ter (o4?ve in ta##ed the a''ro'riate driver, it? time to e tab#i h a databa e connection 4 in! 6DE+. 5he 'ro!rammin! invo#ved to e tab#i h a 6DE+ connection i ,air#( im'#e. Cere are the e im'#e ,o4r te' : I!" rt JD67 Pac=a(e%: )dd i!" rt tatement to (o4r 6ava 'ro!ram to im'ort re@4ired c#a e in (o4r 6ava code. Re(i%ter JD67 Dri)er: 5hi te' ca4 e the 6V2 to #oad the de ired driver im'#ementation into memor( o it can ,4#,i## (o4r 6DE+ re@4e t . Databa%e 5R4 F r!ulati n: 5hi i to create a 'ro'er#( ,ormatted addre that 'oint to the databa e to which (o4 wi h to connect. 7reate 7 nnecti n Object: %ina##(, code a ca## to the 9riverMa$ager ob*ect? getCo$$ectio$ ! method to e tab#i h act4a# databa e connection. I!" rt JD67 Pac=a(e%: 5he I!" rt tatement te## the 6ava com'i#er where to ,ind the c#a e (o4 re,erence in (o4r code and are '#aced at the ver( be!innin! o, (o4r o4rce code. 5o 4 e the tandard 6DE+ 'acka!e, which a##ow (o4 to e#ect, in ert, 4'date, and de#ete data in S]3 tab#e , add the ,o##owin! imports to (o4r o4rce code: im'ort *ava. @#.O J GG ,or tandard 6DE+ 'ro!ram im'ort *ava.math.O J GG ,or Ei!Decima# and Ei!$nte!er 4''ort Re(i%ter JD67 Dri)er: Fo4 m4 t re!i ter the (o4r driver in (o4r 'ro!ram be,ore (o4 4 e it. "e!i terin! the driver i the 'roce b( which the &rac#e driver? c#a ,i#e i #oaded into memor( o it can be 4ti#iBed a an im'#ementation o, the 6DE+ inter,ace . Fo4 need to do thi re!i tration on#( once in (o4r 'ro!ram. Fo4 can re!i ter a driver in one o, two wa( . A""r ach 8I; $ 7la%%+f rNa!e8;: 5he mo t common a''roach to re!i ter a driver i to 4 e 6ava? 7la%%+f rNa!e8; method to d(namica##( #oad the driver? c#a ,i#e into memor(, which a4tomatica##( re!i ter it. 5hi method i 're,erab#e beca4 e it a##ow (o4 to make the driver re!i tration con,i!4rab#e and 'ortab#e. 5he ,o##owin! e<am'#e 4 e +#a .,orName7 ; to re!i ter the &rac#e driver: tr( N +#a .,orName7Porac#e.*dbc.driver.&rac#eDriverP;J Q catch7+#a Not%o4nd9<ce'tion e<; N S( tem.o4t.'rint#n7P9rror: 4nab#e to #oad driver c#a WP;J S( tem.e<it71;J Q Fo4 can 4 e (etIn%tance8; method to work aro4nd noncom'#iant 6V2 , b4t then (o4?## have to code ,or two e<tra 9<ce'tion a ,o##ow : tr( N +#a .,orName7Porac#e.*dbc.driver.&rac#eDriverP;.new$n tance7;J Q catch7+#a Not%o4nd9<ce'tion e<; N S( tem.o4t.'rint#n7P9rror: 4nab#e to #oad driver c#a WP;J S( tem.e<it71;J catch7$##e!a#)cce 9<ce'tion e<; N S( tem.o4t.'rint#n7P9rror: acce 'rob#em whi#e #oadin!WP;J S( tem.e<it72;J catch7$n tantiation9<ce'tion e<; N S( tem.o4t.'rint#n7P9rror: 4nab#e to in tantiate driverWP;J S( tem.e<it71;J Q A""r ach 8II; $ Dri)er&ana(er+re(i%terDri)er8;: 5he econd a''roach (o4 can 4 e to re!i ter a driver i to 4 e the tatic Dri)er&ana(er+re(i%terDri)er8; method. Fo4 ho4#d 4 e the register9river ! method i, (o4 are 4 in! a non-6DH com'#iant 6V2, 4ch a the one 'rovided b( 2icro o,t. 5he ,o##owin! e<am'#e 4 e re!i terDriver7; to re!i ter the &rac#e driver: tr( N +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/-

Driver m(Driver K new orac#e.*dbc.driver.&rac#eDriver7;J Driver2ana!er.re!i terDriver7 m(Driver ;J Q catch7+#a Not%o4nd9<ce'tion e<; N S( tem.o4t.'rint#n7P9rror: 4nab#e to #oad driver c#a WP;J S( tem.e<it71;J Q Databa%e 5R4 F r!ulati n: ),ter (o4?ve #oaded the driver, (o4 can e tab#i h a connection 4 in! the Dri)er&ana(er+(et7 nnecti n8; method. %or ea ( re,erence, #et me #i t the three over#oaded Driver2ana!er.!et+onnection7; method : !et+onnection7Strin! 4r#; !et+onnection7Strin! 4r#, Pro'ertie 'ro'; !et+onnection7Strin! 4r#, Strin! 4 er, Strin! 'a word; Cere each ,orm re@4ire a databa e 5R4. ) databa e ="3 i an addre that 'oint to (o4r databa e. %orm4#atin! a databa e ="3 i where mo t o, the 'rob#em a ociated with e tab#i hin! a connection occ4r. %o##owin! tab#e #i t down 'o'4#ar 6DE+ driver name and databa e ="3. RD6&# JD67 dri)er na!e 5R4 f r!at 2(S]3 com.m( @#.*dbc.Driver jdbc:!y%@l://ho tnameG databa eName jdbc: racle:thin:Pho tname:'ort &")+39 orac#e.*dbc.driver.&rac#eDriver N4mber:databa eName +&2.ibm.db2.*dbc.net.DE2Driv DE2 jdbc:db-:ho tname:'ort N4mberGdataba eName er jdbc:%yba%e:*d%:ho tname: 'ort S(ba e com. (ba e.*dbc.S(bDriver N4mberGdataba eName )## the hi!h#i!hted 'art in ="3 ,ormat i tatic and (o4 need to chan!e on#( remainin! 'art a 'er (o4r databa e et4'. 7reate 7 nnecti n Object: 5%in( a databa%e 5R4 with a u%erna!e and "a%%w rd: $ #i ted down three ,orm o, Dri)er&ana(er+(et7 nnecti n8; method to create a connection ob*ect. 5he mo t common#( 4 ed ,orm o, !et+onnection7; re@4ire (o4 to 'a a databa e ="3, a user$ame, and a pass2ord: ) 4min! (o4 are 4 in! &rac#e? thin driver, (o4?## 'eci,( a ho t:'ort:databa eName va#4e ,or the databa e 'ortion o, the ="3. $, (o4 have a ho t at 5+PG$P addre 1-2.0.0.1 with a ho t name o, amrood, and (o4r &rac#e #i tener i con,i!4red to #i ten on 'ort 1.21, and (o4r databa e name i 92P, then com'#ete databa e ="3 wo4#d then be: *dbc:orac#e:thin:^amrood:1.21:92P Now (o4 have to ca## !et+onnection7; method with a''ro'riate 4 ername and 'a word to !et a 7 nnecti n ob*ect a ,o##ow : Strin! ="3 K P*dbc:orac#e:thin:^amrood:1.21:92PPJ Strin! =S9" K P4 ernamePJ Strin! P)SS K P'a wordP +onnection conn K Driver2ana!er.!et+onnection7="3, =S9", P)SS;J 5%in( nly a databa%e 5R4: ) econd ,orm o, the Driver2ana!er.!et+onnection7 ; method re@4ire on#( a databa e ="3: Driver2ana!er.!et+onnection7Strin! 4r#;J Cowever, in thi ca e, the databa e ="3 inc#4de the 4 ername and 'a word and ha the ,o##owin! !enera# ,orm: *dbc:orac#e:driver:4 ernameG'a word^databa e So the above connection can be created a ,o##ow : Strin! ="3 K P*dbc:orac#e:thin:4 ernameG'a word^amrood:1.21:92PPJ +onnection conn K Driver2ana!er.!et+onnection7="3;J 5%in( a databa%e 5R4 and a Pr "ertie% bject: ) third ,orm o, the Driver2ana!er.!et+onnection7 ; method re@4ire a databa e ="3 and a Pro'ertie ob*ect: +2+ 3imited i a S4b idiar( o, 5+S 3imited 100

Driver2ana!er.!et+onnection7Strin! 4r#, Pro'ertie in,o;J ) Pro'ertie ob*ect ho#d a et o, ke(word-va#4e 'air . $t? 4 ed to 'a driver 'ro'ertie to the driver d4rin! a ca## to the !et+onnection7; method. 5o make the ame connection made b( the 'revio4 e<am'#e , 4 e the ,o##owin! code: im'ort *ava.4ti#.OJ Strin! ="3 K P*dbc:orac#e:thin:^amrood:1.21:92PPJ Pro'ertie in,o K new Pro'ertie 7 ;J in,o.'4t7 P4 erP, P4 ernameP ;J in,o.'4t7 P'a wordP, P'a wordP ;J +onnection conn K Driver2ana!er.!et+onnection7="3, in,o;J 7l %in( JD67 c nnecti n%: )t the end o, (o4r 6DE+ 'ro!ram, it i re@4ired e<'#icit#( c#o e a## the connection to the databa e to end each databa e e ion. Cowever, i, (o4 ,or!et, 6ava? !arba!e co##ector wi## c#o e the connection when it c#ean 4' ta#e ob*ect . "e#(in! on !arba!e co##ection, e 'ecia##( in databa e 'ro!rammin!, i ver( 'oor 'ro!rammin! 'ractice. Fo4 ho4#d make a habit o, a#wa( c#o in! the connection with the c#o e7; method a ociated with connection ob*ect. 5o en 4re that a connection i c#o ed, (o4 co4#d 'rovide a ,ina##( b#ock in (o4r code. ) )i$ally b#ock a#wa( e<ec4te , re!ard#e i, an e<ce'tion occ4r or not. 5o c#o e above o'ened connection (o4 ho4#d ca## c#o e7; method a ,o##ow : conn.c#o e7;J 9<'#icit#( c#o in! a connection con erve DE2S re o4rce , which wi## make (o4r databa e admini trator ha''(. %or a better 4nder tandin!, $ wo4#d 4!!e t to t4d( o4r 6DE+ - Sam'#e +ode. JD67 $ #tate!ent%, Pre"ared#tate!ent and 7allable#tate!ent &nce a connection i obtained we can interact with the databa e. 5he 6DE+ .tateme$t4 Callable.tateme$t4 and Prepared.tateme$t inter,ace de,ine the method and 'ro'ertie that enab#e (o4 to end S]3 or P3GS]3 command and receive data ,rom (o4r databa e. 5he( a# o de,ine method that he#' brid!e data t('e di,,erence between 6ava and S]3 data t('e 4 ed in a databa e. %o##owin! tab#e 'rovide a 4mmar( o, each inter,ace? '4r'o e to 4nder tand how do (o4 decide which inter,ace to 4 e[ Interface% Rec !!ended 5%e = e ,or !enera#-'4r'o e acce to (o4r databa e. = e,4# when Statement (o4 are 4 in! tatic S]3 tatement at r4ntime. 5he Statement inter,ace cannot acce't 'arameter . = e when (o4 '#an to 4 e the S]3 tatement man( time . 5he Pre'aredStatement Pre'aredStatement inter,ace acce't in'4t 'arameter at r4ntime. = e when (o4 want to acce databa e tored 'roced4re . 5he +a##ab#eStatement +a##ab#eStatement inter,ace can a# o acce't r4ntime in'4t 'arameter . *he #tate!ent Object%: 7reatin( #tate!ent Object: Ee,ore (o4 can 4 e a Statement ob*ect to e<ec4te a S]3 tatement, (o4 need to create one 4 in! the +onnection ob*ect? createStatement7 ; method, a in the ,o##owin! e<am'#e: Statement tmt K n4##J tr( N tmt K conn.createStatement7 ;J ... Q catch 7S]39<ce'tion e; N ... Q ,ina##( N ... Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 101

&nce (o4?ve created a Statement ob*ect, (o4 can then 4 e it to e<ec4te a S]3 tatement with one o, it three e<ec4te method . b lean e,ecute8#trin( #N4; : "et4rn a boo#ean va#4e o, tr4e i, a "e 4#tSet ob*ect can be retrievedJ otherwi e, it ret4rn ,a# e. = e thi method to e<ec4te S]3 DD3 tatement or when (o4 need to 4 e tr4#( d(namic S]3. int e,ecute5"date8#trin( #N4; : "et4rn the n4mber o, row a,,ected b( the e<ec4tion o, the S]3 tatement. = e thi method to e<ec4te S]3 tatement ,or which (o4 e<'ect to !et a n4mber o, row a,,ected - ,or e<am'#e, an $NS9"5, =PD)59, or D93959 tatement. Re%ult#et e,ecuteNuery8#trin( #N4; : "et4rn a "e 4#tSet ob*ect. = e thi method when (o4 e<'ect to !et a re 4#t et, a (o4 wo4#d with a S939+5 tatement. 7l %in( #tate!ent Obeject: 64 t a (o4 c#o e a +onnection ob*ect to ave databa e re o4rce , ,or the ame rea on (o4 ho4#d a# o c#o e the Statement ob*ect. ) im'#e ca## to the c#o e7; method wi## do the *ob. $, (o4 c#o e the +onnection ob*ect ,ir t it wi## c#o e the Statement ob*ect a we##. Cowever, (o4 ho4#d a#wa( e<'#icit#( c#o e the Statement ob*ect to en 4re 'ro'er c#ean4'. Statement tmt K n4##J tr( N tmt K conn.createStatement7 ;J ... Q catch 7S]39<ce'tion e; N ... Q ,ina##( N tmt.c#o e7;J Q %or a better 4nder tandin!, $ wo4#d 4!!e t to t4d( Statement. *he Pre"ared#tate!ent Object%: 5he Prepared.tateme$t inter,ace e<tend the Statement inter,ace which !ive (o4 added ,4nctiona#it( with a co4'#e o, advanta!e over a !eneric Statement ob*ect. 5hi tatement !ive (o4 the ,#e<ibi#it( o, 4''#(in! ar!4ment d(namica##(. 7reatin( Pre"ared#tate!ent Object: Pre'aredStatement ' tmt K n4##J tr( N Strin! S]3 K P='date 9m'#o(ee S95 a!e K [ WC9"9 id K [PJ ' tmt K conn.'re'areStatement7S]3;J ... Q catch 7S]39<ce'tion e; N ... Q ,ina##( N ... Q )## 'arameter in 6DE+ are re're ented b( the ? (mbo#, which i known a the 'arameter marker. Fo4 m4 t 4''#( va#4e ,or ever( 'arameter be,ore e<ec4tin! the S]3 tatement. 5he %et0008; method bind va#4e to the 'arameter , where 000 re're ent the 6ava data t('e o, the va#4e (o4 wi h to bind to the in'4t 'arameter. $, (o4 ,or!et to 4''#( the va#4e , (o4 wi## receive an S]39<ce'tion. 9ach 'arameter marker i re,erred to b( it ordina# 'o ition. 5he ,ir t marker re're ent 'o ition 1, the ne<t 'o ition 2, and o ,orth. 5hi method di,,er ,rom that o, 6ava arra( indice , which tart at 0. )## o, the #tate!ent bjectI% method ,or interactin! with the databa e 7a; e<ec4te7;, 7b; e<ec4te]4er(7;, and 7c; e<ec4te='date7; a# o work with the Pre'aredStatement ob*ect. Cowever, the method are modi,ied to 4 e S]3 tatement that can take in'4t the 'arameter . 7l %in( Pre"ared#tate!ent Obeject: 64 t a (o4 c#o e a Statement ob*ect, ,or the ame rea on (o4 ho4#d a# o c#o e the Pre'aredStatement ob*ect. +2+ 3imited i a S4b idiar( o, 5+S 3imited 102

) im'#e ca## to the c#o e7; method wi## do the *ob. $, (o4 c#o e the +onnection ob*ect ,ir t it wi## c#o e the Pre'aredStatement ob*ect a we##. Cowever, (o4 ho4#d a#wa( e<'#icit#( c#o e the Pre'aredStatement ob*ect to en 4re 'ro'er c#ean4'. Pre'aredStatement ' tmt K n4##J tr( N Strin! S]3 K P='date 9m'#o(ee S95 a!e K [ WC9"9 id K [PJ ' tmt K conn.'re'areStatement7S]3;J ... Q catch 7S]39<ce'tion e; N ... Q ,ina##( N ' tmt.c#o e7;J Q %or a better 4nder tandin!, $ wo4#d 4!!e t to t4d(. *he 7allable#tate!ent Object%: 64 t a a +onnection ob*ect create the Statement and Pre'aredStatement ob*ect , it a# o create the +a##ab#eStatement ob*ect which wo4#d be 4 ed to e<ec4te a ca## to a databa e tored 'roced4re. 7reatin( 7allable#tate!ent Object: S4''o e, (o4 need to e<ec4te the ,o##owin! &rac#e tored 'roced4re: +"9)59 &" "9P3)+9 P"&+9D="9 !et9m'Name 792PT$D $N N=2E9", 92PT%$"S5 &=5 V)"+C)"; )S E9G$N S939+5 ,ir t $N5& 92PT%$"S5 %"&2 9m'#o(ee WC9"9 $D K 92PT$DJ 9NDJ NO*E: )bove tored 'roced4re ha been written ,or &rac#e, b4t we are workin! with 2(S]3 databa e o #et 4 write ame tored 'roced4re ,or 2(S]3 a ,o##ow to create it in 92P databa e: D93$2$59" MM D"&P P"&+9D="9 $% 9A$S5S \92P\.\!et9m'Name\ MM +"9)59 P"&+9D="9 \92P\.\!et9m'Name\ 7$N 92PT$D $N5, &=5 92PT%$"S5 V)"+C)"72..;; E9G$N S939+5 ,ir t $N5& 92PT%$"S5 %"&2 9m'#o(ee WC9"9 $D K 92PT$DJ 9ND MM D93$2$59" J 5hree t('e o, 'arameter e<i t: $N, &=5, and $N&=5. 5he Pre'aredStatement ob*ect on#( 4 e the $N 'arameter. 5he +a##ab#eStatement ob*ect can 4 e a## three. Cere are the de,inition o, each: Para!eter De%cri"ti n ) 'arameter who e va#4e i 4nknown when the S]3 tatement i $N created. Fo4 bind va#4e to $N 'arameter with the etAAA7; method . ) 'arameter who e va#4e i 4''#ied b( the S]3 tatement it &=5 ret4rn . Fo4 retrieve va#4e ,rom the&=5 'arameter with the !etAAA7; method . ) 'arameter that 'rovide both in'4t and o4t'4t va#4e . Fo4 bind $N&=5 variab#e with the etAAA7; method and retrieve va#4e with the !etAAA7; method . 5he ,o##owin! code ni''et how how to em'#o( the 7 nnecti n+"re"are7all8; method to in tantiate a 7allable#tate!ent ob*ect ba ed on the 'recedin! tored 'roced4re: +2+ 3imited i a S4b idiar( o, 5+S 3imited 101

+a##ab#eStatement c tmt K n4##J tr( N Strin! S]3 K PNca## !et9m'Name 7[, [;QPJ c tmt K conn.'re'are+a## 7S]3;J ... Q catch 7S]39<ce'tion e; N ... Q ,ina##( N ... Q 5he Strin! variab#e S]3 re're ent the tored 'roced4re, with 'arameter '#aceho#der . = in! +a##ab#eStatement ob*ect i m4ch #ike 4 in! Pre'aredStatement ob*ect . Fo4 m4 t bind va#4e to a## 'arameter be,ore e<ec4tin! the tatement, or (o4 wi## receive an S]39<ce'tion. $, (o4 have $N 'arameter , *4 t ,o##ow the ame r4#e and techni@4e that a''#( to a Pre'aredStatement ob*ectJ 4 e the etAAA7; method that corre 'ond to the 6ava data t('e (o4 are bindin!. When (o4 4 e &=5 and $N&=5 'arameter (o4 m4 t em'#o( an additiona# +a##ab#eStatement method, re!i ter&4tParameter7;. 5he re!i ter&4tParameter7; method bind the 6DE+ data t('e to the data t('e the tored 'roced4re i e<'ected to ret4rn. &nce (o4 ca## (o4r tored 'roced4re, (o4 retrieve the va#4e ,rom the &=5 'arameter with the a''ro'riate !etAAA7; method. 5hi method ca t the retrieved va#4e o, S]3 t('e to a 6ava data t('e. 7l %in( 7allable#tate!ent Obeject: 64 t a (o4 c#o e other Statement ob*ect, ,or the ame rea on (o4 ho4#d a# o c#o e the +a##ab#eStatement ob*ect. ) im'#e ca## to the c#o e7; method wi## do the *ob. $, (o4 c#o e the +onnection ob*ect ,ir t it wi## c#o e the +a##ab#eStatement ob*ect a we##. Cowever, (o4 ho4#d a#wa( e<'#icit#( c#o e the +a##ab#eStatement ob*ect to en 4re 'ro'er c#ean4'. +a##ab#eStatement c tmt K n4##J tr( N Strin! S]3 K PNca## !et9m'Name 7[, [;QPJ c tmt K conn.'re'are+a## 7S]3;J ... Q catch 7S]39<ce'tion e; N ... Q ,ina##( N c tmt.c#o e7;J Q %or a better 4nder tandin!, $ wo4#d 4!!e t to t4d( JD67 $ Re%ult #et% 5he S]3 tatement that read data ,rom a databa e @4er( ret4rn the data in a re 4#t et. 5he S939+5 tatement i the tandard wa( to e#ect row ,rom a databa e and view them in a re 4#t et. 5he java#s6l#,esult.et inter,ace re're ent the re 4#t et o, a databa e @4er(. ) "e 4#tSet ob*ect maintain a c4r or that 'oint to the c4rrent row in the re 4#t et. 5he term Pre 4#t etP re,er to the row and co#4mn data contained in a "e 4#tSet ob*ect. 5he method o, the "e 4#tSet inter,ace can be broken down into three cate!orie : Na)i(ati nal !eth d%: 4 ed to move the c4r or aro4nd. Cet !eth d%: 4 ed to view the data in the co#4mn o, the c4rrent row bein! 'ointed to b( the c4r or. 5"date !eth d%: 4 ed to 4'date the data in the co#4mn o, the c4rrent row. 5he 4'date can then be 4'dated in the 4nder#(in! databa e a we##. 5he c4r or i movab#e ba ed on the 'ro'ertie o, the "e 4#tSet. 5he e 'ro'ertie are de i!nated when the corre 'ondin! Statement that !enerated the "e 4#tSet i created. 6DE+ 'rovide ,o##owin! connection method to create tatement with de ired "e 4#tSet: create#tate!ent8int R#*y"e, int R#7 ncurrency;Q "re"are#tate!ent8#trin( #N4, int R#*y"e, int R#7 ncurrency;Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 10D

"re"are7all8#trin( %@l, int R#*y"e, int R#7 ncurrency;Q 5he ,ir t ar!4ment indicate the t('e o, a "e 4#tSet ob*ect and the econd ar!4ment i one o, two "e 4#tSet con tant ,or 'eci,(in! whether a re 4#t et i read-on#( or 4'datab#e. *y"e f Re%ult#et: 5he 'o ib#e "S5('e are !iven be#ow, $, (o4 do not 'eci,( an( "e 4#tSet t('e, (o4 wi## a4tomatica##( !et one that i 5FP9T%&"W)"DT&N3F. *y"e De%cri"ti n "e 4#tSet.5FP9T%&"W)"DT&N3F 5he c4r or can on#( move ,orward in the re 4#t et. 5he c4r or can cro## ,orward and backward , and "e 4#tSet.5FP9TS+"&33T$NS9NS$5$ the re 4#t et i not en itive to chan!e made b( V9 other to the databa e that occ4r a,ter the re 4#t et wa created. 5he c4r or can cro## ,orward and backward , and "e 4#tSet.5FP9TS+"&33TS9NS$5$V the re 4#t et i en itive to chan!e made b( other 9. to the databa e that occ4r a,ter the re 4#t et wa created. 7 ncurrency f Re%ult#et: 5he 'o ib#e "S+onc4rrenc( are !iven be#ow, $, (o4 do not 'eci,( an( +onc4rrenc( t('e, (o4 wi## a4tomatica##( !et one that i +&N+="T"9)DT&N3F. 7 ncurrency De%cri"ti n "e 4#tSet.+&N+="T"9)DT& +reate a read-on#( re 4#t et. 5hi i the N3F de,a4#t "e 4#tSet.+&N+="T=PD)5)E +reate an 4'dateab#e re 4#t et. 39 &4r a## the e<am'#e written o ,ar can be written a ,o##ow which initia#iBe a Statement ob*ect to create a ,orward-on#(, read on#( "e 4#tSet ob*ect: tr( N Statement tmt K conn.createStatement7 "e 4#tSet.5FP9T%&"W)"DT&N3F, "e 4#tSet.+&N+="T"9)DT&N3F;J Q catch79<ce'tion e<; N .... Q ,ina##( N .... Q Na)i(atin( a Re%ult #et: 5here are evera# method in the "e 4#tSet inter,ace that invo#ve movin! the c4r or, inc#4din!: #+N+ &eth d% A De%cri"ti n "ublic ) id bef reFir%t8; thr w% #N4E,ce"ti n 1 2ove the c4r or to *4 t be,ore the ,ir t row "ublic ) id after4a%t8; thr w% #N4E,ce"ti n 2 2ove the c4r or to *4 t a,ter the #a t row "ublic b lean fir%t8; thr w% #N4E,ce"ti n 1 2ove the c4r or to the ,ir t row "ublic ) id la%t8; thr w% #N4E,ce"ti n D 2ove the c4r or to the #a t row. "ublic b lean ab% lute8int r w; thr w% #N4E,ce"ti n . 2ove the c4r or to the 'eci,ied row "ublic b lean relati)e8int r w; thr w% #N4E,ce"ti n / 2ove the c4r or the !iven n4mber o, row ,orward or backward ,rom where it c4rrent#( i 'ointin!. 0 "ublic b lean "re)i u%8; thr w% #N4E,ce"ti n +2+ 3imited i a S4b idiar( o, 5+S 3imited 10.

2ove the c4r or to the 'revio4 row. 5hi method ret4rn ,a# e i, the 'revio4 row i o,, the re 4#t et "ublic b lean ne,t8; thr w% #N4E,ce"ti n 8 2ove the c4r or to the ne<t row. 5hi method ret4rn ,a# e i, there are no more row in the re 4#t et "ublic int (etR w8; thr w% #N4E,ce"ti n "et4rn the row n4mber that the c4r or i 'ointin! to. "ublic ) id ! )e* In%ertR w8; thr w% #N4E,ce"ti n 10 2ove the c4r or to a 'ecia# row in the re 4#t et that can be 4 ed to in ert a new row into the databa e. 5he c4rrent c4r or #ocation i remembered. "ublic ) id ! )e* 7urrentR w8; thr w% #N4E,ce"ti n 11 2ove the c4r or back to the c4rrent row i, the c4r or i c4rrent#( at the in ert rowJ otherwi e, thi method doe nothin! %or a better 4nder tandin!, $ wo4#d 4!!e t to t4d( Viewin( a Re%ult #et: 5he "e 4#tSet inter,ace contain doBen o, method ,or !ettin! the data o, the c4rrent row. 5here i a !et method ,or each o, the 'o ib#e data t('e , and each !et method ha two ver ion : &ne that take in a co#4mn name. &ne that take in a co#4mn inde<. %or e<am'#e, i, the co#4mn (o4 are intere ted in viewin! contain an int, (o4 need to 4 e one o, the !et$nt7; method o, "e 4#tSet: #+N+ &eth d% A De%cri"ti n "ublic int (etInt8#trin( c lu!nNa!e; thr w% #N4E,ce"ti n 1 "et4rn the int in the c4rrent row in the co#4mn named co#4mnName "ublic int (etInt8int c lu!nInde,; thr w% #N4E,ce"ti n 2 "et4rn the int in the c4rrent row in the 'eci,ied co#4mn inde<. 5he co#4mn inde< tart at 1, meanin! the ,ir t co#4mn o, a row i 1, the econd co#4mn o, a row i 2, and o on. Simi#ar#( there are !et method in the "e 4#tSet inter,ace ,or each o, the ei!ht 6ava 'rimitive t('e , a we## a common t('e 4ch a *ava.#an!.Strin!, *ava.#an!.&b*ect, and *ava.net.="3 5here are a# o method ,or !ettin! S]3 data t('e *ava. @#.Date, *ava. @#.5ime, *ava. @#.5imeStam', *ava. @#.+#ob, and *ava. @#.E#ob. +heck the doc4mentation ,or more in,ormation abo4t 4 in! the e S]3 data t('e . %or a better 4nder tandin!, $ wo4#d 4!!e t to t4d( 5"datin( a Re%ult #et: 5he "e 4#tSet inter,ace contain a co##ection o, 4'date method ,or 4'datin! the data o, a re 4#t et. ) with the !et method , there are two 4'date method ,or each data t('e: &ne that take in a co#4mn name. &ne that take in a co#4mn inde<. %or e<am'#e, to 4'date a Strin! co#4mn o, the c4rrent row o, a re 4#t et, (o4 wo4#d 4 e one o, the ,o##owin! 4'dateStrin!7; method : #+N+ &eth d% A De%cri"ti n "ublic ) id u"date#trin(8int c lu!nInde,, #trin( %; thr w% #N4E,ce"ti n 1 +han!e the Strin! in the 'eci,ied co#4mn to the va#4e o, . "ublic ) id u"date#trin(8#trin( c lu!nNa!e, #trin( %; thr w% #N4E,ce"ti n 2 Simi#ar to the 'revio4 method, e<ce't that the co#4mn i 'eci,ied b( it name in tead o, it inde<. 5here are 4'date method ,or the ei!ht 'rimitive data t('e , a we## a Strin!, &b*ect, ="3, and the S]3 data t('e in the *ava. @# 'acka!e. ='datin! a row in the re 4#t et chan!e the co#4mn o, the c4rrent row in the "e 4#tSet ob*ect, b4t not in the 4nder#(in! databa e. 5o 4'date (o4r chan!e to the row in the databa e, (o4 need to invoke one o, the ,o##owin! method . #+N+ &eth d% A De%cri"ti n "ublic ) id u"dateR w8; 1 ='date the c4rrent row b( 4'datin! the corre 'ondin! row in the databa e. +2+ 3imited i a S4b idiar( o, 5+S 3imited 10/

"ublic ) id deleteR w8; De#ete the c4rrent row ,rom the databa e "ublic ) id refre%hR w8; 1 "e,re he the data in the re 4#t et to re,#ect an( recent chan!e in the databa e. "ublic ) id cancelR w5"date%8; D +ance# an( 4'date made on the c4rrent row. "ublic ) id in%ertR w8; . $n ert a row into the databa e. 5hi method can on#( be invoked when the c4r or i 'ointin! to the in ert row. %or a better 4nder tandin!, $ wo4#d 4!!e t to t4d( JD67 $ Data *y"e% 5he 6DE+ driver convert the 6ava data t('e to the a''ro'riate 6DE+ t('e be,ore endin! it to the databa e. $t 4 e a de,a4#t ma''in! ,or mo t data t('e . %or e<am'#e, a 6ava int i converted to an S]3 $N59G9". De,a4#t ma''in! were created to 'rovide con i tenc( between driver . 5he ,o##owin! tab#e 4mmariBe the de,a4#t 6DE+ data t('e that the 6ava data t('e i converted to when (o4 ca## the etAAA7; method o, the Pre'aredStatement or +a##ab#eStatement ob*ect or the "e 4#tSet.4'dateAAA7; method. #N4 JD67/Ja)a %et000 u"date000 V)"+C)" *ava.#an!.Strin! etStrin! 4'dateStrin! +C)" *ava.#an!.Strin! etStrin! 4'dateStrin! 3&NGV)"+C)" *ava.#an!.Strin! etStrin! 4'dateStrin! E$5 boo#ean etEoo#ean 4'dateEoo#ean N=29"$+ *ava.math.Ei!Decima# etEi!Decima# 4'dateEi!Decima# 5$NF$N5 b(te etE(te 4'dateE(te S2)33$N5 hort etShort 4'dateShort $N59G9" int et$nt 4'date$nt E$G$N5 #on! et3on! 4'date3on! "9)3 ,#oat et%#oat 4'date%#oat %3&)5 ,#oat et%#oat 4'date%#oat D&=E39 do4b#e etDo4b#e 4'dateDo4b#e V)"E$N)"F b(te8 : etE(te 4'dateE(te E$N)"F b(te8 : etE(te 4'dateE(te D)59 *ava. @#.Date etDate 4'dateDate 5$29 *ava. @#.5ime et5ime 4'date5ime 5$29S5)2P *ava. @#.5ime tam' et5ime tam' 4'date5ime tam' +3&E *ava. @#.+#ob et+#ob 4'date+#ob E3&E *ava. @#.E#ob etE#ob 4'dateE#ob )"")F *ava. @#.)rra( et)"")F 4'date)"")F "9% *ava. @#."e, Set"e, 4'date"e, S5"=+5 *ava. @#.Str4ct SetStr4ct 4'dateStr4ct 6DE+ 1.0 ha enhanced 4''ort ,or E3&E, +3&E, )"")F, and "9% data t('e . 5he "e 4#tSet ob*ect now ha 4'dateE3&E7;, 4'date+3&E7;, 4'date)rra(7;, and 4'date"e,7; method that enab#e (o4 to direct#( mani'4#ate the re 'ective data on the erver. 5he etAAA7; and 4'dateAAA7; method enab#e (o4 to convert 'eci,ic 6ava t('e to 'eci,ic 6DE+ data t('e . 5he method , et&b*ect7; and 4'date&b*ect7;, enab#e (o4 to ma' a#mo t an( 6ava t('e to a 6DE+ data t('e. "e 4#tSet ob*ect 'rovide corre 'ondin! !etAAA7; method ,or each data t('e to retrieve co#4mn va#4e. 9ach method can be 4 ed with co#4mn name or b( it ordina# 'o ition. #N4 JD67/Ja)a %et000 (et000 V)"+C)" *ava.#an!.Strin! etStrin! !etStrin! +C)" *ava.#an!.Strin! etStrin! !etStrin! 3&NGV)"+C)" *ava.#an!.Strin! etStrin! !etStrin! E$5 boo#ean etEoo#ean !etEoo#ean N=29"$+ *ava.math.Ei!Decima# etEi!Decima# !etEi!Decima# +2+ 3imited i a S4b idiar( o, 5+S 3imited 100 2

5$NF$N5 b(te etE(te !etE(te S2)33$N5 hort etShort !etShort $N59G9" int et$nt !et$nt E$G$N5 #on! et3on! !et3on! "9)3 ,#oat et%#oat !et%#oat %3&)5 ,#oat et%#oat !et%#oat D&=E39 do4b#e etDo4b#e !etDo4b#e V)"E$N)"F b(te8 : etE(te !etE(te E$N)"F b(te8 : etE(te !etE(te D)59 *ava. @#.Date etDate !etDate 5$29 *ava. @#.5ime et5ime !et5ime 5$29S5)2P *ava. @#.5ime tam' et5ime tam' !et5ime tam' +3&E *ava. @#.+#ob et+#ob !et+#ob E3&E *ava. @#.E#ob etE#ob !etE#ob )"")F *ava. @#.)rra( et)"")F !et)"")F "9% *ava. @#."e, Set"e, !et"e, S5"=+5 *ava. @#.Str4ct SetStr4ct !etStr4ct Date A *i!e Data *y"e%: 5he *ava. @#.Date c#a ma' to the S]3 D)59 t('e, and the *ava. @#.5ime and *ava. @#.5ime tam' c#a e ma' to the S]3 5$29 and S]3 5$29S5)2P data t('e , re 'ective#(. %o##owin! e<am'#e how how the Date and 5ime c#a e ,ormat tandard 6ava date and time va#4e to match the S]3 data t('e re@4irement . im'ort *ava. @#.DateJ im'ort *ava. @#.5imeJ im'ort *ava. @#.5ime tam'J im'ort *ava.4ti#.OJ '4b#ic c#a S@#Date5ime N '4b#ic tatic void main7Strin!8: ar! ; N GGGet tandard date and time *ava.4ti#.Date *avaDate K new *ava.4ti#.Date7;J #on! *ava5ime K *avaDate.!et5ime7;J S( tem.o4t.'rint#n7P5he 6ava Date i :P > *avaDate.toStrin!7;;J GGGet and di '#a( S]3 D)59 *ava. @#.Date @#Date K new *ava. @#.Date7*ava5ime;J S( tem.o4t.'rint#n7P5he S]3 D)59 i : P > @#Date.toStrin!7;;J GGGet and di '#a( S]3 5$29 *ava. @#.5ime @#5ime K new *ava. @#.5ime7*ava5ime;J S( tem.o4t.'rint#n7P5he S]3 5$29 i : P > @#5ime.toStrin!7;;J GGGet and di '#a( S]3 5$29S5)2P *ava. @#.5ime tam' @#5ime tam' K new *ava. @#.5ime tam'7*ava5ime;J S( tem.o4t.'rint#n7P5he S]3 5$29S5)2P i : P > @#5ime tam'.toStrin!7;;J QGGend main QGGend S@#Date5ime Now #et 4 com'i#e above e<am'#e a ,o##ow : +:IR*avac S@#Date5ime.*ava +:IR When (o4 r4n JD67E,a!"le, it 'rod4ce ,o##owin! re 4#t: +:IR*ava S@#Date5ime 5he 6ava Date i :54e )4! 18 11:D/:02 G25>0D:00 200+2+ 3imited i a S4b idiar( o, 5+S 3imited

108

5he S]3 D)59 i : 200--08-18 5he S]3 5$29 i : 11:D/:02 5he S]3 5$29S5)2P i : 200--08-18 11:D/:02.828 +:IR 'andlin( N544 Value%: S]3? 4 e o, N=33 va#4e and 6ava? 4 e o, n4## are di,,erent conce't . So how do (o4 hand#e S]3 N=33 va#4e in 6ava[ 5here are three tactic (o4 can 4 e: )void 4 in! !etAAA7 ; method that ret4rn 'rimitive data t('e . = e wra''er c#a e ,or 'rimitive data t('e , and 4 e the "e 4#tSet ob*ect? wa N4##7 ; method to te t whether the wra''er c#a variab#e that received the va#4e ret4rned b( the !etAAA7 ; method ho4#d be et to n4##. = e 'rimitive data t('e and the "e 4#tSet ob*ect? wa N4##7 ; method to te t whether the 'rimitive variab#e that received the va#4e ret4rned b( the !etAAA7 ; method ho4#d be et to an acce'tab#e va#4e that (o4?ve cho en to re're ent a N=33. Cere i one e<am'#e to hand#e a N=33 va#4e: Statement tmt K conn.createStatement7 ;J Strin! @# K PS939+5 id, ,ir t, #a t, a!e %"&2 9m'#o(ee PJ "e 4#tSet r K tmt.e<ec4te]4er(7 @#;J int id K r .!et$nt71;J i,7 r .wa N4##7 ; ; N id K 0J Q JD67 $ *ran%acti n% $, (o4r 6DE+ +onnection i in auto;commit mode, which it i b( de,a4#t, then ever( S]3 tatement i committed to the databa e 4'on it com'#etion. 5hat ma( be ,ine ,or im'#e a''#ication , b4t there are three rea on wh( (o4 ma( want to t4rn o,, a4tocommit and mana!e (o4r own tran action : 5o increa e 'er,ormance 5o maintain the inte!rit( o, b4 ine 'roce e 5o 4 e di trib4ted tran action 5ran action enab#e (o4 to contro# i,, and when, chan!e are a''#ied to the databa e. $t treat a in!#e S]3 tatement or a !ro4' o, S]3 tatement a one #o!ica# 4nit, and i, an( tatement ,ai# , the who#e tran action ,ai# . 5o enab#e man4a#- tran action 4''ort in tead o, the auto;commit mode that the 6DE+ driver 4 e b( de,a4#t, 4 e the +onnection ob*ect? %etAut 7 !!it8; method. $, (o4 'a a boo#ean ,a# e to et)4to+ommit7 ;, (o4 t4rn o,, a4to-commit. Fo4 can 'a a boo#ean tr4e to t4rn it back on a!ain. %or e<am'#e, i, (o4 have a +onnection ob*ect named conn, code the ,o##owin! to t4rn o,, a4to-commit: conn. et)4to+ommit7,a# e;J 7 !!it A R llbac= &nce (o4 are done with (o4r chan!e and (o4 want to commit the chan!e then ca## c !!it8; method on connection ob*ect a ,o##ow : conn.commit7 ;J &therwi e, to ro## back 4'date to the databa e made 4 in! the +onnection named conn, 4 e the ,o##owin! code: conn.ro##back7 ;J 5he ,o##owin! e<am'#e i##4 trate the 4 e o, a commit and ro##back ob*ect: tr(N GG) 4me a va#id connection ob*ect conn conn. et)4to+ommit7,a# e;J Statement tmt K conn.createStatement7;J Strin! S]3 K P$NS9"5 $N5& 9m'#o(ee P > PV)3=9S 710/, 20, ?"ita?, ?5eB?;PJ tmt.e<ec4te='date7S]3;J GGS4bmit a ma#,ormed S]3 tatement that break Strin! S]3 K P$NS9"59D $N 9m'#o(ee P > PV)3=9S 7100, 22, ?Sita?, ?Sin!h?;PJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 10-

tmt.e<ec4te='date7S]3;J GG $, there i no error. conn.commit7;J Qcatch7S]39<ce'tion e;N GG $, there i an( error. conn.ro##back7;J Q $n thi ca e none o, the abobe $NS9"5 tatement wo4#d 4cce and ever(thin! wo4#d be ro##ed back. %or a better 4nder tandin!, $ wo4#d 4!!e t to t4d( 5%in( #a)e" int%: 5he new 6DE+ 1.0 Save'oint inter,ace !ive (o4 additiona# tran actiona# contro#. 2o t modern DE2S 4''ort ave'oint within their environment 4ch a &rac#e? P3GS]3. When (o4 et a ave'oint (o4 de,ine a #o!ica# ro##back 'oint within a tran action. $, an error occ4r 'a t a ave'oint, (o4 can 4 e the ro##back method to 4ndo either a## the chan!e or on#( the chan!e made a,ter the ave'oint. 5he +onnection ob*ect ha two new method that he#' (o4 mana!e ave'oint : %et#a)e" int8#trin( %a)e" intNa!e;: de,ine a new ave'oint. $t a# o ret4rn a Save'oint ob*ect. relea%e#a)e" int8#a)e" int %a)e" intNa!e;: de#ete a ave'oint. Notice that it re@4ire a Save'oint ob*ect a a 'arameter. 5hi ob*ect i 4 4a##( a ave'oint !enerated b( the etSave'oint7; method. 5here i one r llbac= 8 #trin( %a)e" intNa!e ; method which ro## back work to the 'eci,ied ave'oint. 5he ,o##owin! e<am'#e i##4 trate the 4 e o, a Save'oint ob*ect: tr(N GG) 4me a va#id connection ob*ect conn conn. et)4to+ommit7,a# e;J Statement tmt K conn.createStatement7;J GG et a Save'oint Save'oint ave'oint1 K conn. etSave'oint7PSave'oint1P;J Strin! S]3 K P$NS9"5 $N5& 9m'#o(ee P > PV)3=9S 710/, 20, ?"ita?, ?5eB?;PJ tmt.e<ec4te='date7S]3;J GGS4bmit a ma#,ormed S]3 tatement that break Strin! S]3 K P$NS9"59D $N 9m'#o(ee P > PV)3=9S 7100, 22, ?Sita?, ?5eB?;PJ tmt.e<ec4te='date7S]3;J GG $, there i no error, commit the chan!e . conn.commit7;J Qcatch7S]39<ce'tion e;N GG $, there i an( error. conn.ro##back7 ave'oint1;J Q $n thi ca e none o, the abobe $NS9"5 tatement wo4#d 4cce and ever(thin! wo4#d be ro##ed back. %or a better 4nder tandin!, $ wo4#d 4!!e t to t4d( JD67 $ E,ce"ti n% 'andlin( 9<ce'tion hand#in! a##ow (o4 to hand#e e<ce'tiona# condition 4ch a 'ro!ram-de,ined error in a contro##ed ,a hion. When an e<ce'tion condition occ4r , an e<ce'tion i thrown. 5he term thrown mean that c4rrent 'ro!ram e<ec4tion to' , and contro# i redirected to the neare t a''#icab#e catch c#a4 e. $, no a''#icab#e catch c#a4 e e<i t , then the 'ro!ram? e<ec4tion end . 6DE+ 9<ce'tion hand#in! i ver( imi#ar to 6ava 9<c'etion hand#in! b4t ,or 6DE+, the mo t common e<ce'tion (o4?## dea# with i ja)a+%@l+#N4E,ce"ti n+ #N4E,ce"ti n &eth d%: ) S]39<ce'tion can occ4r both in the driver and the databa e. When 4ch an e<ce'tion occ4r , an ob*ect o, t('e S]39<ce'tion wi## be 'a ed to the catch c#a4 e. +2+ 3imited i a S4b idiar( o, 5+S 3imited 180

5he 'a ed S]39<ce'tion ob*ect ha the ,o##owin! method avai#ab#e ,or retrievin! additiona# in,ormation abo4t the e<ce'tion: &eth d De%cri"ti n !et9rror+ode7 ; Get the error n4mber a ociated with the e<ce'tion. Get the 6DE+ driver? error me a!e ,or an error !et2e a!e7 ; hand#ed b( the driver or !et the &rac#e error n4mber and me a!e ,or a databa e error. Get the A&P9N S]3 tate trin!. %or a 6DE+ driver error, no 4 e,4# in,ormation i ret4rned ,rom thi !etS]3State7 ; method. %or a databa e error, the ,ive-di!it A&P9N S]3 tate code i ret4rned. 5hi method can ret4rn n4##. !etNe<t9<ce'tion7 ; Get the ne<t 9<ce'tion ob*ect in the e<ce'tion chain. Print the c4rrent e<ce'tion, or throwab#e, and it 'rintStack5race7 ; backtrace to a tandard error tream. Print thi throwab#e and it backtrace to the 'rint 'rintStack5race7PrintStream ; tream (o4 'eci,(. Print thi throwab#e and it backtrace to the 'rint 'rintStack5race7PrintWriter w; writer (o4 'eci,(. E( 4ti#iBin! the in,ormation avai#ab#e ,rom the 9<ce'tion ob*ect, (o4 can catch an e<ce'tion and contin4e (o4r 'ro!ram a''ro'riate#(. Cere i the !enera# ,orm o, a tr( b#ock: tr( N GG Fo4r ri k( code !oe between the e c4r#( brace WWW Q catch79<ce'tion e<; N GG Fo4r e<ce'tion hand#in! code !oe between the e GG c4r#( brace , imi#ar to the e<ce'tion c#a4 e GG in a P3GS]3 b#ock. Q ,ina##( N GG Fo4r m4 t-a#wa( -be-e<ec4ted code !oe between the e GG c4r#( brace . 3ike c#o in! databa e connection. Q E,a!"le: St4d( the ,o##owin! e<am'#e code to 4nder tand the 4 a!e o, try++++catch+++finally b#ock . GGS59P 1. $m'ort re@4ired 'acka!e im'ort *ava. @#.OJ '4b#ic c#a 6DE+9<am'#e N GG 6DE+ driver name and databa e ="3 tatic ,ina# Strin! 6DE+TD"$V9" K Pcom.m( @#.*dbc.DriverPJ tatic ,ina# Strin! DET="3 K P*dbc:m( @#:GG#oca#ho tG92PPJ GG Databa e credentia# tatic ,ina# Strin! =S9" K P4 ernamePJ tatic ,ina# Strin! P)SS K P'a wordPJ '4b#ic tatic void main7Strin!8: ar! ; N +onnection conn K n4##J tr(N GGS59P 2: "e!i ter 6DE+ driver +#a .,orName7Pcom.m( @#.*dbc.DriverP;J GGS59P 1: &'en a connection S( tem.o4t.'rint#n7P+onnectin! to databa e...P;J conn K Driver2ana!er.!et+onnection7DET="3,=S9",P)SS;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 181

GGS59P D: 9<ec4te a @4er( S( tem.o4t.'rint#n7P+reatin! tatement...P;J Statement tmt K conn.createStatement7;J Strin! @#J @# K PS939+5 id, ,ir t, #a t, a!e %"&2 9m'#o(ee PJ "e 4#tSet r K tmt.e<ec4te]4er(7 @#;J GGS59P .: 9<tract data ,rom re 4#t et whi#e7r .ne<t7;;N GG"etrieve b( co#4mn name int id K r .!et$nt7PidP;J int a!e K r .!et$nt7Pa!eP;J Strin! ,ir t K r .!etStrin!7P,ir tP;J Strin! #a t K r .!etStrin!7P#a tP;J GGDi '#a( va#4e S( tem.o4t.'rint7P$D: P > id;J S( tem.o4t.'rint7P, )!e: P > a!e;J S( tem.o4t.'rint7P, %ir t: P > ,ir t;J S( tem.o4t.'rint#n7P, 3a t: P > #a t;J Q GGS59P /: +#ean-4' environment r .c#o e7;J tmt.c#o e7;J conn.c#o e7;J Qcatch7S]39<ce'tion e;N GGCand#e error ,or 6DE+ e.'rintStack5race7;J Qcatch79<ce'tion e;N GGCand#e error ,or +#a .,orName e.'rintStack5race7;J Q,ina##(N GG,ina##( b#ock 4 ed to c#o e re o4rce tr(N i,7connWKn4##; conn.c#o e7;J Qcatch7S]39<ce'tion e;N e.'rintStack5race7;J QGGend ,ina##( tr( QGGend tr( S( tem.o4t.'rint#n7PGoodb(eWP;J QGGend main QGGend 6DE+9<am'#e Now #et 4 com'i#e above e<am'#e a ,o##ow : +:IR*avac 6DE+9<am'#e.*ava +:IR When (o4 r4n JD67E,a!"le, it 'rod4ce ,o##owin! re 4#t i, there i no 'rob#em, otherwi e corre 'ondin! error wo4#d be ca4!ht and error me a!e wo4#d be di '#a(ed: +:IR*ava 6DE+9<am'#e +onnectin! to databa e... +reatin! tatement... $D: 100, )!e: 18, %ir t: Sara, 3a t: )#i $D: 101, )!e: 2., %ir t: 2ahnaB, 3a t: %atma $D: 102, )!e: 10, %ir t: Said, 3a t: Hhan $D: 101, )!e: 28, %ir t: S4mit, 3a t: 2itta# +:IR 5r( above e<am'#e b( 'a in! wron! databa e name or wron! 4 ername or 'a word and check the re 4#t. +2+ 3imited i a S4b idiar( o, 5+S 3imited 182

JD67 $ 6atch Pr ce%%in( Eatch Proce in! a##ow (o4 to !ro4' re#ated S]3 tatement into a batch and 4bmit them with one ca## to the databa e. When (o4 end evera# S]3 tatement to the databa e at once, (o4 red4ce the amo4nt o, comm4nication overhead, thereb( im'rovin! 'er,ormance. 6DE+ driver are not re@4ired to 4''ort thi ,eat4re. Fo4 ho4#d 4 e the 9atabaseMeta9ata#supports<atch=pdates ! method to determine i, the tar!et databa e 4''ort batch 4'date 'roce in!. 5he method ret4rn tr4e i, (o4r 6DE+ driver 4''ort thi ,eat4re. 5he add6atch8; method o, .tateme$t4 Prepared.tateme$t4 and Callable.tateme$t i 4 ed to add individ4a# tatement to the batch. 5he e,ecute6atch8; i 4 ed to tart the e<ec4tion o, a## the tatement !ro4'ed to!ether. 5he e,ecute6atch8; ret4rn an arra( o, inte!er , and each e#ement o, the arra( re're ent the 4'date co4nt ,or the re 'ective 4'date tatement. 64 t a (o4 can add tatement to a batch ,or 'roce in!, (o4 can remove them with the clear6atch8; method. 5hi method remove a## the tatement (o4 added with the addEatch7; method. Cowever, (o4 cannot e#ective#( choo e which tatement to remove. 6atchin( with #tate!ent Object: Cere i a t('ica# e@4ence o, te' to 4 e Eatch Proce in! with Statment &b*ect: +reate a Statement ob*ect 4 in! either create.tateme$t ! method . Set a4to-commit to ,a# e 4 in! set*utoCommit !. )dd a man( a S]3 tatement (o4 #ike into batch 4 in! add<atch ! method on created tatement ob*ect. 9<ec4te a## the S]3 tatement 4 in! e1ecute<atch ! method on created tatement ob*ect. %ina##(, commit a## the chan!e 4 in! commit ! method. E,a!"le: 5he ,o##owin! code ni''et 'rovide an e<am'#e o, a batch 4'date 4 in! Statement ob*ect: GG +reate tatement ob*ect Statement tmt K conn.createStatement7;J GG Set a4to-commit to ,a# e conn. et)4to+ommit7,a# e;J GG +reate S]3 tatement Strin! S]3 K P$NS9"5 $N5& 9m'#o(ee 7id, ,ir t, #a t, a!e; P > PV)3=9S7200,?Sia?, ?)#i?, 10;PJ GG )dd above S]3 tatement in the batch. tmt.addEatch7S]3;J GG +reate one more S]3 tatement Strin! S]3 K P$NS9"5 $N5& 9m'#o(ee 7id, ,ir t, #a t, a!e; P > PV)3=9S7201,?"a*?, ?H4mar?, 1.;PJ GG )dd above S]3 tatement in the batch. tmt.addEatch7S]3;J GG +reate one more S]3 tatement Strin! S]3 K P=PD)59 9m'#o(ee S95 a!e K 1. P > PWC9"9 id K 100PJ GG )dd above S]3 tatement in the batch. tmt.addEatch7S]3;J GG +reate an int8: to ho#d ret4rned va#4e int8: co4nt K tmt.e<ec4teEatch7;J GG9<'#icit#( commit tatement to a''#( chan!e conn.commit7;J %or a better 4nder tandin!, $ wo4#d 4!!e t to t4d( 6atchin( with Pre"are#tate!ent Object: Cere i a t('ica# e@4ence o, te' to 4 e Eatch Proce

in! with Pre'areStatement &b*ect: 181

+2+ 3imited i a S4b idiar( o, 5+S 3imited

+reate S]3 tatement with '#aceho#der . +reate Pre'areStatement ob*ect 4 in! either prepare.tateme$t ! method . Set a4to-commit to ,a# e 4 in! set*utoCommit !. )dd a man( a S]3 tatement (o4 #ike into batch 4 in! add<atch ! method on created tatement ob*ect. 9<ec4te a## the S]3 tatement 4 in! e1ecute<atch ! method on created tatement ob*ect. %ina##(, commit a## the chan!e 4 in! commit ! method. 5he ,o##owin! code ni''et 'rovide an e<am'#e o, a batch 4'date 4 in! Pre'areStatement ob*ect: GG +reate S]3 tatement Strin! S]3 K P$NS9"5 $N5& 9m'#o(ee 7id, ,ir t, #a t, a!e; P > PV)3=9S7[, [, [, [;PJ GG +reate Pre'areStatement ob*ect Pre'aredStatemen ' tmt K conn.'re'areStatement7S]3;J GGSet a4to-commit to ,a# e conn. et)4to+ommit7,a# e;J GG Set the variab#e ' tmt. et$nt7 1, D00 ;J ' tmt. etStrin!7 2, PPa''4P ;J ' tmt. etStrin!7 1, PSin!hP ;J ' tmt. et$nt7 D, 11 ;J GG )dd it to the batch ' tmt.addEatch7;J GG Set the variab#e ' tmt. et$nt7 1, D01 ;J ' tmt. etStrin!7 2, PPawanP ;J ' tmt. etStrin!7 1, PSin!hP ;J ' tmt. et$nt7 D, 11 ;J GG )dd it to the batch ' tmt.addEatch7;J GGadd more batche . . . . GG+reate an int8: to ho#d ret4rned va#4e int8: co4nt K tmt.e<ec4teEatch7;J GG9<'#icit#( commit tatement to a''#( chan!e conn.commit7;J %or a better 4nder tandin!, $ wo4#d 4!!e t to t4d( JD67 $ #t red Pr cedure% $ have e<'#ained how to 4 e #t red Pr cedure% in 6DE+ whi#e di c4 in! 6DE+ - Statement . 5hi t4toria# i imi#ar to that ection b4t it wo4#d !ive (o4 additiona# in,ormation abo4t 6DE+ S]3 e ca'e (nta<. 64 t a a +onnection ob*ect create the Statement and Pre'aredStatement ob*ect , it a# o create the +a##ab#eStatement ob*ect which wo4#d be 4 ed to e<ec4te a ca## to a databa e tored 'roced4re. 7reatin( 7allable#tate!ent Object: S4''o e, (o4 need to e<ec4te the ,o##owin! &rac#e tored 'roced4re: +"9)59 &" "9P3)+9 P"&+9D="9 !et9m'Name 792PT$D $N N=2E9", 92PT%$"S5 &=5 V)"+C)"; )S E9G$N S939+5 ,ir t $N5& 92PT%$"S5 %"&2 9m'#o(ee +2+ 3imited i a S4b idiar( o, 5+S 3imited 18D

WC9"9 $D K 92PT$DJ 9NDJ NO*E: )bove tored 'roced4re ha been written ,or &rac#e, b4t we are workin! with 2(S]3 databa e o #et 4 write ame tored 'roced4re ,or 2(S]3 a ,o##ow to create it in 92P databa e: D93$2$59" MM D"&P P"&+9D="9 $% 9A$S5S \92P\.\!et9m'Name\ MM +"9)59 P"&+9D="9 \92P\.\!et9m'Name\ 7$N 92PT$D $N5, &=5 92PT%$"S5 V)"+C)"72..;; E9G$N S939+5 ,ir t $N5& 92PT%$"S5 %"&2 9m'#o(ee WC9"9 $D K 92PT$DJ 9ND MM D93$2$59" J 5hree t('e o, 'arameter e<i t: $N, &=5, and $N&=5. 5he Pre'aredStatement ob*ect on#( 4 e the $N 'arameter. 5he +a##ab#eStatement ob*ect can 4 e a## three. Cere are the de,inition o, each: Para!eter De%cri"ti n ) 'arameter who e va#4e i 4nknown when the S]3 tatement i $N created. Fo4 bind va#4e to $N 'arameter with the etAAA7; method . ) 'arameter who e va#4e i 4''#ied b( the S]3 tatement it &=5 ret4rn . Fo4 retrieve va#4e ,rom the&=5 'arameter with the !etAAA7; method . ) 'arameter that 'rovide both in'4t and o4t'4t va#4e . Fo4 bind $N&=5 variab#e with the etAAA7; method and retrieve va#4e with the !etAAA7; method . 5he ,o##owin! code ni''et how how to em'#o( the 7 nnecti n+"re"are7all8; method to in tantiate a 7allable#tate!ent ob*ect ba ed on the 'recedin! tored 'roced4re: +a##ab#eStatement c tmt K n4##J tr( N Strin! S]3 K PNca## !et9m'Name 7[, [;QPJ c tmt K conn.'re'are+a## 7S]3;J ... Q catch 7S]39<ce'tion e; N ... Q ,ina##( N ... Q 5he Strin! variab#e S]3 re're ent the tored 'roced4re, with 'arameter '#aceho#der . = in! +a##ab#eStatement ob*ect i m4ch #ike 4 in! Pre'aredStatement ob*ect . Fo4 m4 t bind va#4e to a## 'arameter be,ore e<ec4tin! the tatement, or (o4 wi## receive an S]39<ce'tion. $, (o4 have $N 'arameter , *4 t ,o##ow the ame r4#e and techni@4e that a''#( to a Pre'aredStatement ob*ectJ 4 e the etAAA7; method that corre 'ond to the 6ava data t('e (o4 are bindin!. When (o4 4 e &=5 and $N&=5 'arameter (o4 m4 t em'#o( an additiona# +a##ab#eStatement method, re!i ter&4tParameter7;. 5he re!i ter&4tParameter7; method bind the 6DE+ data t('e to the data t('e the tored 'roced4re i e<'ected to ret4rn. &nce (o4 ca## (o4r tored 'roced4re, (o4 retrieve the va#4e ,rom the &=5 'arameter with the a''ro'riate !etAAA7; method. 5hi method ca t the retrieved va#4e o, S]3 t('e to a 6ava data t('e. 7l %in( 7allable#tate!ent Obeject: 64 t a (o4 c#o e other Statement ob*ect, ,or the ame rea on (o4 ho4#d a# o c#o e the +a##ab#eStatement ob*ect. +2+ 3imited i a S4b idiar( o, 5+S 3imited 18.

) im'#e ca## to the c#o e7; method wi## do the *ob. $, (o4 c#o e the +onnection ob*ect ,ir t it wi## c#o e the +a##ab#eStatement ob*ect a we##. Cowever, (o4 ho4#d a#wa( e<'#icit#( c#o e the +a##ab#eStatement ob*ect to en 4re 'ro'er c#ean4'. +a##ab#eStatement c tmt K n4##J tr( N Strin! S]3 K PNca## !et9m'Name 7[, [;QPJ c tmt K conn.'re'are+a## 7S]3;J ... Q catch 7S]39<ce'tion e; N ... Q ,ina##( N c tmt.c#o e7;J Q %or a better 4nder tandin!, $ wo4#d 4!!e t to t4d( JD67 #N4 e%ca"e %ynta,: 5he e ca'e (nta< !ive (o4 the ,#e<ibi#it( to 4 e databa e 'eci,ic ,eat4re 4navai#ab#e to (o4 b( 4 in! tandard 6DE+ method and 'ro'ertie . 5he !enera# S]3 e ca'e (nta< ,ormat i a ,o##ow : Nke(word ?'arameter ?Q Cere are ,o##owin! e ca'e e@4ence which (o4 wo4#d ,ind ver( 4 e,4# whi#e doin! 6DE+ 'ro!rammin!: d, t, t% <eyw rd%: 5he( he#' identi,( date, time, and time tam' #itera# . ) (o4 know, no two DE2S re're ent time and date the ame wa(. 5hi e ca'e (nta< te## the driver to render the date or time in the tar!et databa e? ,ormat. %or 9<am'#e: Nd ?((((-mm-dd?Q Where (((( K (ear, mm K monthJ dd K date. = in! thi (nta< Nd ?200--0--01?Q i 2arch -, 200-. Cere i a im'#e e<am'#e howin! how to $NS9"5 date in a tab#e: GG+reate a Statement ob*ect tmt K conn.createStatement7;J GG$n ert data KKR $D, %ir t Name, 3a t Name, D&E Strin! @#KP$NS9"5 $N5& S5=D9N5S V)3=9SP > P7100,?Sara?,?)#i?, Nd ?2001-12-1/?Q;PJ tmt.e<ec4te='date7 @#;J Simi#ar#(, (o4 can 4 e one o, the ,o##owin! two (nta<e , either t or t%: Nt ?hh:mm: ?Q Where hh K ho4rJ mm K min4teJ K econd. = in! thi (nta< Nt ?11:10:2-?Q i 1:10:2- P2. Nt ?((((-mm-dd hh:mm: ?Q 5hi i combined (nta< o, the above two (nta< ,or ?d? and ?t? to re're ent time tam'. e%ca"e <eyw rd: 5hi ke(word identi,ie the e ca'e character 4 ed in 3$H9 c#a4 e . = e,4# when 4 in! the S]3 wi#dcard L, which matche Bero or more character . %or e<am'#e: Strin! @# K PS939+5 (mbo# %"&2 2athS(mbo# WC9"9 (mbo# 3$H9 ?IL? Ne ca'e ?I?QPJ tmt.e<ec4te7 @#;J $, (o4 4 e the back #a h character 7I; a the e ca'e character, (o4 a# o have to 4 e two back #a h character in (o4r 6ava Strin! #itera#, beca4 e the back #a h i a# o a 6ava e ca'e character. fn <eyw rd: 5hi ke(word re're ent ca#ar ,4nction 4 ed in a DE2S. %or e<am'#e, (o4 can 4 e S]3 ,4nction le$gth to !e the #en!th o, a trin!: N,n #en!th7?Ce##o Wor#d?;Q 5hi ret4rn 11, the #en!th o, the character trin! ?Ce##o Wor#d?. call <eyw rd: 5hi ke(work i 4 ed to ca## tored 'roced4re . %or e<am'#e, ,or a tored 'roced4re re@4irin! an $N 'arameter, 4 e ,o##owin! (nta<: Nca## m(T'roced4re7[;QJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 18/

%or a tored 'roced4re re@4irin! an $N 'arameter and ret4rnin! an &=5 'arameter, 4 e ,o##owin! (nta<: N[ K ca## m(T'roced4re7[;QJ j <eyw rd: 5hi ke(word i 4 ed to i!ni,( o4ter *oin . 5he (nta< i a ,o##ow : No* o4ter-*oinQ Where o4ter-*oin K tab#e N39%5Y"$GC5Y%=33Q &=59"6&$N Ntab#e Y o4ter-*oinQ on earch-condition. %or e<am'#e: Strin! @# K PS939+5 9m'#o(ee %"&2 No* 5hi 5ab#e "$GC5 &=59" 6&$N 5hat5ab#e on id K ?100?QPJ tmt.e<ec4te7 @#;J JD67 $ #trea!in( A#7II and 6inary Data ) Pre'aredStatement ob*ect ha the abi#it( to 4 e in'4t and o4t'4t tream to 4''#( 'arameter data. 5hi enab#e (o4 to '#ace entire ,i#e into databa e co#4mn that can ho#d #ar!e va#4e , 4ch a +3&E and E3&E data t('e . 5here are ,o##owin! method which can be 4 ed to tream data: %etA%cii#trea!8;: 5hi method i 4 ed to 4''#( #ar!e )S+$$ va#4e . %et7haracter#trea!8;: 5hi method i 4 ed to 4''#( #ar!e =N$+&D9 va#4e . %et6inary#trea!8;: 5hi method i 4 ed to 4''#( #ar!e binar( va#4e . 5he etAAAStream7; method re@4ire an e<tra 'arameter, the ,i#e iBe, be ide the 'arameter '#aceho#der. 5hi 'arameter in,orm the driver how m4ch data ho4#d be ent to the databa e 4 in! the tream. E,a!"le +on ider we want to 4'#oad an A23 ,i#e A23TData.<m# into a databa e tab#e. Cere i the content o, thi A23 ,i#e: V[<m# ver ionKP1.0P[R V9m'#o(eeR VidR100VGidR V,ir tRSaraVG,ir tR V#a tR)#iVG#a tR VSa#ar(R10000VGSa#ar(R VDobR18-08-1-08VGDobR V9m'#o(eeR Hee' thi A23 ,i#e in the ame director( where (o4 are !oin! to r4n thi e<am'#e. 5hi e<am'#e wo4#d create a databa e tab#e A23TData and then ,i#e A23TData.<m# wo4#d be 4'#oaded into thi tab#e. +o'( and 'a t ,o##owin! e<am'#e in 6DE+9<am'#e.*ava, com'i#e and r4n a ,o##ow : GG $m'ort re@4ired 'acka!e im'ort *ava. @#.OJ im'ort *ava.io.OJ im'ort *ava.4ti#.OJ '4b#ic c#a 6DE+9<am'#e N GG 6DE+ driver name and databa e ="3 tatic ,ina# Strin! 6DE+TD"$V9" K Pcom.m( @#.*dbc.DriverPJ tatic ,ina# Strin! DET="3 K P*dbc:m( @#:GG#oca#ho tG92PPJ GG Databa e credentia# tatic ,ina# Strin! =S9" K P4 ernamePJ tatic ,ina# Strin! P)SS K P'a wordPJ '4b#ic tatic void main7Strin!8: ar! ; N +onnection conn K n4##J Pre'aredStatement ' tmt K n4##J Statement tmt K n4##J "e 4#tSet r K n4##J tr(N GG "e!i ter 6DE+ driver +2+ 3imited i a S4b idiar( o, 5+S 3imited 180

+#a

.,orName7Pcom.m( @#.*dbc.DriverP;J

GG &'en a connection S( tem.o4t.'rint#n7P+onnectin! to databa e...P;J conn K Driver2ana!er.!et+onnection7DET="3,=S9",P)SS;J GG+reate a Statement ob*ect and b4i#d tab#e tmt K conn.createStatement7;J createA235ab#e7 tmt;J GG&'en a %i#e$n'4tStream %i#e , K new %i#e7PA23TData.<m#P;J #on! ,i#e3en!th K ,.#en!th7;J %i#e$n'4tStream ,i K new %i#e$n'4tStream7,;J GG+reate Pre'aredStatement and tream data Strin! S]3 K P$NS9"5 $N5& A23TData V)3=9S 7[,[;PJ ' tmt K conn.'re'areStatement7S]3;J ' tmt. et$nt71,100;J ' tmt. et) ciiStream72,,i ,7int;,i#e3en!th;J ' tmt.e<ec4te7;J GG+#o e in'4t tream ,i .c#o e7;J GG Do a @4er( to !et the row S]3 K PS939+5 Data %"&2 A23TData WC9"9 idK100PJ r K tmt.e<ec4te]4er( 7S]3;J GG Get the ,ir t row i, 7r .ne<t 7;;N GG"etrieve data ,rom in'4t tream $n'4tStream <m#$n'4tStream K r .!et) ciiStream 71;J int cJ E(te)rra(&4t'4tStream bo K new E(te)rra(&4t'4tStream7;J whi#e 77 c K <m#$n'4tStream.read 7;; WK -1; bo .write7c;J GGPrint re 4#t S( tem.o4t.'rint#n7bo .toStrin!7;;J Q GG +#ean-4' environment r .c#o e7;J tmt.c#o e7;J ' tmt.c#o e7;J conn.c#o e7;J Qcatch7S]39<ce'tion e;N GGCand#e error ,or 6DE+ e.'rintStack5race7;J Qcatch79<ce'tion e;N GGCand#e error ,or +#a .,orName e.'rintStack5race7;J Q,ina##(N GG,ina##( b#ock 4 ed to c#o e re o4rce tr(N i,7 tmtWKn4##; tmt.c#o e7;J Qcatch7S]39<ce'tion e2;N QGG nothin! we can do tr(N i,7' tmtWKn4##; +2+ 3imited i a S4b idiar( o, 5+S 3imited

188

' tmt.c#o e7;J Qcatch7S]39<ce'tion e2;N QGG nothin! we can do tr(N i,7connWKn4##; conn.c#o e7;J Qcatch7S]39<ce'tion e;N e.'rintStack5race7;J QGGend ,ina##( tr( QGGend tr( S( tem.o4t.'rint#n7PGoodb(eWP;J QGGend main '4b#ic tatic void createA235ab#e7Statement tmt; throw S]39<ce'tionN S( tem.o4t.'rint#n7P+reatin! A23TData tab#e...P ;J GG+reate S]3 Statement Strin! treamin!DataS@# K P+"9)59 5)E39 A23TData P > P7id $N59G9", Data 3&NG;PJ GGDro' tab#e ,ir t i, it e<i t . tr(N tmt.e<ec4te='date7PD"&P 5)E39 A23TDataP;J Qcatch7S]39<ce'tion e;N QGG do nothin! GGE4i#d tab#e. tmt.e<ec4te='date7 treamin!DataS@#;J QGGend createA235ab#e QGGend 6DE+9<am'#e Now #et 4 com'i#e above e<am'#e a ,o##ow : +:IR*avac 6DE+9<am'#e.*ava +:IR When (o4 r4n JD67E,a!"le, it 'rod4ce ,o##owin! re 4#t: +:IR*ava 6DE+9<am'#e +onnectin! to databa e... +reatin! A23TData tab#e... V[<m# ver ionKP1.0P[R V9m'#o(eeR VidR100VGidR V,ir tRSaraVG,ir tR V#a tR)#iVG#a tR VSa#ar(R10000VGSa#ar(R VDobR18-08-1-08VGDobR V9m'#o(eeR Goodb(eW +:IR #er)let% O)er)iew What are #er)let%? 6ava Serv#et are 'ro!ram that r4n on a Web or )''#ication erver and act a a midd#e #a(er between a re@4e t comin! ,rom a Web brow er or other C55P c#ient and databa e or a''#ication on the C55P erver. = in! Serv#et , (o4 can co##ect in'4t ,rom 4 er thro4!h web 'a!e ,orm , 're ent record ,rom a databa e or another o4rce, and create web 'a!e d(namica##(. 6ava Serv#et o,ten erve the ame '4r'o e a 'ro!ram im'#emented 4 in! the +ommon Gatewa( $nter,ace 7+G$;. E4t Serv#et o,,er evera# advanta!e in com'ari on with the +G$. Per,ormance i i!ni,icant#( better. Serv#et e<ec4te within the addre 'ace o, a Web erver. $t i not nece ar( to create a e'arate 'roce to hand#e each c#ient re@4e t. Serv#et are '#at,orm-inde'endent beca4 e the( are written in 6ava. +2+ 3imited i a S4b idiar( o, 5+S 3imited 18-

6ava ec4rit( mana!er on the erver en,orce a et o, re triction to 'rotect the re o4rce on a erver machine. So erv#et are tr4 ted. 5he ,4## ,4nctiona#it( o, the 6ava c#a #ibrarie i avai#ab#e to a erv#et. $t can comm4nicate with a''#et , databa e , or other o,tware via the ocket and "2$ mechani m that (o4 have een a#read(. #er)let% Architecture: %o##owin! dia!ram how the 'o ition o, Serve#t in a Web )''#ication.

#er)let% *a%=%: Serv#et 'er,orm the ,o##owin! ma*or ta k : "ead the e<'#icit data ent b( the c#ient 7brow er ;. 5hi inc#4de an C523 ,orm on a Web 'a!e or it co4#d a# o come ,rom an a''#et or a c4 tom C55P c#ient 'ro!ram. "ead the im'#icit C55P re@4e t data ent b( the c#ient 7brow er ;. 5hi inc#4de cookie , media t('e and com're ion cheme the brow er 4nder tand , and o ,orth. Proce the data and !enerate the re 4#t . 5hi 'roce ma( re@4ire ta#kin! to a databa e, e<ec4tin! an "2$ or +&"E) ca##, invokin! a Web ervice, or com'4tin! the re 'on e direct#(. Send the e<'#icit data 7i.e., the doc4ment; to the c#ient 7brow er ;. 5hi doc4ment can be ent in a variet( o, ,ormat , inc#4din! te<t 7C523 or A23;, binar( 7G$% ima!e ;, 9<ce#, etc. Send the im'#icit C55P re 'on e to the c#ient 7brow er ;. 5hi inc#4de te##in! the brow er or other c#ient what t('e o, doc4ment i bein! ret4rned 7e.!., C523;, ettin! cookie and cachin! 'arameter , and other 4ch ta k . #er)let% Pac=a(e%: 6ava Serv#et are 6ava c#a e r4n b( a web erver that ha an inter'reter that 4''ort the 6ava Serv#et 'eci,ication. Serv#et can be created 4 in! the ja)a,+%er)let and ja)a,+%er)let+htt" 'acka!e , which are a tandard 'art o, the 6ava? enter'ri e edition, an e<'anded ver ion o, the 6ava c#a #ibrar( that 4''ort #ar!e- ca#e deve#o'ment 'ro*ect . 5he e c#a e im'#ement the 6ava Serv#et and 6SP 'eci,ication . )t the time o, writin! thi t4toria#, the ver ion are 6ava Serv#et 2.. and 6SP 2.1. 6ava erv#et have been created and com'i#ed *4 t #ike an( other 6ava c#a . ),ter (o4 in ta## the erv#et 'acka!e and add them to (o4r com'4ter? +#a 'ath, (o4 can com'i#e erv#et with the 6DH? 6ava com'i#er or an( other c4rrent com'i#er. What i% Ne,t? $ wo4#d take (o4 te' b( te' to et 4' (o4r environment to tart with Serv#et . So ,a ten (o4r be#t ,or a nice drive with Serv#et . $?m 4re (o4 are !oin! to en*o( thi t4toria# ver( m4ch. #er)let% $ 4ife 7ycle ) erv#et #i,e c(c#e can be de,ined a the entire 'roce ,rom it creation ti## the de tr4ction. 5he ,o##owin! are the 'ath ,o##owed b( a erv#et 5he erv#et i initia#iBed b( ca##in! the init 8; method. 5he erv#et ca## %er)ice8; method to 'roce a c#ient? re@4e t. 5he erv#et i terminated b( ca##in! the de%tr y8; method. %ina##(, erv#et i !arba!e co##ected b( the !arba!e co##ector o, the 6V2. Now #et 4 di c4 the #i,e c(c#e method in detai# . *he init8; !eth d : 5he init method i de i!ned to be ca##ed on#( once. $t i ca##ed when the erv#et i ,ir t created, and not ca##ed a!ain ,or each 4 er re@4e t. So, it i 4 ed ,or one-time initia#iBation , *4 t a with the init method o, a''#et . +2+ 3imited i a S4b idiar( o, 5+S 3imited 1-0

5he erv#et i norma##( created when a 4 er ,ir t invoke a ="3 corre 'ondin! to the erv#et, b4t (o4 can a# o 'eci,( that the erv#et be #oaded when the erver i ,ir t tarted. When a 4 er invoke a erv#et, a in!#e in tance o, each erv#et !et created, with each 4 er re@4e t re 4#tin! in a new thread that i handed o,, to doGet or doPo t a a''ro'riate. 5he init7; method im'#( create or #oad ome data that wi## be 4 ed thro4!ho4t the #i,e o, the erv#et. 5he init method de,inition #ook #ike thi : '4b#ic void init7; throw Serv#et9<ce'tion N GG $nitia#iBation code... Q *he %er)ice8; !eth d : 5he ervice7; method i the main method to 'er,orm the act4a# ta k. 5he erv#et container 7i.e. web erver; ca## the ervice7; method to hand#e re@4e t comin! ,rom the c#ient7 brow er ; and to write the ,ormatted re 'on e back to the c#ient. 9ach time the erver receive a re@4e t ,or a erv#et, the erver 'awn a new thread and ca## ervice. 5he ervice7; method check the C55P re@4e t t('e 7G95, P&S5, P=5, D93959, etc.; and ca## doGet, doPo t, doP4t, doDe#ete, etc. method a a''ro'riate. Cere i the i!nat4re o, thi method: '4b#ic void ervice7Serv#et"e@4e t re@4e t, Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tionN Q 5he ervice 7; method i ca##ed b( the container and ervice method invoke doGe, doPo t, doP4t, doDe#ete, etc. method a a''ro'riate. So (o4 have nothin! to do with ervice7; method b4t (o4 override either doGet7; or doPo t7; de'endin! on what t('e o, re@4e t (o4 receive ,rom the c#ient. 5he doGet7; and doPo t7; are mo t ,re@4ent#( 4 ed method with in each ervice re@4e t. Cere are the i!nat4re o, the e two method . *he d Cet8; &eth d ) G95 re@4e t re 4#t ,rom a norma# re@4e t ,or a ="3 or ,rom an C523 ,orm that ha no 295C&D 'eci,ied and it ho4#d be hand#ed b( doGet7; method. '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Serv#et code Q *he d P %t8; &eth d ) P&S5 re@4e t re 4#t ,rom an C523 ,orm that 'eci,ica##( #i t P&S5 a the 295C&D and it ho4#d be hand#ed b( doPo t7; method. '4b#ic void doPo t7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Serv#et code Q *he de%tr y8; !eth d : 5he de tro(7; method i ca##ed on#( once at the end o, the #i,e c(c#e o, a erv#et. 5hi method !ive (o4r erv#et a chance to c#o e databa e connection , ha#t back!ro4nd thread , write cookie #i t or hit co4nt to di k, and 'er,orm other 4ch c#ean4' activitie . ),ter the de tro(7; method i ca##ed, the erv#et ob*ect i marked ,or !arba!e co##ection. 5he de tro( method de,inition #ook #ike thi : '4b#ic void de tro(7; N GG %ina#iBation code... Q Architecture Di(ra!: 5he ,o##owin! ,i!4re de'ict a t('ica# erv#et #i,e-c(c#e cenario. %ir t the C55P re@4e t comin! to the erver are de#e!ated to the erv#et container. 5he erv#et container #oad the erv#et be,ore invokin! the ervice7; method. 5hen the erv#et container hand#e m4#ti'#e re@4e t b( 'awnin! m4#ti'#e thread , each thread e<ec4tin! the ervice7; method o, a in!#e in tance o, the erv#et. +2+ 3imited i a S4b idiar( o, 5+S 3imited 1-1

#er)let% $ E,a!"le% Serv#et are 6ava c#a e which ervice C55P re@4e t and im'#ement the ja)a,+%er)let+#er)let inter,ace. Web a''#ication deve#o'er t('ica##( write erv#et that e<tend *ava<. erv#et.htt'.Ctt'Serv#et, an ab tract c#a that im'#ement the Serv#et inter,ace and i 'ecia##( de i!ned to hand#e C55P re@4e t . #a!"le 7 de f r 'ell W rld: %o##owin! i the am'#e o4rce code tr4ct4re o, a erv#et e<am'#e to write Ce##o Wor#d: GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a Ce##oWor#d e<tend Ctt'Serv#et N 'rivate Strin! me a!eJ '4b#ic void init7; throw Serv#et9<ce'tion N GG Do re@4ired initia#iBation me a!e K PCe##o Wor#dPJ Q '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J GG )ct4a# #o!ic !oe here. PrintWriter o4t K re 'on e.!etWriter7;J o4t.'rint#n7PVh1RP > me a!e > PVGh1RP;J Q '4b#ic void de tro(7; +2+ 3imited i a S4b idiar( o, 5+S 3imited 1-2

N GG do nothin!. Q Q 7 !"ilin( a #er)let: 3et 4 '4t above code i, Ce##oWor#d.*ava ,i#e and '4t thi ,i#e in +:IServ#etDeve# 7Window ; or G4 rGServ#etDeve# 7=ni<; then (o4 wo4#d need to add the e directorie a we## in +3)SSP)5C. ) 4min! (o4r environment i et4' 'ro'er#(, !o in #er)letDe)el director( and com'i#e Ce##oWor#d.*ava a ,o##ow : M *avac Ce##oWor#d.*ava $, the erv#et de'end on an( other #ibrarie , (o4 have to inc#4de tho e 6)" ,i#e on (o4r +3)SSP)5C a we##. $ have inc#4ded on#( erv#et-a'i.*ar 6)" ,i#e beca4 e $?m not 4 in! an( other #ibrar( in Ce##o Wor#d 'ro!ram. 5hi command #ine 4 e the b4i#t-in *avac com'i#er that come with the S4n 2icro ( tem 6ava So,tware Deve#o'ment Hit 76DH;. %or thi command to work 'ro'er#(, (o4 have to inc#4de the #ocation o, the 6ava SDH that (o4 are 4 in! in the P)5C environment variab#e. $, ever(thin! !oe ,ine, above com'i#ation wo4#d 'rod4ce 'ell W rld+cla%% ,i#e in the ame director(. Ne<t ection wo4#d e<'#ain how a com'i#ed erv#et wo4#d be de'#o(ed in 'rod4ction. #er)let De"l y!ent: E( de,a4#t, a erv#et a''#ication i #ocated at the 'ath V5omcat-in ta##ation-director(RGweba'' G"&&5 and the c#a ,i#e wo4#d re ide in V5omcat-in ta##ation-director(RGweba'' G"&&5GW9E-$N%Gc#a e . $, (o4 have a ,4##( @4a#i,ied c#a name o, c !+!y r(+&y#er)let, then thi erv#et c#a m4 t be #ocated in W9E-$N%Gc#a e GcomGm(or!G2(Serv#et.c#a . %or now, #et 4 co'( Ce##oWor#d.c#a into V5omcat-in ta##ation-director(RGweba'' G"&&5GW9E$N%Gc#a e and create ,o##owin! entrie in web+,!l ,i#e #ocated in V5omcat-in ta##ationdirector(RGweba'' G"&&5GW9E-$N%G V erv#etR V erv#et-nameRCe##oWor#dVG erv#et-nameR V erv#et-c#a RCe##oWor#dVG erv#et-c#a R VG erv#etR V erv#et-ma''in!R V erv#et-nameRCe##oWor#dVG erv#et-nameR V4r#-'atternRGCe##oWor#dVG4r#-'atternR VG erv#et-ma''in!R )bove entrie to be created in ide Vweb-a''R...VGweb-a''R ta! avai#ab#e in web.<m# ,i#e. 5here co4#d be vario4 entrie in thi tab#e a#read( avai#ab#e, b4t never mind. Fo4 are a#mo t done, now #et 4 tart tomcat erver 4 in! V5omcat-in ta##ationdirector(RIbinI tart4'.bat 7on window ; or V5omcat-in ta##ation-director(RGbinG tart4'. h 7on 3in4<GSo#ari etc.; and ,ina##( t('e htt"://l calh %t:3.3./'ell W rld in brow er? addre bo<. $, ever(thin! !oe ,ine, (o4 wo4#d !et ,o##owin! re 4#t:

+2+ 3imited i a S4b idiar( o, 5+S 3imited

1-1

#er)let% F r! Data Fo4 m4 t have come acro man( it4ation when (o4 need to 'a ome in,ormation ,rom (o4r brow er to web erver and 4#timate#( to (o4r backend 'ro!ram. 5he brow er 4 e two method to 'a thi in,ormation to web erver. 5he e method are G95 2ethod and P&S5 2ethod. CE* !eth d: 5he G95 method end the encoded 4 er in,ormation a''ended to the 'a!e re@4e t. 5he 'a!e and the encoded in,ormation are e'arated b( the [ character a ,o##ow : htt':GGwww.te t.comGhe##o[ke(1Kva#4e1Xke(2Kva#4e2 5he G95 method i the de,4a#t method to 'a in,ormation ,rom brow er to web erver and it 'rod4ce a #on! trin! that a''ear in (o4r brow er? 3ocation:bo<. Never 4 e the G95 method i, (o4 have 'a word or other en itive in,ormation to 'a to the erver. 5he G95 method ha iBe #imtation: on#( 102D character can be in a re@4e t trin!. 5hi in,ormation i 'a ed 4 in! ]=9"FTS5"$NG header and wi## be acce ib#e thro4!h ]=9"FTS5"$NG environment variab#e and Serv#et hand#e thi t('e o, re@4e t 4 in! d Cet8; method. PO#* !eth d: ) !enera##( more re#iab#e method o, 'a in! in,ormation to a backend 'ro!ram i the P&S5 method. 5hi 'acka!e the in,ormation in e<act#( the ame wa( a G95 method , b4t in tead o, endin! it a a te<t trin! a,ter a [ in the ="3 it end it a a e'arate me a!e. 5hi me a!e come to the backend 'ro!ram in the ,orm o, the tandard in'4t which (o4 can 'ar e and 4 e ,or (o4r 'roce in!. Serv#et hand#e thi t('e o, re@4e t 4 in! d P %t8; method. Readin( F r! Data u%in( #er)let: Serv#et hand#e ,orm data 'ar in! a4tomatica##( 4 in! the ,o##owin! method de'endin! on the it4ation: (etPara!eter8;: Fo4 ca## re@4e t.!etParameter7; method to !et the va#4e o, a ,orm 'arameter. (etPara!eterValue%8;: +a## thi method i, the 'arameter a''ear more than once and ret4rn m4#ti'#e va#4e , ,or e<am'#e checkbo<. (etPara!eterNa!e%8;: +a## thi method i, (o4 want a com'#ete #i t o, a## 'arameter in the c4rrent re@4e t. CE* &eth d E,a!"le 5%in( 5R4: Cere i a im'#e ="3 which wi## 'a two va#4e to Ce##o%orm 'ro!ram 4 in! G95 method. htt"://l calh %t:3.3./'ell F r!?fir%tRna!eSDARAAla%tRna!eSA4I Ee#ow i 'ell F r!+ja)a erv#et 'ro!ram to hand#e in'4t !iven b( web brow er. We are !oin! to 4 e (etPara!eter8; method which make it ver( ea ( to acce 'a ed in,ormation: GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a Ce##o%orm e<tend Ctt'Serv#et N '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K P= in! G95 2ethod to "ead %orm DataPJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > +2+ 3imited i a S4b idiar( o, 5+S 3imited 1-D

PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PV4#RInP > P V#iRVbR%ir t NameVGbR: P > re@4e t.!etParameter7P,ir tTnameP; > PInP > P V#iRVbR3a t NameVGbR: P > re@4e t.!etParameter7P#a tTnameP; > PInP > PVG4#RInP > PVGbod(RVGhtm#RP;J Q Q ) 4min! (o4r environment i et4' 'ro'er#(, com'i#e Ce##o%orm.*ava a ,o##ow : M *avac Ce##o%orm.*ava $, ever(thin! !oe ,ine, above com'i#ation wo4#d 'rod4ce 'ell F r!+cla%% ,i#e. Ne<t (o4 wo4#d have to co'( thi c#a ,i#e in V5omcat-in ta##ation-director(RGweba'' G"&&5GW9E-$N%Gc#a e and create ,o##owin! entrie in web+,!l ,i#e #ocated in V5omcat-in ta##ation-director(RGweba'' G"&&5GW9E-$N%G V erv#etR V erv#et-nameRCe##o%ormVG erv#et-nameR V erv#et-c#a RCe##o%ormVG erv#et-c#a R VG erv#etR V erv#et-ma''in!R V erv#et-nameRCe##o%ormVG erv#et-nameR V4r#-'atternRGCe##o%ormVG4r#-'atternR VG erv#et-ma''in!R Now t('e http:>>localhost:?@?@>HelloFormA)irst0$ameB7*,*Clast0$ameB*53 in (o4r brow er? 3ocation:bo< and make 4re (o4 a#read( tarted tomcat erver, be,ore ,irin! above command in the brow er. 5hi wo4#d !enerate ,o##owin! re 4#t: 5%in( CE* &eth d t Read F r! Data Fir%t Na!e: S)") 4a%t Na!e: )3$ CE* &eth d E,a!"le 5%in( F r!: Cere i a im'#e e<am'#e which 'a e two va#4e 4 in! C523 %&"2 and 4bmit b4tton. We are !oin! to 4 e ame Serv#et Ce##o%orm to hand#e thi im'4t. Vhtm#R Vbod(R V,orm actionKPCe##o%ormP methodKPG95PR %ir t Name: Vin'4t t('eKPte<tP nameKP,ir tTnamePR Vbr GR 3a t Name: Vin'4t t('eKPte<tP nameKP#a tTnameP GR Vin'4t t('eKP 4bmitP va#4eKPS4bmitP GR VG,ormR VGbod(R VGhtm#R Hee' thi C523 in a ,i#e Ce##o.htm and '4t it in V5omcat-in ta##ation-director(RGweba'' G"&&5 director(. When (o4 wo4#d acce http:>>localhost:?@?@>Hello#htm, here i the act4a# o4t'4t o, the above ,orm. %ir t Name: 3a t Name: 5r( to enter %ir t Name and 3a t Name and then c#ick 4bmit b4tton to ee the re 4#t on (o4r #oca# machine where tomcat i r4nnin!. Ea ed on the in'4t 'rovided, it wi## !enerate imi#ar re 4#t a mentioned in the above e<am'#e. PO#* &eth d E,a!"le 5%in( F r!: 3et 4 do #itt#e modi,ication in the above erv#et, o that it can hand#e G95 a we## a P&S5 method . Ee#ow i 'ell F r!+ja)a erv#et 'ro!ram to hand#e in'4t !iven b( web brow er 4 in! G95 or P&S5 method . +2+ 3imited i a S4b idiar( o, 5+S 3imited 1-.

GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a Ce##o%orm e<tend Ctt'Serv#et N GG 2ethod to hand#e G95 method re@4e t. '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K P= in! G95 2ethod to "ead %orm DataPJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PV4#RInP > P V#iRVbR%ir t NameVGbR: P > re@4e t.!etParameter7P,ir tTnameP; > PInP > P V#iRVbR3a t NameVGbR: P > re@4e t.!etParameter7P#a tTnameP; > PInP > PVG4#RInP > PVGbod(RVGhtm#RP;J Q GG 2ethod to hand#e P&S5 method re@4e t. '4b#ic void doPo t7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N doGet7re@4e t, re 'on e;J Q Q Now com'i#e, de'#o( the above Serv#et and te t it 4 in! Ce##o.htm with the P&S5 method a ,o##ow : Vhtm#R Vbod(R V,orm actionKPCe##o%ormP methodKPP&S5PR %ir t Name: Vin'4t t('eKPte<tP nameKP,ir tTnamePR Vbr GR 3a t Name: Vin'4t t('eKPte<tP nameKP#a tTnameP GR Vin'4t t('eKP 4bmitP va#4eKPS4bmitP GR VG,ormR VGbod(R VGhtm#R Cere i the act4a# o4t'4t o, the above ,orm, 5r( to enter %ir t and 3a t Name and then c#ick 4bmit b4tton to ee the re 4#t on (o4r #oca# machine where tomcat i r4nnin!. %ir t Name: 3a t Name: +2+ 3imited i a S4b idiar( o, 5+S 3imited 1-/

Ea ed on the in'4t 'rovided, it wo4#d !enerate imi#ar re 4#t a mentioned in the above e<am'#e . Pa%%in( 7hec=b , Data t #er)let Pr (ra! +heckbo<e are 4 ed when more than one o'tion i re@4ired to be e#ected. Cere i e<am'#e C523 code, +heckEo<.htm, ,or a ,orm with two checkbo<e Vhtm#R Vbod(R V,orm actionKP+heckEo<P methodKPP&S5P tar!etKPTb#ankPR Vin'4t t('eKPcheckbo<P nameKPmath P checkedKPcheckedP GR 2ath Vin'4t t('eKPcheckbo<P nameKP'h( ic P GR Ph( ic Vin'4t t('eKPcheckbo<P nameKPchemi tr(P checkedKPcheckedP GR +hemi tr( Vin'4t t('eKP 4bmitP va#4eKPSe#ect S4b*ectP GR VG,ormR VGbod(R VGhtm#R 5he re 4#t o, thi code i the ,o##owin! ,orm 2ath Ph( ic +hemi tr( Ee#ow i +heckEo<.*ava erv#et 'ro!ram to hand#e in'4t !iven b( web brow er ,or checkbo< b4tton. GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a +heckEo< e<tend Ctt'Serv#et N GG 2ethod to hand#e G95 method re@4e t. '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K P"eadin! +heckbo< DataPJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PV4#RInP > P V#iRVbR2ath %#a! : VGbR: P > re@4e t.!etParameter7Pmath P; > PInP > P V#iRVbRPh( ic %#a!: VGbR: P > re@4e t.!etParameter7P'h( ic P; > PInP > P V#iRVbR+hemi tr( %#a!: VGbR: P > re@4e t.!etParameter7Pchemi tr(P; > PInP > PVG4#RInP > PVGbod(RVGhtm#RP;J Q GG 2ethod to hand#e P&S5 method re@4e t. '4b#ic void doPo t7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; +2+ 3imited i a S4b idiar( o, 5+S 3imited 1-0

throw Serv#et9<ce'tion, $&9<ce'tion N doGet7re@4e t, re 'on e;J Q Q %or the above e<am'#e, it wo4#d di '#a( ,o##owin! re 4#t: Readin( 7hec=b , Data &ath% Fla( : : on Phy%ic% Fla(: : n4## 7he!i%try Fla(: : on Readin( All F r! Para!eter%: %o##owin! i the !eneric e<am'#e which 4 e (etPara!eterNa!e%8; method o, Ctt'Serv#et"e@4e t to read a## the avai#ab#e ,orm 'arameter . 5hi method ret4rn an 9n4meration that contain the 'arameter name in an 4n 'eci,ied order. &nce we have an 9n4meration, we can #oo' down the 9n4meration in the tandard manner, 4 in! hasMore%leme$ts ! method to determine when to to' and 4 in! $e1t%leme$t ! method to !et each 'arameter name. GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a "eadParam e<tend Ctt'Serv#et N GG 2ethod to hand#e G95 method re@4e t. '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K P"eadin! )## %orm Parameter PJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PVtab#e widthKIP100LIP borderKIP1IP a#i!nKIPcenterIPRInP > PVtr b!co#orKIPb-D-D-DIPRInP > PVthRParam NameVGthRVthRParam Va#4e7 ;VGthRInP> PVGtrRInP;J 9n4meration 'aramName K re@4e t.!etParameterName 7;J whi#e7'aramName .ha 2ore9#ement 7;; N Strin! 'aramName K 7Strin!;'aramName .ne<t9#ement7;J o4t.'rint7PVtrRVtdRP > 'aramName > PVGtdRInVtdRP;J Strin!8: 'aramVa#4e K re@4e t.!etParameterVa#4e 7'aramName;J GG "ead in!#e va#4ed data i, 7'aramVa#4e .#en!th KK 1; N Strin! 'aramVa#4e K 'aramVa#4e 80:J i, 7'aramVa#4e.#en!th7; KK 0; +2+ 3imited i a S4b idiar( o, 5+S 3imited 1-8

o4t.'rint#n7PViRNo Va#4eVGiRP;J e# e o4t.'rint#n7'aramVa#4e;J Q e# e N GG "ead m4#ti'#e va#4ed data o4t.'rint#n7PV4#RP;J ,or7int iK0J i V 'aramVa#4e .#en!thJ i>>; N o4t.'rint#n7PV#iRP > 'aramVa#4e 8i:;J Q o4t.'rint#n7PVG4#RP;J Q Q o4t.'rint#n7PVGtrRInVGtab#eRInVGbod(RVGhtm#RP;J Q GG 2ethod to hand#e P&S5 method re@4e t. '4b#ic void doPo t7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N doGet7re@4e t, re 'on e;J Q Q Now, tr( above erv#et with the ,o##owin! ,orm: Vhtm#R Vbod(R V,orm actionKP"eadParam P methodKPP&S5P tar!etKPTb#ankPR Vin'4t t('eKPcheckbo<P nameKPmath P checkedKPcheckedP GR 2ath Vin'4t t('eKPcheckbo<P nameKP'h( ic P GR Ph( ic Vin'4t t('eKPcheckbo<P nameKPchemi tr(P checkedKPcheckedP GR +hem Vin'4t t('eKP 4bmitP va#4eKPSe#ect S4b*ectP GR VG,ormR VGbod(R VGhtm#R Now ca##in! erv#et 4 in! above ,orm wo4#d !enerate ,o##owin! re 4#t: Readin( All F r! Para!eter% Para! Na!e math chemi tr( on on Para! Value8%;

Fo4 can tr( above erv#et to read an( other ,orm? data which i havin! other ob*ect #ike te<t bo<, radio b4tton or dro' down bo< etc. #er)let% $ 7lient '**P Re@ue%t When a brow er re@4e t ,or a web 'a!e, it end #ot o, in,ormation to the web erver which can not be read direct#( beca4 e thi in,ormation trave# a a 'art o, header o, C55P re@4e t. Fo4 can check C55P Protoco# ,or more in,ormation on thi . %o##owin! i the im'ortant header in,ormation which come ,rom brow er ide and (o4 wo4#d 4 e ver( ,re@4ent#( in web 'ro!rammin!: 'eader De%cri"ti n 5hi header 'eci,ie the 2$29 t('e that the brow er or other )cce't c#ient can hand#e. Va#4e o, i!a(e/"n( or i!a(e/j"e( are the two mo t common 'o ibi#itie . 5hi header 'eci,ie the character et the brow er can 4 e to )cce't-+har et di '#a( the in,ormation. %or e<am'#e $S&-88.--1. 5hi header 'eci,ie the t('e o, encodin! that the brow er )cce't-9ncodin! know how to hand#e. Va#4e o, (>i" or c !"re%% are the two mo t common 'o ibi#itie . )cce't-3an!4a!e 5hi header 'eci,ie the c#ient? 're,erred #an!4a!e in ca e +2+ 3imited i a S4b idiar( o, 5+S 3imited 1--

the erv#et can 'rod4ce re 4#t in more than one #an!4a!e. %or e<am'#e en, en-4 , r4, etc. 5hi header i 4 ed b( c#ient to identi,( them e#ve when )4thoriBation acce in! 'a word-'rotected Web 'a!e . 5hi header indicate whether the c#ient can hand#e 'er i tent C55P connection . Per i tent connection 'ermit the c#ient or +onnection other brow er to retrieve m4#ti'#e ,i#e with a in!#e re@4e t. ) va#4e o, <ee"$Ali)e mean that 'er i tent connection ho4#d be 4 ed 5hi header i a''#icab#e on#( to P&S5 re@4e t and !ive the +ontent-3en!th iBe o, the P&S5 data in b(te . 5hi header ret4rn cookie to erver that 'revio4 #( ent them +ookie to the brow er. 5hi header 'eci,ie the ho t and 'ort a !iven in the ori!ina# Co t ="3. 5hi header indicate that the c#ient want the 'a!e on#( i, it ha been chan!ed a,ter the 'eci,ied date. 5he erver end a code, $,-2odi,ied-Since 10D which mean N t & dified header i, no newer re 4#t i avai#ab#e. 5hi header i the rever e o, $,-2odi,ied-SinceJ it 'eci,ie that $,-=nmodi,ied-Since the o'eration ho4#d 4cceed on#( i, the doc4ment i o#der than the 'eci,ied date. 5hi header indicate the ="3 o, the re,errin! Web 'a!e. %or e<am'#e, i, (o4 are at Web 'a!e 1 and c#ick on a #ink to Web "e,erer 'a!e 2, the ="3 o, Web 'a!e 1 i inc#4ded in the "e,erer header when the brow er re@4e t Web 'a!e 2. 5hi header identi,ie the brow er or other c#ient makin! the = er-)!ent re@4e t and can be 4 ed to ret4rn di,,erent content to di,,erent t('e o, brow er . &eth d% t read '**P 'eader: 5here are ,o##owin! method which can be 4 ed to read C55P header in (o4r erv#et 'ro!ram. 5he e method are avai#ab#e with Http.ervlet,e6uest ob*ect. #+N+ &eth d A De%cri"ti n 7 =ie9: (et7 =ie%8; 1 "et4rn an arra( containin! a## o, the +ookie ob*ect the c#ient ent with thi re@4e t. Enu!erati n (etAttributeNa!e%8; 2 "et4rn an 9n4meration containin! the name o, the attrib4te avai#ab#e to thi re@4e t. Enu!erati n (et'eaderNa!e%8; 1 "et4rn an en4meration o, a## the header name thi re@4e t contain . Enu!erati n (etPara!eterNa!e%8; D "et4rn an 9n4meration o, Strin! ob*ect containin! the name o, the 'arameter contained in thi re@4e t. 'tt"#e%%i n (et#e%%i n8; . "et4rn the c4rrent e ion a ociated with thi re@4e t, or i, the re@4e t doe not have a e ion, create one. 'tt"#e%%i n (et#e%%i n8b lean create; / "et4rn the c4rrent Ctt'Se ion a ociated with thi re@4e t or, i, i, there i no c4rrent e ion and create i tr4e, ret4rn a new e ion. 4 cale (et4 cale8; 0 "et4rn the 're,erred 3oca#e that the c#ient wi## acce't content in, ba ed on the )cce't3an!4a!e header 8 Object (etAttribute8#trin( na!e; +2+ 3imited i a S4b idiar( o, 5+S 3imited 200

10 11 12 11 1D 1.

1/

10 18 120 21 22

21 2D 2. 2/

20

28 2-

"et4rn the va#4e o, the named attrib4te a an &b*ect, or n4## i, no attrib4te o, the !iven name e<i t . #er)letIn"ut#trea! (etIn"ut#trea!8; "etrieve the bod( o, the re@4e t a binar( data 4 in! a Serv#et$n'4tStream. #trin( (etAuth*y"e8; "et4rn the name o, the a4thentication cheme 4 ed to 'rotect the erv#et, ,or e<am'#e, PE)S$+P or PSS3,P or n4## i, the 6SP wa not 'rotected #trin( (et7haracterEnc din(8; "et4rn the name o, the character encodin! 4 ed in the bod( o, thi re@4e t. #trin( (et7 ntent*y"e8; "et4rn the 2$29 t('e o, the bod( o, the re@4e t, or n4## i, the t('e i not known. #trin( (et7 nte,tPath8; "et4rn the 'ortion o, the re@4e t ="$ that indicate the conte<t o, the re@4e t. #trin( (et'eader8#trin( na!e; "et4rn the va#4e o, the 'eci,ied re@4e t header a a Strin!. #trin( (et&eth d8; "et4rn the name o, the C55P method with which thi re@4e t wa made, ,or e<am'#e, G95, P&S5, or P=5. #trin( (etPara!eter8#trin( na!e; "et4rn the va#4e o, a re@4e t 'arameter a a Strin!, or n4## i, the 'arameter doe not e<i t. #trin( (etPathInf 8; "et4rn an( e<tra 'ath in,ormation a ociated with the ="3 the c#ient ent when it made thi re@4e t. #trin( (etPr t c l8; "et4rn the name and ver ion o, the 'rotoco# the re@4e t. #trin( (etNuery#trin(8; "et4rn the @4er( trin! that i contained in the re@4e t ="3 a,ter the 'ath. #trin( (etRe! teAddr8; "et4rn the $nternet Protoco# 7$P; addre o, the c#ient that ent the re@4e t. #trin( (etRe! te' %t8; "et4rn the ,4##( @4a#i,ied name o, the c#ient that ent the re@4e t. #trin( (etRe! te5%er8; "et4rn the #o!in o, the 4 er makin! thi re@4e t, i, the 4 er ha been a4thenticated, or n4## i, the 4 er ha not been a4thenticated. #trin( (etRe@ue%t5RI8; "et4rn the 'art o, thi re@4e t? ="3 ,rom the 'rotoco# name 4' to the @4er( trin! in the ,ir t #ine o, the C55P re@4e t. #trin( (etRe@ue%ted#e%%i nId8; "et4rn the e ion $D 'eci,ied b( the c#ient. #trin( (et#er)letPath8; "et4rn the 'art o, thi re@4e t? ="3 that ca## the 6SP. #trin(9: (etPara!eterValue%8#trin( na!e; "et4rn an arra( o, Strin! ob*ect containin! a## o, the va#4e the !iven re@4e t 'arameter ha , or n4## i, the 'arameter doe not e<i t. b lean i%#ecure8; "et4rn a boo#ean indicatin! whether thi re@4e t wa made 4 in! a ec4re channe#, 4ch a C55PS. int (et7 ntent4en(th8; "et4rn the #en!th, in b(te , o, the re@4e t bod( and made avai#ab#e b( the in'4t tream, or -1 i, the #en!th i not known. int (etInt'eader8#trin( na!e; +2+ 3imited i a S4b idiar( o, 5+S 3imited

201

"et4rn the va#4e o, the 'eci,ied re@4e t header a an int. int (et#er)erP rt8; 10 "et4rn the 'ort n4mber on which thi re@4e t wa received. '**P 'eader Re@ue%t E,a!"le: %o##owin! i the e<am'#e which 4 e (et'eaderNa!e%8; method o, Ctt'Serv#et"e@4e t to read the C55P header in,romation. 5hi method ret4rn an 9n4meration that contain the header in,ormation a ociated with the c4rrent C55P re@4e t. &nce we have an 9n4meration, we can #oo' down the 9n4meration in the tandard manner, 4 in! hasMore%leme$ts ! method to determine when to to' and 4 in! $e1t%leme$t ! method to !et each 'arameter name. GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a Di '#a(Ceader e<tend Ctt'Serv#et N GG 2ethod to hand#e G95 method re@4e t. '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K PC55P Ceader "e@4e t 9<am'#ePJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP> PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PVtab#e widthKIP100LIP borderKIP1IP a#i!nKIPcenterIPRInP > PVtr b!co#orKIPb-D-D-DIPRInP > PVthRCeader NameVGthRVthRCeader Va#4e7 ;VGthRInP> PVGtrRInP;J 9n4meration headerName K re@4e t.!etCeaderName 7;J whi#e7headerName .ha 2ore9#ement 7;; N Strin! 'aramName K 7Strin!;headerName .ne<t9#ement7;J o4t.'rint7PVtrRVtdRP > 'aramName > PVGtdRInP;J Strin! 'aramVa#4e K re@4e t.!etCeader7'aramName;J o4t.'rint#n7PVtdR P > 'aramVa#4e > PVGtdRVGtrRInP;J Q o4t.'rint#n7PVGtab#eRInVGbod(RVGhtm#RP;J Q GG 2ethod to hand#e P&S5 method re@4e t. '4b#ic void doPo t7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N doGet7re@4e t, re 'on e;J Q Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 202

Now ca##in! the above erv#et wo4#d !enerate ,o##owin! re 4#t: '**P 'eader Re@ue%t E,a!"le 'eader Na!e acce't acce't-#an!4a!e 4 er-a!ent acce't-encodin! ho t connection cache-contro# OGO en-4 2oBi##aGD.0 7com'atib#eJ 2S$9 0.0J Window N5 ..1J 5ridentGD.0J $n,oPath.2J 2S-"5+ 32 8; !Bi', de,#ate #oca#ho t:8080 Hee'-)#ive no-cache 'eader Value8%;

#er)let% $ #er)er '**P Re%" n%e ) di c4 ed in 'revio4 cha'ter, when a Web erver re 'ond to a C55P re@4e t to the brow er, the re 'on e t('ica##( con i t o, a tat4 #ine, ome re 'on e header , a b#ank #ine, and the doc4ment. ) t('ica# re 'on e #ook #ike thi : C55PG1.1 200 &H +ontent-5('e: te<tGhtm# Ceader2: ... ... CeaderN: ... 7E#ank 3ine; VWdoct('e ...R Vhtm#R VheadR...VGheadR Vbod(R ... VGbod(R VGhtm#R 5he tat4 #ine con i t o, the C55P ver ion 7C55PG1.1 in the e<am'#e;, a tat4 code 7200 in the e<am'#e;, and a ver( hort me a!e corre 'ondin! to the tat4 code 7&H in the e<am'#e;. %o##owin! i a 4mmar( o, the mo t 4 e,4# C55P 1.1 re 'on e header which !o back to the brow er ,rom web erver ide and (o4 wo4#d 4 e them ver( ,re@4ent#( in web 'ro!rammin!: 'eader De%cri"ti n 5hi header 'eci,ie the re@4e t method 7G95, P&S5, etc.; )##ow that the erver 4''ort . 5hi header 'eci,ie the circ4m tance in which the re 'on e doc4ment can a,e#( be cached. $t can have va#4e "ublic, "ri)ate or n $cache etc. P4b#ic mean doc4ment i cacheab#e, +ache-+ontro# Private mean doc4ment i ,or a in!#e 4 er and can on#( be tored in 'rivate 7non hared; cache and no-cache mean doc4ment ho4#d never be cached. 5hi header in tr4ct the brow er whether to 4 e 'er i tent in C55P connection or not. ) va#4e o, cl %e in tr4ct the brow er +onnection not to 4 e 'er i tent C55P connection and =ee"$ali)e mean 4 in! 'er i tent connection . 5hi header #et (o4 re@4e t that the brow er a k the 4 er to +ontent-Di 'o ition ave the re 'on e to di k in a ,i#e o, the !iven name. 5hi header 'eci,ie the wa( in which the 'a!e wa encoded +ontent-9ncodin! d4rin! tran mi ion. 5hi header i!ni,ie the #an!4a!e in which the doc4ment i +ontent-3an!4a!e written. %or e<am'#e en, en-4 , r4, etc. +ontent-3en!th 5hi header indicate the n4mber o, b(te in the re 'on e. 5hi +2+ 3imited i a S4b idiar( o, 5+S 3imited 201

in,ormation i needed on#( i, the brow er i 4 in! a 'er i tent 7kee'-a#ive; C55P connection. 5hi header !ive the 2$29 724#ti'4r'o e $nternet 2ai# +ontent-5('e 9<ten ion; t('e o, the re 'on e doc4ment. 5hi header 'eci,ie the time at which the content ho4#d be 9<'ire con idered o4t-o,-date and th4 no #on!er be cached. 5hi header indicate when the doc4ment wa #a t chan!ed. 3a t-2odi,ied 5he c#ient can then cache the doc4ment and 4''#( a date b( an If$& dified$#ince re@4e t header in #ater re@4e t . 5hi header ho4#d be inc#4ded with a## re 'on e that have a tat4 code in the 100 . 5hi noti,ie the brow er o, the 3ocation doc4ment addre . 5he brow er a4tomatica##( reconnect to thi #ocation and retrieve the new doc4ment. 5hi header 'eci,ie how oon the brow er ho4#d a k ,or an "e,re h 4'dated 'a!e. Fo4 can 'eci,( time in n4mber o, econd a,ter which a 'a!e wo4#d be re,re hed. 5hi header can be 4 ed in con*4nction with a .01 7Service "etr(-),ter =navai#ab#e; re 'on e to te## the c#ient how oon it can re'eat it re@4e t. Set-+ookie 5hi header 'eci,ie a cookie a ociated with the 'a!e. &eth d% t #et '**P Re%" n%e 'eader: 5here are ,o##owin! method which can be 4 ed to et C55P re 'on e header in (o4r erv#et 'ro!ram. 5he e method are avai#ab#e with Http.ervlet,espo$se ob*ect. #+N+ &eth d A De%cri"ti n #trin( enc deRedirect5R48#trin( url; 1 9ncode the 'eci,ied ="3 ,or 4 e in the end"edirect method or, i, encodin! i not needed, ret4rn the ="3 4nchan!ed. #trin( enc de5R48#trin( url; 2 9ncode the 'eci,ied ="3 b( inc#4din! the e ion $D in it, or, i, encodin! i not needed, ret4rn the ="3 4nchan!ed. b lean c ntain%'eader8#trin( na!e; 1 "et4rn a boo#ean indicatin! whether the named re 'on e header ha a#read( been et. b lean i%7 !!itted8; D "et4rn a boo#ean indicatin! i, the re 'on e ha been committed. ) id add7 =ie87 =ie c =ie; . )dd the 'eci,ied cookie to the re 'on e. ) id addDate'eader8#trin( na!e, l n( date; / )dd a re 'on e header with the !iven name and date-va#4e. ) id add'eader8#trin( na!e, #trin( )alue; 0 )dd a re 'on e header with the !iven name and va#4e. ) id addInt'eader8#trin( na!e, int )alue; 8 )dd a re 'on e header with the !iven name and inte!er va#4e. ) id flu%h6uffer8; %orce an( content in the b4,,er to be written to the c#ient. ) id re%et8; 10 +#ear an( data that e<i t in the b4,,er a we## a the tat4 code and header . ) id re%et6uffer8; 11 +#ear the content o, the 4nder#(in! b4,,er in the re 'on e witho4t c#earin! header or tat4 code. 12 ) id %endErr r8int %c; Send an error re 'on e to the c#ient 4 in! the 'eci,ied tat4 code and c#earin! the +2+ 3imited i a S4b idiar( o, 5+S 3imited 20D

b4,,er. ) id %endErr r8int %c, #trin( !%(; 11 Send an error re 'on e to the c#ient 4 in! the 'eci,ied tat4 . ) id %endRedirect8#trin( l cati n; 1D Send a tem'orar( redirect re 'on e to the c#ient 4 in! the 'eci,ied redirect #ocation ="3. ) id %et6uffer#i>e8int %i>e; 1. Set the 're,erred b4,,er iBe ,or the bod( o, the re 'on e. ) id %et7haracterEnc din(8#trin( char%et; 1/ Set the character encodin! 72$29 char et; o, the re 'on e bein! ent to the c#ient, ,or e<am'#e, to =5%-8. ) id %et7 ntent4en(th8int len; 10 Set the #en!th o, the content bod( in the re 'on e $n C55P erv#et , thi method et the C55P +ontent-3en!th header. ) id %et7 ntent*y"e8#trin( ty"e; 18 Set the content t('e o, the re 'on e bein! ent to the c#ient, i, the re 'on e ha not been committed (et. ) id %etDate'eader8#trin( na!e, l n( date; 1Set a re 'on e header with the !iven name and date-va#4e. ) id %et'eader8#trin( na!e, #trin( )alue; 20 Set a re 'on e header with the !iven name and va#4e. ) id %etInt'eader8#trin( na!e, int )alue; 21 Set a re 'on e header with the !iven name and inte!er va#4e. ) id %et4 cale84 cale l c; 22 Set the #oca#e o, the re 'on e, i, the re 'on e ha not been committed (et. ) id %et#tatu%8int %c; 21 Set the tat4 code ,or thi re 'on e. '**P 'eader Re%" n%e E,a!"le: Fo4 a#read( have een et+ontent5('e7; method workin! in 'revio4 e<am'#e and ,o##owin! e<am'#e wo4#d a# o 4 e ame method, additiona##( we wo4#d 4 e %etInt'eader8; method to et Refre%h header. GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a "e,re h e<tend Ctt'Serv#et N GG 2ethod to hand#e G95 method re@4e t. '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re,re h, a4to#oad time a . econd re 'on e. et$ntCeader7P"e,re hP, .;J GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J GG Get c4rrent time +a#endar ca#endar K new Gre!orian+a#endar7;J Strin! amT'mJ int ho4r K ca#endar.!et7+a#endar.C&=";J int min4te K ca#endar.!et7+a#endar.2$N=59;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 20.

int econd K ca#endar.!et7+a#endar.S9+&ND;J i,7ca#endar.!et7+a#endar.)2TP2; KK 0; amT'm K P)2PJ e# e amT'm K PP2PJ Strin! +5 K ho4r>P:P> min4te >P:P> econd >P P> amT'mJ PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K P)4to "e,re h Ceader Settin!PJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP> PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PV'R+4rrent 5ime i : P > +5 > PVG'RInP;J Q GG 2ethod to hand#e P&S5 method re@4e t. '4b#ic void doPo t7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N doGet7re@4e t, re 'on e;J Q Q Now ca##in! the above erv#et wo4#d di '#a( c4rrent ( tem time a,ter ever( . econd a ,o##ow . 64 t r4n the erv#et and wait to ee the re 4#t: Aut Refre%h 'eader #ettin( +4rrent 5ime i : -:DD:.0 P2 #er)let% $ 'tt" #tatu% 7 de% 5he ,ormat o, the C55P re@4e t and C55P re 'on e me a!e are imi#ar and wi## have ,o##owin! tr4ct4re: )n initia# tat4 #ine > +"3% 7 +arria!e "et4rn > 3ine %eed ie. New 3ine ; Sero or more header #ine > +"3% ) b#ank #ine ie. a +"3% )n o'tioan# me a!e bod( #ike ,i#e, @4er( data or @4er( o4t'4t. %or e<am'#e, a erver re 'on e header #ook a ,o##ow : C55PG1.1 200 &H +ontent-5('e: te<tGhtm# Ceader2: ... ... CeaderN: ... 7E#ank 3ine; VWdoct('e ...R Vhtm#R VheadR...VGheadR Vbod(R ... VGbod(R VGhtm#R 5he tat4 #ine con i t o, the C55P ver ion 7C55PG1.1 in the e<am'#e;, a tat4 code 7200 in the e<am'#e;, and a ver( hort me a!e corre 'ondin! to the tat4 code 7&H in the e<am'#e;. %o##owin! i a #i t o, C55P tat4 code and a ociated me a!e that mi!ht be ret4rned ,rom the Web Server: 7 de: &e%%a(e: De%cri"ti n: +2+ 3imited i a S4b idiar( o, 5+S 3imited 20/

100 +ontin4e 101 200 201 202 201 20D 20. 20/ 100 101 102 101 10D 10. 10/ 100 D00 D01 D02 D01 D0D D0. D0/ D00 D08 D0D10 D11 D12 D11 D1D "e@4e t-4r# 5oo 3on! D1. =n 4''orted 2edia 5('e Switchin! Protoco# &H +reated )cce'ted Non-a4thoritative $n,ormation No +ontent "e et +ontent Partia# +ontent 24#ti'#e +hoice 2oved Permanent#( %o4nd See &ther Not 2odi,ied = e Pro<( =$used 5em'orar( "edirect Ead "e@4e t =na4thoriBed Pa(ment "e@4ired %orbidden Not %o4nd 2ethod Not )##owed Not )cce'tab#e Pro<( )4thentication "e@4ired "e@4e t 5imeo4t +on,#ict Gone 3en!th "e@4ired Precondition %ai#ed "e@4e t 9ntit( 5oo 3ar!e

&n#( a 'art o, the re@4e t ha been received b( the erver, b4t a #on! a it ha not been re*ected, the c#ient ho4#d contin4e with the re@4e t 5he erver witche 'rotoco#. 5he re@4e t i &H 5he re@4e t i com'#ete, and a new re o4rce i created 5he re@4e t i acce'ted ,or 'roce in!, b4t the 'roce in! i not com'#ete.

) #ink #i t. 5he 4 er can e#ect a #ink and !o to that #ocation. 2a<im4m ,ive addre e 5he re@4e ted 'a!e ha moved to a new 4r# 5he re@4e ted 'a!e ha moved tem'orari#( to a new 4r# 5he re@4e ted 'a!e can be ,o4nd 4nder a di,,erent 4r#

5hi code wa 4 ed in a 'revio4 ver ion. $t i no #on!er 4 ed, b4t the code i re erved. 5he re@4e ted 'a!e ha moved tem'orari#( to a new 4r#. 5he erver did not 4nder tand the re@4e t 5he re@4e ted 'a!e need a 4 ername and a 'a word Dou ca$ $ot use this code yet )cce i ,orbidden to the re@4e ted 'a!e 5he erver can not ,ind the re@4e ted 'a!e. 5he method 'eci,ied in the re@4e t i not a##owed. 5he erver can on#( !enerate a re 'on e that i not acce'ted b( the c#ient. Fo4 m4 t a4thenticate with a 'ro<( erver be,ore thi re@4e t can be erved. 5he re@4e t took #on!er than the erver wa 're'ared to wait. 5he re@4e t co4#d not be com'#eted beca4 e o, a con,#ict. 5he re@4e ted 'a!e i no #on!er avai#ab#e. 5he P+ontent-3en!thP i not de,ined. 5he erver wi## not acce't the re@4e t witho4t it. 5he 'recondition !iven in the re@4e t eva#4ated to ,a# e b( the erver. 5he erver wi## not acce't the re@4e t, beca4 e the re@4e t entit( i too #ar!e. 5he erver wi## not acce't the re@4e t, beca4 e the 4r# i too #on!. &cc4r when (o4 convert a P'o tP re@4e t to a P!etP re@4e t with a #on! @4er( in,ormation. 5he erver wi## not acce't the re@4e t, beca4 e the 200

+2+ 3imited i a S4b idiar( o, 5+S 3imited

media t('e i not 4''orted. D10 .00 .01 .02 .01 .0D .0. 9<'ectation %ai#ed $nterna# Server 9rror Not $m'#emented Ead Gatewa( Service =navai#ab#e 5he re@4e t wa not com'#eted. 5he erver met an 4ne<'ected condition 5he re@4e t wa not com'#eted. 5he erver did not 4''ort the ,4nctiona#it( re@4ired. 5he re@4e t wa not com'#eted. 5he erver received an inva#id re 'on e ,rom the 4' tream erver 5he re@4e t wa not com'#eted. 5he erver i tem'orari#( over#oadin! or down. 5he !atewa( ha timed o4t. 5he erver doe not 4''ort the Phtt' 'rotoco#P ver ion.

Gatewa( 5imeo4t C55P Ver ion Not S4''orted &eth d% t #et '**P #tatu% 7 de: 5here are ,o##owin! method which can be 4 ed to et C55P Stat4 +ode in (o4r erv#et 'ro!ram. 5he e method are avai#ab#e with Http.ervlet,espo$se ob*ect. #+N+ &eth d A De%cri"ti n "ublic ) id %et#tatu% 8 int %tatu%7 de ; 5hi method et an arbitrar( tat4 code. 5he etStat4 method take an int 7the tat4 1 code; a an ar!4ment. $, (o4r re 'on e inc#4de a 'ecia# tat4 code and a doc4ment, be 4re to ca## etStat4 be,ore act4a##( ret4rnin! an( o, the content with the Pri$tWriter. "ublic ) id %endRedirect8#trin( url; 2 5hi method !enerate a 102 re 'on e a#on! with a 5ocatio$ header !ivin! the ="3 o, the new doc4ment. "ublic ) id %endErr r8int c de, #trin( !e%%a(e; 1 5hi method end a tat4 code 74 4a##( D0D; a#on! with a hort me a!e that i a4tomatica##( ,ormatted in ide an C523 doc4ment and ent to the c#ient. '**P #tatu% 7 de E,a!"le: %o##owin! i the e<am'#e which wo4#d end D00 error code to the c#ient brow er and brow er wo4#d how (o4 PNeed a4thenticationWWWP me a!e. GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a how9rror e<tend Ctt'Serv#et N GG 2ethod to hand#e G95 method re@4e t. '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set error code and rea on. re 'on e. end9rror7D00, PNeed a4thenticationWWWP ;J Q GG 2ethod to hand#e P&S5 method re@4e t. '4b#ic void doPo t7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N doGet7re@4e t, re 'on e;J Q Q Now ca##in! the above erv#et wo4#d di '#a( ,o##owin! re 4#t: +2+ 3imited i a S4b idiar( o, 5+S 3imited 208

'**P #tatu% K.M $ Need authenticati nTTT ty"e Stat4 re'ort !e%%a(e Need a4thenticationWWW de%cri"ti n 5he c#ient m4 t ,ir t a4thenticate it e#, with the 'ro<( 7Need a4thenticationWWW;. A"ache * !cat/2+2+-1 #er)let% $ Writin( Filter% Serv#et %i#ter are 6ava c#a e that can be 4 ed in Serv#et Pro!rammin! ,or the ,o##owin! '4r'o e : 5o interce't re@4e t ,rom a c#ient be,ore the( acce a re o4rce at back end. 5o mani'4#ate re 'on e ,rom erver be,ore the( are ent back to the c#ient. 5here are are vario4 t('e o, ,i#ter 4!!e ted b( the 'eci,ication : )4thentication %i#ter . Data com're ion %i#ter . 9ncr('tion %i#ter . %i#ter that tri!!er re o4rce acce event . $ma!e +onver ion %i#ter . 3o!!in! and )4ditin! %i#ter . 2$29-5FP9 +hain %i#ter . 5okeniBin! %i#ter . AS3G5 %i#ter 5hat 5ran ,orm A23 +ontent. %i#ter are de'#o(ed in the de'#o(ment de cri'tor ,i#e web+,!l and then ma' to either erv#et name or ="3 'attern in (o4r a''#ication? de'#o(ment de cri'tor. When the web container tart 4' (o4r web a''#ication, it create an in tance o, each ,i#ter that (o4 have dec#ared in the de'#o(ment de cri'tor. 5he ,i#ter e<ec4te in the order that the( are dec#ared in the de'#o(ment de cri'tor. #er)let Filter &eth d%: ) ,i#ter i im'#( a 6ava c#a that im'#ement the *ava<. erv#et.%i#ter inter,ace. 5he *ava<. erv#et.%i#ter inter,ace de,ine three method : #+N+ &eth d A De%cri"ti n "ublic ) id d Filter 8#er)letRe@ue%t, #er)letRe%" n%e, Filter7hain; 1 5hi method i ca##ed b( the container each time a re@4e tGre 'on e 'air i 'a ed thro4!h the chain d4e to a c#ient re@4e t ,or a re o4rce at the end o, the chain. "ublic ) id init8Filter7 nfi( filter7 nfi(; 2 5hi method i ca##ed b( the web container to indicate to a ,i#ter that it i bein! '#aced into ervice. "ublic ) id de%tr y8; 1 5hi method i ca##ed b( the web container to indicate to a ,i#ter that it i bein! taken o4t o, ervice. #er)let Filter E,a!"le: %o##owin! i the Serv#et %i#ter 9<am'#e that wo4#d 'rint the c#ient $P addre and c4rrent date time. 5hi e<am'#e wo4#d !ive (o4 ba ic 4nder tandin! o, Serv#et %i#ter, b4t (o4 can write more o'hi ticated ,i#ter a''#ication 4 in! the ame conce't: GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.OJ GG $m'#ement %i#ter c#a '4b#ic c#a 3o!%i#ter im'#ement %i#ter N '4b#ic void init7%i#ter+on,i! con,i!; throw Serv#et9<ce'tionN GG Get init 'arameter Strin! te tParam K con,i!.!et$nitParameter7Pte t-'aramP;J GGPrint the init 'arameter S( tem.o4t.'rint#n7P5e t Param: P > te tParam;J +2+ 3imited i a S4b idiar( o, 5+S 3imited

20-

Q '4b#ic void do%i#ter7Serv#et"e@4e t re@4e t, Serv#et"e 'on e re 'on e, %i#ter+hain chain; throw *ava.io.$&9<ce'tion, Serv#et9<ce'tion N GG Get the $P addre o, c#ient machine. Strin! i')ddre K re@4e t.!et"emote)ddr7;J GG 3o! the $P addre and c4rrent time tam'. S( tem.o4t.'rint#n7P$P P> i')ddre > P, 5ime P > new Date7;.toStrin!7;;J GG Pa re@4e t back down the ,i#ter chain chain.do%i#ter7re@4e t,re 'on e;J Q '4b#ic void de tro(7 ;N GO +a##ed be,ore the %i#ter in tance i removed ,rom ervice b( the web containerOG Q Q +om'i#e 4 (Filter+ja)a in 4 4a# wa( and '4t (o4r c#a ,i#e in V5omcat-in ta##ationdirector(RGweba'' G"&&5GW9E-$N%Gc#a e . #er)let Filter &a""in( in Web+,!l: %i#ter are de,ined and then ma''ed to a ="3 or Serv#et, in m4ch the ame wa( a Serv#et i de,ined and then ma''ed to a ="3 'attern. +reate the ,o##owin! entr( ,or ,i#ter ta! in the de'#o(ment de cri'tor ,i#e web+,!l V,i#terR V,i#ter-nameR3o!%i#terVG,i#ter-nameR V,i#ter-c#a R3o!%i#terVG,i#ter-c#a R Vinit-'aramR V'aram-nameRte t-'aramVG'aram-nameR V'aram-va#4eR$nitia#iBation ParamterVG'aram-va#4eR VGinit-'aramR VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR3o!%i#terVG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R 5he above ,i#ter wo4#d a''#( to a## the erv#et beca4 e we 'eci,ied /U in o4r con,i!4ration. Fo4 can 'ecic( a 'artic4#ar erv#et 'ath i, (o4 want to a''#( ,i#ter on ,ew erv#et on#(. Now tr( to ca## an( erv#et in 4 4a# wa( and (o4 wo4#d ee !enerated #o! in (o4r web erver #o!. Fo4 can 4 e 3o!D6 #o!!er to #o! above #o! in a e'arate ,i#e. 5%in( &ulti"le Filter%: Fo4r web a''#ication ma( de,ine evera# di,,erent ,i#ter with a 'eci,ic '4r'o e. +on ider, (o4 de,ine two ,i#ter *uthe$Filter and 5ogFilter. "e t o, the 'roce wo4#d remain a e<'#ained above e<ce't (o4 need to create a di,,erent ma''in! a mentioned be#ow: V,i#terR V,i#ter-nameR3o!%i#terVG,i#ter-nameR V,i#ter-c#a R3o!%i#terVG,i#ter-c#a R Vinit-'aramR V'aram-nameRte t-'aramVG'aram-nameR V'aram-va#4eR$nitia#iBation ParamterVG'aram-va#4eR VGinit-'aramR VG,i#terR V,i#terR V,i#ter-nameR)4then%i#terVG,i#ter-nameR V,i#ter-c#a R)4then%i#terVG,i#ter-c#a R +2+ 3imited i a S4b idiar( o, 5+S 3imited

210

Vinit-'aramR V'aram-nameRte t-'aramVG'aram-nameR V'aram-va#4eR$nitia#iBation ParamterVG'aram-va#4eR VGinit-'aramR VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR3o!%i#terVG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R V,i#ter-ma''in!R V,i#ter-nameR)4then%i#terVG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R Filter% A""licati n Order: 5he order o, ,i#ter-ma''in! e#ement in web.<m# determine the order in which the web container a''#ie the ,i#ter to the erv#et. 5o rever e the order o, the ,i#ter, (o4 *4 t need to rever e the ,i#terma''in! e#ement in the web.<m# ,i#e. %or e<am'#e, above e<am'#e wo4#d a''#( 3o!%i#ter ,ir t and then it wo4#d a''#( )4then%i#ter to an( erv#et b4t the ,o##owin! e<am'#e wo4#d rever e the order: V,i#ter-ma''in!R V,i#ter-nameR)4then%i#terVG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R V,i#ter-ma''in!R V,i#ter-nameR3o!%i#terVG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R #er)let% $ E,ce"ti n 'andlin( When a erv#et throw an e<ce'tion, the web container earche the con,i!4ration in web+,!l that 4 e the e<ce'tion-t('e e#ement ,or a match with the thrown e<ce'tion t('e. Fo4 wo4#d have to 4 e the err r$"a(e e#ement in web.<m# to 'eci,( the invocation o, erv#et in re 'on e to certain e,ce"ti n% or C55P %tatu% c de%. web+,!l 7 nfi(urati n: +on ider, (o4 have an %rrorHa$dler erve#t which wo4#d be ca##ed whenever there i an( de,ined e<ce'tion or error. %o##owin! wo4#d be the entr( created in web.<m#. VW-- erv#et de,inition --R V erv#etR V erv#et-nameR9rrorCand#erVG erv#et-nameR V erv#et-c#a R9rrorCand#erVG erv#et-c#a R VG erv#etR VW-- erv#et ma''in! --R V erv#et-ma''in!R V erv#et-nameR9rrorCand#erVG erv#et-nameR V4r#-'atternRG9rrorCand#erVG4r#-'atternR VG erv#et-ma''in!R VW-- error-code re#ated error 'a!e --R Verror-'a!eR Verror-codeRD0DVGerror-codeR V#ocationRG9rrorCand#erVG#ocationR VGerror-'a!eR Verror-'a!eR Verror-codeRD01VGerror-codeR V#ocationRG9rrorCand#erVG#ocationR VGerror-'a!eR +2+ 3imited i a S4b idiar( o, 5+S 3imited 211

VW-- e<ce'tion-t('e re#ated error 'a!e --R Verror-'a!eR Ve<ce'tion-t('eR *ava<. erv#et.Serv#et9<ce'tion VGe<ce'tion-t('e R V#ocationRG9rrorCand#erVG#ocationR VGerror-'a!eR Verror-'a!eR Ve<ce'tion-t('eR*ava.io.$&9<ce'tionVGe<ce'tion-t('e R V#ocationRG9rrorCand#erVG#ocationR VGerror-'a!eR $, (o4 want to have a !eneric 9rror Cand#er ,or a## the e<ce'tion then (o4 ho4#d de,ine ,o##owin! error'a!e in tead o, de,inin! e'arate error-'a!e e#ement ,or ever( e<ce'tion: Verror-'a!eR Ve<ce'tion-t('eR*ava.#an!.5hrowab#eVGe<ce'tion-t('e R V#ocationRG9rrorCand#erVG#ocationR VGerror-'a!eR %o##owin! are the 'oint to be noted abo4t above web.<m# ,or 9<ce'tion Cand#in!: 5he erve#t 9rrorCand#er i de,ined in 4 4a# wa( a an( other erv#et and con,i!4red in web.<m#. $, there i an( error with tat4 code either D0D 7 Not %o4nd; or D01 7 %orbidden ;, then 9rrorCand#er erv#et wo4#d be ca##ed. $, the web a''#ication throw either .ervlet%1ceptio$ or 3+%1ceptio$, then the web container invoke the G9rrorCand#er erv#et. Fo4 can de,ine di,,erent 9rror Cand#er to hand#e di,,erent t('e o, error or e<ce'tion . )bove e<am'#e i ver( m4ch !eneric and ho'e it erve the '4r'o e to e<'#ain (o4 the ba ic conce't. Re@ue%t Attribute% $ Err r%/E,ce"ti n%: %o##owin! i the #i t o, re@4e t attrib4te that an error-hand#in! erv#et can acce to ana#( e the nat4re o, errorGe<ce'tion. #+N+ Attribute A De%cri"ti n ja)a,+%er)let+err r+%tatu%Rc de 1 5hi attrib4te !ive tat4 code which can be tored and ana#( ed a,ter torin! in a *ava.#an!.$nte!er data t('e. ja)a,+%er)let+err r+e,ce"ti nRty"e 2 5hi attrib4te !ive in,ormation abo4t e<ce'tion t('e which can be tored and ana#( ed a,ter torin! in a *ava.#an!.+#a data t('e. ja)a,+%er)let+err r+!e%%a(e 1 5hi attrib4te !ive in,ormation e<act error me a!e which can be tored and ana#( ed a,ter torin! in a *ava.#an!.Strin! data t('e. ja)a,+%er)let+err r+re@ue%tRuri D 5hi attrib4te !ive in,ormation abo4t ="3 ca##in! the erv#et and it can be tored and ana#( ed a,ter torin! in a *ava.#an!.Strin! data t('e. ja)a,+%er)let+err r+e,ce"ti n . 5hi attrib4te !ive in,ormation the e<ce'tion rai ed which can be tored and ana#( ed a,ter torin! in a *ava.#an!.5hrowab#e data t('e. ja)a,+%er)let+err r+%er)letRna!e / 5hi attrib4te !ive erv#et name which can be tored and ana#( ed a,ter torin! in a *ava.#an!.Strin! data t('e. Err r 'andler #er)let E,a!"le: %o##owin! i the Serv#et 9<am'#e that wo4#d be 4 ed a 9rror Cand#er in ca e o, an( error or e<ce'tion occ4r with (o4r an( o, the erv#et de,ined. 5hi e<am'#e wo4#d !ive (o4 ba ic 4nder tandin! o, 9<ce'tion Cand#in! in Serv#et, b4t (o4 can write more o'hi ticated ,i#ter a''#ication 4 in! the ame conce't: GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 212

im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a 9rrorCand#er e<tend Ctt'Serv#et N GG 2ethod to hand#e G95 method re@4e t. '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG )na#(Be the erv#et e<ce'tion 5hrowab#e throwab#e K 75hrowab#e; re@4e t.!et)ttrib4te7P*ava<. erv#et.error.e<ce'tionP;J $nte!er tat4 +ode K 7$nte!er; re@4e t.!et)ttrib4te7P*ava<. erv#et.error. tat4 TcodeP;J Strin! erv#etName K 7Strin!; re@4e t.!et)ttrib4te7P*ava<. erv#et.error. erv#etTnameP;J i, 7 erv#etName KK n4##;N erv#etName K P=nknownPJ Q Strin! re@4e t=ri K 7Strin!; re@4e t.!et)ttrib4te7P*ava<. erv#et.error.re@4e tT4riP;J i, 7re@4e t=ri KK n4##;N re@4e t=ri K P=nknownPJ Q GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K P9rrorG9<ce'tion $n,ormationPJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP;J i, 7throwab#e KK n4## XX tat4 +ode KK n4##;N o4t.'rint#n7PVh2R9rror in,ormation i mi in!VGh2RP;J o4t.'rint#n7PP#ea e ret4rn to the Va hre,KIPP > re 'on e.encode="37Phtt':GG#oca#ho t:8080GP; > PIPRCome Pa!eVGaR.P;J Qe# e i, 7 tat4 +ode WK n4##;N o4t.'rint#n7P5he tat4 code : P > tat4 +ode;J Qe# eN o4t.'rint#n7PVh2R9rror in,ormationVGh2RP;J o4t.'rint#n7PServ#et Name : P > erv#etName > PVGbrRVGbrRP;J o4t.'rint#n7P9<ce'tion 5('e : P > throwab#e.!et+#a 7 ;.!etName7 ; > PVGbrRVGbrRP;J o4t.'rint#n7P5he re@4e t ="$: P > re@4e t=ri > PVbrRVbrRP;J o4t.'rint#n7P5he e<ce'tion me a!e: P > throwab#e.!et2e a!e7 ;;J +2+ 3imited i a S4b idiar( o, 5+S 3imited

211

Q o4t.'rint#n7PVGbod(RP;J o4t.'rint#n7PVGhtm#RP;J Q GG 2ethod to hand#e P&S5 method re@4e t. '4b#ic void doPo t7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N doGet7re@4e t, re 'on e;J Q Q +om'i#e Err r'andler+ja)a in 4 4a# wa( and '4t (o4r c#a ,i#e in V5omcat-in ta##ationdirector(RGweba'' G"&&5GW9E-$N%Gc#a e . 3et 4 add the ,o##owin! con,i!4ration in web.<m# to hand#e e<ce'tion : V erv#etR V erv#et-nameR9rrorCand#erVG erv#et-nameR V erv#et-c#a R9rrorCand#erVG erv#et-c#a R VG erv#etR VW-- erv#et ma''in! --R V erv#et-ma''in!R V erv#et-nameR9rrorCand#erVG erv#et-nameR V4r#-'atternRG9rrorCand#erVG4r#-'atternR VG erv#et-ma''in!R Verror-'a!eR Verror-codeRD0DVGerror-codeR V#ocationRG9rrorCand#erVG#ocationR VGerror-'a!eR Verror-'a!eR Ve<ce'tion-t('eR*ava.#an!.5hrowab#eVGe<ce'tion-t('e R V#ocationRG9rrorCand#erVG#ocationR VGerror-'a!eR Now tr( to 4 e a erv#et which rai e an( e<ce'tion or t('e a wron! ="3, thi wo4#d tri!!er Web +ontainer to ca## Err r'andler erv#et and di '#a( an a''ro'riate me a!e a 'ro!rammed. %or e<am'#e, i, (o4 t('e a wron! ="3 then it wo4#d di '#a( the ,o##owin! re 4#t: 5he tat4 code : D0D )bove code ma( not work with ome web brow er . So tr( with 2oBi##a and Sa,ari and it ho4#d work. #er)let% $ 7 =ie% 'andlin( +ookie are te<t ,i#e tored on the c#ient com'4ter and the( are ke't ,or vario4 in,ormation trackin! '4r'o e. 6ava Serv#et tran 'arent#( 4''ort C55P cookie . 5here are three te' invo#ved in identi,(in! ret4rnin! 4 er : Server cri't end a et o, cookie to the brow er. %or e<am'#e name, a!e, or identi,ication n4mber etc. Erow er tore thi in,ormation on #oca# machine ,or ,4t4re 4 e. When ne<t time brow er end an( re@4e t to web erver then it end tho e cookie in,ormation to the erver and erver 4 e that in,ormation to identi,( the 4 er. 5hi cha'ter wi## teach (o4 how to et or re et cookie , how to acce them and how to de#ete them. *he Anat !y f a 7 =ie: +ookie are 4 4a##( et in an C55P header 7a#tho4!h 6avaScri't can a# o et a cookie direct#( on a brow er;. ) erv#et that et a cookie mi!ht end header that #ook omethin! #ike thi : C55PG1.1 200 &H Date: %ri, 0D %eb 2000 21:01:18 G25 Server: )'acheG1.1.- 7=N$A; PCPGD.0b1 Set-+ookie: nameK<(BJ e<'ire K%rida(, 0D-%eb-00 22:01:18 G25J 'athKGJ domainKt4toria# 'oint.com +onnection: c#o e +ontent-5('e: te<tGhtm#

+2+ 3imited i a S4b idiar( o, 5+S 3imited

21D

) (o4 can ee, the Set-+ookie header contain a name va#4e 'air, a G25 date, a 'ath and a domain. 5he name and va#4e wi## be ="3 encoded. 5he e<'ire ,ie#d i an in tr4ction to the brow er to P,or!etP the cookie a,ter the !iven time and date. $, the brow er i con,i!4red to tore cookie , it wi## then kee' thi in,ormation 4nti# the e<'ir( date. $, the 4 er 'oint the brow er at an( 'a!e that matche the 'ath and domain o, the cookie, it wi## re end the cookie to the erver. 5he brow er? header mi!ht #ook omethin! #ike thi : G95 G C55PG1.0 +onnection: Hee'-)#ive = er-)!ent: 2oBi##aGD./ 7A11J $J 3in4< 2.2./-1.a'mac ''c; Co t: Bink.demon.co.4k:112/ )cce't: ima!eG!i,, OGO )cce't-9ncodin!: !Bi' )cce't-3an!4a!e: en )cce't-+har et: i o-88.--1,O,4t,-8 +ookie: nameK<(B ) erv#et wi## then have acce to the cookie thro4!h the re@4e t method re6uest#getCookies ! which ret4rn an arra( o, Cookie ob*ect . #er)let 7 =ie% &eth d%: %o##owin! i the #i t o, 4 e,4# method which (o4 can 4 e whi#e mani'4#atin! cookie in erv#et. #+N+ &eth d A De%cri"ti n "ublic ) id %etD !ain8#trin( "attern; 1 5hi method et the domain to which cookie a''#ie , ,or e<am'#e t4toria# 'oint.com. "ublic #trin( (etD !ain8; 2 5hi method !et the domain to which cookie a''#ie , ,or e<am'#e t4toria# 'oint.com. "ublic ) id %et&a,A(e8int e,"iry; 1 5hi method et how m4ch time 7in econd ; ho4#d e#a' e be,ore the cookie e<'ire . $, (o4 don?t et thi , the cookie wi## #a t on#( ,or the c4rrent e ion. "ublic int (et&a,A(e8; D 5hi method ret4rn the ma<im4m a!e o, the cookie, 'eci,ied in econd , E( de,a4#t, -1 indicatin! the cookie wi## 'er i t 4nti# brow er h4tdown. "ublic #trin( (etNa!e8; . 5hi method ret4rn the name o, the cookie. 5he name cannot be chan!ed a,ter creation. "ublic ) id %etValue8#trin( newValue; / 5hi method et the va#4e a ociated with the cookie. "ublic #trin( (etValue8; 0 5hi method !et the va#4e a ociated with the cookie. "ublic ) id %etPath8#trin( uri; 5hi method et the 'ath to which thi cookie a''#ie . $, (o4 don?t 'eci,( a 'ath, the 8 cookie i ret4rned ,or a## ="3 in the ame director( a the c4rrent 'a!e a we## a a## 4bdirectorie . "ublic #trin( (etPath8; 5hi method !et the 'ath to which thi cookie a''#ie . "ublic ) id %et#ecure8b lean fla(; 10 5hi method et the boo#ean va#4e indicatin! whether the cookie ho4#d on#( be ent over encr('ted 7i.e. SS3; connection . "ublic ) id %et7 !!ent8#trin( "ur" %e; 11 5hi method 'eci,ie a comment that de cribe a cookie? '4r'o e. 5he comment i 4 e,4# i, the brow er 're ent the cookie to the 4 er. "ublic #trin( (et7 !!ent8; 12 5hi method ret4rn the comment de cribin! the '4r'o e o, thi cookie, or n4## i, the cookie ha no comment. #ettin( 7 =ie% with #er)let: Settin! cookie with erv#et invo#ve three te' : +2+ 3imited i a S4b idiar( o, 5+S 3imited 21.

8B; 7reatin( a 7 =ie bject: Fo4 ca## the +ookie con tr4ctor with a cookie name and a cookie va#4e, both o, which are trin! . +ookie cookie K new +ookie7Pke(P,Pva#4eP;J Hee' in mind, neither the name nor the va#4e ho4#d contain white 'ace or an( o, the ,o##owin! character : 8:7;K,PG[^:J 8-; #ettin( the !a,i!u! a(e: Fo4 4 e et2a<)!e to 'eci,( how #on! 7in econd ; the cookie ho4#d be va#id. %o##owin! wo4#d et 4' a cookie ,or 2D ho4r . cookie. et2a<)!e7/0O/0O2D;J 8J; #endin( the 7 =ie int the '**P re%" n%e header%: Fo4 4 e re%" n%e+add7 =ie to add cookie in the C55P re 'on e header a ,o##ow : re 'on e.add+ookie7cookie;J E,a!"le: 3et 4 modi,( o4r %orm 9<am'#e to et the cookie ,or ,ir t and #a t name. GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a Ce##o%orm e<tend Ctt'Serv#et N '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG +reate cookie ,or ,ir t and #a t name . +ookie ,ir tName K new +ookie7P,ir tTnameP, re@4e t.!etParameter7P,ir tTnameP;;J +ookie #a tName K new +ookie7P#a tTnameP, re@4e t.!etParameter7P#a tTnameP;;J GG Set e<'ir( date a,ter 2D Cr ,or both the cookie . ,ir tName. et2a<)!e7/0O/0O2D;J #a tName. et2a<)!e7/0O/0O2D;J GG )dd both the cookie in the re 'on e header. re 'on e.add+ookie7 ,ir tName ;J re 'on e.add+ookie7 #a tName ;J GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K PSettin! +ookie 9<am'#ePJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PV4#RInP > P V#iRVbR%ir t NameVGbR: P > re@4e t.!etParameter7P,ir tTnameP; > PInP > P V#iRVbR3a t NameVGbR: P > re@4e t.!etParameter7P#a tTnameP; > PInP > PVG4#RInP > +2+ 3imited i a S4b idiar( o, 5+S 3imited 21/

PVGbod(RVGhtm#RP;J Q Q +om'i#e above erv#et 'ell F r! and create a''ro'riate entr( in web.<m# ,i#e and ,ina##( tr( ,o##owin! C523 'a!e to ca## erv#et. Vhtm#R Vbod(R V,orm actionKPCe##o%ormP methodKPG95PR %ir t Name: Vin'4t t('eKPte<tP nameKP,ir tTnamePR Vbr GR 3a t Name: Vin'4t t('eKPte<tP nameKP#a tTnameP GR Vin'4t t('eKP 4bmitP va#4eKPS4bmitP GR VG,ormR VGbod(R VGhtm#R Hee' above C523 content in a ,i#e Ce##o.htm and '4t it in V5omcat-in ta##ationdirector(RGweba'' G"&&5 director(. When (o4 wo4#d acce http:>>localhost:?@?@>Hello#htm, here i the act4a# o4t'4t o, the above ,orm. %ir t Name: 3a t Name: 5r( to enter %ir t Name and 3a t Name and then c#ick 4bmit b4tton. 5hi wo4#d di '#a( ,ir t name and #a t name on (o4r creen and ame time it wo4#d et two cookie ,ir tName and #a tName which wo4#d be 'a ed back to the erver when ne<t time (o4 wo4#d 're S4bmit b4tton. Ne<t ection wo4#d e<'#ain (o4 how (o4 wo4#d acce the e cookie back in (o4r web a''#ication. Readin( 7 =ie% with #er)let: 5o read cookie , (o4 need to create an arra( o, java1#servlet#http#Cookie ob*ect b( ca##in! the (et7 =ie%8 ; method o, Http.ervlet,e6uest. 5hen c(c#e thro4!h the arra(, and 4 e !etName7; and !etVa#4e7; method to acce each cookie and a ociated va#4e. E,a!"le: 3et 4 read cookie which we have et in 'revio4 e<am'#e: GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a "ead+ookie e<tend Ctt'Serv#et N '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N +ookie cookie K n4##J +ookie8: cookie K n4##J GG Get an arra( o, +ookie a ociated with thi domain cookie K re@4e t.!et+ookie 7;J GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K P"eadin! +ookie 9<am'#ePJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 210

o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP ;J i,7 cookie WK n4## ;N o4t.'rint#n7PVh2R %o4nd +ookie Name and Va#4eVGh2RP;J ,or 7int i K 0J i V cookie .#en!thJ i>>;N cookie K cookie 8i:J o4t.'rint7PName : P > cookie.!etName7 ; > P, P;J o4t.'rint7PVa#4e: P > cookie.!etVa#4e7 ;>P VbrGRP;J Q Qe# eN o4t.'rint#n7 PVh2RNo cookie ,o4nd VGh2RP;J Q o4t.'rint#n7PVGbod(RP;J o4t.'rint#n7PVGhtm#RP;J Q Q +om'i#e above erv#et Read7 =ie% and create a''ro'riate entr( in web.<m# ,i#e. $, (o4 wo4#d have et ,ir tTname cookie a P6ohnP and #a tTname cookie a PP#a(erP then r4nnin! http:>>localhost:?@?@>,eadCookies wo4#d di '#a( the ,o##owin! re 4#t: F und 7 =ie% Na!e and Value Name : ,ir tTname, Va#4e: 6ohn Name : #a tTname, Va#4e: P#a(er Delete 7 =ie% with #er)let: 5o de#ete cookie i ver( im'#e. $, (o4 want to de#ete a cookie then (o4 im'#( need to ,o##ow 4' ,o##owin! three te' : "ead an a#read( e< itin! cookie and tore it in +ookie ob*ect. Set cookie a!e a Bero 4 in! %et&a,A(e8; method to de#ete an e<i tin! cookie. )dd thi cookie back into re 'on e header. E,a!"le: %o##owin! e<am'#e wo4#d de#ete and e<i tin! cookie named P,ir tTnameP and when (o4 wo4#d r4n "ead+ookie erv#et ne<t time it wo4#d ret4rn n4## va#4e ,or ,ir tTname. GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a De#ete+ookie e<tend Ctt'Serv#et N '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N +ookie cookie K n4##J +ookie8: cookie K n4##J GG Get an arra( o, +ookie a ociated with thi domain cookie K re@4e t.!et+ookie 7;J GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K PDe#ete +ookie 9<am'#ePJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > +2+ 3imited i a S4b idiar( o, 5+S 3imited 218

Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP ;J i,7 cookie WK n4## ;N o4t.'rint#n7PVh2R +ookie Name and Va#4eVGh2RP;J ,or 7int i K 0J i V cookie .#en!thJ i>>;N cookie K cookie 8i:J i,77cookie.!etName7 ;;.com'are5o7P,ir tTnameP; KK 0 ;N cookie. et2a<)!e70;J re 'on e.add+ookie7cookie;J o4t.'rint7PDe#eted cookie : P > cookie.!etName7 ; > PVbrGRP;J Q o4t.'rint7PName : P > cookie.!etName7 ; > P, P;J o4t.'rint7PVa#4e: P > cookie.!etVa#4e7 ;>P VbrGRP;J Q Qe# eN o4t.'rint#n7 PVh2RNo cookie ,o4nd VGh2RP;J Q o4t.'rint#n7PVGbod(RP;J o4t.'rint#n7PVGhtm#RP;J Q Q +om'i#e above erv#et Delete7 =ie% and create a''ro'riate entr( in web.<m# ,i#e. Now r4nnin! http:>>localhost:?@?@>9eleteCookies wo4#d di '#a( the ,o##owin! re 4#t: 7 =ie% Na!e and Value De#eted cookie : ,ir tTname Name : ,ir tTname, Va#4e: 6ohn Name : #a tTname, Va#4e: P#a(er Now tr( to r4n http:>>localhost:?@?@>,eadCookies and it wo4#d di '#a( on#( one cookie a ,o##ow : F und 7 =ie% Na!e and Value Name : #a tTname, Va#4e: P#a(er Fo4 can de#ete (o4r cookie in $nternet 9<'#orer man4a##(. Start at the 5oo# men4 and e#ect $nternet &'tion . 5o de#ete a## cookie , 're De#ete +ookie . #er)let% $ #e%%i n *rac=in( C55P i a P tate#e P 'rotoco# which mean each time a c#ient retrieve a Web 'a!e, the c#ient o'en a e'arate connection to the Web erver and the erver a4tomatica##( doe not kee' an( record o, 'revio4 c#ient re@4e t. Sti## there are ,o##owin! three wa( to maintain e ion between web c#ient and web erver: 7 =ie%: ) web erver can a i!n a 4ni@4e e ion $D a a cookie to each web c#ient and ,or 4b e@4ent re@4e t ,rom the c#ient the( can be reco!niBed 4 in! the recieved cookie. 5hi ma( not be an e,,ective wa( beca4 e man( time brow er doe not 4''ort a cookie, o $ wo4#d not recommend to 4 e thi 'roced4re to maintain the e ion . 'idden F r! Field%: ) web erver can end a hidden C523 ,orm ,ie#d a#on! with a 4ni@4e e ion $D a ,o##ow : Vin'4t t('eKPhiddenP nameKP e ionidP va#4eKP121D.PR 5hi entr( mean that, when the ,orm i 4bmitted, the 'eci,ied name and va#4e are a4tomatica##( inc#4ded in the G95 or P&S5 data. 9ach time when web brow er end re@4e t back, then e ionTid va#4e can be 4 ed to kee' the track o, di,,erent web brow er . 5hi co4#d be an e,,ective wa( o, kee'in! track o, the e ion b4t c#ickin! on a re!4#ar 7V) C"9%...R; h('erte<t #ink doe not re 4#t in a ,orm 4bmi ion, o hidden ,orm ,ie#d a# o cannot 4''ort !enera# e ion trackin!. 5R4 Rewritin(: +2+ 3imited i a S4b idiar( o, 5+S 3imited 21-

Fo4 can a''end ome e<tra data on the end o, each ="3 that identi,ie the e ion, and the erver can a ociate that e ion identi,ier with data it ha tored abo4t that e ion. %or e<am'#e, with htt':GGt4toria# 'oint.comG,i#e.htmJ e ionidK121D., the e ion identi,ier i attached a e ionidK121D. which can be acce ed at the web erver to identi,( the c#ient. ="3 rewritin! i a better wa( to maintain e ion and work ,or the brow er when the( don?t 4''ort cookie b4t here drawback i that (o4 wo4#d have !enerate ever( ="3 d(namica##( to a i!n a e ion $D tho4!h 'a!e i im'#e tatic C523 'a!e. *he 'tt"#e%%i n Object: )'art ,rom the above mentioned three wa( , erv#et 'rovide Ctt'Se ion $nter,ace which 'rovide a wa( to identi,( a 4 er acro more than one 'a!e re@4e t or vi it to a Web ite and to tore in,ormation abo4t that 4 er. 5he erv#et container 4 e thi inter,ace to create a e ion between an C55P c#ient and an C55P erver. 5he e ion 'er i t ,or a 'eci,ied time 'eriod, acro more than one connection or 'a!e re@4e t ,rom the 4 er. Fo4 wo4#d !et Ctt'Se ion ob*ect b( ca##in! the '4b#ic method (et#e%%i n8; o, Ctt'Serv#et"e@4e t, a be#ow: Ctt'Se ion e ion K re@4e t.!etSe ion7;J Fo4 need to ca## re6uest#get.essio$ ! be,ore (o4 end an( doc4ment content to the c#ient. Cere i a 4mmar( o, the im'ortant method avai#ab#e thro4!h Ctt'Se ion ob*ect: #+N+ &eth d A De%cri"ti n "ublic Object (etAttribute8#trin( na!e; 1 5hi method ret4rn the ob*ect bo4nd with the 'eci,ied name in thi e ion, or n4## i, no ob*ect i bo4nd 4nder the name. "ublic Enu!erati n (etAttributeNa!e%8; 2 5hi method ret4rn an 9n4meration o, Strin! ob*ect containin! the name o, a## the ob*ect bo4nd to thi e ion. "ublic l n( (et7reati n*i!e8; 1 5hi method ret4rn the time when thi e ion wa created, mea 4red in mi##i econd ince midni!ht 6an4ar( 1, 1-00 G25. "ublic #trin( (etId8; D 5hi method ret4rn a trin! containin! the 4ni@4e identi,ier a i!ned to thi e ion. "ublic l n( (et4a%tAcce%%ed*i!e8; . 5hi method ret4rn the #a t time the c#ient ent a re@4e t a ociated with thi e ion, a the n4mber o, mi##i econd ince midni!ht 6an4ar( 1, 1-00 G25. "ublic int (et&a,Inacti)eInter)al8; / 5hi method ret4rn the ma<im4m time interva#, in econd , that the erv#et container wi## kee' thi e ion o'en between c#ient acce e . "ublic ) id in)alidate8; 0 5hi method inva#idate thi e ion and 4nbind an( ob*ect bo4nd to it. "ublic b lean i%New8 8 5hi method ret4rn tr4e i, the c#ient doe not (et know abo4t the e ion or i, the c#ient choo e not to *oin the e ion. "ublic ) id re! )eAttribute8#trin( na!e; 5hi method remove the ob*ect bo4nd with the 'eci,ied name ,rom thi e ion. "ublic ) id %etAttribute8#trin( na!e, Object )alue; 10 5hi method bind an ob*ect to thi e ion, 4 in! the name 'eci,ied. "ublic ) id %et&a,Inacti)eInter)al8int inter)al; 11 5hi method 'eci,ie the time, in econd , between c#ient re@4e t be,ore the erv#et container wi## inva#idate thi e ion. #e%%i n *rac=in( E,a!"le: 5hi e<am'#e de cribe how to 4 e the Ctt'Se ion ob*ect to ,ind o4t the creation time and the #a tacce ed time ,or a e ion. We wo4#d a ociate a new e ion with the re@4e t i, one doe not a#read( e<i t. GG $m'ort re@4ired *ava #ibrarie +2+ 3imited i a S4b idiar( o, 5+S 3imited 220

im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a Se ion5rack e<tend Ctt'Serv#et N '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG +reate a e ion ob*ect i, it i a#read( not created. Ctt'Se ion e ion K re@4e t.!etSe ion7tr4e;J GG Get e ion creation time. Date create5ime K new Date7 e ion.!et+reation5ime7;;J GG Get #a t acce time o, thi web 'a!e. Date #a t)cce 5ime K new Date7 e ion.!et3a t)cce ed5ime7;;J Strin! tit#e K PWe#come Eack to m( web itePJ $nte!er vi it+o4nt K new $nte!er70;J Strin! vi it+o4ntHe( K new Strin!7Pvi it+o4ntP;J Strin! 4 er$DHe( K new Strin!7P4 er$DP;J Strin! 4 er$D K new Strin!7P)E+DP;J GG +heck i, thi i new comer on (o4r web 'a!e. i, 7 e ion.i New7;;N tit#e K PWe#come to m( web itePJ e ion. et)ttrib4te74 er$DHe(, 4 er$D;J Q e# e N vi it+o4nt K 7$nte!er; e ion.!et)ttrib4te7vi it+o4ntHe(;J vi it+o4nt K vi it+o4nt > 1J 4 er$D K 7Strin!; e ion.!et)ttrib4te74 er$DHe(;J Q e ion. et)ttrib4te7vi it+o4ntHe(, vi it+o4nt;J GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PVh2 a#i!nKIPcenterIPRSe ion $n,omationVGh2RInP > PVtab#e borderKIP1IP a#i!nKIPcenterIPRInP > PVtr b!co#orKIPb-D-D-DIPRInP > P VthRSe ion in,oVGthRVthRva#4eVGthRVGtrRInP > PVtrRInP > P VtdRidVGtdRInP > P VtdRP > e ion.!et$d7; > PVGtdRVGtrRInP > PVtrRInP > P VtdR+reation 5imeVGtdRInP > P VtdRP > create5ime > +2+ 3imited i a S4b idiar( o, 5+S 3imited

221

P VGtdRVGtrRInP > PVtrRInP > P VtdR5ime o, 3a t )cce VGtdRInP > P VtdRP > #a t)cce 5ime > P VGtdRVGtrRInP > PVtrRInP > P VtdR= er $DVGtdRInP > P VtdRP > 4 er$D > P VGtdRVGtrRInP > PVtrRInP > P VtdRN4mber o, vi it VGtdRInP > P VtdRP > vi it+o4nt > PVGtdRVGtrRInP > PVGtab#eRInP > PVGbod(RVGhtm#RP;J Q Q +om'i#e above erv#et #e%%i n*rac= and create a''ro'riate entr( in web.<m# ,i#e. Now r4nnin! http:>>localhost:?@?@>.essio$(rack wo4#d di '#a( the ,o##owin! re 4#t when (o4 wo4#d r4n ,or the ,ir t time: Welc !e t !y web%ite #e%%i n Inf !ati n #e%%i n inf )alue id +reation 5ime 5ime o, 3a t )cce = er $D 0)919+-1%%DD91+.2.ED1.1E00) EE2D. 54e 64n 08 10:2/:D0 G25>0D:00 2010 54e 64n 08 10:2/:D0 G25>0D:00 2010 )E+D

N4mber o, vi it 0 Now tr( to r4n the ame erv#et ,or econd time, it wo4#d di '#a( ,o##owin! re 4#t. Welc !e 6ac= t !y web%ite #e%%i n Inf !ati n inf ty"e )alue id +reation 5ime 5ime o, 3a t )cce = er $D 0)919+-1%%DD91+.2.ED1.1E00) EE2D. 54e 64n 08 10:2/:D0 G25>0D:00 2010 54e 64n 08 10:2/:D0 G25>0D:00 2010 )E+D

N4mber o, vi it 1 Deletin( #e%%i n Data: When (o4 are done with a 4 er? e ion data, (o4 have evera# o'tion : Re! )e a "articular attribute: Fo4 can ca## public void remove*ttribute .tri$g $ame! method to de#ete the va#4e a ociated with a 'artic4#ar ke(. Delete the wh le %e%%i n: Fo4 can ca## public void i$validate ! method to di card an entire e ion. #ettin( #e%%i n ti!e ut: Fo4 can ca## public void setMa13$active3$terval i$t i$terval! method to et the timeo4t ,or a e ion individ4a##(. +2+ 3imited i a S4b idiar( o, 5+S 3imited 222

4 ( the u%er ut: 5he erver that 4''ort erv#et 2.D, (o4 can ca## l ( ut to #o! the c#ient o4t o, the Web erver and inva#idate a## e ion be#on!in! to a## the 4 er . web+,!l 7 nfi(urati n: $, (o4 are 4 in! 5omcat, a'art ,rom the above mentioned method , (o4 can con,i!4re e ion time o4t in web.<m# ,i#e a ,o##ow . V e ion-con,i!R V e ion-timeo4tR1.VG e ion-timeo4tR VG e ion-con,i!R 5he timeo4t i e<'re ed a min4te , and override the de,a4#t timeo4t which i 10 min4te in 5omcat. 5he !et2a<$nactive$nterva#7 ; method in a erv#et ret4rn the timeo4t 'eriod ,or that e ion in econd . So i, (o4r e ion i con,i!4red in web.<m# ,or 1. min4te , !et2a<$nactive$nterva#7 ; ret4rn -00. #er)let% $ Databa%e Acce%% 5hi t4toria# a 4me (o4 have 4nder tandin! on how 6DE+ a''#ication work . Ee,ore tartin! with databa e acce thro4!h a erv#et, make 4re (o4 have 'ro'er 6DE+ environment et4' a#on! with a databa e. %or more detai# on how to acce databa e 4 in! 6DE+ and it environment et4' (o4 can !o thro4!h o4r 6DE+ 54toria#. 5o tart with ba ic conce't, #et 4 create a im'#e tab#e and create ,ew record in that tab#e a ,o##ow : 7reate *able 5o create the E!"l yee% tab#e in 59S5 databa e, 4 e the ,o##owin! te' : #te" B: &'en a 7 !!and Pr !"t and chan!e to the in ta##ation director( a ,o##ow : +:IR +:IRcd Pro!ram %i#e I2(S]3Ibin +:IPro!ram %i#e I2(S]3IbinR #te" -: 3o!in to databa e a ,o##ow +:IPro!ram %i#e I2(S]3IbinRm( @# -4 root -' 9nter 'a word: OOOOOOOO m( @#R #te" J: +reate the tab#e E!"l yee in *E#* databa e a ,o##ow : m( @#R 4 e 59S5J m( @#R create tab#e 9m'#o(ee 7 id int not n4##, a!e int not n4##, ,ir t varchar 72..;, #a t varchar 72..; ;J ]4er( &H, 0 row a,,ected 70.08 ec; m( @#R 7reate Data Rec rd% %ina##( (o4 create ,ew record in 9m'#o(ee tab#e a ,o##ow : m( @#R $NS9"5 $N5& 9m'#o(ee V)3=9S 7100, 18, ?Sara?, ?)#i?;J ]4er( &H, 1 row a,,ected 70.0. ec; m( @#R $NS9"5 $N5& 9m'#o(ee V)3=9S 7101, 2., ?2ahnaB?, ?%atma?;J ]4er( &H, 1 row a,,ected 70.00 ec; m( @#R $NS9"5 $N5& 9m'#o(ee V)3=9S 7102, 10, ?Said?, ?Hhan?;J ]4er( &H, 1 row a,,ected 70.00 ec; m( @#R $NS9"5 $N5& 9m'#o(ee V)3=9S 7101, 28, ?S4mit?, ?2itta#?;J ]4er( &H, 1 row a,,ected 70.00 ec; m( @#R Acce%%in( a Databa%e: Cere i an e<am'#e which how how to acce 59S5 databa e 4 in! Serv#et. GG 3oadin! re@4ired #ibrarie +2+ 3imited i a S4b idiar( o, 5+S 3imited

221

im'ort *ava.io.OJ im'ort *ava.4ti#.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava. @#.OJ '4b#ic c#a Databa e)cce e<tend Ctt'Serv#etN

'4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG 6DE+ driver name and databa e ="3 tatic ,ina# Strin! 6DE+TD"$V9"KPcom.m( @#.*dbc.DriverPJ tatic ,ina# Strin! DET="3KP*dbc:m( @#:GG#oca#ho tG59S5PJ GG Databa e credentia# tatic ,ina# Strin! =S9" K ProotPJ tatic ,ina# Strin! P)SS K P'a wordPJ GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K PDataba e "e 4#tPJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP;J tr(N GG "e!i ter 6DE+ driver +#a .,orName7Pcom.m( @#.*dbc.DriverP;J GG &'en a connection conn K Driver2ana!er.!et+onnection7DET="3,=S9",P)SS;J GG 9<ec4te S]3 @4er( tmt K conn.createStatement7;J Strin! @#J @# K PS939+5 id, ,ir t, #a t, a!e %"&2 9m'#o(ee PJ "e 4#tSet r K tmt.e<ec4te]4er(7 @#;J GG 9<tract data ,rom re 4#t et whi#e7r .ne<t7;;N GG"etrieve b( co#4mn name int id K r .!et$nt7PidP;J int a!e K r .!et$nt7Pa!eP;J Strin! ,ir t K r .!etStrin!7P,ir tP;J Strin! #a t K r .!etStrin!7P#a tP;J GGDi '#a( va#4e o4t.'rint#n7P$D: P > id > PVbrRP;J o4t.'rint#n7P, )!e: P > a!e > PVbrRP;J o4t.'rint#n7P, %ir t: P > ,ir t > PVbrRP;J o4t.'rint#n7P, 3a t: P > #a t > PVbrRP;J Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 22D

o4t.'rint#n7PVGbod(RVGhtm#RP;J GG +#ean-4' environment r .c#o e7;J tmt.c#o e7;J conn.c#o e7;J Qcatch7S]39<ce'tion e;N GGCand#e error ,or 6DE+ e.'rintStack5race7;J Qcatch79<ce'tion e;N GGCand#e error ,or +#a .,orName e.'rintStack5race7;J Q,ina##(N GG,ina##( b#ock 4 ed to c#o e re o4rce tr(N i,7 tmtWKn4##; tmt.c#o e7;J Qcatch7S]39<ce'tion e2;N QGG nothin! we can do tr(N i,7connWKn4##; conn.c#o e7;J Qcatch7S]39<ce'tion e;N e.'rintStack5race7;J QGGend ,ina##( tr( Q GGend tr( Q Q Now #et 4 com'i#e above erv#et and create ,o##owin! entrie in web.<m# .... V erv#etR V erv#et-nameRDataba e)cce VG erv#et-nameR V erv#et-c#a RDataba e)cce VG erv#et-c#a R VG erv#etR V erv#et-ma''in!R V erv#et-nameRDataba e)cce VG erv#et-nameR V4r#-'atternRGDataba e)cce VG4r#-'atternR VG erv#et-ma''in!R .... Now ca## thi erv#et 4 in! ="3 htt':GG#oca#ho t:8080GDataba e)cce re 'on e: Databa%e Re%ult

which wo4#d di '#a( ,o##owin!

$D: 100, )!e: 18, %ir t: Sara, 3a t: )#i $D: 101, )!e: 2., %ir t: 2ahnaB, 3a t: %atma $D: 102, )!e: 10, %ir t: Said, 3a t: Hhan $D: 101, )!e: 28, %ir t: S4mit, 3a t: 2itta# #er)let% $ File 5"l adin( ) Serv#et can be 4 ed with an C523 ,orm ta! to a##ow 4 er to 4'#oad ,i#e to the erver. )n 4'#oaded ,i#e co4#d be a te<t ,i#e or ima!e ,i#e or an( doc4ment. 7reatin( a File 5"l ad F r!: 5he ,o##owin! C52 code be#ow create an 4'#oader ,orm. %o##owin! are the im'ortant 'oint to be noted down: 5he ,orm !eth d attrib4te ho4#d be et to PO#* method and G95 method can not be 4 ed. 5he ,orm encty"e attrib4te ho4#d be et to !ulti"art/f r!$data. 5he ,orm acti n attrib4te ho4#d be et to a erv#et ,i#e which wo4#d hand#e ,i#e 4'#oadin! at backend erver. %o##owin! e<am'#e i 4 in! 5"l ad#er)let erv#et to 4'#oad ,i#e. +2+ 3imited i a S4b idiar( o, 5+S 3imited 22.

5o 4'#oad a in!#e ,i#e (o4 ho4#d 4 e a in!#e Vin'4t ...GR ta! with attrib4te t('eKP,i#eP. 5o a##ow m4#ti'#e ,i#e 4'#oadin!, inc#4de more than one in'4t ta! with di,,erent va#4e ,or the name attrib4te. 5he brow er a ociate a Erow e b4tton with each o, them.

Vhtm#R VheadR Vtit#eR%i#e ='#oadin! %ormVGtit#eR VGheadR Vbod(R Vh1R%i#e ='#oad:VGh1R Se#ect a ,i#e to 4'#oad: Vbr GR V,orm actionKP='#oadServ#etP methodKP'o tP enct('eKPm4#ti'artG,orm-dataPR Vin'4t t('eKP,i#eP nameKP,i#eP iBeKP.0P GR Vbr GR Vin'4t t('eKP 4bmitP va#4eKP='#oad %i#eP GR VG,ormR VGbod(R VGhtm#R 5hi wi## di '#a( ,o##owin! re 4#t which wo4#d a##ow to e#ect a ,i#e ,rom #oca# P+ and when 4 er wo4#d c#ick at P='#oad %i#eP, ,orm wo4#d be 4bmitted a#on! with the e#ected ,i#e: File 5"l ad: Se#ect a ,i#e to 4'#oad: N&59: 5hi i *4 t d4mm( ,orm and wo4#d not work. Writin( 6ac=end #er)let: %o##owin! i the erv#et 5"l ad#er)let which wo4#d take care o, acce'tin! 4'#oaded ,i#e and to tore it in director( V5omcat-in ta##ation-director(RGweba'' Gdata. 5hi director( name co4#d a# o be added 4 in! an e<terna# con,i!4ration 4ch a a c nte,t$"ara! e#ement in web.<m# a ,o##ow : Vweb-a''R .... Vconte<t-'aramR Vde cri'tionR3ocation to tore 4'#oaded ,i#eVGde cri'tionR V'aram-nameR,i#e-4'#oadVG'aram-nameR V'aram-va#4eR c:Ia'ache-tomcat-....2-Iweba'' IdataI VG'aram-va#4eR VGconte<t-'aramR .... VGweb-a''R %o##owin! i the o4rce code ,or ='#oadServ#et which can hand#e m4#ti'#e ,i#e 4'#oadin! at a time. Ee,ore 'roceddin! (o4 have make 4re the ,o##owin! : %o##owin! e<am'#e de'end on %i#e='#oad, o make 4re (o4 have the #ate t ver ion o, c !! n%$fileu"l ad+,+,+jar ,i#e in (o4r c#a 'ath. %i#e='#oad de'end on +ommon $&, o make 4re (o4 have the #ate t ver ion o, c !! n%$ i $,+,+jar ,i#e in (o4r c#a 'ath. Whi#e te tin! ,o##owin! e<am'#e, (o4 ho4#d 4'#oad a ,i#e which ha #e iBe than ma1File.i/e otherwi e ,i#e wo4#d not be 4'#oaded. 2ake 4re (o4 have created directorie c:Item' and c:Ia'ache-tomcat-....2-Iweba'' Idata we## in advance. GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava.4ti#.OJ im'ort *ava<. im'ort *ava<. im'ort *ava<. im'ort *ava<. erv#et.Serv#et+on,i!J erv#et.Serv#et9<ce'tionJ erv#et.htt'.Ctt'Serv#etJ erv#et.htt'.Ctt'Serv#et"e@4e tJ +2+ 3imited i a S4b idiar( o, 5+S 3imited

22/

im'ort *ava<. erv#et.htt'.Ctt'Serv#et"e 'on eJ im'ort or!.a'ache.common im'ort or!.a'ache.common im'ort or!.a'ache.common im'ort or!.a'ache.common im'ort or!.a'ache.common '4b#ic c#a .,i#e4'#oad.%i#e$temJ .,i#e4'#oad.%i#e='#oad9<ce'tionJ .,i#e4'#oad.di k.Di k%i#e$tem%actor(J .,i#e4'#oad. erv#et.Serv#et%i#e='#oadJ .io.o4t'4t.OJ

='#oadServ#et e<tend Ctt'Serv#et N

'rivate boo#ean i 24#ti'artJ 'rivate Strin! ,i#ePathJ 'rivate int ma<%i#eSiBe K .0 O 102DJ 'rivate int ma<2emSiBe K D O 102DJ 'rivate %i#e ,i#e J '4b#ic void init7 ;N GG Get the ,i#e #ocation where it wo4#d be tored. ,i#ePath K !etServ#et+onte<t7;.!et$nitParameter7P,i#e-4'#oadP;J Q '4b#ic void doPo t7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, *ava.io.$&9<ce'tion N GG +heck that we have a ,i#e 4'#oad re@4e t i 24#ti'art K Serv#et%i#e='#oad.i 24#ti'art+ontent7re@4e t;J re 'on e. et+ontent5('e7Pte<tGhtm#P;J *ava.io.PrintWriter o4t K re 'on e.!etWriter7 ;J i,7 Wi 24#ti'art ;N o4t.'rint#n7PVhtm#RP;J o4t.'rint#n7PVheadRP;J o4t.'rint#n7PVtit#eRServ#et 4'#oadVGtit#eRP;J o4t.'rint#n7PVGheadRP;J o4t.'rint#n7PVbod(RP;J o4t.'rint#n7PV'RNo ,i#e 4'#oadedVG'RP;J o4t.'rint#n7PVGbod(RP;J o4t.'rint#n7PVGhtm#RP;J ret4rnJ Q Di k%i#e$tem%actor( ,actor( K new Di k%i#e$tem%actor(7;J GG ma<im4m iBe that wi## be tored in memor( ,actor(. etSiBe5hre ho#d7ma<2emSiBe;J GG 3ocation to ave data that i #ar!er than ma<2emSiBe. ,actor(. et"e'o itor(7new %i#e7Pc:IItem'P;;J GG +reate a new ,i#e 4'#oad hand#er Serv#et%i#e='#oad 4'#oad K new Serv#et%i#e='#oad7,actor(;J GG ma<im4m ,i#e iBe to be 4'#oaded. 4'#oad. etSiBe2a<7 ma<%i#eSiBe ;J tr(N GG Par e the re@4e t to !et ,i#e item . 3i t ,i#e$tem K 4'#oad.'ar e"e@4e t7re@4e t;J GG Proce the 4'#oaded ,i#e item $terator i K ,i#e$tem .iterator7;J o4t.'rint#n7PVhtm#RP;J o4t.'rint#n7PVheadRP;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 220

o4t.'rint#n7PVtit#eRServ#et 4'#oadVGtit#eRP;J o4t.'rint#n7PVGheadRP;J o4t.'rint#n7PVbod(RP;J whi#e 7 i.ha Ne<t 7; ; N %i#e$tem ,i K 7%i#e$tem;i.ne<t7;J i, 7 W,i.i %orm%ie#d 7; ; N GG Get the 4'#oaded ,i#e 'arameter Strin! ,ie#dName K ,i.!et%ie#dName7;J Strin! ,i#eName K ,i.!etName7;J Strin! content5('e K ,i.!et+ontent5('e7;J boo#ean i $n2emor( K ,i.i $n2emor(7;J #on! iBe$nE(te K ,i.!etSiBe7;J GG Write the ,i#e i,7 ,i#eName.#a t$nde<&,7PIIP; RK 0 ;N ,i#e K new %i#e7 ,i#ePath > ,i#eName. 4b trin!7 ,i#eName.#a t$nde<&,7PIIP;;; J Qe# eN ,i#e K new %i#e7 ,i#ePath > ,i#eName. 4b trin!7,i#eName.#a t$nde<&,7PIIP;>1;; J Q ,i.write7 ,i#e ; J o4t.'rint#n7P='#oaded %i#ename: P > ,i#eName > PVbrRP;J Q Q o4t.'rint#n7PVGbod(RP;J o4t.'rint#n7PVGhtm#RP;J Qcatch79<ce'tion e<; N S( tem.o4t.'rint#n7e<;J Q Q '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, *ava.io.$&9<ce'tion N throw new Serv#et9<ce'tion7PG95 method 4 ed with P > !et+#a 7 ;.!etName7 ;>P: P&S5 method re@4ired.P;J Q Q 7 !"ile and Runnin( #er)let: +om'i#e above erv#et ='#oadServ#et and create re@4ired entr( in web.<m# ,i#e a ,o##ow . V erv#etR V erv#et-nameR='#oadServ#etVG erv#et-nameR V erv#et-c#a R='#oadServ#etVG erv#et-c#a R VG erv#etR V erv#et-ma''in!R V erv#et-nameR='#oadServ#etVG erv#et-nameR V4r#-'atternRG='#oadServ#etVG4r#-'atternR VG erv#et-ma''in!R Now tr( to 4'#oad ,i#e 4 in! the C523 ,orm which (o4 created above. When (o4 wo4#d tr( htt':GG#oca#ho t:8080G='#oad%i#e.htm, it wo4#d di '#a( ,o##owin! re 4#t which wo4#d he#' (o4 4'#oadin! an( ,i#e ,rom (o4r #oca# machine. File 5"l ad: Se#ect a ,i#e to 4'#oad: $, (o4r erve#t cri't work ,ine, (o4r ,i#e ho4#d be 4'#oaded in c:Ia'ache-tomcat-....2-Iweba'' IdataI director(. +2+ 3imited i a S4b idiar( o, 5+S 3imited 228

#er)let $ 'andlin( Date &ne o, the mo t im'ortant advanta!e o, 4 in! Serv#et i that (o4 can 4 e mo t o, the method avai#ab#e in core 6ava. 5hi t4toria# wo4#d take (o4 thro4!h 6ava 'rovided Date c#a which i avai#ab#e in ja)a+util 'acka!e, thi c#a enca' 4#ate the c4rrent date and time. 5he Date c#a 4''ort two con tr4ctor . 5he ,ir t con tr4ctor initia#iBe the ob*ect with the c4rrent date and time. Date7 ; 5he ,o##owin! con tr4ctor acce't one ar!4ment that e@4a# the n4mber o, mi##i econd that have e#a' ed ince midni!ht, 6an4ar( 1, 1-00 Date7#on! mi##i ec; &nce (o4 have a Date ob*ect avai#ab#e, (o4 can ca## an( o, the ,o##owin! 4''ort method to '#a( with date : #N &eth d% with De%cri"ti n b lean after8Date date; 1 "et4rn tr4e i, the invokin! Date ob*ect contain a date that i #ater than the one 'eci,ied b( date, otherwi e, it ret4rn ,a# e. b lean bef re8Date date; 2 "et4rn tr4e i, the invokin! Date ob*ect contain a date that i ear#ier than the one 'eci,ied b( date, otherwi e, it ret4rn ,a# e. Object cl ne8 ; 1 D4'#icate the invokin! Date ob*ect. int c !"are* 8Date date; +om'are the va#4e o, the invokin! ob*ect with that o, date. "et4rn 0 i, the va#4e are D e@4a#. "et4rn a ne!ative va#4e i, the invokin! ob*ect i ear#ier than date. "et4rn a 'o itive va#4e i, the invokin! ob*ect i #ater than date. int c !"are* 8Object bj; . &'erate identica##( to com'are5o7Date; i, ob* i o, c#a Date. &therwi e, it throw a +#a +a t9<ce'tion. b lean e@ual%8Object date; / "et4rn tr4e i, the invokin! Date ob*ect contain the ame time and date a the one 'eci,ied b( date, otherwi e, it ret4rn ,a# e. l n( (et*i!e8 ; 0 "et4rn the n4mber o, mi##i econd that have e#a' ed ince 6an4ar( 1, 1-00. int ha%h7 de8 ; 8 "et4rn a ha h code ,or the invokin! ob*ect. ) id %et*i!e8l n( ti!e; Set the time and date a 'eci,ied b( time, which re're ent an e#a' ed time in mi##i econd ,rom midni!ht, 6an4ar( 1, 1-00 #trin( t #trin(8 ; 10 +onvert the invokin! Date ob*ect into a trin! and ret4rn the re 4#t. Cettin( 7urrent Date A *i!e 5hi i ver( ea ( to !et c4rrent date and time in 6ava Serv#et. Fo4 can 4 e a im'#e Date ob*ect with to.tri$g ! method to 'rint c4rrent date and time a ,o##ow : GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava.4ti#.DateJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a +4rrentDate e<tend Ctt'Serv#et N '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion +2+ 3imited i a S4b idiar( o, 5+S 3imited

22-

N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K PDi '#a( +4rrent Date X 5imePJ Date date K new Date7;J Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PVh2 a#i!nKIPcenterIPRP > date.toStrin!7; > PVGh2RInP > PVGbod(RVGhtm#RP;J Q Q Now #et 4 com'i#e above erv#et and create a''ro'riate entrie in web.<m# and then ca## thi erv#et 4 in! ="3 htt':GG#oca#ho t:8080G+4rrentDate. 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Di%"lay 7urrent Date A *i!e & n Jun -B -B:KL:K1 C&*V.K:.. -.B. 5r( to re,er h ="3 htt':GG#oca#ho t:8080G+4rrentDate and (o4 wo4#d ,ind di,,erence in econd ever(time (o4 wo4#d re,re h. Date 7 !"ari% n: ) $ mentioned above (o4 can 4 e a## the avai#ab#e 6ava method in (o4r Serv#et. $n ca e (o4 need to com'are two date , ,o##owin! are the method : Fo4 can 4 e !et5ime7 ; to obtain the n4mber o, mi##i econd that have e#a' ed ince midni!ht, 6an4ar( 1, 1-00, ,or both ob*ect and then com'are the e two va#4e . Fo4 can 4 e the method be,ore7 ;, a,ter7 ;, and e@4a# 7 ;. Eeca4 e the 12th o, the month come be,ore the 18th, ,or e<am'#e, new Date7--, 2, 12;.be,ore7new Date 7--, 2, 18;; ret4rn tr4e. Fo4 can 4 e the com'are5o7 ; method, which i de,ined b( the +om'arab#e inter,ace and im'#emented b( Date. Date F r!attin( u%in( #i!"leDateF r!at: Sim'#eDate%ormat i a concrete c#a ,or ,ormattin! and 'ar in! date in a #oca#e- en itive manner. Sim'#eDate%ormat a##ow (o4 to tart b( choo in! an( 4 er-de,ined 'attern ,or date-time ,ormattin!. 3et 4 modi,( above e<am'#e a ,o##ow : GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava.te<t.OJ im'ort *ava.4ti#.DateJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a +4rrentDate e<tend Ctt'Serv#et N '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K PDi '#a( +4rrent Date X 5imePJ Date dNow K new Date7 ;J Sim'#eDate%ormat ,t K +2+ 3imited i a S4b idiar( o, 5+S 3imited

210

new Sim'#eDate%ormat 7P9 ((((.22.dd ?at? hh:mm: a BBBP;J Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PVh2 a#i!nKIPcenterIPRP > ,t.,ormat7dNow; > PVGh2RInP > PVGbod(RVGhtm#RP;J Q Q +om'i#e above erv#et once a!ain and then ca## thi erv#et 4 in! ="3 htt':GG#oca#ho t:8080G+4rrentDate. 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Di%"lay 7urrent Date A *i!e & n -.B.+.L+-B at B.:.L:KK P& C&*V.K:.. #i!"le DateF r!at f r!at c de%: 5o 'eci,( the time ,ormat 4 e a time 'attern trin!. $n thi 'attern, a## )S+$$ #etter are re erved a 'attern #etter , which are de,ined a the ,o##owin!: 7haract De%cri"ti n E,a!"le er G 9ra de i!nator )D ( Fear in ,o4r di!it 2001 2 2onth in (ear 64#( or 00 d Da( in month 10 h Co4r in ).2.GP.2. 71Z12; 12 C Co4r in da( 70Z21; 22 m 2in4te in ho4r 10 Second in min4te .. S 2i##i econd 21D 9 Da( in week 54e da( D Da( in (ear 1/0 % Da( o, week in month 2 7 econd Wed. in 64#(; w Week in (ear D0 W Week in month 1 a ).2.GP.2. marker P2 k Co4r in da( 71Z2D; 2D H Co4r in ).2.GP.2. 70Z11; 10 B 5ime Bone 9a tern Standard 5ime ? 9 ca'e ,or te<t De#imiter P Sin!#e @4ote \ %or a com'#ete #i t o, con tant avai#ab#e method to mani'4#ate date, (o4 can re,er to tandard 6ava doc4mentation. #er)let% $ Pa(e Redirecti n Pa!e redirection i !enera##( 4 ed when a doc4ment move to a new #ocation and we need to end the c#ient to thi new #ocation or ma( be beca4 e o, #oad ba#ancin!, or ,or im'#e randomiBation. 5he im'#e t wa( o, redirectin! a re@4e t to another 'a!e i 4 in! method %endRedirect8; o, re 'on e ob*ect. %o##owin! i the i!nat4re o, thi method: '4b#ic void Ctt'Serv#et"e 'on e. end"edirect7Strin! #ocation; throw $&9<ce'tion 5hi method end back the re 'on e to the brow er a#on! with the tat4 code and new 'a!e #ocation. Fo4 can a# o 4 e etStat4 7; and etCeader7; method to!ether to achieve the ame: .... Strin! ite K Phtt':GGwww.new'a!e.comP J +2+ 3imited i a S4b idiar( o, 5+S 3imited 211

re 'on e. etStat4 7re 'on e.S+T2&V9DT592P&")"$3F;J re 'on e. etCeader7P3ocationP, ite;J .... E,a!"le: 5hi e<am'#e how how a erv#et 'er,orm 'a!e redirection to an another #ocation: im'ort *ava.io.OJ im'ort *ava. @#.DateJ im'ort *ava.4ti#.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ '4b#ic c#a Pa!e"edirect e<tend Ctt'Serv#etN

'4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J GG New #ocation to be redirected Strin! ite K new Strin!7Phtt':GGwww.'hoto,4ntoo .comP;J re 'on e. etStat4 7re 'on e.S+T2&V9DT592P&")"$3F;J re 'on e. etCeader7P3ocationP, ite;J Q Q Now #et 4 com'i#e above erv#et and create ,o##owin! entrie in web.<m# .... V erv#etR V erv#et-nameRPa!e"edirectVG erv#et-nameR V erv#et-c#a RPa!e"edirectVG erv#et-c#a R VG erv#etR V erv#et-ma''in!R V erv#et-nameRPa!e"edirectVG erv#et-nameR V4r#-'atternRGPa!e"edirectVG4r#-'atternR VG erv#et-ma''in!R .... Now ca## thi erv#et 4 in! ="3 htt':GG#oca#ho t:8080GPa!e"edirect. 5hi wo4#d take (o4 !iven ="3 htt':GGwww.'hoto,4ntoo .com. #er)let% $ 'it% 7 unter 'it 7 unter f r a Web Pa(e: 2an( time (o4 wo4#d be intere ted in knowin! tota# n4mber o, hit on a 'artic4#ar 'a!e o, (o4r web ite. $t i ver( im'#e to co4nt the e hit 4 in! a erv#et beca4 e the #i,e c(c#e o, a erv#et i contro##ed b( the container in which it r4n . %o##owin! are the te' to be taken to im'#ement a im'#e 'a!e hit co4nter which i ba ed on Serv#et 3i,e +(c#e: $nitia#iBe a !#oba# variab#e in init7; method. $ncrea e !#oba# variab#e ever( time either doGet7; or doPo t7; method i ca##ed. $, re@4ired, (o4 can 4 e a databa e tab#e to tore the va#4e o, !#oba# variab#e in de tro(7; method. 5hi va#4e can be read in ide init7; method when erv#et wo4#d be initia#iBed ne<t time. 5hi te' i o'tiona#. $, (o4 want to co4nt on#( 4ni@4e 'a!e hit with-in a e ion then (o4 can 4 e i New7; method to check i, ame 'a!e a#read( have been hit with-in that e ion. 5hi te' i o'tiona#. Fo4 can di '#a( va#4e o, the !#oba# co4nter to how tota# n4mber o, hit on (o4r web ite. 5hi te' i a# o o'tiona#. +2+ 3imited i a S4b idiar( o, 5+S 3imited 212

Cere $?m a 4min! that the web container wi## not be re tarted. $, it i re tarted or erv#et de tro(ed, the hit co4nter wi## be re et. E,a!"le: 5hi e<am'#e how how to im'#ement a im'#e 'a!e hit co4nter: im'ort *ava.io.OJ im'ort *ava. @#.DateJ im'ort *ava.4ti#.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ '4b#ic c#a Pa!eCit+o4nter e<tend Ctt'Serv#etN

'rivate int hit+o4ntJ '4b#ic void init7; N GG "e et hit co4nter. hit+o4nt K 0J Q '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J GG 5hi method e<ec4te whenever the erv#et i hit GG increment hit+o4nt hit+o4nt>>J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K P5ota# N4mber o, Cit PJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PVh2 a#i!nKIPcenterIPRP > hit+o4nt > PVGh2RInP > PVGbod(RVGhtm#RP;J Q '4b#ic void de tro(7; N GG 5hi i o'tiona# te' b4t i, (o4 #ike (o4 GG can write hit+o4nt va#4e in (o4r databa e. Q Q Now #et 4 com'i#e above erv#et and create ,o##owin! entrie in web.<m# .... V erv#etR V erv#et-nameRPa!eCit+o4nterVG erv#et-nameR V erv#et-c#a RPa!eCit+o4nterVG erv#et-c#a R VG erv#etR V erv#et-ma''in!R V erv#et-nameRPa!eCit+o4nterVG erv#et-nameR V4r#-'atternRGPa!eCit+o4nterVG4r#-'atternR +2+ 3imited i a S4b idiar( o, 5+S 3imited

211

VG erv#et-ma''in!R .... Now ca## thi erv#et 4 in! ="3 htt':GG#oca#ho t:8080GPa!eCit+o4nter. 5hi wo4#d increa e co4nter b( one ever( time thi 'a!e !et re,re hed and it wo4#d di '#a( ,o##owin! re 4#t: * tal Nu!ber f 'it% L 'it 7 unter f r a Web%ite: 2an( time (o4 wo4#d be intere ted in knowin! tota# n4mber o, hit on (o4r who#e web ite. 5hi i a# o ver( im'#e in Serv#et and we can achieve thi 4 in! ,i#ter . %o##owin! are the te' to be taken to im'#ement a im'#e web ite hit co4nter which i ba ed on %i#ter 3i,e +(c#e: $nitia#iBe a !#oba# variab#e in init7; method o, a ,i#ter. $ncrea e !#oba# variab#e ever( time do%i#ter method i ca##ed. $, re@4ired, (o4 can 4 e a databa e tab#e to tore the va#4e o, !#oba# variab#e in de tro(7; method o, ,i#ter. 5hi va#4e can be read in ide init7; method when ,i#ter wo4#d be initia#iBed ne<t time. 5hi te' i o'tiona#. Cere $?m a 4min! that the web container wi## not be re tarted. $, it i re tarted or erv#et de tro(ed, the hit co4nter wi## be re et. E,a!"le: 5hi e<am'#e how how to im'#ement a im'#e web ite hit co4nter: GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.OJ '4b#ic c#a SiteCit+o4nter im'#ement %i#terN

'rivate int hit+o4ntJ '4b#ic void init7%i#ter+on,i! con,i!; throw Serv#et9<ce'tionN GG "e et hit co4nter. hit+o4nt K 0J Q '4b#ic void do%i#ter7Serv#et"e@4e t re@4e t, Serv#et"e 'on e re 'on e, %i#ter+hain chain; throw *ava.io.$&9<ce'tion, Serv#et9<ce'tion N GG increa e co4nter b( one hit+o4nt>>J GG Print the co4nter. S( tem.o4t.'rint#n7PSite vi it co4nt :P> hit+o4nt ;J GG Pa re@4e t back down the ,i#ter chain chain.do%i#ter7re@4e t,re 'on e;J Q '4b#ic void de tro(7; N GG 5hi i o'tiona# te' b4t i, (o4 #ike (o4 GG can write hit+o4nt va#4e in (o4r databa e. Q Q Now #et 4 com'i#e above erv#et and create ,o##owin! entrie in web.<m# .... +2+ 3imited i a S4b idiar( o, 5+S 3imited 21D

V,i#terR V,i#ter-nameRSiteCit+o4nterVG,i#ter-nameR V,i#ter-c#a RSiteCit+o4nterVG,i#ter-c#a R VG,i#terR V,i#ter-ma''in!R V,i#ter-nameRSiteCit+o4nterVG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R .... Now ca## an( ="3 #ike ="3 htt':GG#oca#ho t:8080G. 5hi wo4#d increa e co4nter b( one ever( time an( 'a!e !et a hit and it wo4#d di '#a( ,o##owin! me a!e in the #o!: Site vi it co4nt : 1 Site vi it co4nt : 2 Site vi it co4nt : 1 Site vi it co4nt : D Site vi it co4nt : . .................. #er)let% $ Aut Pa(e Refre%h +on ider a web'a!e which i di '#a(in! #ive !ame core or tock market tat4 or c4rrenc( e<chan!e ration. %or a## 4ch t('e o, 'a!e , (o4 wo4#d need to re,re h (o4r web 'a!e re!4#ar#( 4 in! re,ere h or re#oad b4tton with (o4r brow er. 6ava Serv#et make thi *ob ea ( b( 'rovidin! (o4 a mechani m where (o4 can make a web'a!e in 4ch a wa( that it wo4#d re,re h a4tomatica##( a,ter a !iven interva#. 5he im'#e t wa( o, re,re hin! a web 'a!e i 4 in! method %etInt'eader8; o, re 'on e ob*ect. %o##owin! i the i!nat4re o, thi method: '4b#ic void et$ntCeader7Strin! header, int headerVa#4e; 5hi method end back header P"e,re hP to the brow er a#on! with an inte!er va#4e which indicate time interva# in econd . Aut Pa(e Refre%h E,a!"le: 5hi e<am'#e how how a erv#et 'er,orm a4to 'a!e re,re h 4 in! %etInt'eader8; method to et Refre%h header. GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.OJ GG 9<tend Ctt'Serv#et c#a '4b#ic c#a "e,re h e<tend Ctt'Serv#et N GG 2ethod to hand#e G95 method re@4e t. '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re,re h, a4to#oad time a . econd re 'on e. et$ntCeader7P"e,re hP, .;J GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J GG Get c4rrent time +a#endar ca#endar K new Gre!orian+a#endar7;J Strin! amT'mJ int ho4r K ca#endar.!et7+a#endar.C&=";J int min4te K ca#endar.!et7+a#endar.2$N=59;J int econd K ca#endar.!et7+a#endar.S9+&ND;J i,7ca#endar.!et7+a#endar.)2TP2; KK 0; +2+ 3imited i a S4b idiar( o, 5+S 3imited

21.

amT'm K P)2PJ e# e amT'm K PP2PJ Strin! +5 K ho4r>P:P> min4te >P:P> econd >P P> amT'mJ PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K P)4to Pa!e "e,re h 4 in! Serv#etPJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP> PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PV'R+4rrent 5ime i : P > +5 > PVG'RInP;J Q GG 2ethod to hand#e P&S5 method re@4e t. '4b#ic void doPo t7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N doGet7re@4e t, re 'on e;J Q Q Now #et 4 com'i#e above erv#et and create ,o##owin! entrie in web.<m# .... V erv#etR V erv#et-nameR"e,re hVG erv#et-nameR V erv#et-c#a R"e,re hVG erv#et-c#a R VG erv#etR V erv#et-ma''in!R V erv#et-nameR"e,re hVG erv#et-nameR V4r#-'atternRG"e,re hVG4r#-'atternR VG erv#et-ma''in!R .... Now ca## thi erv#et 4 in! ="3 htt':GG#oca#ho t:8080G"e,re h which wo4#d di '#a( c4rrent ( tem time a,ter ever( . econd a ,o##ow . 64 t r4n the erv#et and wait to ee the re 4#t: Aut Pa(e Refre%h u%in( #er)let +4rrent 5ime i : -:DD:.0 P2 #er)let% $ #endin( E!ail 5o end an emai# 4 in! (o4r a Serv#et i im'#e eno4!h b4t to tart with (o4 ho4#d have Ja)a&ail API and Ja)a Acti)ati n Fra!ew r= 8JAF; in ta##ed on (o4r machine. Fo4 can down#oad #ate t ver ion o, 6ava2ai# 7Ver ion 1.2; ,rom 6ava? tandard web ite. Fo4 can down#oad #ate t ver ion o, 6)% 7Ver ion 1.1.1; ,rom 6ava? tandard web ite. Down#oad and 4nBi' the e ,i#e , in the new#( created to' #eve# directorie (o4 wi## ,ind a n4mber o, *ar ,i#e ,or both the a''#ication . Fo4 need to add !ail+jar and acti)ati n+jar ,i#e in (o4r +3)SSP)5C. #end a #i!"le E!ail: Cere i an e<am'#e to end a im'#e emai# ,rom (o4r machine. Cere it i a 4med that (o4r l calh %t i connected to the internet and ca'ab#e eno4!h to end an emai#. Same time make 4re a## the *ar ,i#e ,rom 6ava 9mai# )P$ 'acka!e and 6)% 'acka!e ara avai#ab#e in +3)SSP)5C. GG %i#e Name Send9mai#.*ava im'ort *ava.io.OJ im'ort *ava.4ti#.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava<.mai#.OJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 21/

im'ort *ava<.mai#.internet.OJ im'ort *ava<.activation.OJ '4b#ic c#a Send9mai# e<tend Ctt'Serv#etN

'4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG "eci'ient? emai# $D need to be mentioned. Strin! to K Pabcd^!mai#.comPJ GG Sender? emai# $D need to be mentioned Strin! ,rom K Pweb^!mai#.comPJ GG ) 4min! (o4 are endin! emai# ,rom #oca#ho t Strin! ho t K P#oca#ho tPJ GG Get ( tem 'ro'ertie Pro'ertie 'ro'ertie K S( tem.!etPro'ertie 7;J GG Set4' mai# erver 'ro'ertie . etPro'ert(7Pmai#. mt'.ho tP, ho t;J GG Get the de,a4#t Se ion ob*ect. Se ion e ion K Se ion.!etDe,a4#t$n tance7'ro'ertie ;J GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J tr(N GG +reate a de,a4#t 2ime2e a!e ob*ect. 2ime2e a!e me a!e K new 2ime2e a!e7 e ion;J GG Set %rom: header ,ie#d o, the header. me a!e. et%rom7new $nternet)ddre 7,rom;;J GG Set 5o: header ,ie#d o, the header. me a!e.add"eci'ient72e a!e."eci'ient5('e.5&, new $nternet)ddre 7to;;J GG Set S4b*ect: header ,ie#d me a!e. etS4b*ect7P5hi i the S4b*ect 3ineWP;J GG Now et the act4a# me a!e me a!e. et5e<t7P5hi i act4a# me a!eP;J GG Send me a!e 5ran 'ort. end7me a!e;J Strin! tit#e K PSend 9mai#PJ Strin! re K PSent me a!e 4cce ,4##(....PJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PV' a#i!nKIPcenterIPRP > re > PVG'RInP > PVGbod(RVGhtm#RP;J Qcatch 72e a!in!9<ce'tion me<; N me<.'rintStack5race7;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 210

Q Q Q Now #et 4 com'i#e above erv#et and create ,o##owin! entrie in web.<m# .... V erv#etR V erv#et-nameRSend9mai#VG erv#et-nameR V erv#et-c#a RSend9mai#VG erv#et-c#a R VG erv#etR V erv#et-ma''in!R V erv#et-nameRSend9mai#VG erv#et-nameR V4r#-'atternRGSend9mai#VG4r#-'atternR VG erv#et-ma''in!R .... Now ca## thi erv#et 4 in! ="3 htt':GG#oca#ho t:8080GSend9mai# which wo4#d end an emai# to !iven emai# $D abcdEgmail#com and wo4#d di '#a( ,o##owin! re 'on e: #end E!ail Sent me a!e 4cce ,4##(.... $, (o4 want to end an emai# to m4#ti'#e reci'ient then ,o##owin! method wo4#d be 4 ed to 'eci,( m4#ti'#e emai# $D : void add"eci'ient 72e a!e."eci'ient5('e t('e, )ddre 8: addre e ; throw 2e a!in!9<ce'tion Cere i the de cri'tion o, the 'arameter : ty"e: 5hi wo4#d be et to 5&, ++ or E++. Cere ++ re're ent +arbon +o'( and E++ re're ent E#ack +arbon +o'(. 9<am'#e Message#,ecipie$t(ype#(+ addre%%e%: 5hi i the arra( o, emai# $D. Fo4 wo4#d need to 4 e $nternet)ddre 7; method whi#e 'eci,(in! emai# $D #end an '*&4 E!ail: Cere i an e<am'#e to end an C523 emai# ,rom (o4r machine. Cere it i a 4med that (o4r l calh %t i connected to the internet and ca'ab#e eno4!h to end an emai#. Same time make 4re a## the *ar ,i#e ,rom 6ava 9mai# )P$ 'acka!e and 6)% 'acka!e ara avai#ab#e in +3)SSP)5C. 5hi e<am'#e i ver( imi#ar to 'revio4 one, e<ce't here we are 4 in! et+ontent7; method to et content who e econd ar!4ment i Pte<tGhtm#P to 'eci,( that the C523 content i inc#4ded in the me a!e. = in! thi e<am'#e, (o4 can end a bi! a C523 content (o4 #ike. GG %i#e Name Send9mai#.*ava im'ort *ava.io.OJ im'ort *ava.4ti#.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava<.mai#.OJ im'ort *ava<.mai#.internet.OJ im'ort *ava<.activation.OJ '4b#ic c#a Send9mai# e<tend Ctt'Serv#etN '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG "eci'ient? emai# $D need to be mentioned. Strin! to K Pabcd^!mai#.comPJ GG Sender? emai# $D need to be mentioned Strin! ,rom K Pweb^!mai#.comPJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 218

GG ) 4min! (o4 are endin! emai# ,rom #oca#ho t Strin! ho t K P#oca#ho tPJ GG Get ( tem 'ro'ertie Pro'ertie 'ro'ertie K S( tem.!etPro'ertie 7;J GG Set4' mai# erver 'ro'ertie . etPro'ert(7Pmai#. mt'.ho tP, ho t;J GG Get the de,a4#t Se ion ob*ect. Se ion e ion K Se ion.!etDe,a4#t$n tance7'ro'ertie ;J GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J tr(N GG +reate a de,a4#t 2ime2e a!e ob*ect. 2ime2e a!e me a!e K new 2ime2e a!e7 e ion;J GG Set %rom: header ,ie#d o, the header. me a!e. et%rom7new $nternet)ddre 7,rom;;J GG Set 5o: header ,ie#d o, the header. me a!e.add"eci'ient72e a!e."eci'ient5('e.5&, new $nternet)ddre 7to;;J GG Set S4b*ect: header ,ie#d me a!e. etS4b*ect7P5hi i the S4b*ect 3ineWP;J GG Send the act4a# C523 me a!e, a bi! a (o4 #ike me a!e. et+ontent7PVh1R5hi i act4a# me a!eVGh1RP, Pte<tGhtm#P ;J GG Send me a!e 5ran 'ort. end7me a!e;J Strin! tit#e K PSend 9mai#PJ Strin! re K PSent me a!e 4cce ,4##(....PJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PV' a#i!nKIPcenterIPRP > re > PVG'RInP > PVGbod(RVGhtm#RP;J Qcatch 72e a!in!9<ce'tion me<; N me<.'rintStack5race7;J Q Q Q +om'i#e and r4n above erv#et to end C523 me a!e on a !iven emai# $D. #end Attach!ent in E!ail: Cere i an e<am'#e to end an emai# with attachment ,rom (o4r machine. Cere it i a 4med that (o4r l calh %t i connected to the internet and ca'ab#e eno4!h to end an emai#. GG %i#e Name Send9mai#.*ava im'ort *ava.io.OJ im'ort *ava.4ti#.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava<.mai#.OJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 21-

im'ort *ava<.mai#.internet.OJ im'ort *ava<.activation.OJ '4b#ic c#a Send9mai# e<tend Ctt'Serv#etN

'4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG "eci'ient? emai# $D need to be mentioned. Strin! to K Pabcd^!mai#.comPJ GG Sender? emai# $D need to be mentioned Strin! ,rom K Pweb^!mai#.comPJ GG ) 4min! (o4 are endin! emai# ,rom #oca#ho t Strin! ho t K P#oca#ho tPJ GG Get ( tem 'ro'ertie Pro'ertie 'ro'ertie K S( tem.!etPro'ertie 7;J GG Set4' mai# erver 'ro'ertie . etPro'ert(7Pmai#. mt'.ho tP, ho t;J GG Get the de,a4#t Se ion ob*ect. Se ion e ion K Se ion.!etDe,a4#t$n tance7'ro'ertie ;J GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J tr(N GG +reate a de,a4#t 2ime2e a!e ob*ect. 2ime2e a!e me a!e K new 2ime2e a!e7 e ion;J GG Set %rom: header ,ie#d o, the header. me a!e. et%rom7new $nternet)ddre 7,rom;;J GG Set 5o: header ,ie#d o, the header. me a!e.add"eci'ient72e a!e."eci'ient5('e.5&, new $nternet)ddre 7to;;J GG Set S4b*ect: header ,ie#d me a!e. etS4b*ect7P5hi i the S4b*ect 3ineWP;J GG +reate the me a!e 'art Eod(Part me a!eEod(Part K new 2imeEod(Part7;J GG %i## the me a!e me a!eEod(Part. et5e<t7P5hi i me a!e bod(P;J GG +reate a m4#ti'ar me a!e 24#ti'art m4#ti'art K new 2ime24#ti'art7;J GG Set te<t me a!e 'art m4#ti'art.addEod(Part7me a!eEod(Part;J GG Part two i attachment me a!eEod(Part K new 2imeEod(Part7;J Strin! ,i#ename K P,i#e.t<tPJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 2D0

DataSo4rce o4rce K new %i#eDataSo4rce7,i#ename;J me a!eEod(Part. etDataCand#er7new DataCand#er7 o4rce;;J me a!eEod(Part. et%i#eName7,i#ename;J m4#ti'art.addEod(Part7me a!eEod(Part;J GG Send the com'#ete me a!e 'art me a!e. et+ontent7m4#ti'art ;J GG Send me a!e 5ran 'ort. end7me a!e;J Strin! tit#e K PSend 9mai#PJ Strin! re K PSent me a!e 4cce ,4##(....PJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > PV' a#i!nKIPcenterIPRP > re > PVG'RInP > PVGbod(RVGhtm#RP;J Qcatch 72e a!in!9<ce'tion me<; N me<.'rintStack5race7;J Q Q Q +om'i#e and r4n above erv#et to end a ,i#e a an attachement a#on! with a me a!e on a !iven emai# $D. 5%er Authenticati n Part: $, it i re@4ired to 'rovide 4 er $D and Pa word to the emai# erver ,or a4thentication '4r'o e then (o4 can et the e 'ro'ertie a ,o##ow : 'ro' . etPro'ert(7Pmai#.4 erP, Pm(4 erP;J 'ro' . etPro'ert(7Pmai#.'a wordP, Pm('wdP;J "e t o, the emai# endin! mechani m wo4#d remain a e<'#ained above. #er)let% Pac=a(in( 5he web a''#ication tr4ct4re invo#vin! the W9E-$N% 4bdirector( i tandard to a## 6ava web a''#ication and 'eci,ied b( the erv#et )P$ 'eci,ication. Given a to'-#eve# director( name o, m(a'', Cere i what thi director( tr4ct4re #ook #ike: Gm(a'' Gima!e GW9E-$N% Gc#a e G#ib 5he W9E-$N% 4bdirector( contain the a''#ication? de'#o(ment de cri'tor, named web.<m#. )## the C523 ,i#e #ive in the to'-#eve# director( which i myapp. %or admin 4 er, (o4 wo4#d ,ind "&&5 director( a 'arent director( a m(a''. 7reatin( #er)let% in Pac=a(e%: 5he W9E-$N%Gc#a e director( contain a## the erv#et c#a e and other c#a ,i#e , in a tr4ct4re that matche their 'acka!e name. %or e<am'#e, $, (o4 have a ,4##( @4a#i,ied c#a name o, c !+!y r(+&y#er)let, then thi erv#et c#a m4 t be #ocated in the ,o##owin! director(: Gm(a''GW9E-$N%Gc#a e GcomGm(or!G2(Serv#et.c#a %o##owin! i the e<am'#e to create 2(Serv#et c#a with a 'acka!e name com#myorg GG Name (o4r 'acka!e 'acka!e com.m(or!J GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ +2+ 3imited i a S4b idiar( o, 5+S 3imited

2D1

im'ort *ava<. erv#et.htt'.OJ '4b#ic c#a 2(Serv#et e<tend Ctt'Serv#et N

'rivate Strin! me a!eJ '4b#ic void init7; throw Serv#et9<ce'tion N GG Do re@4ired initia#iBation me a!e K PCe##o Wor#dPJ Q '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J GG )ct4a# #o!ic !oe here. PrintWriter o4t K re 'on e.!etWriter7;J o4t.'rint#n7PVh1RP > me a!e > PVGh1RP;J Q '4b#ic void de tro(7; N GG do nothin!. Q Q 7 !"ilin( #er)let% in Pac=a(e%: 5here i nothin! m4ch di,,erent to com'i#e a c#a avai#ab#e in 'acka!e. 5he im'#e t wa( i to kee' (o4r *ava ,i#e in ,4##( @4a#i,ied 'ath, a mentioned above c#a wo4#d be ke't in com.m(or!. Fo4 wo4#d a# o need to add the e director( in +3)SSP)5C. ) 4min! (o4r environment i et4' 'ro'er#(, !o in W* !cat$in%tallati n$ direct ryX/weba""%/ROO*/WE6$INF/cla%%e% director( and com'i#e 2(Serv#et.*ava a ,o##ow : M *avac 2(Serv#et.*ava $, the erv#et de'end on an( other #ibrarie , (o4 have to inc#4de tho e 6)" ,i#e on (o4r +3)SSP)5C a we##. $ have inc#4ded on#( erv#et-a'i.*ar 6)" ,i#e beca4 e $?m not 4 in! an( other #ibrar( in Ce##o Wor#d 'ro!ram. 5hi command #ine 4 e the b4i#t-in *avac com'i#er that come with the S4n 2icro ( tem 6ava So,tware Deve#o'ment Hit 76DH;. %or thi command to work 'ro'er#(, (o4 have to inc#4de the #ocation o, the 6ava SDH that (o4 are 4 in! in the P)5C environment variab#e. $, ever(thin! !oe ,ine, above com'i#ation wo4#d 'rod4ce &y#er)let+cla%% ,i#e in the ame director(. Ne<t ection wo4#d e<'#ain how a com'i#ed erv#et wo4#d be de'#o(ed in 'rod4ction. Pac=a(ed #er)let De"l y!ent: E( de,a4#t, a erv#et a''#ication i #ocated at the 'ath V5omcat-in ta##ation-director(RGweba'' G"&&5 and the c#a ,i#e wo4#d re ide in V5omcat-in ta##ation-director(RGweba'' G"&&5GW9E-$N%Gc#a e . $, (o4 have a ,4##( @4a#i,ied c#a name o, c !+!y r(+&y#er)let, then thi erv#et c#a m4 t be #ocated in W9E-$N%Gc#a e GcomGm(or!G2(Serv#et.c#a and (o4 wo4#d need to create ,o##owin! entrie in web+,!l ,i#e #ocated in V5omcat-in ta##ation-director(RGweba'' G"&&5GW9E-$N%G V erv#etR V erv#et-nameR2(Serv#etVG erv#et-nameR V erv#et-c#a Rcom.m(or!.2(Serv#etVG erv#et-c#a R VG erv#etR V erv#et-ma''in!R V erv#et-nameR2(Serv#etVG erv#et-nameR V4r#-'atternRG2(Serv#etVG4r#-'atternR VG erv#et-ma''in!R +2+ 3imited i a S4b idiar( o, 5+S 3imited 2D2

)bove entrie to be created in ide Vweb-a''R...VGweb-a''R ta! avai#ab#e in web.<m# ,i#e. 5here co4#d be vario4 entrie in thi tab#e a#read( avai#ab#e, b4t never mind. Fo4 are a#mo t done, now #et 4 tart tomcat erver 4 in! V5omcat-in ta##ationdirector(RIbinI tart4'.bat 7on window ; or V5omcat-in ta##ation-director(RGbinG tart4'. h 7on 3in4<GSo#ari etc.; and ,ina##( t('e htt"://l calh %t:3.3./&y#er)let in brow er? addre bo<. $, ever(thin! !oe ,ine, (o4 wo4#d !et ,o##owin! re 4#t: 'ell W rld #er)let% Debu((in( $t i a#wa( di,,ic4#t to te tin!Gdeb4!!in! a erv#et . Serv#et tend to invo#ve a #ar!e amo4nt o, c#ientG erver interaction, makin! error #ike#( b4t hard to re'rod4ce. Cere are a ,ew hint and 4!!e tion that ma( aid (o4 in (o4r deb4!!in!. #y%te!+ ut+"rintln8; S( tem.o4t.'rint#n7; i ea ( to 4 e a a marker to te t whether a certain 'iece o, code i bein! e<ec4ted or not. We can 'rint o4t variab#e va#4e a we##. )dditiona##(: Since the S( tem ob*ect i 'art o, the core 6ava ob*ect , it can be 4 ed ever(where witho4t the need to in ta## an( e<tra c#a e . 5hi inc#4de Serv#et , 6SP, "2$, 96E? , ordinar( Eean and c#a e , and tanda#one a''#ication . +om'ared to to''in! at break'oint , writin! to S( tem.o4t doe n?t inter,ere m4ch with the norma# e<ec4tion ,#ow o, the a''#ication, which make it ver( va#4ab#e when timin! i cr4cia#. %o##owin! i the (nta< to 4 e S( tem.o4t.'rint#n7;: S( tem.o4t.'rint#n7PDeb4!!in! me a!eP;J )## the me a!e !enerated b( above (nta< wo4#d be #o!!ed in web erver #o! ,i#e. &e%%a(e 4 ((in(: $t i a#wa( !reat idea to 4 e 'ro'er #o!!in! method to #o! a## the deb4!, warnin! and error me a!e 4 in! a tandard #o!!in! method. $ 4 e #o!D6 to #o! a## the me a!e . 5he Serv#et )P$ a# o 'rovide a im'#e wa( o, o4t'4ttin! in,ormation b( 4 in! the #o!7; method a ,o##ow : GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ '4b#ic c#a +onte<t3o! e<tend Ctt'Serv#et N '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, *ava.io.$&9<ce'tion N Strin! 'ar K re@4e t.!etParameter7P'ar1P;J GG+a## the two Serv#et+onte<t.#o! method Serv#et+onte<t conte<t K !etServ#et+onte<t7 ;J i, 7'ar KK n4## YY 'ar.e@4a# 7PP;; GG#o! ver ion with 5hrowab#e 'arameter conte<t.#o!7PNo me a!e received:P, new $##e!a#State9<ce'tion7P2i in! 'arameterP;;J e# e conte<t.#o!7PCere i the vi itor? me a!e: P > 'ar;J re 'on e. et+ontent5('e7Pte<tGhtm#P;J *ava.io.PrintWriter o4t K re 'on e.!etWriter7 ;J Strin! tit#e K P+onte<t 3o!PJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > tit#e > PVGh1RInP > +2+ 3imited i a S4b idiar( o, 5+S 3imited 2D1

PVh2 a#i!nKIPcenterIPR2e a!e PVGbod(RVGhtm#RP;J Q GGdoGet

entVGh2RInP >

Q 5he Serv#et+onte<t #o! it te<t me a!e to the erv#et container? #o! ,i#e. With 5omcat the e #o! are ,o4nd in V5omcat-in ta##ation-director(RG#o! . 5he #o! ,i#e do !ive an indication o, new emer!in! b4! or the ,re@4enc( o, 'rob#em . %or that rea on it? !ood to 4 e the #o!7; ,4nction in the catch c#a4 e o, e<ce'tion which ho4#d norma##( not occ4r. 5%in( JD6 Debu((er: Fo4 can deb4! erv#et with the ame *db command (o4 4 e to deb4! an a''#et or an a''#ication. 5o deb4! a erv#et, we can deb4! 4n. erv#et.htt'.Ctt'Server, then watch a Ctt'Server e<ec4te erv#et in re 'on e to C55P re@4e t we make ,rom a brow er. 5hi i ver( imi#ar to how a''#et are deb4!!ed. 5he di,,erence i that with a''#et , the act4a# 'ro!ram bein! deb4!!ed i 4n.a''#et.)''#etViewer. 2o t deb4!!er hide thi detai# b( a4tomatica##( knowin! how to deb4! a''#et . =nti# the( do the ame ,or erv#et , (o4 have to he#' (o4r deb4!!er b( doin! the ,o##owin!: Set (o4r deb4!!er? c#a 'ath o that it can ,ind 4n. erv#et.htt'.Ctt'-Server and a ociated c#a e . Set (o4r deb4!!er? c#a 'ath o that it can a# o ,ind (o4r erv#et and 4''ort c#a e , t('ica##( erverTrootG erv#et and erverTrootGc#a e . Fo4 norma##( wo4#dn?t want erverTrootG erv#et in (o4r c#a 'ath beca4 e it di ab#e erv#et re#oadin!. 5hi inc#4 ion, however, i 4 e,4# ,or deb4!!in!. $t a##ow (o4r deb4!!er to et break'oint in a erv#et be,ore the c4 tom erv#et #oader in Ctt'Server #oad the erv#et. &nce (o4 have et the 'ro'er c#a 'ath, tart deb4!!in! 4n. erv#et.htt'.Ctt'Server. Fo4 can et break'oint in whatever erv#et (o4?re intere ted in deb4!!in!, then 4 e a web brow er to make a re@4e t to the Ctt'Server ,or the !iven erv#et 7htt':GG#oca#ho t:8080G erv#etGServ#et5oDeb4!;. Fo4 ho4#d ee e<ec4tion to' at (o4r break'oint . 5%in( 7 !!ent%: +omment in (o4r code can he#' the deb4!!in! 'roce in vario4 wa( . +omment can be 4 ed in #ot o, other wa( in the deb4!!in! 'roce . 5he Serv#et 4 e 6ava comment and in!#e #ine 7GG ...; and m4#ti'#e #ine 7GO ... OG; comment can be 4 ed to tem'orari#( remove 'art o, (o4r 6ava code. $, the b4! di a''ear , take a c#o er #ook at the code (o4 *4 t commented and ,ind o4t the 'rob#em. 7lient and #er)er 'eader%: Sometime when a erv#et doe n?t behave a e<'ected, it? 4 e,4# to #ook at the raw C55P re@4e t and re 'on e. $, (o4?re ,ami#iar with the tr4ct4re o, C55P, (o4 can read the re@4e t and re 'on e and ee e<act#( what e<act#( i !oin! with tho e header . I!" rtant Debu((in( *i"%: Cere i a #i t o, ome more deb4!!in! ti' on erv#et deb4!!in!: Ee aware that erverTrootGc#a e doe n?t re#oad and that erverTrootG erv#et 'robab#( doe . ) k a brow er to how the raw content o, the 'a!e it i di '#a(in!. 5hi can he#' identi,( ,ormattin! 'rob#em . $t? 4 4a##( an o'tion 4nder the View men4. 2ake 4re the brow er i n?t cachin! a 'revio4 re@4e t? o4t'4t b( ,orcin! a ,4## re#oad o, the 'a!e. With Net ca'e Navi!ator, 4 e Shi,t-"e#oadJ with $nternet 9<'#orer 4 e Shi,t-"e,re h. Veri,( that (o4r erv#et? init7; method take a Serv#et+on,i! 'arameter and ca## 4'er.init7con,i!; ri!ht awa(. #er)let% $ Internati nali>ati n Ee,ore we 'roceed, #et me e<'#ain three im'ortant term : Internati nali>ati n 8iB3n;: 5hi mean enab#in! a web ite to 'rovide di,,erent ver ion o, content tran #ated into the vi itor? #an!4a!e or nationa#it(. 4 cali>ati n 8lB.n;: 5hi mean addin! re o4rce to a web ite to ada't it to a 'artic4#ar !eo!ra'hica# or c4#t4ra# re!ion ,or e<am'#e Cindi tran #ation to a web ite. l cale: 5hi i a 'artic4#ar c4#t4ra# or !eo!ra'hica# re!ion. $t i 4 4a##( re,erred to a a #an!4a!e (mbo# ,o##owed b( a co4ntr( (mbo# which are e'arated b( an 4nder core. %or e<am'#e PenT=SP re're ent en!#i h #oca#e ,or =S. 5here are n4mber o, item which ho4#d be taken care whi#e b4i#din! 4' a !#oba# web ite. 5hi t4toria# wo4#d not !ive (o4 com'#ete detai# on thi b4t it wo4#d !ive (o4 a !ood e<am'#e on how (o4 can o,,er (o4r web 'a!e in di,,erent #an!4a!e to internet comm4nit( b( di,,erentiatin! their #ocation ie. #oca#e. +2+ 3imited i a S4b idiar( o, 5+S 3imited 2DD

) erv#et can 'ick4' a''ro'riate ver ion o, the ite ba ed on the re@4e ter? #oca#e and 'rovide a''ro'riate ite ver ion accordin! to the #oca# #an!4a!e, c4#t4re and re@4irement . %o##owin! i the method o, re@4e t ob*ect which ret4rn 3oca#e ob*ect. *ava.4ti#.3oca#e re@4e t.!et3oca#e7; Detectin( 4 cale: %o##owin! are the im'ortant #oca#e method which (o4 can 4 e to detect re@4e ter? #ocation, #an!4a!e and o, co4r e #oca#e. )## the be#ow method di '#a( co4ntr( name and #an!4a!e name et in re@4e ter? brow er. #+N+ &eth d A De%cri"ti n #trin( (et7 untry8; 1 5hi method ret4rn the co4ntr(Gre!ion code in 4''er ca e ,or thi #oca#e in $S& 11// 2#etter ,ormat. #trin( (etDi%"lay7 untry8; 2 5hi method ret4rn a name ,or the #oca#e? co4ntr( that i a''ro'riate ,or di '#a( to the 4 er. #trin( (et4an(ua(e8; 1 5hi method ret4rn the #an!4a!e code in #ower ca e ,or thi #oca#e in $S& /1- ,ormat. #trin( (etDi%"lay4an(ua(e8; D 5hi method ret4rn a name ,or the #oca#e? #an!4a!e that i a''ro'riate ,or di '#a( to the 4 er. #trin( (etI#OJ7 untry8; . 5hi method ret4rn a three-#etter abbreviation ,or thi #oca#e? co4ntr(. #trin( (etI#OJ4an(ua(e8; / 5hi method ret4rn a three-#etter abbreviation ,or thi #oca#e? #an!4a!e. E,a!"le: 5hi e<am'#e how how (o4 di '#a( a #an!4a!e and a ociated co4ntr( ,or a re@4e t: im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.3oca#eJ '4b#ic c#a Get3oca#e e<tend Ctt'Serv#etN '4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GGGet the c#ient? 3oca#e 3oca#e #oca#e K re@4e t.!et3oca#e7;J Strin! #an!4a!e K #oca#e.!et3an!4a!e7;J Strin! co4ntr( K #oca#e.!et+o4ntr(7;J GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J Strin! tit#e K PDetectin! 3oca#ePJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > #an!4a!e > PVGh1RInP > PVh2 a#i!nKIPcenterIPRP > co4ntr( > PVGh2RInP > PVGbod(RVGhtm#RP;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 2D.

Q Q 4an(ua(e% #ettin(: ) erv#et can o4t'4t a 'a!e written in a We tern 94ro'ean #an!4a!e 4ch a 9n!#i h, S'ani h, German, %rench, $ta#ian, D4tch etc. Cere it i im'ortant to et +ontent-3an!4a!e header to di '#a( a## the character 'ro'er#(. Second 'oint i to di '#a( a## the 'ecia# character 4 in! C523 entitie , %or e<am'#e, PXb2D1JP re're ent PeP, and PXb1/1JP re're ent PfP a ,o##ow : im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.3oca#eJ '4b#ic c#a Di '#a(S'ani h e<tend Ctt'Serv#etN

'4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J GG Set 'ani h #an!4a!e code. re 'on e. etCeader7P+ontent-3an!4a!eP, Pe P;J Strin! tit#e K P9n 9 'aXnti#deJo#PJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1RP > P9n 9 'aXnti#deJo#:P > PVGh1RInP > PVh1RP > PXie<c#JCo#a 24ndoWP > PVGh1RInP > PVGbod(RVGhtm#RP;J Q Q 4 cale #"ecific Date%: Fo4 can 4 e the *ava.te<t.Date%ormat c#a and it tatic !etDate5ime$n tance7 ; method to ,ormat date and time 'eci,ic to #oca#e. %o##owin! i the e<am'#e which how how to ,ormat date 'eci,ic to a !iven #oca#e: im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.3oca#eJ im'ort *ava.te<t.Date%ormatJ im'ort *ava.4ti#.DateJ '4b#ic c#a Date3oca#e e<tend Ctt'Serv#etN

'4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J GGGet the c#ient? 3oca#e +2+ 3imited i a S4b idiar( o, 5+S 3imited

2D/

3oca#e #oca#e K re@4e t.!et3oca#e7 ;J Strin! date K Date%ormat.!etDate5ime$n tance7 Date%ormat.%=33, Date%ormat.SC&"5, #oca#e;.,ormat7new Date7 ;;J Strin! tit#e K P3oca#e S'eci,ic Date PJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > date > PVGh1RInP > PVGbod(RVGhtm#RP;J Q Q 4 cale #"ecific 7urrency Fo4 can 4 e the *ava.t<t.N4mber%ormat c#a and it tatic !et+4rrenc($n tance7 ; method to ,ormat a n4mber, 4ch a a #on! or do4b#e t('e, in a #oca#e 'eci,ic c4rren(. %o##owin! i the e<am'#e which how how to ,ormat c4rrenc( 'eci,ic to a !iven #oca#e: im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.3oca#eJ im'ort *ava.te<t.N4mber%ormatJ im'ort *ava.4ti#.DateJ '4b#ic c#a +4rrenc(3oca#e e<tend Ctt'Serv#etN

'4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J GGGet the c#ient? 3oca#e 3oca#e #oca#e K re@4e t.!et3oca#e7 ;J N4mber%ormat n,t K N4mber%ormat.!et+4rrenc($n tance7#oca#e;J Strin! ,ormatted+4rr K n,t.,ormat71000000;J Strin! tit#e K P3oca#e S'eci,ic +4rrenc(PJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > ,ormatted+4rr > PVGh1RInP > PVGbod(RVGhtm#RP;J Q Q 4 cale #"ecific Percenta(e Fo4 can 4 e the *ava.t<t.N4mber%ormat c#a and it tatic !etPercent$n tance7 ; method to !et #oca#e 'eci,ic 'ercenta!e. %o##owin! i the e<am'#e which how how to ,ormat 'ercenta!e 'eci,ic to a !iven #oca#e: +2+ 3imited i a S4b idiar( o, 5+S 3imited 2D0

im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.3oca#eJ im'ort *ava.te<t.N4mber%ormatJ im'ort *ava.4ti#.DateJ '4b#ic c#a Percenta!e3oca#e e<tend Ctt'Serv#etN

'4b#ic void doGet7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; throw Serv#et9<ce'tion, $&9<ce'tion N GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J PrintWriter o4t K re 'on e.!etWriter7;J GGGet the c#ient? 3oca#e 3oca#e #oca#e K re@4e t.!et3oca#e7 ;J N4mber%ormat n,t K N4mber%ormat.!etPercent$n tance7#oca#e;J Strin! ,ormattedPerc K n,t.,ormat70..1;J Strin! tit#e K P3oca#e S'eci,ic Percenta!ePJ Strin! doc5('e K PVWdoct('e htm# '4b#ic IP-GGw1cGGdtd htm# D.0 P > Ptran itiona#GGenIPRInPJ o4t.'rint#n7doc5('e > PVhtm#RInP > PVheadRVtit#eRP > tit#e > PVGtit#eRVGheadRInP > PVbod( b!co#orKIPb,0,0,0IPRInP > PVh1 a#i!nKIPcenterIPRP > ,ormattedPerc > PVGh1RInP > PVGbod(RVGhtm#RP;J Q Q J#P $ O)er)iew What i% Ja)a#er)er Pa(e%? 6avaServer Pa!e 76SP; i a techno#o!( ,or deve#o'in! web 'a!e that 4''ort d(namic content which he#' deve#o'er in ert *ava code in C523 'a!e b( makin! 4 e o, 'ecia# 6SP ta! , mo t o, which tart with VL and end with LR. ) 6avaServer Pa!e com'onent i a t('e o, 6ava erv#et that i de i!ned to ,4#,i## the ro#e o, a 4 er inter,ace ,or a 6ava web a''#ication. Web deve#o'er write 6SP a te<t ,i#e that combine C523 or AC523 code, A23 e#ement , and embedded 6SP action and command . = in! 6SP, (o4 can co##ect in'4t ,rom 4 er thro4!h web 'a!e ,orm , 're ent record ,rom a databa e or another o4rce, and create web 'a!e d(namica##(. 6SP ta! can be 4 ed ,or a variet( o, '4r'o e , 4ch a retrievin! in,ormation ,rom a databa e or re!i terin! 4 er 're,erence , acce in! 6avaEean com'onent , 'a in! contro# between 'a!e and harin! in,ormation between re@4e t , 'a!e etc. Why 5%e J#P? 6avaServer Pa!e o,ten erve the ame '4r'o e a 'ro!ram im'#emented 4 in! the +ommon Gatewa( $nter,ace 7+G$;. E4t 6SP o,,er evera# advanta!e in com'ari on with the +G$. Per,ormance i i!ni,icant#( better beca4 e 6SP a##ow embeddin! D(namic 9#ement in C523 Pa!e it e#, in tead o, havin! a e'arate +G$ ,i#e . 6SP are a#wa( com'i#ed be,ore it? 'roce ed b( the erver 4n#ike +G$GPer# which re@4ire the erver to #oad an inter'reter and the tar!et cri't each time the 'a!e i re@4e ted. 6avaServer Pa!e are b4i#t on to' o, the 6ava Serv#et )P$, o #ike Serv#et , 6SP a# o ha acce to a## the 'ower,4# 9nter'ri e 6ava )P$ , inc#4din! 6DE+, 6ND$, 96E, 6)AP etc. 6SP 'a!e can be 4 ed in combination with erv#et that hand#e the b4 ine #o!ic, the mode# 4''orted b( 6ava erv#et tem'#ate en!ine . +2+ 3imited i a S4b idiar( o, 5+S 3imited 2D8

%ina##(, 6SP i an inte!ra# 'art o, 6299, a com'#ete '#at,orm ,or enter'ri e c#a a''#ication . 5hi mean that 6SP can '#a( a 'art in the im'#e t a''#ication to the mo t com'#e< and demandin!. Ad)anta(e% f J#P: %o##owin! i the #i t o, other advanta!e o, 4 in! 6SP over other techno#o!ie : )%+ Acti)e #er)er Pa(e% 8A#P;: 5he advanta!e o, 6SP are two,o#d. %ir t, the d(namic 'art i written in 6ava, not Vi 4a# Ea ic or other 2S 'eci,ic #an!4a!e, o it i more 'ower,4# and ea ier to 4 e. Second, it i 'ortab#e to other o'eratin! ( tem and non-2icro o,t Web erver . )%+ Pure #er)let%: $t i more convenient to write 7and to modi,(W; re!4#ar C523 than to have '#ent( o, 'rint#n tatement that !enerate the C523. )%+ #er)er$#ide Include% 8##I;: SS$ i rea##( on#( intended ,or im'#e inc#4 ion , not ,or Prea#P 'ro!ram that 4 e ,orm data, make databa e connection , and the #ike. )%+ Ja)a#cri"t: 6avaScri't can !enerate C523 d(namica##( on the c#ient b4t can hard#( interact with the web erver to 'er,orm com'#e< ta k #ike databa e acce and ima!e 'roce in! etc. )%+ #tatic '*&4: "e!4#ar C523, o, co4r e, cannot contain d(namic in,ormation. What i% Ne,t? $ wo4#d take (o4 te' b( te' to et 4' (o4r environment to tart with 6SP. $?m a 4min! (o4 have !ood hand on with 6ava Pro!rammin! to 'roceed with #earnin! 6SP. $, (o4 are not aware o, 6ava Pro!rammin! 3an!4a!e then $ wo4#d recommend to !o thro4!h 6ava 54toria# to 4nder tand 6ava Pro!rammin!. J#P $ Architecture 5he web erver need a 6SP en!ine ie. container to 'roce 6SP 'a!e . 5he 6SP container i re 'on ib#e ,or interce'tin! re@4e t ,or 6SP 'a!e . 5hi t4toria# make 4 e o, )'ache which ha b4i#tin 6SP container to 4''ort 6SP 'a!e deve#o'ment. ) 6SP container work with the Web erver to 'rovide the r4ntime environment and other ervice a 6SP need . $t know how to 4nder tand the 'ecia# e#ement that are 'art o, 6SP . %o##owin! dia!ram how the 'o ition o, 6SP container and 6SP ,i#e in a Web )''#ication.

J#P Pr ce%%in(: 5he ,o##owin! te' e<'#ain how the web erver create the web 'a!e 4 in! 6SP: ) with a norma# 'a!e, (o4r brow er end an C55P re@4e t to the web erver. 5he web erver reco!niBe that the C55P re@4e t i ,or a 6SP 'a!e and ,orward it to a 6SP en!ine. 5hi i done b( 4 in! the ="3 or 6SP 'a!e which end with +j%" in tead o, .htm#. 5he 6SP en!ine #oad the 6SP 'a!e ,rom di k and convert it into a erv#et content. 5hi conver ion i ver( im'#e in which a## tem'#ate te<t i converted to 'rint#n7 ; tatement and a## 6SP e#ement are converted to 6ava code that im'#ement the corre 'ondin! d(namic behavior o, the 'a!e. 5he 6SP en!ine com'i#e the erv#et into an e<ec4tab#e c#a and ,orward the ori!ina# re@4e t to a erv#et en!ine. ) 'art o, the web erver ca##ed the erv#et en!ine #oad the Serv#et c#a and e<ec4te it. D4rin! e<ec4tion, the erv#et 'rod4ce an o4t'4t in C523 ,ormat, which the erv#et en!ine 'a e to the web erver in ide an C55P re 'on e. 5he web erver ,orward the C55P re 'on e to (o4r brow er in term o, tatic C523 content. %ina##( web brow er hand#e the d(namica##( !enerated C523 'a!e in ide the C55P re 'on e e<act#( a i, it were a tatic 'a!e. )## the above mentioned te' can be hown be#ow in the ,o##owin! dia!ram: +2+ 3imited i a S4b idiar( o, 5+S 3imited 2D-

5('ica##(, the 6SP en!ine check to ee whether a erv#et ,or a 6SP ,i#e a#read( e<i t and whether the modi,ication date on the 6SP i o#der than the erv#et. $, the 6SP i o#der than it !enerated erv#et, the 6SP container a 4me that the 6SP ha n?t chan!ed and that the !enerated erv#et ti## matche the 6SP? content . 5hi make the 'roce more e,,icient than with other cri'tin! #an!4a!e 7 4ch a PCP; and there,ore ,a ter. So in a wa(, a 6SP 'a!e i rea##( *4 t another wa( to write a erv#et witho4t havin! to be a 6ava 'ro!rammin! wiB. 9<ce't ,or the tran #ation 'ha e, a 6SP 'a!e i hand#ed e<act#( #ike a re!4#ar erv#et J#P $ 4ife 7ycle 5he ke( to 4nder tandin! the #ow-#eve# ,4nctiona#it( o, 6SP i to 4nder tand the im'#e #i,e c(c#e the( ,o##ow. ) 6SP #i,e c(c#e can be de,ined a the entire 'roce ,rom it creation ti## the de tr4ction which i imi#ar to a erv#et #i,e c(c#e with an additiona# te' which i re@4ired to com'i#e a 6SP into erv#et. 5he ,o##owin! are the 'ath ,o##owed b( a 6SP +om'i#ation $nitia#iBation 9<ec4tion +#ean4' 5he ,o4r ma*or 'ha e o, 6SP #i,e c(c#e are ver( imi#ar to Serv#et 3i,e +(c#e and the( are a ,o##ow :

J#P 7 !"ilati n: When a brow er a k ,or a 6SP, the 6SP en!ine ,ir t check to ee whether it need to com'i#e the 'a!e. $, the 'a!e ha never been com'i#ed, or i, the 6SP ha been modi,ied ince it wa #a t com'i#ed, the 6SP en!ine com'i#e the 'a!e. 5he com'i#ation 'roce invo#ve three te' : Par in! the 6SP. +2+ 3imited i a S4b idiar( o, 5+S 3imited 2.0

54rnin! the 6SP into a erv#et. +om'i#in! the erv#et. J#P Initiali>ati n: When a container #oad a 6SP it invoke the * '$nit7; method be,ore ervicin! an( re@4e t . $, (o4 need to 'er,orm 6SP- 'eci,ic initia#iBation, override the * '$nit7; method: '4b#ic void * '$nit7;N GG $nitia#iBation code... Q 5('ica##( initia#iBation i 'er,ormed on#( once and a with the erv#et init method, (o4 !enera##( initia#iBe databa e connection , o'en ,i#e , and create #ook4' tab#e in the * '$nit method. J#P E,ecuti n: 5hi 'ha e o, the 6SP #i,e c(c#e re're ent a## interaction with re@4e t 4nti# the 6SP i de tro(ed. Whenever a brow er re@4e t a 6SP and the 'a!e ha been #oaded and initia#iBed, the 6SP en!ine invoke the Rj%"#er)ice8; method in the 6SP. 5he T* 'Service7; method take an 'tt"#er)letRe@ue%t and an 'tt"#er)letRe%" n%e a it 'arameter a ,o##ow : void T* 'Service7Ctt'Serv#et"e@4e t re@4e t, Ctt'Serv#et"e 'on e re 'on e; N GG Service hand#in! code... Q 5he T* 'Service7; method o, a 6SP i invoked once 'er a re@4e t and i re 'on ib#e ,or !eneratin! the re 'on e ,or that re@4e t and thi method i a# o re 'on ib#e ,or !eneratin! re 'on e to a## even o, the C55P method ie. G95, P&S5, D93959 etc. J#P 7leanu": 5he de tr4ction 'ha e o, the 6SP #i,e c(c#e re're ent when a 6SP i bein! removed ,rom 4 e b( a container. 5he j%"De%tr y8; method i the 6SP e@4iva#ent o, the de tro( method ,or erv#et . &verride * 'De tro( when (o4 need to 'er,orm an( c#ean4', 4ch a re#ea in! databa e connection or c#o in! o'en ,i#e . 5he * 'De tro(7; method ha the ,o##owin! ,orm: '4b#ic void * 'De tro(7; N GG Fo4r c#ean4' code !oe here. Q J#P $ #ynta, 5hi t4toria# wi## !ive ba ic idea on im'#e (nta< 7ie. e#ement ; invo#ved with 6SP deve#o'ment: *he #cri"tlet: ) cri't#et can contain an( n4mber o, 6)V) #an!4a!e tatement , variab#e or method dec#aration , or e<'re ion that are va#id in the 'a!e cri'tin! #an!4a!e. %o##owin! i the (nta< o, Scri't#et: VL code ,ra!ment LR Fo4 can write A23 e@4iva#ent o, the above (nta< a ,o##ow : V* ': cri't#etR code ,ra!ment VG* ': cri't#etR )n( te<t, C523 ta! , or 6SP e#ement (o4 write m4 t be o4t ide the cri't#et. %o##owin! i the im'#e and ,ir t e<am'#e ,or 6SP: Vhtm#R VheadRVtit#eRCe##o Wor#dVGtit#eRVGheadR Vbod(R Ce##o Wor#dWVbrGR VL o4t.'rint#n7PFo4r $P addre i P > re@4e t.!et"emote)ddr7;;J LR VGbod(R VGhtm#R NO*E: ) 4min! that )'ache 5omcat i in ta##ed in +:Ia'ache-tomcat-0.0.2 and (o4r environment i et4' a 'er environment et4' t4toria#. +2+ 3imited i a S4b idiar( o, 5+S 3imited 2.1

3et 4 kee' above code in 6SP ,i#e he##o.* ' and '4t thi ,i#e in 7:Ya"ache$t !cat$ M+.+-Yweba""%YROO* director( and tr( to brow e it b( !ivin! ="3 htt':GG#oca#ho t:8080Ghe##o.* '. 5hi wo4#d !enerate ,o##owin! re 4#t:

J#P Declarati n%: ) dec#aration dec#are one or more variab#e or method that (o4 can 4 e in 6ava code #ater in the 6SP ,i#e. Fo4 m4 t dec#are the variab#e or method be,ore (o4 4 e it in the 6SP ,i#e. %o##owin! i the (nta< o, 6SP Dec#aration : VLW dec#arationJ 8 dec#arationJ :> ... LR Fo4 can write A23 e@4iva#ent o, the above (nta< a ,o##ow : V* ':dec#arationR code ,ra!ment VG* ':dec#arationR %o##owin! i the im'#e e<am'#e ,or 6SP Dec#aration : VLW int i K 0J LR VLW int a, b, cJ LR VLW +irc#e a K new +irc#e72.0;J LR J#P E,"re%%i n: ) 6SP e<'re ion e#ement contain a cri'tin! #an!4a!e e<'re ion that i eva#4ated, converted to a Strin!, and in erted where the e<'re ion a''ear in the 6SP ,i#e. Eeca4 e the va#4e o, an e<'re ion i converted to a Strin!, (o4 can 4 e an e<'re ion within a #ine o, te<t, whether or not it i ta!!ed with C523, in a 6SP ,i#e. 5he e<'re ion e#ement can contain an( e<'re ion that i va#id accordin! to the 6ava 3an!4a!e S'eci,ication b4t (o4 cannot 4 e a emico#on to end an e<'re ion. %o##owin! i the (nta< o, 6SP 9<'re ion: VLK e<'re ion LR Fo4 can write A23 e@4iva#ent o, the above (nta< a ,o##ow : V* ':e<'re ionR e<'re ion VG* ':e<'re ionR %o##owin! i the im'#e e<am'#e ,or 6SP 9<'re ion: Vhtm#R VheadRVtit#eR) +omment 5e tVGtit#eRVGheadR Vbod(R V'R 5oda(? date: VLK 7new *ava.4ti#.Date7;;.to3oca#eStrin!7;LR VG'R VGbod(R VGhtm#R 5hi wo4#d !enerate ,o##owin! re 4#t: 5oda(? date: 11-Se'-2010 21:2D:2. J#P 7 !!ent%: 6SP comment mark te<t or tatement that the 6SP container ho4#d i!nore. ) 6SP comment i 4 e,4# when (o4 want to hide or Pcomment o4tP 'art o, (o4r 6SP 'a!e. %o##owin! i the (nta< o, 6SP comment : VL-- 5hi i 6SP comment --LR %o##owin! i the im'#e e<am'#e ,or 6SP +omment : Vhtm#R VheadRVtit#eR) +omment 5e tVGtit#eRVGheadR +2+ 3imited i a S4b idiar( o, 5+S 3imited 2.2

Vbod(R Vh2R) 5e t o, +omment VGh2R VL-- 5hi comment wi## not be vi ib#e in the 'a!e o4rce --LR VGbod(R VGhtm#R 5hi wo4#d !enerate ,o##owin! re 4#t: A *e%t f 7 !!ent% 5here are a ma## n4mber o, 'ecia# con tr4ct (o4 can 4 e in vario4 ca e to in ert comment or character that wo4#d otherwi e be treated 'ecia##(. Cere? a 4mmar(: #ynta, Pur" %e VL-- comment --LR ) 6SP comment. $!nored b( the 6SP en!ine. VW-- comment --R )n C523 comment. $!nored b( the brow er. VIL "e're ent tatic VL #itera#. LIR "e're ent tatic LR #itera#. ) in!#e @4ote in an attrib4te that 4 e in!#e I? @4ote . ) do4b#e @4ote in an attrib4te that 4 e do4b#e IP @4ote . J#P Directi)e%: ) 6SP directive a,,ect the overa## tr4ct4re o, the erv#et c#a . $t 4 4a##( ha the ,o##owin! ,orm: VL^ directive attrib4teKPva#4eP LR 5here are three t('e o, directive ta!: Directi)e De%cri"ti n De,ine 'a!e-de'endent attrib4te , 4ch a cri'tin! #an!4a!e, VL^ 'a!e ... LR error 'a!e, and b4,,erin! re@4irement . VL^ inc#4de ... LR $nc#4de a ,i#e d4rin! the tran #ation 'ha e. Dec#are a ta! #ibrar(, containin! c4 tom action , 4 ed in the VL^ ta!#ib ... LR 'a!e We wo4#d e<'#ain 6SP directive in e'arate cha'ter J#P Acti n%: 6SP action 4 e con tr4ct in A23 (nta< to contro# the behavior o, the erv#et en!ine. Fo4 can d(namica##( in ert a ,i#e, re4 e 6avaEean com'onent , ,orward the 4 er to another 'a!e, or !enerate C523 ,or the 6ava '#4!in. 5here i on#( one (nta< ,or the )ction e#ement, a it con,orm to the A23 tandard: V* ':actionTname attrib4teKPva#4eP GR )ction e#ement are ba ica##( 'rede,ined ,4nction and there are ,o##owin! 6SP action avai#ab#e: #ynta, Pur" %e * ':inc#4de $nc#4de a ,i#e at the time the 'a!e i re@4e ted * ':inc#4de $nc#4de a ,i#e at the time the 'a!e i re@4e ted * ':4 eEean %ind or in tantiate a 6avaEean * ': etPro'ert( Set the 'ro'ert( o, a 6avaEean * ':!etPro'ert( $n ert the 'ro'ert( o, a 6avaEean into the o4t'4t * ':,orward %orward the re@4e ter to a new 'a!e Generate brow er- 'eci,ic code that make an &E69+5 or * ':'#4!in 92E9D ta! ,or the 6ava '#4!in * ':e#ement De,ine A23 e#ement d(namica##(. * ':attrib4te De,ine d(namica##( de,ined A23 e#ement? attrib4te. * ':bod( De,ine d(namica##( de,ined A23 e#ement? bod(. * ':te<t = e to write tem'#ate te<t in 6SP 'a!e and doc4ment . We wo4#d e<'#ain 6SP action in e'arate cha'ter 6SP - )ction J#P I!"licit Object%: 6SP 4''ort nine a4tomatica##( de,ined variab#e , which are a# o ca##ed im'#icit ob*ect . 5he e variab#e are: Object% De%cri"ti n re@4e t 5hi i the 'tt"#er)letRe@ue%t ob*ect a ociated with the +2+ 3imited i a S4b idiar( o, 5+S 3imited 2.1

re@4e t. 5hi i the 'tt"#er)letRe%" n%e ob*ect a ociated with the re 'on e re 'on e to the c#ient. o4t 5hi i the PrintWriter ob*ect 4 ed to end o4t'4t to the c#ient. e ion 5hi i the 'tt"#e%%i n ob*ect a ociated with the re@4e t. 5hi i the #er)let7 nte,t ob*ect a ociated with a''#ication a''#ication conte<t. con,i! 5hi i the #er)let7 nfi( ob*ect a ociated with the 'a!e. 5hi enca' 4#ate 4 e o, erver- 'eci,ic ,eat4re #ike hi!her 'a!e+onte<t 'er,ormance J%"Writer%. 5hi i im'#( a (non(m ,or thi%, and i 4 ed to ca## the 'a!e method de,ined b( the tran #ated erv#et c#a . 5he E,ce"ti n ob*ect a##ow the e<ce'tion data to be acce ed 9<ce'tion b( de i!nated 6SP. We wo4#d e<'#ain 6SP $m'#icit &b*ect in e'arate cha'ter 7 ntr l$Fl w #tate!ent%: 6SP 'rovide ,4## 'ower o, 6ava to be embedded in (o4r web a''#ication. Fo4 can 4 e a## the )P$ and b4i#din! b#ock o, 6ava in (o4r 6SP 'ro!rammin! inc#4din! deci ion makin! tatement , #oo' etc. Deci%i n$&a=in( #tate!ent%: 5he if+++el%e b#ock tart o4t #ike an ordinar( Scri't#et, b4t the Scri't#et i c#o ed at each #ine with C523 te<t inc#4ded between Scri't#et ta! . VLW int da( K 1J LR Vhtm#R VheadRVtit#eR$%...93S9 9<am'#eVGtit#eRVGheadR Vbod(R VL i, 7da( KK 1 Y da( KK 0; N LR V'R 5oda( i weekendVG'R VL Q e# e N LR V'R 5oda( i not weekendVG'R VL Q LR VGbod(R VGhtm#R 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 5oda( i not weekend Now #ook at the ,o##owin! %witch+++ca%e b#ock which ha been written a bit di,,erent#t( 4 in! ut+"rintln8; and in ide Scri't#eta : VLW int da( K 1J LR Vhtm#R VheadRVtit#eRSW$5+C...+)S9 9<am'#eVGtit#eRVGheadR Vbod(R VL witch7da(; N ca e 0: o4t.'rint#n7P$tI? S4nda(.P;J breakJ ca e 1: o4t.'rint#n7P$tI? 2onda(.P;J breakJ ca e 2: o4t.'rint#n7P$tI? 54e da(.P;J breakJ ca e 1: o4t.'rint#n7P$tI? Wedne da(.P;J breakJ ca e D: o4t.'rint#n7P$tI? 5h4r da(.P;J breakJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 2.D

ca e .: o4t.'rint#n7P$tI? %rida(.P;J breakJ de,a4#t: o4t.'rint#n7P$t? Sat4rda(.P;J Q LR VGbod(R VGhtm#R 5hi wo4#d 'rod4ce ,o##owin! re 4#t: $t? Wedne da(. 4 " #tate!ent%: Fo4 can a# o 4 e three ba ic t('e o, #oo'in! b#ock in 6ava: f r, while,and d Zwhile b#ock in (o4r 6SP 'ro!rammin!. 3et 4 #ook at the ,o##owin! f r #oo' e<am'#e: VLW int ,ontSiBeJ LR Vhtm#R VheadRVtit#eR%&" 3&&P 9<am'#eVGtit#eRVGheadR Vbod(R VL,or 7 ,ontSiBe K 1J ,ontSiBe VK 1J ,ontSiBe>>;N LR V,ont co#orKP!reenP iBeKPVLK ,ontSiBe LRPR 6SP 54toria# VG,ontRVbr GR VLQLR VGbod(R VGhtm#R 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 6SP 54toria# 6SP 54toria# 6SP 54toria# )bove e<am'#e can be written 4 in! while #oo' a ,o##ow : VLW int ,ontSiBeJ LR Vhtm#R VheadRVtit#eRWC$39 3&&P 9<am'#eVGtit#eRVGheadR Vbod(R VLwhi#e 7 ,ontSiBe VK 1;N LR V,ont co#orKP!reenP iBeKPVLK ,ontSiBe LRPR 6SP 54toria# VG,ontRVbr GR VL,ontSiBe>>JLR VLQLR VGbod(R VGhtm#R 5hi wo4#d a# o 'rod4ce ,o##owin! re 4#t: 6SP 54toria# 6SP 54toria# 6SP 54toria# J#P O"erat r%: 6SP 4''ort a## the #o!ica# and arithmetic o'erator 4''orted b( 6ava. %o##owin! tab#e !ive a #i t o, a## the o'erator with the hi!he t 'recedence a''ear at the to' o, the tab#e, tho e with the #owe t a''ear at the bottom. +2+ 3imited i a S4b idiar( o, 5+S 3imited 2..

Within an e<'re ion, hi!her 'recedence o'erator wi## be eva#4ated ,ir t. A%% ciati)it 7ate( ry O"erat r y Po t,i< 7; 8: . 7dot o'erator; 3e,t to ri!ht =nar( >> - - W Z "i!ht to #e,t 24#ti'#icative OGL 3e,t to ri!ht )dditive Shi,t "e#ationa# 9@4a#it( Eitwi e )ND Eitwi e A&" Eitwi e &" 3o!ica# )ND 3o!ica# &" +onditiona# ) i!nment >RR RRR VV R RK V VK KK WK X U Y XX YY [: K >K -K OK GK LK RRK VVK XK UK YK , 3e,t to ri!ht 3e,t to ri!ht 3e,t to ri!ht 3e,t to ri!ht 3e,t to ri!ht 3e,t to ri!ht 3e,t to ri!ht 3e,t to ri!ht 3e,t to ri!ht "i!ht to #e,t "i!ht to #e,t

+omma 3e,t to ri!ht J#P 4iteral%: 5he 6SP e<'re ion #an!4a!e de,ine the ,o##owin! #itera# : 6 lean: tr4e and ,a# e Inte(er: a in 6ava Fl atin( " int: a in 6ava #trin(: with in!#e and do4b#e @4ote J P i e ca'ed a IP, ? i e ca'ed a I?, and I i e ca'ed a II. Null: n4## J#P $ Directi)e% 6SP directive 'rovide direction and in tr4ction to the container, te##in! it how to hand#e certain a 'ect o, 6SP 'roce in!. ) 6SP directive a,,ect the overa## tr4ct4re o, the erv#et c#a . $t 4 4a##( ha the ,o##owin! ,orm: VL^ directive attrib4teKPva#4eP LR Directive can have a n4mber o, attrib4te which (o4 can #i t down a ke(-va#4e 'air and e'arated b( comma . 5he b#ank between the ^ (mbo# and the directive name, and between the #a t attrib4te and the c#o in! LR, are o'tiona#. 5here are three t('e o, directive ta!: Directi)e De%cri"ti n De,ine 'a!e-de'endent attrib4te , 4ch a cri'tin! #an!4a!e, VL^ 'a!e ... LR error 'a!e, and b4,,erin! re@4irement . VL^ inc#4de ... LR $nc#4de a ,i#e d4rin! the tran #ation 'ha e. Dec#are a ta! #ibrar(, containin! c4 tom action , 4 ed in the VL^ ta!#ib ... LR 'a!e *he "a(e Directi)e: 5he "a(e directive i 4 ed to 'rovide in tr4ction to the container that 'ertain to the c4rrent 6SP 'a!e. Fo4 ma( code 'a!e directive an(where in (o4r 6SP 'a!e. E( convention, 'a!e directive are coded at the to' o, the 6SP 'a!e. %o##owin! i the ba ic (nta< o, 'a!e directive: VL^ 'a!e attrib4teKPva#4eP LR Fo4 can write A23 e@4iva#ent o, the above (nta< a ,o##ow : V* ':directive.'a!e attrib4teKPva#4eP GR Attribute%: %o##owin! i the #i t o, attrib4te a ociated with 'a!e directive: Attribute Pur" %e +2+ 3imited i a S4b idiar( o, 5+S 3imited 2./

b4,,er a4to%#4 h content5('e

S'eci,ie a b4,,erin! mode# ,or the o4t'4t tream. +ontro# the behavior o, the erv#et o4t'4t b4,,er. De,ine the character encodin! cheme. De,ine the ="3 o, another 6SP that re'ort on 6ava 4nchecked errorPa!e r4ntime e<ce'tion . $ndicate i, thi 6SP 'a!e i a ="3 'eci,ied b( another 6SP i 9rrorPa!e 'a!e? errorPa!e attrib4te. e<tend S'eci,ie a 4'erc#a that the !enerated erv#et m4 t e<tend S'eci,ie a #i t o, 'acka!e or c#a e ,or 4 e in the 6SP a the im'ort 6ava im'ort tatement doe ,or 6ava c#a e . De,ine a trin! that can be acce ed with the erv#et? in,o !etServ#et$n,o7; method. i 5hreadSa,e De,ine the threadin! mode# ,or the !enerated erv#et. #an!4a!e De,ine the 'ro!rammin! #an!4a!e 4 ed in the 6SP 'a!e. S'eci,ie whether or not the 6SP 'a!e 'artici'ate in C55P e ion e ion S'eci,ie whether or not 93 e<'re ion within the 6SP 'a!e wi## i 93$!nored be i!nored. i Scri'tin!9nab#ed Determine i, cri'tin! e#ement are a##owed ,or 4 e. +heck more detai# re#ated to a## the above attrib4te at Pa!e Directive. *he include Directi)e: 5he include directive i 4 ed to inc#4de a ,i#e d4rin! the tran #ation 'ha e. 5hi directive te## the container to mer!e the content o, other e<terna# ,i#e with the c4rrent 6SP d4rin! the tran #ation 'ha e. Fo4 ma( code i$clude directive an(where in (o4r 6SP 'a!e. 5he !enera# 4 a!e ,orm o, thi directive i a ,o##ow : VL^ inc#4de ,i#eKPre#ative 4r#P R 5he ,i#ename in the inc#4de directive i act4a##( a re#ative ="3. $, (o4 *4 t 'eci,( a ,i#ename with no a ociated 'ath, the 6SP com'i#er a 4me that the ,i#e i in the ame director( a (o4r 6SP. Fo4 can write A23 e@4iva#ent o, the above (nta< a ,o##ow : V* ':directive.inc#4de ,i#eKPre#ative 4r#P GR +heck more detai# re#ated to inc#4de directive at $nc#4de Directive. *he ta(lib Directi)e: 5he 6avaServer Pa!e )P$ a##ow (o4 to de,ine c4 tom 6SP ta! that #ook #ike C523 or A23 ta! and a ta! #ibrar( i a et o, 4 er-de,ined ta! that im'#ement c4 tom behavior. 5he ta(lib directive dec#are that (o4r 6SP 'a!e 4 e a et o, c4 tom ta! , identi,ie the #ocation o, the #ibrar(, and 'rovide a mean ,or identi,(in! the c4 tom ta! in (o4r 6SP 'a!e. 5he ta!#ib directive ,o##ow the ,o##owin! (nta<: VL^ ta!#ib 4riKP4riP 're,i<KP're,i<&,5a!P R Where the uri attrib4te va#4e re o#ve to a #ocation the container 4nder tand and the "refi, attrib4te in,orm a container what bit o, mark4' are c4 tom action . Fo4 can write A23 e@4iva#ent o, the above (nta< a ,o##ow : V* ':directive.ta!#ib 4riKP4riP 're,i<KP're,i<&,5a!P GR +heck more detai# re#ated to ta!#ib directive at 5a!#ib Directive. J#P Acti n% 6SP action 4 e con tr4ct in A23 (nta< to contro# the behavior o, the erv#et en!ine. Fo4 can d(namica##( in ert a ,i#e, re4 e 6avaEean com'onent , ,orward the 4 er to another 'a!e, or !enerate C523 ,or the 6ava '#4!in. 5here i on#( one (nta< ,or the )ction e#ement, a it con,orm to the A23 tandard: V* ':actionTname attrib4teKPva#4eP GR )ction e#ement are ba ica##( 'rede,ined ,4nction and there are ,o##owin! 6SP action avai#ab#e: #ynta, Pur" %e * ':inc#4de $nc#4de a ,i#e at the time the 'a!e i re@4e ted * ':inc#4de $nc#4de a ,i#e at the time the 'a!e i re@4e ted * ':4 eEean %ind or in tantiate a 6avaEean * ': etPro'ert( Set the 'ro'ert( o, a 6avaEean +2+ 3imited i a S4b idiar( o, 5+S 3imited 2.0

* ':!etPro'ert( * ':,orward * ':'#4!in

* ':e#ement * ':attrib4te * ':bod( * ':te<t 7 !! n Attribute%: 5here are two attrib4te that are common to a## )ction e#ement : the id attrib4te and the %c "e attrib4te. Id attribute: 5he id attrib4te 4ni@4e#( identi,ie the )ction e#ement, and a##ow the action to be re,erenced in ide the 6SP 'a!e. $, the )ction create an in tance o, an ob*ect the id va#4e can be 4 ed to re,erence it thro4!h the im'#icit ob*ect Pa!e+onte<t #c "e attribute: 5hi attrib4te identi,ie the #i,ec(c#e o, the )ction e#ement. 5he id attrib4te and the co'e attrib4te are direct#( re#ated, a the co'e attrib4te determine the #i,e 'an o, the ob*ect a ociated with the id. 5he co'e attrib4te ha ,o4r 'o ib#e va#4e : 7a; 'a!e, 7b;re@4e t, 7c; e ion, and 7d; a''#ication. *he Wj%":includeX Acti n 5hi action #et (o4 in ert ,i#e into the 'a!e bein! !enerated. 5he (nta< #ook #ike thi : V* ':inc#4de 'a!eKPre#ative ="3P ,#4 hKPtr4eP GR =n#ike the include directive, which in ert the ,i#e at the time the 6SP 'a!e i tran #ated into a erv#et, thi action in ert the ,i#e at the time the 'a!e i re@4e ted. %o##owin! i the #i t o, attrib4te a ociated with inc#4de action: Attribute De%cri"ti n 'a!e 5he re#ative ="3 o, the 'a!e to be inc#4ded. 5he boo#ean attrib4te determine whether the inc#4ded re o4rce ,#4 h ha it b4,,er ,#4 hed be,ore it i inc#4ded. E,a!"le: 3et 4 de,ine ,o##owin! two ,i#e 7a;date.*' and 7b; main.* ' a ,o##ow : %o##owin! i the content o, date.* ' ,i#e: V'R 5oda(? date: VLK 7new *ava.4ti#.Date7;;.to3oca#eStrin!7;LR VG'R Cere i the content o, main.* ' ,i#e: Vhtm#R VheadR Vtit#eR5he inc#4de )ction 9<am'#eVGtit#eR VGheadR Vbod(R VcenterR Vh2R5he inc#4de action 9<am'#eVGh2R V* ':inc#4de 'a!eKPdate.* 'P ,#4 hKPtr4eP GR VGcenterR VGbod(R VGhtm#R Now #et 4 kee' a## the e ,i#e in root director( and tr( to acce main.* '. 5hi wo4#d di '#a( re 4#t omethin! #ike thi : *he include acti n E,a!"le 5oda(? date: 12-Se'-2010 1D:.D:22 *he Wj%":u%e6eanX Acti n 5he u%e6ean action i @4ite ver ati#e. $t ,ir t earche ,or an e<i tin! ob*ect 4ti#iBin! the id and co'e variab#e . $, an ob*ect i not ,o4nd, it then trie to create the 'eci,ied ob*ect. 5he im'#e t wa( to #oad a bean i a ,o##ow : V* ':4 eEean GR &nce a bean c#a i #oaded, (o4 can 4 e j%":%etPr "erty and j%":(etPr "erty action to modi,( and retrieve bean 'ro'ertie . +2+ 3imited i a S4b idiar( o, 5+S 3imited 2.8

$n ert the 'ro'ert( o, a 6avaEean into the o4t'4t %orward the re@4e ter to a new 'a!e Generate brow er- 'eci,ic code that make an &E69+5 or 92E9D ta! ,or the 6ava '#4!in De,ine A23 e#ement d(namica##(. De,ine d(namica##( de,ined A23 e#ement? attrib4te. De,ine d(namica##( de,ined A23 e#ement? bod(. = e to write tem'#ate te<t in 6SP 'a!e and doc4ment .

%o##owin! i the #i t o, attrib4te a ociated with 4 eEean action: Attribute De%cri"ti n c#a De i!nate the ,4## 'acka!e name o, the bean. t('e S'eci,ie the t('e o, the variab#e that wi## re,er to the ob*ect. Give the name o, the bean a 'eci,ied b( the in tantiate 7; beanName method o, the *ava.bean .Eean c#a . 3et 4 di c4 abo4t j%":%etPr "erty and j%":(etPr "erty action be,ore !ivin! a va#id e<am'#e re#ated to the e action . *he Wj%":%etPr "ertyX Acti n 5he %etPr "erty action et the 'ro'ertie o, a Eean. 5he Eean m4 t have been 'revio4 #( de,ined be,ore thi action. 5here are two ba ic wa( to 4 e the etPro'ert( action: Fo4 can 4 e * ': etPro'ert( a,ter, b4t o4t ide o,, a * ':4 eEean e#ement, a be#ow: V* ':4 eEean ... GR ... V* ': etPro'ert( nameKPm(NameP 'ro'ert(KP omePro'ert(P ...GR $n thi ca e, the * ': etPro'ert( i e<ec4ted re!ard#e o, whether a new bean wa in tantiated or an e<i tin! bean wa ,o4nd. ) econd conte<t in which * ': etPro'ert( can a''ear i in ide the bod( o, a * ':4 eEean e#ement, a be#ow: V* ':4 eEean ... R ... V* ': etPro'ert( nameKPm(NameP 'ro'ert(KP omePro'ert(P ...GR VG* ':4 eEeanR Cere, the * ': etPro'ert( i e<ec4ted on#( i, a new ob*ect wa in tantiated, not i, an e<i tin! one wa ,o4nd. %o##owin! i the #i t o, attrib4te a ociated with etPro'ert( action: Attribute De%cri"ti n De i!nate the bean who e 'ro'ert( wi## be et. 5he Eean m4 t name have been 'revio4 #( de,ined. $ndicate the 'ro'ert( (o4 want to et. ) va#4e o, POP mean that 'ro'ert( a## re@4e t 'arameter who e name match bean 'ro'ert( name wi## be 'a ed to the a''ro'riate etter method . 5he va#4e that i to be a i!ned to the !iven 'ro'ert(. 5he the va#4e 'arameter? va#4e i n4##, or the 'arameter doe not e<i t, the etPro'ert( action i i!nored. 5he 'aram attrib4te i the name o, the re@4e t 'arameter who e 'aram va#4e the 'ro'ert( i to receive. Fo4 can?t 4 e both va#4e and 'aram, b4t it i 'ermi ib#e to 4 e neither. *he Wj%":(etPr "ertyX Acti n 5he (etPr "erty action i 4 ed to retrieve the va#4e o, a !iven 'ro'ert( and convert it to a trin!, and ,ina##( in ert it into the o4t'4t. 5he !etPro'ert( action ha on#( two attrib4te , both o, which are re@4ired an im'#e (nta< i a ,o##ow : V* ':4 eEean ... GR ... V* ':!etPro'ert( nameKPm(NameP 'ro'ert(KP omePro'ert(P ...GR %o##owin! i the #i t o, re@4ired attrib4te a ociated with etPro'ert( action: Attribute De%cri"ti n 5he name o, the Eean that ha a 'ro'ert( to be retrieved. 5he name Eean m4 t have been 'revio4 #( de,ined. 5he 'ro'ert( attrib4te i the name o, the Eean 'ro'ert( to be 'ro'ert( retrieved. E,a!"le: 3et 4 de,ine a te t bean which we wi## 4 e in o4r e<am'#e: GO %i#e: 5e tEean.*ava OG 'acka!e actionJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 2.-

'4b#ic c#a 5e tEean N 'rivate Strin! me a!e K PNo me a!e 'eci,iedPJ '4b#ic Strin! !et2e a!e7; N ret4rn7me a!e;J Q '4b#ic void et2e a!e7Strin! me a!e; N thi .me a!e K me a!eJ Q Q +om'i#e above code to !enerated 5e tEean.c#a ,i#e and make 4re that (o4 co'ied 5e tEean.c#a in +:Ia'ache-tomcat-0.0.2Iweba'' IW9E-$N%Ic#a e Iaction ,o#der and +3)SSP)5C variab#e ho4#d a# o be et to thi ,o#der: Now 4 e the ,o##owin! code in main.* ' ,i#e which #oad the bean and et G!et a im'#e Strin! 'arameter: Vhtm#R VheadR Vtit#eR= in! 6avaEean in 6SPVGtit#eR VGheadR Vbod(R VcenterR Vh2R= in! 6avaEean in 6SPVGh2R V* ':4 eEean GR V* ': etPro'ert( nameKPte tP 'ro'ert(KPme a!eP va#4eKPCe##o 6SP...P GR V'RGot me a!e....VG'R V* ':!etPro'ert( nameKPte tP 'ro'ert(KPme a!eP GR VGcenterR VGbod(R VGhtm#R Now tr( to acce main.* ', it wo4#d di '#a( ,o##owin! re 4#t: 5%in( Ja)a6ean% in J#P Got me a!e.... Ce##o 6SP... *he Wj%":f rwardX Acti n 5he f rward action terminate the action o, the c4rrent 'a!e and ,orward the re@4e t to another re o4rce 4ch a a tatic 'a!e, another 6SP 'a!e, or a 6ava Serv#et. 5he im'#e (nta< o, thi action i a ,o##ow : V* ':,orward 'a!eKP"e#ative ="3P GR %o##owin! i the #i t o, re@4ired attrib4te a ociated with ,orward action: Attribute De%cri"ti n Sho4#d con i t o, a re#ative ="3 o, another re o4rce 4ch a a 'a!e tatic 'a!e, another 6SP 'a!e, or a 6ava Serv#et. E,a!"le: 3et 4 re4 e ,o##owin! two ,i#e 7a; date.*' and 7b; main.* ' a ,o##ow : %o##owin! i the content o, date.* ' ,i#e: V'R 5oda(? date: VLK 7new *ava.4ti#.Date7;;.to3oca#eStrin!7;LR VG'R Cere i the content o, main.* ' ,i#e: Vhtm#R +2+ 3imited i a S4b idiar( o, 5+S 3imited 2/0

VheadR Vtit#eR5he inc#4de )ction 9<am'#eVGtit#eR VGheadR Vbod(R VcenterR Vh2R5he inc#4de action 9<am'#eVGh2R V* ':,orward 'a!eKPdate.* 'P GR VGcenterR VGbod(R VGhtm#R Now #et 4 kee' a## the e ,i#e in root director( and tr( to acce main.* '. 5hi wo4#d di '#a( re 4#t omethin! #ike a be#ow. Cere it di carded content ,rom main 'a!e and di '#a(ed content ,rom ,orwarded 'a!e on#(. 5oda(? date: 12-Se'-2010 1D:.D:22 *he Wj%":"lu(inX Acti n 5he "lu(in action i 4 ed to in ert 6ava com'onent into a 6SP 'a!e. $t determine the t('e o, brow er and in ert the Vob*ectR or VembedR ta! a needed. $, the needed '#4!in i not 're ent, it down#oad the '#4!in and then e<ec4te the 6ava com'onent. 5he 6ava com'onent can be either an )''#et or a 6avaEean. 5he '#4!in action ha evera# attrib4te that corre 'ond to common C523 ta! 4 ed to ,ormat 6ava com'onent . 5he V'aramR e#ement can a# o be 4 ed to end 'arameter to the )''#et or Eean. %o##owin! i the t('ica# (nta< o, 4 in! '#4!in action: V* ':'#4!in t('eKPa''#etP codeba eKPdirnameP codeKP2()''#et.c#a P widthKP/0P hei!htKP80PR V* ':'aram nameKP,ontco#orP va#4eKPredP GR V* ':'aram nameKPback!ro4ndP va#4eKPb#ackP GR V* ':,a##backR =nab#e to initia#iBe 6ava P#4!in VG* ':,a##backR VG* ':'#4!inR Fo4 can tr( thi action 4 in! ome a''#et i, (o4 are intere ted. ) new e#ement, the V,a##backR e#ement, can be 4 ed to 'eci,( an error trin! to be ent to the 4 er in ca e the com'onent ,ai# . *he Wj%":ele!entX Acti n *he Wj%":attributeX Acti n *he Wj%":b dyX Acti n 5he V* ':e#ementR, #tJ* ':attrib4teR and V* ':bod(R action are 4 ed to de,ine A23 e#ement d(namica##(. 5he word d(namica##( i im'ortant, beca4 e it mean that the A23 e#ement can be !enerated at re@4e t time rather than tatica##( at com'i#e time. %o##owin! i a im'#e e<am'#e to de,ine A23 e#ement d(namica##(: VL^'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#PLR Vhtm# <m#n KPhtt':GGwww.w1c.or!G1---G<htm#P <m#n :* 'KPhtt':GG*ava. 4n.comG6SPGPa!ePR VheadRVtit#eRGenerate A J#P $ I!"licit Object% 6SP $m'#icit &b*ect are the 6ava ob*ect that the 6SP +ontainer make avai#ab#e to deve#o'er in each 'a!e and deve#o'er can ca## them direct#( witho4t bein! e<'#icit#( dec#ared. 6SP $m'#icit &b*ect are a# o ca##ed 're-de,ined variab#e . 6SP 4''ort nine $m'#icit &b*ect which are #i ted be#ow: Object De%cri"ti n 5hi i the 'tt"#er)letRe@ue%t ob*ect a ociated with the re@4e t re@4e t. 5hi i the 'tt"#er)letRe%" n%e ob*ect a ociated with the re 'on e re 'on e to the c#ient. +2+ 3imited i a S4b idiar( o, 5+S 3imited 2/1

o4t e ion a''#ication con,i! 'a!e+onte<t 'a!e 9<ce'tion

5hi i the PrintWriter ob*ect 4 ed to end o4t'4t to the c#ient. 5hi i the 'tt"#e%%i n ob*ect a ociated with the re@4e t. 5hi i the #er)let7 nte,t ob*ect a ociated with a''#ication conte<t. 5hi i the #er)let7 nfi( ob*ect a ociated with the 'a!e. 5hi enca' 4#ate 4 e o, erver- 'eci,ic ,eat4re #ike hi!her 'er,ormance J%"Writer%. 5hi i im'#( a (non(m ,or thi%, and i 4 ed to ca## the method de,ined b( the tran #ated erv#et c#a . 5he E,ce"ti n ob*ect a##ow the e<ce'tion data to be acce ed b( de i!nated 6SP.

*he re@ue%t Object: 5he re@4e t ob*ect i an in tance o, a *ava<. erv#et.htt'.Ctt'Serv#et"e@4e t ob*ect. 9ach time a c#ient re@4e t a 'a!e the 6SP en!ine create a new ob*ect to re're ent that re@4e t. 5he re@4e t ob*ect 'rovide method to !et C55P header in,ormation inc#4din! ,orm data, cookie , C55P method etc. We wo4#d ee com'#ete et o, method a ociated with re@4e t ob*ect in comin! cha'ter: 6SP - +#ient "e@4e t. *he re%" n%e Object: 5he re 'on e ob*ect i an in tance o, a *ava<. erv#et.htt'.Ctt'Serv#et"e 'on e ob*ect. 64 t a the erver create the re@4e t ob*ect, it a# o create an ob*ect to re're ent the re 'on e to the c#ient. 5he re 'on e ob*ect a# o de,ine the inter,ace that dea# with creatin! new C55P header . 5hro4!h thi ob*ect the 6SP 'ro!rammer can add new cookie or date tam' , C55P tat4 code etc. We wo4#d ee com'#ete et o, method a ociated with re 'on e ob*ect in comin! cha'ter: 6SP Server "e 'on e. *he ut Object: 5he o4t im'#icit ob*ect i an in tance o, a *ava<. erv#et.* '.6 'Writer ob*ect and i 4 ed to end content in a re 'on e. 5he initia# 6 'Writer ob*ect i in tantiated di,,erent#( de'endin! on whether the 'a!e i b4,,ered or not. E4,,erin! can be ea i#( t4rned o,, b( 4 in! the b4,,eredK?,a# e? attrib4te o, the 'a!e directive. 5he 6 'Writer ob*ect contain mo t o, the ame method a the *ava.io.PrintWriter c#a . Cowever, 6 'Writer ha ome additiona# method de i!ned to dea# with b4,,erin!. =n#ike the PrintWriter ob*ect, 6 'Writer throw $&9<ce'tion . %o##owin! are the im'ortant method which we wo4#d 4 e to write boo#ean char, int, do4b#e, ob*ect, Strin! etc. &eth d De%cri"ti n ut+"rint8data*y"e dt; Print a data t('e va#4e Print a data t('e va#4e then terminate the #ine with new ut+"rintln8data*y"e dt; #ine character. ut+flu%h8; %#4 h the tream. *he %e%%i n Object: 5he e ion ob*ect i an in tance o, *ava<. erv#et.htt'.Ctt'Se ion and behave e<act#( the ame wa( that e ion ob*ect behave 4nder 6ava Serv#et . 5he e ion ob*ect i 4 ed to track c#ient e ion between c#ient re@4e t . We wo4#d ee com'#ete 4 a!e o, e ion ob*ect in comin! cha'ter: 6SP - Se ion 5rackin!. *he a""licati n Object: 5he a''#ication ob*ect i direct wra''er aro4nd the Serv#et+onte<t ob*ect ,or the !enerated Serv#et and in rea#it( an in tance o, a *ava<. erv#et.Serv#et+onte<t ob*ect. 5hi ob*ect i a re're entation o, the 6SP 'a!e thro4!h it entire #i,ec(c#e. 5hi ob*ect i created when the 6SP 'a!e i initia#iBed and wi## be removed when the 6SP 'a!e i removed b( the * 'De tro(7; method. E( addin! an attrib4te to a''#ication, (o4 can en 4re that a## 6SP ,i#e that make 4' (o4r web a''#ication have acce to it. Fo4 can check a im'#e 4 e o, )''#ication &b*ect in cha'ter: 6SP - Cit +o4nter *he c nfi( Object: +2+ 3imited i a S4b idiar( o, 5+S 3imited 2/2

5he con,i! ob*ect i an in tantiation o, *ava<. erv#et.Serv#et+on,i! and i a direct wra''er aro4nd the Serv#et+on,i! ob*ect ,or the !enerated erv#et. 5hi ob*ect a##ow the 6SP 'ro!rammer acce to the Serv#et or 6SP en!ine initia#iBation 'arameter 4ch a the 'ath or ,i#e #ocation etc. 5he ,o##owin! con,i! method i the on#( one (o4 mi!ht ever 4 e, and it 4 a!e i trivia#: con,i!.!etServ#etName7;J 5hi ret4rn the erv#et name, which i the trin! contained in the V erv#et-nameR e#ement de,ined in the W9E-$N%Iweb.<m# ,i#e *he "a(e7 nte,t Object: 5he 'a!e+onte<t ob*ect i an in tance o, a *ava<. erv#et.* '.Pa!e+onte<t ob*ect. 5he 'a!e+onte<t ob*ect i 4 ed to re're ent the entire 6SP 'a!e. 5hi ob*ect i intended a a mean to acce in,ormation abo4t the 'a!e whi#e avoidin! mo t o, the im'#ementation detai# . 5hi ob*ect tore re,erence to the re@4e t and re 'on e ob*ect ,or each re@4e t. 5he a''#ication, con,i!, e ion, and o4t ob*ect are derived b( acce in! attrib4te o, thi ob*ect. 5he 'a!e+onte<t ob*ect a# o contain in,ormation abo4t the directive i 4ed to the 6SP 'a!e, inc#4din! the b4,,erin! in,ormation, the errorPa!e="3, and 'a!e co'e. 5he Pa!e+onte<t c#a de,ine evera# ,ie#d , inc#4din! P)G9TS+&P9, "9]=9S5TS+&P9, S9SS$&NTS+&P9, and )PP3$+)5$&NTS+&P9, which identi,( the ,o4r co'e . $t a# o 4''ort more than D0 method , abo4t ha#, o, which are inherited ,rom the *ava<. erv#et.* '. 6 '+onte<t c#a . &ne o, the im'ortant method i re! )eAttribute, which acce't either one or two ar!4ment . %or e<am'#e, 'a!e+onte<t.remove)ttrib4te 7PattrNameP; remove the attrib4te ,rom a## co'e , whi#e the ,o##owin! code on#( remove it ,rom the 'a!e co'e: 'a!e+onte<t.remove)ttrib4te7PattrNameP, P)G9TS+&P9;J Fo4 can check a ver( !ood 4 a!e o, 'a!e+onte<t in comin! cha'ter: 6SP - %i#e ='#oadin!. *he "a(e Object: 5hi ob*ect i an act4a# re,erence to the in tance o, the 'a!e. $t can be tho4!ht o, a an ob*ect that re're ent the entire 6SP 'a!e. 5he 'a!e ob*ect i rea##( a direct (non(m ,or the thi% ob*ect. *he e,ce"ti n Object: 5he e<ce'tion ob*ect i a wra''er containin! the e<ce'tion thrown ,rom the 'revio4 'a!e. $t i t('ica##( 4 ed to !enerate an a''ro'riate re 'on e to the error condition. We wo4#d ee com'#ete 4 a!e o, thi ob*ect in comin! cha'ter: 6SP - 9<ce'tion Cand#in!. J#P $ 7lient Re@ue%t When a brow er re@4e t ,or a web 'a!e, it end #ot o, in,ormation to the web erver which can not be read direct#( beca4 e thi in,ormation trave# a a 'art o, header o, C55P re@4e t. Fo4 can check C55P Protoco# ,or more in,ormation on thi . %o##owin! i the im'ortant header in,ormation which come ,rom brow er ide and (o4 wo4#d 4 e ver( ,re@4ent#( in web 'ro!rammin!: 'eader De%cri"ti n 5hi header 'eci,ie the 2$29 t('e that the brow er or other )cce't c#ient can hand#e. Va#4e o, i!a(e/"n( or i!a(e/j"e( are the two mo t common 'o ibi#itie . 5hi header 'eci,ie the character et the brow er can 4 e to )cce't-+har et di '#a( the in,ormation. %or e<am'#e $S&-88.--1. 5hi header 'eci,ie the t('e o, encodin! that the brow er )cce't-9ncodin! know how to hand#e. Va#4e o, (>i" or c !"re%% are the two mo t common 'o ibi#itie . 5hi header 'eci,ie the c#ient? 're,erred #an!4a!e in ca e )cce't-3an!4a!e the erv#et can 'rod4ce re 4#t in more than one #an!4a!e. %or e<am'#e en, en-4 , r4, etc. 5hi header i 4 ed b( c#ient to identi,( them e#ve when )4thoriBation acce in! 'a word-'rotected Web 'a!e . +onnection 5hi header indicate whether the c#ient can hand#e 'er i tent +2+ 3imited i a S4b idiar( o, 5+S 3imited 2/1

C55P connection . Per i tent connection 'ermit the c#ient or other brow er to retrieve m4#ti'#e ,i#e with a in!#e re@4e t. ) va#4e o, <ee"$Ali)e mean that 'er i tent connection ho4#d be 4 ed 5hi header i a''#icab#e on#( to P&S5 re@4e t and !ive the +ontent-3en!th iBe o, the P&S5 data in b(te . 5hi header ret4rn cookie to erver that 'revio4 #( ent them +ookie to the brow er. 5hi header 'eci,ie the ho t and 'ort a !iven in the ori!ina# Co t ="3. 5hi header indicate that the c#ient want the 'a!e on#( i, it ha been chan!ed a,ter the 'eci,ied date. 5he erver end a code, $,-2odi,ied-Since 10D which mean N t & dified header i, no newer re 4#t i avai#ab#e. 5hi header i the rever e o, $,-2odi,ied-SinceJ it 'eci,ie that $,-=nmodi,ied-Since the o'eration ho4#d 4cceed on#( i, the doc4ment i o#der than the 'eci,ied date. 5hi header indicate the ="3 o, the re,errin! Web 'a!e. %or e<am'#e, i, (o4 are at Web 'a!e 1 and c#ick on a #ink to Web "e,erer 'a!e 2, the ="3 o, Web 'a!e 1 i inc#4ded in the "e,erer header when the brow er re@4e t Web 'a!e 2. 5hi header identi,ie the brow er or other c#ient makin! the = er-)!ent re@4e t and can be 4 ed to ret4rn di,,erent content to di,,erent t('e o, brow er . *he 'tt"#er)letRe@ue%t Object: 5he re@4e t ob*ect i an in tance o, a *ava<. erv#et.htt'.Ctt'Serv#et"e@4e t ob*ect. 9ach time a c#ient re@4e t a 'a!e the 6SP en!ine create a new ob*ect to re're ent that re@4e t. 5he re@4e t ob*ect 'rovide method to !et C55P header in,ormation inc#4din! ,orm data, cookie , C55P method etc. 5here are ,o##owin! im'ortant method which can be 4 ed to read C55P header in (o4r 6SP 'ro!ram. 5he e method are avai#ab#e with Http.ervlet,e6uest ob*ect which re're ent c#ient re@4e t to web erver. #+N+ &eth d A De%cri"ti n 7 =ie9: (et7 =ie%8; 1 "et4rn an arra( containin! a## o, the +ookie ob*ect the c#ient ent with thi re@4e t. Enu!erati n (etAttributeNa!e%8; 2 "et4rn an 9n4meration containin! the name o, the attrib4te avai#ab#e to thi re@4e t. Enu!erati n (et'eaderNa!e%8; 1 "et4rn an en4meration o, a## the header name thi re@4e t contain . Enu!erati n (etPara!eterNa!e%8; D "et4rn an 9n4meration o, Strin! ob*ect containin! the name o, the 'arameter contained in thi re@4e t. 'tt"#e%%i n (et#e%%i n8; . "et4rn the c4rrent e ion a ociated with thi re@4e t, or i, the re@4e t doe not have a e ion, create one. 'tt"#e%%i n (et#e%%i n8b lean create; / "et4rn the c4rrent Ctt'Se ion a ociated with thi re@4e t or, i, i, there i no c4rrent e ion and create i tr4e, ret4rn a new e ion. 4 cale (et4 cale8; 0 "et4rn the 're,erred 3oca#e that the c#ient wi## acce't content in, ba ed on the )cce't3an!4a!e header 8 Object (etAttribute8#trin( na!e; +2+ 3imited i a S4b idiar( o, 5+S 3imited 2/D

10 11 12 11 1D 1.

1/

10 18 120 21 22

21 2D 2. 2/

20

28 2-

"et4rn the va#4e o, the named attrib4te a an &b*ect, or n4## i, no attrib4te o, the !iven name e<i t . #er)letIn"ut#trea! (etIn"ut#trea!8; "etrieve the bod( o, the re@4e t a binar( data 4 in! a Serv#et$n'4tStream. #trin( (etAuth*y"e8; "et4rn the name o, the a4thentication cheme 4 ed to 'rotect the erv#et, ,or e<am'#e, PE)S$+P or PSS3,P or n4## i, the 6SP wa not 'rotected #trin( (et7haracterEnc din(8; "et4rn the name o, the character encodin! 4 ed in the bod( o, thi re@4e t. #trin( (et7 ntent*y"e8; "et4rn the 2$29 t('e o, the bod( o, the re@4e t, or n4## i, the t('e i not known. #trin( (et7 nte,tPath8; "et4rn the 'ortion o, the re@4e t ="$ that indicate the conte<t o, the re@4e t. #trin( (et'eader8#trin( na!e; "et4rn the va#4e o, the 'eci,ied re@4e t header a a Strin!. #trin( (et&eth d8; "et4rn the name o, the C55P method with which thi re@4e t wa made, ,or e<am'#e, G95, P&S5, or P=5. #trin( (etPara!eter8#trin( na!e; "et4rn the va#4e o, a re@4e t 'arameter a a Strin!, or n4## i, the 'arameter doe not e<i t. #trin( (etPathInf 8; "et4rn an( e<tra 'ath in,ormation a ociated with the ="3 the c#ient ent when it made thi re@4e t. #trin( (etPr t c l8; "et4rn the name and ver ion o, the 'rotoco# the re@4e t. #trin( (etNuery#trin(8; "et4rn the @4er( trin! that i contained in the re@4e t ="3 a,ter the 'ath. #trin( (etRe! teAddr8; "et4rn the $nternet Protoco# 7$P; addre o, the c#ient that ent the re@4e t. #trin( (etRe! te' %t8; "et4rn the ,4##( @4a#i,ied name o, the c#ient that ent the re@4e t. #trin( (etRe! te5%er8; "et4rn the #o!in o, the 4 er makin! thi re@4e t, i, the 4 er ha been a4thenticated, or n4## i, the 4 er ha not been a4thenticated. #trin( (etRe@ue%t5RI8; "et4rn the 'art o, thi re@4e t? ="3 ,rom the 'rotoco# name 4' to the @4er( trin! in the ,ir t #ine o, the C55P re@4e t. #trin( (etRe@ue%ted#e%%i nId8; "et4rn the e ion $D 'eci,ied b( the c#ient. #trin( (et#er)letPath8; "et4rn the 'art o, thi re@4e t? ="3 that ca## the 6SP. #trin(9: (etPara!eterValue%8#trin( na!e; "et4rn an arra( o, Strin! ob*ect containin! a## o, the va#4e the !iven re@4e t 'arameter ha , or n4## i, the 'arameter doe not e<i t. b lean i%#ecure8; "et4rn a boo#ean indicatin! whether thi re@4e t wa made 4 in! a ec4re channe#, 4ch a C55PS. int (et7 ntent4en(th8; "et4rn the #en!th, in b(te , o, the re@4e t bod( and made avai#ab#e b( the in'4t tream, or -1 i, the #en!th i not known. int (etInt'eader8#trin( na!e; +2+ 3imited i a S4b idiar( o, 5+S 3imited

2/.

"et4rn the va#4e o, the 'eci,ied re@4e t header a an int. int (et#er)erP rt8; 10 "et4rn the 'ort n4mber on which thi re@4e t wa received. '**P 'eader Re@ue%t E,a!"le: %o##owin! i the e<am'#e which 4 e (et'eaderNa!e%8; method o, Ctt'Serv#et"e@4e t to read the C55P header in,romation. 5hi method ret4rn an 9n4meration that contain the header in,ormation a ociated with the c4rrent C55P re@4e t. &nce we have an 9n4meration, we can #oo' down the 9n4meration in the tandard manner, 4 in! hasMore%leme$ts ! method to determine when to to' and 4 in! $e1t%leme$t ! method to !et each 'arameter name. VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.OP LR Vhtm#R VheadR Vtit#eRC55P Ceader "e@4e t 9<am'#eVGtit#eR VGheadR Vbod(R VcenterR Vh2RC55P Ceader "e@4e t 9<am'#eVGh2R Vtab#e widthKP100LP borderKP1P a#i!nKPcenterPR Vtr b!co#orKPb-D-D-DPR VthRCeader NameVGthRVthRCeader Va#4e7 ;VGthR VGtrR VL 9n4meration headerName K re@4e t.!etCeaderName 7;J whi#e7headerName .ha 2ore9#ement 7;; N Strin! 'aramName K 7Strin!;headerName .ne<t9#ement7;J o4t.'rint7PVtrRVtdRP > 'aramName > PVGtdRInP;J Strin! 'aramVa#4e K re@4e t.!etCeader7'aramName;J o4t.'rint#n7PVtdR P > 'aramVa#4e > PVGtdRVGtrRInP;J Q LR VGtab#eR VGcenterR VGbod(R VGhtm#R Now '4t the above code in main.* ' and tr( to acce it. 5hi wo4#d 'rod4ce re 4#t omethin! a ,o##ow : '**P 'eader Re@ue%t E,a!"le 'eader Na!e acce't acce't-#an!4a!e 4 er-a!ent acce't-encodin! ho t connection cache-contro# OGO en-4 2oBi##aGD.0 7com'atib#eJ 2S$9 0.0J Window N5 ..1J 5ridentGD.0J $n,oPath.2J 2S-"5+ 32 8; !Bi', de,#ate #oca#ho t:8080 Hee'-)#ive no-cache 'eader Value8%;

5o become more com,ortab#e with other method (o4 can tr( ,ew more above #i ted method in the ame ,a hion. J#P #er)er Re%" n%e When a Web erver re 'ond to a C55P re@4e t to the brow er, the re 'on e t('ica##( con i t o, a tat4 #ine, ome re 'on e header , a b#ank #ine, and the doc4ment. ) t('ica# re 'on e #ook #ike thi : C55PG1.1 200 &H +ontent-5('e: te<tGhtm# +2+ 3imited i a S4b idiar( o, 5+S 3imited 2//

Ceader2: ... ... CeaderN: ... 7E#ank 3ine; VWdoct('e ...R Vhtm#R VheadR...VGheadR Vbod(R ... VGbod(R VGhtm#R 5he tat4 #ine con i t o, the C55P ver ion 7C55PG1.1 in the e<am'#e;, a tat4 code 7200 in the e<am'#e;, and a ver( hort me a!e corre 'ondin! to the tat4 code 7&H in the e<am'#e;. %o##owin! i a 4mmar( o, the mo t 4 e,4# C55P 1.1 re 'on e header which !o back to the brow er ,rom web erver ide and (o4 wo4#d 4 e them ver( ,re@4ent#( in web 'ro!rammin!: 'eader De%cri"ti n 5hi header 'eci,ie the re@4e t method 7G95, P&S5, etc.; )##ow that the erver 4''ort . 5hi header 'eci,ie the circ4m tance in which the re 'on e doc4ment can a,e#( be cached. $t can have va#4e "ublic, "ri)ate or n $cache etc. P4b#ic mean doc4ment i cacheab#e, +ache-+ontro# Private mean doc4ment i ,or a in!#e 4 er and can on#( be tored in 'rivate 7non hared; cache and no-cache mean doc4ment ho4#d never be cached. 5hi header in tr4ct the brow er whether to 4 e 'er i tent in C55P connection or not. ) va#4e o, cl %e in tr4ct the brow er +onnection not to 4 e 'er i tent C55P connection and =ee"$ali)e mean 4 in! 'er i tent connection . 5hi header #et (o4 re@4e t that the brow er a k the 4 er to +ontent-Di 'o ition ave the re 'on e to di k in a ,i#e o, the !iven name. 5hi header 'eci,ie the wa( in which the 'a!e wa encoded +ontent-9ncodin! d4rin! tran mi ion. 5hi header i!ni,ie the #an!4a!e in which the doc4ment i +ontent-3an!4a!e written. %or e<am'#e en, en-4 , r4, etc. 5hi header indicate the n4mber o, b(te in the re 'on e. 5hi +ontent-3en!th in,ormation i needed on#( i, the brow er i 4 in! a 'er i tent 7kee'-a#ive; C55P connection. 5hi header !ive the 2$29 724#ti'4r'o e $nternet 2ai# +ontent-5('e 9<ten ion; t('e o, the re 'on e doc4ment. 5hi header 'eci,ie the time at which the content ho4#d be 9<'ire con idered o4t-o,-date and th4 no #on!er be cached. 5hi header indicate when the doc4ment wa #a t chan!ed. 3a t-2odi,ied 5he c#ient can then cache the doc4ment and 4''#( a date b( an If$& dified$#ince re@4e t header in #ater re@4e t . 5hi header ho4#d be inc#4ded with a## re 'on e that have a tat4 code in the 100 . 5hi noti,ie the brow er o, the 3ocation doc4ment addre . 5he brow er a4tomatica##( reconnect to thi #ocation and retrieve the new doc4ment. 5hi header 'eci,ie how oon the brow er ho4#d a k ,or an "e,re h 4'dated 'a!e. Fo4 can 'eci,( time in n4mber o, econd a,ter which a 'a!e wo4#d be re,re hed. "etr(-),ter 5hi header can be 4 ed in con*4nction with a .01 7Service =navai#ab#e; re 'on e to te## the c#ient how oon it can re'eat it +2+ 3imited i a S4b idiar( o, 5+S 3imited 2/0

re@4e t. Set-+ookie 5hi header 'eci,ie a cookie a ociated with the 'a!e. *he 'tt"#er)letRe%" n%e Object: 5he re 'on e ob*ect i an in tance o, a *ava<. erv#et.htt'.Ctt'Serv#et"e@4e t ob*ect. 64 t a the erver create the re@4e t ob*ect, it a# o create an ob*ect to re're ent the re 'on e to the c#ient. 5he re 'on e ob*ect a# o de,ine the inter,ace that dea# with creatin! new C55P header . 5hro4!h thi ob*ect the 6SP 'ro!rammer can add new cookie or date tam' , C55P tat4 code etc. 5here are ,o##owin! method which can be 4 ed to et C55P re 'on e header in (o4r erv#et 'ro!ram. 5he e method are avai#ab#e with Http.ervlet,espo$se ob*ect which re're ent erver re 'on e. #+N+ &eth d A De%cri"ti n #trin( enc deRedirect5R48#trin( url; 1 9ncode the 'eci,ied ="3 ,or 4 e in the end"edirect method or, i, encodin! i not needed, ret4rn the ="3 4nchan!ed. #trin( enc de5R48#trin( url; 2 9ncode the 'eci,ied ="3 b( inc#4din! the e ion $D in it, or, i, encodin! i not needed, ret4rn the ="3 4nchan!ed. b lean c ntain%'eader8#trin( na!e; 1 "et4rn a boo#ean indicatin! whether the named re 'on e header ha a#read( been et. b lean i%7 !!itted8; D "et4rn a boo#ean indicatin! i, the re 'on e ha been committed. ) id add7 =ie87 =ie c =ie; . )dd the 'eci,ied cookie to the re 'on e. ) id addDate'eader8#trin( na!e, l n( date; / )dd a re 'on e header with the !iven name and date-va#4e. ) id add'eader8#trin( na!e, #trin( )alue; 0 )dd a re 'on e header with the !iven name and va#4e. ) id addInt'eader8#trin( na!e, int )alue; 8 )dd a re 'on e header with the !iven name and inte!er va#4e. ) id flu%h6uffer8; %orce an( content in the b4,,er to be written to the c#ient. ) id re%et8; 10 +#ear an( data that e<i t in the b4,,er a we## a the tat4 code and header . ) id re%et6uffer8; 11 +#ear the content o, the 4nder#(in! b4,,er in the re 'on e witho4t c#earin! header or tat4 code. ) id %endErr r8int %c; 12 Send an error re 'on e to the c#ient 4 in! the 'eci,ied tat4 code and c#earin! the b4,,er. ) id %endErr r8int %c, #trin( !%(; 11 Send an error re 'on e to the c#ient 4 in! the 'eci,ied tat4 . ) id %endRedirect8#trin( l cati n; 1D Send a tem'orar( redirect re 'on e to the c#ient 4 in! the 'eci,ied redirect #ocation ="3. ) id %et6uffer#i>e8int %i>e; 1. Set the 're,erred b4,,er iBe ,or the bod( o, the re 'on e. ) id %et7haracterEnc din(8#trin( char%et; 1/ Set the character encodin! 72$29 char et; o, the re 'on e bein! ent to the c#ient, ,or e<am'#e, to =5%-8. ) id %et7 ntent4en(th8int len; 10 Set the #en!th o, the content bod( in the re 'on e $n C55P erv#et , thi method et the C55P +ontent-3en!th header. +2+ 3imited i a S4b idiar( o, 5+S 3imited 2/8

) id %et7 ntent*y"e8#trin( ty"e; Set the content t('e o, the re 'on e bein! ent to the c#ient, i, the re 'on e ha not been committed (et. ) id %etDate'eader8#trin( na!e, l n( date; 1Set a re 'on e header with the !iven name and date-va#4e. ) id %et'eader8#trin( na!e, #trin( )alue; 20 Set a re 'on e header with the !iven name and va#4e. ) id %etInt'eader8#trin( na!e, int )alue; 21 Set a re 'on e header with the !iven name and inte!er va#4e. ) id %et4 cale84 cale l c; 22 Set the #oca#e o, the re 'on e, i, the re 'on e ha not been committed (et. ) id %et#tatu%8int %c; 21 Set the tat4 code ,or thi re 'on e. '**P 'eader Re%" n%e E,a!"le: %o##owin! e<am'#e wo4#d 4 e %etInt'eader8; method to et Refre%h header to im4#ate a di!ita# c#ock: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.OP LR Vhtm#R VheadR Vtit#eR)4to "e,re h Ceader 9<am'#eVGtit#eR VGheadR Vbod(R VcenterR Vh2R)4to "e,re h Ceader 9<am'#eVGh2R VL GG Set re,re h, a4to#oad time a . econd re 'on e. et$ntCeader7P"e,re hP, .;J GG Get c4rrent time +a#endar ca#endar K new Gre!orian+a#endar7;J Strin! amT'mJ int ho4r K ca#endar.!et7+a#endar.C&=";J int min4te K ca#endar.!et7+a#endar.2$N=59;J int econd K ca#endar.!et7+a#endar.S9+&ND;J i,7ca#endar.!et7+a#endar.)2TP2; KK 0; amT'm K P)2PJ e# e amT'm K PP2PJ Strin! +5 K ho4r>P:P> min4te >P:P> econd >P P> amT'mJ o4t.'rint#n7P+4rrent 5ime i : P > +5 > PInP;J LR VGcenterR VGbod(R VGhtm#R Now '4t the above code in main.* ' and tr( to acce it. 5hi wo4#d di '#a( c4rrent ( tem time a,ter ever( . econd a ,o##ow . 64 t r4n the 6SP and wait to ee the re 4#t: Aut Refre%h 'eader E,a!"le +4rrent 5ime i : -:DD:.0 P2 5o become more com,ortab#e with other method (o4 can tr( ,ew more above #i ted method in the ame ,a hion. J#P $ 'tt" #tatu% 7 de% 5he ,ormat o, the C55P re@4e t and C55P re 'on e me a!e are imi#ar and wi## have ,o##owin! tr4ct4re: )n initia# tat4 #ine > +"3% 7 +arria!e "et4rn > 3ine %eed ie. New 3ine ; Sero or more header #ine > +"3% ) b#ank #ine ie. a +"3% )n o'tiona# me a!e bod( #ike ,i#e, @4er( data or @4er( o4t'4t. %or e<am'#e, a erver re 'on e header #ook a ,o##ow : 18 +2+ 3imited i a S4b idiar( o, 5+S 3imited 2/-

C55PG1.1 200 &H +ontent-5('e: te<tGhtm# Ceader2: ... ... CeaderN: ... 7E#ank 3ine; VWdoct('e ...R Vhtm#R VheadR...VGheadR Vbod(R ... VGbod(R VGhtm#R 5he tat4 #ine con i t o, the C55P ver ion 7C55PG1.1 in the e<am'#e;, a tat4 code 7200 in the e<am'#e;, and a ver( hort me a!e corre 'ondin! to the tat4 code 7&H in the e<am'#e;. %o##owin! i a #i t o, C55P tat4 code and a ociated me a!e that mi!ht be ret4rned ,rom the Web Server: 7 de: &e%%a(e: De%cri"ti n: 100 &n#( a 'art o, the re@4e t ha been received b( the +ontin4e erver, b4t a #on! a it ha not been re*ected, the c#ient ho4#d contin4e with the re@4e t 101 Switchin! Protoco# 5he erver witche 'rotoco#. 200 &H 5he re@4e t i &H 201 5he re@4e t i com'#ete, and a new re o4rce i +reated created 202 5he re@4e t i acce'ted ,or 'roce in!, b4t the )cce'ted 'roce in! i not com'#ete. 201 Non-a4thoritative $n,ormation 20D No +ontent 20. "e et +ontent 20/ Partia# +ontent 100 ) #ink #i t. 5he 4 er can e#ect a #ink and !o to that 24#ti'#e +hoice #ocation. 2a<im4m ,ive addre e 101 2oved Permanent#( 5he re@4e ted 'a!e ha moved to a new 4r# 102 5he re@4e ted 'a!e ha moved tem'orari#( to a new %o4nd 4r# 101 See &ther 5he re@4e ted 'a!e can be ,o4nd 4nder a di,,erent 4r# 10D Not 2odi,ied 10. = e Pro<( 10/ 5hi code wa 4 ed in a 'revio4 ver ion. $t i no =$used #on!er 4 ed, b4t the code i re erved. 100 5he re@4e ted 'a!e ha moved tem'orari#( to a new 5em'orar( "edirect 4r#. D00 Ead "e@4e t 5he erver did not 4nder tand the re@4e t D01 5he re@4e ted 'a!e need a 4 ername and a =na4thoriBed 'a word D02 Pa(ment "e@4ired Dou ca$ $ot use this code yet D01 %orbidden )cce i ,orbidden to the re@4e ted 'a!e D0D Not %o4nd 5he erver can not ,ind the re@4e ted 'a!e. D0. 2ethod Not )##owed 5he method 'eci,ied in the re@4e t i not a##owed. D0/ 5he erver can on#( !enerate a re 'on e that i not Not )cce'tab#e acce'ted b( the c#ient. D00 Pro<( )4thentication Fo4 m4 t a4thenticate with a 'ro<( erver be,ore thi +2+ 3imited i a S4b idiar( o, 5+S 3imited 200

"e@4ired D08 D0D10 D11 D12 D11 D1D "e@4e t-4r# 5oo 3on! D1. D10 .00 .01 .02 .01 .0D .0. =n 4''orted 2edia 5('e 9<'ectation %ai#ed $nterna# Server 9rror Not $m'#emented Ead Gatewa( Service =navai#ab#e "e@4e t 5imeo4t +on,#ict Gone 3en!th "e@4ired Precondition %ai#ed "e@4e t 9ntit( 5oo 3ar!e

re@4e t can be erved. 5he re@4e t took #on!er than the erver wa 're'ared to wait. 5he re@4e t co4#d not be com'#eted beca4 e o, a con,#ict. 5he re@4e ted 'a!e i no #on!er avai#ab#e. 5he P+ontent-3en!thP i not de,ined. 5he erver wi## not acce't the re@4e t witho4t it. 5he 'recondition !iven in the re@4e t eva#4ated to ,a# e b( the erver. 5he erver wi## not acce't the re@4e t, beca4 e the re@4e t entit( i too #ar!e. 5he erver wi## not acce't the re@4e t, beca4 e the 4r# i too #on!. &cc4r when (o4 convert a P'o tP re@4e t to a P!etP re@4e t with a #on! @4er( in,ormation. 5he erver wi## not acce't the re@4e t, beca4 e the media t('e i not 4''orted. 5he re@4e t wa not com'#eted. 5he erver met an 4ne<'ected condition 5he re@4e t wa not com'#eted. 5he erver did not 4''ort the ,4nctiona#it( re@4ired. 5he re@4e t wa not com'#eted. 5he erver received an inva#id re 'on e ,rom the 4' tream erver 5he re@4e t wa not com'#eted. 5he erver i tem'orari#( over#oadin! or down. 5he !atewa( ha timed o4t. 5he erver doe not 4''ort the Phtt' 'rotoco#P ver ion.

Gatewa( 5imeo4t C55P Ver ion Not S4''orted &eth d% t #et '**P #tatu% 7 de: 5here are ,o##owin! method which can be 4 ed to et C55P Stat4 +ode in (o4r erv#et 'ro!ram. 5he e method are avai#ab#e with Http.ervlet,espo$se ob*ect. #+N+ &eth d A De%cri"ti n "ublic ) id %et#tatu% 8 int %tatu%7 de ; 5hi method et an arbitrar( tat4 code. 5he etStat4 method take an int 7the tat4 1 code; a an ar!4ment. $, (o4r re 'on e inc#4de a 'ecia# tat4 code and a doc4ment, be 4re to ca## etStat4 be,ore act4a##( ret4rnin! an( o, the content with the Pri$tWriter. "ublic ) id %endRedirect8#trin( url; 2 5hi method !enerate a 102 re 'on e a#on! with a 5ocatio$ header !ivin! the ="3 o, the new doc4ment. "ublic ) id %endErr r8int c de, #trin( !e%%a(e; 1 5hi method end a tat4 code 74 4a##( D0D; a#on! with a hort me a!e that i a4tomatica##( ,ormatted in ide an C523 doc4ment and ent to the c#ient. '**P #tatu% 7 de E,a!"le: %o##owin! i the e<am'#e which wo4#d end D00 error code to the c#ient brow er and brow er wo4#d how (o4 PNeed a4thenticationWWWP me a!e. Vhtm#R VheadR Vtit#eRSettin! C55P Stat4 +odeVGtit#eR VGheadR Vbod(R VL GG Set error code and rea on. +2+ 3imited i a S4b idiar( o, 5+S 3imited 201

re 'on e. end9rror7D00, PNeed a4thenticationWWWP ;J LR VGbod(R VGhtm#R Now ca##in! the above 6SP wo4#d di '#a( ,o##owin! re 4#t: '**P #tatu% K.M $ Need authenticati nTTT ty"e Stat4 re'ort !e%%a(e Need a4thenticationWWW de%cri"ti n 5he c#ient m4 t ,ir t a4thenticate it e#, with the 'ro<( 7Need a4thenticationWWW;. A"ache * !cat/2+2+-1 5o become more com,ortab#e with C55P tat4 code , tr( to et di,,erent tat4 code and their de cri'tion. J#P $ F r! Pr ce%%in( Fo4 m4 t have come acro man( it4ation when (o4 need to 'a ome in,ormation ,rom (o4r brow er to web erver and 4#timate#( to (o4r backend 'ro!ram. 5he brow er 4 e two method to 'a thi in,ormation to web erver. 5he e method are G95 2ethod and P&S5 2ethod. CE* !eth d: 5he G95 method end the encoded 4 er in,ormation a''ended to the 'a!e re@4e t. 5he 'a!e and the encoded in,ormation are e'arated b( the [ character a ,o##ow : htt':GGwww.te t.comGhe##o[ke(1Kva#4e1Xke(2Kva#4e2 5he G95 method i the de,a4#t method to 'a in,ormation ,rom brow er to web erver and it 'rod4ce a #on! trin! that a''ear in (o4r brow er? 3ocation:bo<. Never 4 e the G95 method i, (o4 have 'a word or other en itive in,ormation to 'a to the erver. 5he G95 method ha iBe #imitation: on#( 102D character can be in a re@4e t trin!. 5hi in,ormation i 'a ed 4 in! ]=9"FTS5"$NG header and wi## be acce ib#e thro4!h ]=9"FTS5"$NG environment variab#e which can be hand#ed 4 in! !et]4er(Strin!7; and !etParameter7; method o, re@4e t ob*ect. PO#* !eth d: ) !enera##( more re#iab#e method o, 'a in! in,ormation to a backend 'ro!ram i the P&S5 method. 5hi method 'acka!e the in,ormation in e<act#( the ame wa( a G95 method , b4t in tead o, endin! it a a te<t trin! a,ter a [ in the ="3 it end it a a e'arate me a!e. 5hi me a!e come to the backend 'ro!ram in the ,orm o, the tandard in'4t which (o4 can 'ar e and 4 e ,or (o4r 'roce in!. 6SP hand#e thi t('e o, re@4e t 4 in! !etParameter7; method to read im'#e 'arameter and !et$n'4tStream7; method to read binar( data tream comin! ,rom the c#ient. Readin( F r! Data u%in( J#P 6SP hand#e ,orm data 'ar in! a4tomatica##( 4 in! the ,o##owin! method de'endin! on the it4ation: (etPara!eter8;: Fo4 ca## re@4e t.!etParameter7; method to !et the va#4e o, a ,orm 'arameter. (etPara!eterValue%8;: +a## thi method i, the 'arameter a''ear more than once and ret4rn m4#ti'#e va#4e , ,or e<am'#e checkbo<. (etPara!eterNa!e%8;: +a## thi method i, (o4 want a com'#ete #i t o, a## 'arameter in the c4rrent re@4e t. (etIn"ut#trea!8;: +a## thi method to read binar( data tream comin! ,rom the c#ient. CE* &eth d E,a!"le 5%in( 5R4: Cere i a im'#e ="3 which wi## 'a two va#4e to Ce##o%orm 'ro!ram 4 in! G95 method. htt"://l calh %t:3.3./!ain+j%"?fir%tRna!eSDARAAla%tRna!eSA4I Ee#ow i !ain+j%" 6SP 'ro!ram to hand#e in'4t !iven b( web brow er. We are !oin! to 4 e (etPara!eter8; method which make it ver( ea ( to acce 'a ed in,ormation: Vhtm#R VheadR Vtit#eR= in! G95 2ethod to "ead %orm DataVGtit#eR VGheadR Vbod(R VcenterR Vh1R= in! G95 2ethod to "ead %orm DataVGh1R V4#R V#iRV'RVbR%ir t Name:VGbR +2+ 3imited i a S4b idiar( o, 5+S 3imited 202

VLK re@4e t.!etParameter7P,ir tTnameP;LR VG'RVG#iR V#iRV'RVbR3a t Name:VGbR VLK re@4e t.!etParameter7P#a tTnameP;LR VG'RVG#iR VG4#R VGbod(R VGhtm#R Now t('e http:>>localhost:?@?@>mai$#jspA)irst0$ameB7*,*Clast0$ameB*53 in (o4r brow er? 3ocation:bo<. 5hi wo4#d !enerate ,o##owin! re 4#t: 5%in( CE* &eth d t Read F r! Data Fir%t Na!e: S)") 4a%t Na!e: )3$ CE* &eth d E,a!"le 5%in( F r!: Cere i a im'#e e<am'#e which 'a e two va#4e 4 in! C523 %&"2 and 4bmit b4tton. We are !oin! to 4 e ame 6SP main.* ' to hand#e thi in'4t. Vhtm#R Vbod(R V,orm actionKPmain.* 'P methodKPG95PR %ir t Name: Vin'4t t('eKPte<tP nameKP,ir tTnamePR Vbr GR 3a t Name: Vin'4t t('eKPte<tP nameKP#a tTnameP GR Vin'4t t('eKP 4bmitP va#4eKPS4bmitP GR VG,ormR VGbod(R VGhtm#R Hee' thi C523 in a ,i#e Ce##o.htm and '4t it in V5omcat-in ta##ation-director(RGweba'' G"&&5 director(. When (o4 wo4#d acce http:>>localhost:?@?@>Hello#htm, here i the act4a# o4t'4t o, the above ,orm. %ir t Name: 3a t Name: 5r( to enter %ir t Name and 3a t Name and then c#ick 4bmit b4tton to ee the re 4#t on (o4r #oca# machine where tomcat i r4nnin!. Ea ed on the in'4t 'rovided, it wi## !enerate imi#ar re 4#t a mentioned in the above e<am'#e. PO#* &eth d E,a!"le 5%in( F r!: 3et 4 do #itt#e modi,ication in the above 6SP to hand#e G95 a we## a P&S5 method . Ee#ow i !ain+j%" 6SP 'ro!ram to hand#e in'4t !iven b( web brow er 4 in! G95 or P&S5 method . $n,act there i no chan!e in above 6SP beca4 e on#( wa( o, 'a in! 'arameter i chan!ed and no binar( data i bein! 'a ed to the 6SP 'ro!ram. %i#e hand#in! re#ated conce't wo4#d be e<'#ained in e'arate cha'ter where we need to read binar( data tream. Vhtm#R VheadR Vtit#eR= in! G95 and P&S5 2ethod to "ead %orm DataVGtit#eR VGheadR Vbod(R VcenterR Vh1R= in! G95 2ethod to "ead %orm DataVGh1R V4#R V#iRV'RVbR%ir t Name:VGbR VLK re@4e t.!etParameter7P,ir tTnameP;LR VG'RVG#iR V#iRV'RVbR3a t Name:VGbR VLK re@4e t.!etParameter7P#a tTnameP;LR VG'RVG#iR VG4#R +2+ 3imited i a S4b idiar( o, 5+S 3imited 201

VGbod(R VGhtm#R %o##owin! i the content o, Ce##o.htm ,i#e: Vhtm#R Vbod(R V,orm actionKPmain.* 'P methodKPP&S5PR %ir t Name: Vin'4t t('eKPte<tP nameKP,ir tTnamePR Vbr GR 3a t Name: Vin'4t t('eKPte<tP nameKP#a tTnameP GR Vin'4t t('eKP 4bmitP va#4eKPS4bmitP GR VG,ormR VGbod(R VGhtm#R Now #et 4 kee' main.* ' and he##o.htm in V5omcat-in ta##ation-director(RGweba'' G"&&5 director(. When (o4 wo4#d acce http:>>localhost:?@?@>Hello#htm, be#ow i the act4a# o4t'4t o, the above ,orm. %ir t Name: 3a t Name: 5r( to enter %ir t and 3a t Name and then c#ick 4bmit b4tton to ee the re 4#t on (o4r #oca# machine where tomcat i r4nnin!. Ea ed on the in'4t 'rovided, it wo4#d !enerate imi#ar re 4#t a mentioned in the above e<am'#e . Pa%%in( 7hec=b , Data t J#P Pr (ra! +heckbo<e are 4 ed when more than one o'tion i re@4ired to be e#ected. Cere i e<am'#e C523 code, +heckEo<.htm, ,or a ,orm with two checkbo<e Vhtm#R Vbod(R V,orm actionKPmain.* 'P methodKPP&S5P tar!etKPTb#ankPR Vin'4t t('eKPcheckbo<P nameKPmath P checkedKPcheckedP GR 2ath Vin'4t t('eKPcheckbo<P nameKP'h( ic P GR Ph( ic Vin'4t t('eKPcheckbo<P nameKPchemi tr(P checkedKPcheckedP GR +hemi tr( Vin'4t t('eKP 4bmitP va#4eKPSe#ect S4b*ectP GR VG,ormR VGbod(R VGhtm#R 5he re 4#t o, thi code i the ,o##owin! ,orm 2ath Ph( ic +hemi tr( Ee#ow i main.* ' 6SP 'ro!ram to hand#e in'4t !iven b( web brow er ,or checkbo< b4tton. Vhtm#R VheadR Vtit#eR"eadin! +heckbo< DataVGtit#eR VGheadR Vbod(R VcenterR Vh1R"eadin! +heckbo< DataVGh1R V4#R V#iRV'RVbR2ath %#a!:VGbR VLK re@4e t.!etParameter7Pmath P;LR VG'RVG#iR V#iRV'RVbRPh( ic %#a!:VGbR VLK re@4e t.!etParameter7P'h( ic P;LR VG'RVG#iR V#iRV'RVbR+hemi tr( %#a!:VGbR VLK re@4e t.!etParameter7Pchemi tr(P;LR VG'RVG#iR +2+ 3imited i a S4b idiar( o, 5+S 3imited 20D

VG4#R VGbod(R VGhtm#R %or the above e<am'#e, it wo4#d di '#a( ,o##owin! re 4#t: Readin( 7hec=b , Data &ath% Fla( : : on Phy%ic% Fla(: : n4## 7he!i%try Fla(: : on Readin( All F r! Para!eter%: %o##owin! i the !eneric e<am'#e which 4 e (etPara!eterNa!e%8; method o, Ctt'Serv#et"e@4e t to read a## the avai#ab#e ,orm 'arameter . 5hi method ret4rn an 9n4meration that contain the 'arameter name in an 4n 'eci,ied order. &nce we have an 9n4meration, we can #oo' down the 9n4meration in the tandard manner, 4 in! hasMore%leme$ts ! method to determine when to to' and 4 in! $e1t%leme$t ! method to !et each 'arameter name. VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.OP LR Vhtm#R VheadR Vtit#eRC55P Ceader "e@4e t 9<am'#eVGtit#eR VGheadR Vbod(R VcenterR Vh2RC55P Ceader "e@4e t 9<am'#eVGh2R Vtab#e widthKP100LP borderKP1P a#i!nKPcenterPR Vtr b!co#orKPb-D-D-DPR VthRParam NameVGthRVthRParam Va#4e7 ;VGthR VGtrR VL 9n4meration 'aramName K re@4e t.!etParameterName 7;J whi#e7'aramName .ha 2ore9#ement 7;; N Strin! 'aramName K 7Strin!;'aramName .ne<t9#ement7;J o4t.'rint7PVtrRVtdRP > 'aramName > PVGtdRInP;J Strin! 'aramVa#4e K re@4e t.!etCeader7'aramName;J o4t.'rint#n7PVtdR P > 'aramVa#4e > PVGtdRVGtrRInP;J Q LR VGtab#eR VGcenterR VGbod(R VGhtm#R %o##owin! i the content o, Ce##o.htm: Vhtm#R Vbod(R V,orm actionKPmain.* 'P methodKPP&S5P tar!etKPTb#ankPR Vin'4t t('eKPcheckbo<P nameKPmath P checkedKPcheckedP GR 2ath Vin'4t t('eKPcheckbo<P nameKP'h( ic P GR Ph( ic Vin'4t t('eKPcheckbo<P nameKPchemi tr(P checkedKPcheckedP GR +hem Vin'4t t('eKP 4bmitP va#4eKPSe#ect S4b*ectP GR VG,ormR VGbod(R VGhtm#R Now tr( ca##in! 6SP 4 in! above Ce##o.htm, thi wo4#d !enerate a re 4#t omethin! #ike a be#ow ba ed on the 'rovided in'4t: Readin( All F r! Para!eter% Para! Na!e Para! Value8%; 20.

+2+ 3imited i a S4b idiar( o, 5+S 3imited

math chemi tr(

on on

Fo4 can tr( above 6SP to read an( other ,orm? data which i havin! other ob*ect #ike te<t bo<, radio b4tton or dro' down bo< etc. J#P $ Filter% Serv#et and 6SP %i#ter are 6ava c#a e that can be 4 ed in Serv#et and 6SP Pro!rammin! ,or the ,o##owin! '4r'o e : 5o interce't re@4e t ,rom a c#ient be,ore the( acce a re o4rce at back end. 5o mani'4#ate re 'on e ,rom erver be,ore the( are ent back to the c#ient. 5here are vario4 t('e o, ,i#ter 4!!e ted b( the 'eci,ication : )4thentication %i#ter . Data com're ion %i#ter 9ncr('tion %i#ter . %i#ter that tri!!er re o4rce acce event . $ma!e +onver ion %i#ter . 3o!!in! and )4ditin! %i#ter . 2$29-5FP9 +hain %i#ter . 5okeniBin! %i#ter . AS3G5 %i#ter 5hat 5ran ,orm A23 +ontent. %i#ter are de'#o(ed in the de'#o(ment de cri'tor ,i#e web+,!l and then ma' to either erv#et or 6SP name or ="3 'attern in (o4r a''#ication? de'#o(ment de cri'tor. 5he de'#o(ment de cri'tor ,i#e web.<m# can be ,o4nd in F(omcat;i$stallatio$;directoryG\co$) director(. When the 6SP container tart 4' (o4r web a''#ication, it create an in tance o, each ,i#ter that (o4 have dec#ared in the de'#o(ment de cri'tor. 5he ,i#ter e<ec4te in the order that the( are dec#ared in the de'#o(ment de cri'tor. #er)let Filter &eth d%: ) ,i#ter i im'#( a 6ava c#a that im'#ement the *ava<. erv#et.%i#ter inter,ace. 5he *ava<. erv#et.%i#ter inter,ace de,ine three method : #+N+ &eth d A De%cri"ti n "ublic ) id d Filter 8#er)letRe@ue%t, #er)letRe%" n%e, Filter7hain; 1 5hi method i ca##ed b( the container each time a re@4e tGre 'on e 'air i 'a ed thro4!h the chain d4e to a c#ient re@4e t ,or a re o4rce at the end o, the chain. "ublic ) id init8Filter7 nfi( filter7 nfi(; 2 5hi method i ca##ed b( the web container to indicate to a ,i#ter that it i bein! '#aced into ervice. "ublic ) id de%tr y8; 1 5hi method i ca##ed b( the web container to indicate to a ,i#ter that it i bein! taken o4t o, ervice. J#P Filter E,a!"le: %o##owin! i the 6SP %i#ter 9<am'#e that wo4#d 'rint the c#ient $P addre and c4rrent date time each time it wo4#d acce an( 6SP ,i#e. 5hi e<am'#e wo4#d !ive (o4 ba ic 4nder tandin! o, 6SP %i#ter, b4t (o4 can write more o'hi ticated ,i#ter a''#ication 4 in! the ame conce't: GG $m'ort re@4ired *ava #ibrarie im'ort *ava.io.OJ im'ort *ava<. erv#et.OJ im'ort *ava<. erv#et.htt'.OJ im'ort *ava.4ti#.OJ GG $m'#ement %i#ter c#a '4b#ic c#a 3o!%i#ter im'#ement %i#ter N '4b#ic void init7%i#ter+on,i! con,i!; throw Serv#et9<ce'tionN GG Get init 'arameter Strin! te tParam K con,i!.!et$nitParameter7Pte t-'aramP;J GGPrint the init 'arameter +2+ 3imited i a S4b idiar( o, 5+S 3imited 20/

S( tem.o4t.'rint#n7P5e t Param: P > te tParam;J Q '4b#ic void do%i#ter7Serv#et"e@4e t re@4e t, Serv#et"e 'on e re 'on e, %i#ter+hain chain; throw *ava.io.$&9<ce'tion, Serv#et9<ce'tion N GG Get the $P addre o, c#ient machine. Strin! i')ddre K re@4e t.!et"emote)ddr7;J GG 3o! the $P addre and c4rrent time tam'. S( tem.o4t.'rint#n7P$P P> i')ddre > P, 5ime P > new Date7;.toStrin!7;;J GG Pa re@4e t back down the ,i#ter chain chain.do%i#ter7re@4e t,re 'on e;J Q '4b#ic void de tro(7 ;N GO +a##ed be,ore the %i#ter in tance i removed ,rom ervice b( the web containerOG Q Q +om'i#e 4 (Filter+ja)a in 4 4a# wa( and '4t (o4r 3o!%i#ter.c#a c#a ,i#e in V5omcat-in ta##ationdirector(RGweba'' G"&&5GW9E-$N%Gc#a e . J#P Filter &a""in( in Web+,!l: %i#ter are de,ined and then ma''ed to a ="3 or 6SP ,i#e name, in m4ch the ame wa( a Serv#et i de,ined and then ma''ed to a ="3 'attern in web.<m# ,i#e. +reate the ,o##owin! entr( ,or ,i#ter ta! in the de'#o(ment de cri'tor ,i#e web+,!l V,i#terR V,i#ter-nameR3o!%i#terVG,i#ter-nameR V,i#ter-c#a R3o!%i#terVG,i#ter-c#a R Vinit-'aramR V'aram-nameRte t-'aramVG'aram-nameR V'aram-va#4eR$nitia#iBation ParamterVG'aram-va#4eR VGinit-'aramR VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR3o!%i#terVG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R 5he above ,i#ter wo4#d a''#( to a## the erv#et and 6SP beca4 e we 'eci,ied /U in o4r con,i!4ration. Fo4 can 'eci,( a 'artic4#ar erv#et or 6SP 'ath i, (o4 want to a''#( ,i#ter on ,ew erv#et or 6SP on#(. Now tr( to ca## an( erv#et or 6SP in 4 4a# wa( and (o4 wo4#d ee !enerated #o! in (o4 web erver #o!. Fo4 can 4 e 3o!D6 #o!!er to #o! above #o! in a e'arate ,i#e. 5%in( &ulti"le Filter%: Fo4r web a''#ication ma( de,ine evera# di,,erent ,i#ter with a 'eci,ic '4r'o e. +on ider, (o4 de,ine two ,i#ter *uthe$Filter and 5ogFilter. "e t o, the 'roce wo4#d remain a e<'#ained above e<ce't (o4 need to create a di,,erent ma''in! a mentioned be#ow: V,i#terR V,i#ter-nameR3o!%i#terVG,i#ter-nameR V,i#ter-c#a R3o!%i#terVG,i#ter-c#a R Vinit-'aramR V'aram-nameRte t-'aramVG'aram-nameR V'aram-va#4eR$nitia#iBation ParamterVG'aram-va#4eR VGinit-'aramR VG,i#terR V,i#terR V,i#ter-nameR)4then%i#terVG,i#ter-nameR +2+ 3imited i a S4b idiar( o, 5+S 3imited 200

V,i#ter-c#a R)4then%i#terVG,i#ter-c#a R Vinit-'aramR V'aram-nameRte t-'aramVG'aram-nameR V'aram-va#4eR$nitia#iBation ParamterVG'aram-va#4eR VGinit-'aramR VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR3o!%i#terVG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R V,i#ter-ma''in!R V,i#ter-nameR)4then%i#terVG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R Filter% A""licati n Order: 5he order o, ,i#ter-ma''in! e#ement in web.<m# determine the order in which the web container a''#ie the ,i#ter to the erv#et or 6SP. 5o rever e the order o, the ,i#ter, (o4 *4 t need to rever e the ,i#ter-ma''in! e#ement in the web.<m# ,i#e. %or e<am'#e, above e<am'#e wo4#d a''#( 3o!%i#ter ,ir t and then it wo4#d a''#( )4then%i#ter to an( erv#et or 6SP b4t the ,o##owin! e<am'#e wo4#d rever e the order: V,i#ter-ma''in!R V,i#ter-nameR)4then%i#terVG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R V,i#ter-ma''in!R V,i#ter-nameR3o!%i#terVG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R J#P $ 7 =ie% 'andlin( +ookie are te<t ,i#e tored on the c#ient com'4ter and the( are ke't ,or vario4 in,ormation trackin! '4r'o e. 6SP tran 'arent#( 4''ort C55P cookie 4 in! 4nder#(in! erv#et techno#o!(. 5here are three te' invo#ved in identi,(in! ret4rnin! 4 er : Server cri't end a et o, cookie to the brow er. %or e<am'#e name, a!e, or identi,ication n4mber etc. Erow er tore thi in,ormation on #oca# machine ,or ,4t4re 4 e. When ne<t time brow er end an( re@4e t to web erver then it end tho e cookie in,ormation to the erver and erver 4 e that in,ormation to identi,( the 4 er or ma( be ,or ome other '4r'o e a we##. 5hi cha'ter wi## teach (o4 how to et or re et cookie , how to acce them and how to de#ete them 4 in! 6SP 'ro!ram . *he Anat !y f a 7 =ie: +ookie are 4 4a##( et in an C55P header 7a#tho4!h 6avaScri't can a# o et a cookie direct#( on a brow er;. ) 6SP that et a cookie mi!ht end header that #ook omethin! #ike thi : C55PG1.1 200 &H Date: %ri, 0D %eb 2000 21:01:18 G25 Server: )'acheG1.1.- 7=N$A; PCPGD.0b1 Set-+ookie: nameK<(BJ e<'ire K%rida(, 0D-%eb-00 22:01:18 G25J 'athKGJ domainKt4toria# 'oint.com +onnection: c#o e +ontent-5('e: te<tGhtm# ) (o4 can ee, the Set-+ookie header contain a name va#4e 'air, a G25 date, a 'ath and a domain. 5he name and va#4e wi## be ="3 encoded. 5he e<'ire ,ie#d i an in tr4ction to the brow er to P,or!etP the cookie a,ter the !iven time and date. $, the brow er i con,i!4red to tore cookie , it wi## then kee' thi in,ormation 4nti# the e<'ir( date. $, the 4 er 'oint the brow er at an( 'a!e that matche the 'ath and domain o, the cookie, it wi## re end the cookie to the erver. 5he brow er? header mi!ht #ook omethin! #ike thi : +2+ 3imited i a S4b idiar( o, 5+S 3imited 208

G95 G C55PG1.0 +onnection: Hee'-)#ive = er-)!ent: 2oBi##aGD./ 7A11J $J 3in4< 2.2./-1.a'mac ''c; Co t: Bink.demon.co.4k:112/ )cce't: ima!eG!i,, OGO )cce't-9ncodin!: !Bi' )cce't-3an!4a!e: en )cce't-+har et: i o-88.--1,O,4t,-8 +ookie: nameK<(B ) 6SP cri't wi## then have acce to the cookie thro4!h the re@4e t method re6uest#getCookies ! which ret4rn an arra( o, Cookie ob*ect . #er)let 7 =ie% &eth d%: %o##owin! i the #i t o, 4 e,4# method a ociated with +ookie ob*ect which (o4 can 4 e whi#e mani'4#atin! cookie in 6SP: #+N+ &eth d A De%cri"ti n "ublic ) id %etD !ain8#trin( "attern; 1 5hi method et the domain to which cookie a''#ie , ,or e<am'#e t4toria# 'oint.com. "ublic #trin( (etD !ain8; 2 5hi method !et the domain to which cookie a''#ie , ,or e<am'#e t4toria# 'oint.com. "ublic ) id %et&a,A(e8int e,"iry; 1 5hi method et how m4ch time 7in econd ; ho4#d e#a' e be,ore the cookie e<'ire . $, (o4 don?t et thi , the cookie wi## #a t on#( ,or the c4rrent e ion. "ublic int (et&a,A(e8; D 5hi method ret4rn the ma<im4m a!e o, the cookie, 'eci,ied in econd , E( de,a4#t, -1 indicatin! the cookie wi## 'er i t 4nti# brow er h4tdown. "ublic #trin( (etNa!e8; . 5hi method ret4rn the name o, the cookie. 5he name cannot be chan!ed a,ter creation. "ublic ) id %etValue8#trin( newValue; / 5hi method et the va#4e a ociated with the cookie. "ublic #trin( (etValue8; 0 5hi method !et the va#4e a ociated with the cookie. "ublic ) id %etPath8#trin( uri; 5hi method et the 'ath to which thi cookie a''#ie . $, (o4 don?t 'eci,( a 'ath, the 8 cookie i ret4rned ,or a## ="3 in the ame director( a the c4rrent 'a!e a we## a a## 4bdirectorie . "ublic #trin( (etPath8; 5hi method !et the 'ath to which thi cookie a''#ie . "ublic ) id %et#ecure8b lean fla(; 10 5hi method et the boo#ean va#4e indicatin! whether the cookie ho4#d on#( be ent over encr('ted 7i.e. SS3; connection . "ublic ) id %et7 !!ent8#trin( "ur" %e; 11 5hi method 'eci,ie a comment that de cribe a cookie? '4r'o e. 5he comment i 4 e,4# i, the brow er 're ent the cookie to the 4 er. "ublic #trin( (et7 !!ent8; 12 5hi method ret4rn the comment de cribin! the '4r'o e o, thi cookie, or n4## i, the cookie ha no comment. #ettin( 7 =ie% with J#P: Settin! cookie with 6SP invo#ve three te' : 8B; 7reatin( a 7 =ie bject: Fo4 ca## the +ookie con tr4ctor with a cookie name and a cookie va#4e, both o, which are trin! . +ookie cookie K new +ookie7Pke(P,Pva#4eP;J Hee' in mind, neither the name nor the va#4e ho4#d contain white 'ace or an( o, the ,o##owin! character : +2+ 3imited i a S4b idiar( o, 5+S 3imited 20-

8:7;K,PG[^:J 8-; #ettin( the !a,i!u! a(e: Fo4 4 e et2a<)!e to 'eci,( how #on! 7in econd ; the cookie ho4#d be va#id. %o##owin! wo4#d et 4' a cookie ,or 2D ho4r . cookie. et2a<)!e7/0O/0O2D;J 8J; #endin( the 7 =ie int the '**P re%" n%e header%: Fo4 4 e re%" n%e+add7 =ie to add cookie in the C55P re 'on e header a ,o##ow : re 'on e.add+ookie7cookie;J E,a!"le: 3et 4 modi,( o4r %orm 9<am'#e to et the cookie ,or ,ir t and #a t name. VL GG +reate cookie ,or ,ir t and #a t name . +ookie ,ir tName K new +ookie7P,ir tTnameP, re@4e t.!etParameter7P,ir tTnameP;;J +ookie #a tName K new +ookie7P#a tTnameP, re@4e t.!etParameter7P#a tTnameP;;J GG Set e<'ir( date a,ter 2D Cr ,or both the cookie . ,ir tName. et2a<)!e7/0O/0O2D;J #a tName. et2a<)!e7/0O/0O2D;J GG )dd both the cookie in the re 'on e header. re 'on e.add+ookie7 ,ir tName ;J re 'on e.add+ookie7 #a tName ;J LR Vhtm#R VheadR Vtit#eRSettin! +ookie VGtit#eR VGheadR Vbod(R VcenterR Vh1RSettin! +ookie VGh1R VGcenterR V4#R V#iRV'RVbR%ir t Name:VGbR VLK re@4e t.!etParameter7P,ir tTnameP;LR VG'RVG#iR V#iRV'RVbR3a t Name:VGbR VLK re@4e t.!etParameter7P#a tTnameP;LR VG'RVG#iR VG4#R VGbod(R VGhtm#R 3et 4 '4t above code in main.* ' ,i#e and 4 e it in the ,o##owin! C523 'a!e: Vhtm#R Vbod(R V,orm actionKPmain.* 'P methodKPG95PR %ir t Name: Vin'4t t('eKPte<tP nameKP,ir tTnamePR Vbr GR 3a t Name: Vin'4t t('eKPte<tP nameKP#a tTnameP GR Vin'4t t('eKP 4bmitP va#4eKPS4bmitP GR VG,ormR VGbod(R VGhtm#R Hee' above C523 content in a ,i#e he##o.* ' and '4t he##o.* ' and main.* ' in V5omcat-in ta##ationdirector(RGweba'' G"&&5 director(. When (o4 wo4#d acce http:>>localhost:?@?@>hello#jsp, here i the act4a# o4t'4t o, the above ,orm. +2+ 3imited i a S4b idiar( o, 5+S 3imited 280

%ir t Name: 3a t Name: 5r( to enter %ir t Name and 3a t Name and then c#ick 4bmit b4tton. 5hi wo4#d di '#a( ,ir t name and #a t name on (o4r creen and ame time it wo4#d et two cookie ,ir tName and #a tName which wo4#d be 'a ed back to the erver when ne<t time (o4 wo4#d 're S4bmit b4tton. Ne<t ection wo4#d e<'#ain (o4 how (o4 wo4#d acce the e cookie back in (o4r web a''#ication. Readin( 7 =ie% with J#P: 5o read cookie , (o4 need to create an arra( o, java1#servlet#http#Cookie ob*ect b( ca##in! the (et7 =ie%8 ; method o, Http.ervlet,e6uest. 5hen c(c#e thro4!h the arra(, and 4 e !etName7; and !etVa#4e7; method to acce each cookie and a ociated va#4e. E,a!"le: 3et 4 read cookie which we have et in 'revio4 e<am'#e: Vhtm#R VheadR Vtit#eR"eadin! +ookie VGtit#eR VGheadR Vbod(R VcenterR Vh1R"eadin! +ookie VGh1R VGcenterR VL +ookie cookie K n4##J +ookie8: cookie K n4##J GG Get an arra( o, +ookie a ociated with thi domain cookie K re@4e t.!et+ookie 7;J i,7 cookie WK n4## ;N o4t.'rint#n7PVh2R %o4nd +ookie Name and Va#4eVGh2RP;J ,or 7int i K 0J i V cookie .#en!thJ i>>;N cookie K cookie 8i:J o4t.'rint7PName : P > cookie.!etName7 ; > P, P;J o4t.'rint7PVa#4e: P > cookie.!etVa#4e7 ;>P VbrGRP;J Q Qe# eN o4t.'rint#n7PVh2RNo cookie ,o4nd VGh2RP;J Q LR VGbod(R VGhtm#R Now #et 4 '4t above code in main.* ' ,i#e and tr( to acce it. $, (o4 wo4#d have et ,ir tTname cookie a P6ohnP and #a tTname cookie a PP#a(erP then r4nnin! http:>>localhost:?@?@>mai$#jsp wo4#d di '#a( the ,o##owin! re 4#t: F und 7 =ie% Na!e and Value Name : ,ir tTname, Va#4e: 6ohn Name : #a tTname, Va#4e: P#a(er Delete 7 =ie% with J#P: 5o de#ete cookie i ver( im'#e. $, (o4 want to de#ete a cookie then (o4 im'#( need to ,o##ow 4' ,o##owin! three te' : "ead an a#read( e<i tin! cookie and tore it in +ookie ob*ect. Set cookie a!e a Bero 4 in! %et&a,A(e8; method to de#ete an e<i tin! cookie. )dd thi cookie back into re 'on e header. E,a!"le: %o##owin! e<am'#e wo4#d de#ete and e<i tin! cookie named P,ir tTnameP and when (o4 wo4#d r4n main.* ' 6SP ne<t time it wo4#d ret4rn n4## va#4e ,or ,ir tTname. Vhtm#R VheadR +2+ 3imited i a S4b idiar( o, 5+S 3imited 281

Vtit#eR"eadin! +ookie VGtit#eR VGheadR Vbod(R VcenterR Vh1R"eadin! +ookie VGh1R VGcenterR VL +ookie cookie K n4##J +ookie8: cookie K n4##J GG Get an arra( o, +ookie a ociated with thi domain cookie K re@4e t.!et+ookie 7;J i,7 cookie WK n4## ;N o4t.'rint#n7PVh2R %o4nd +ookie Name and Va#4eVGh2RP;J ,or 7int i K 0J i V cookie .#en!thJ i>>;N cookie K cookie 8i:J i,77cookie.!etName7 ;;.com'are5o7P,ir tTnameP; KK 0 ;N cookie. et2a<)!e70;J re 'on e.add+ookie7cookie;J o4t.'rint7PDe#eted cookie: P > cookie.!etName7 ; > PVbrGRP;J Q o4t.'rint7PName : P > cookie.!etName7 ; > P, P;J o4t.'rint7PVa#4e: P > cookie.!etVa#4e7 ;>P VbrGRP;J Q Qe# eN o4t.'rint#n7 PVh2RNo cookie ,o4nd VGh2RP;J Q LR VGbod(R VGhtm#R Now #et 4 '4t above code in main.* ' ,i#e and tr( to acce it. $t wo4#d di '#a( the ,o##owin! re 4#t: 7 =ie% Na!e and Value De#eted cookie : ,ir tTname Name : ,ir tTname, Va#4e: 6ohn Name : #a tTname, Va#4e: P#a(er Now tr( to r4n http:>>localhost:?@?@>mai$#jsp once a!ain and it ho4#d di '#a( on#( one cookie a ,o##ow : F und 7 =ie% Na!e and Value Name : #a tTname, Va#4e: P#a(er Fo4 can de#ete (o4r cookie in $nternet 9<'#orer man4a##(. Start at the 5oo# men4 and e#ect $nternet &'tion . 5o de#ete a## cookie , 're De#ete +ookie . J#P $ #e%%i n *rac=in( C55P i a P tate#e P 'rotoco# which mean each time a c#ient retrieve a Web 'a!e, the c#ient o'en a e'arate connection to the Web erver and the erver a4tomatica##( doe not kee' an( record o, 'revio4 c#ient re@4e t. Sti## there are ,o##owin! three wa( to maintain e ion between web c#ient and web erver: 7 =ie%: ) web erver can a i!n a 4ni@4e e ion $D a a cookie to each web c#ient and ,or 4b e@4ent re@4e t ,rom the c#ient the( can be reco!niBed 4 in! the received cookie. 5hi ma( not be an e,,ective wa( beca4 e man( time brow er doe not 4''ort a cookie, o $ wo4#d not recommend to 4 e thi 'roced4re to maintain the e ion . 'idden F r! Field%: ) web erver can end a hidden C523 ,orm ,ie#d a#on! with a 4ni@4e e ion $D a ,o##ow : Vin'4t t('eKPhiddenP nameKP e ionidP va#4eKP121D.PR 5hi entr( mean that, when the ,orm i 4bmitted, the 'eci,ied name and va#4e are a4tomatica##( inc#4ded in the G95 or P&S5 data. 9ach time when web brow er end re@4e t back, then e ionTid va#4e can be 4 ed to kee' the track o, di,,erent web brow er . +2+ 3imited i a S4b idiar( o, 5+S 3imited 282

5hi co4#d be an e,,ective wa( o, kee'in! track o, the e ion b4t c#ickin! on a re!4#ar 7V) C"9%...R; h('erte<t #ink doe not re 4#t in a ,orm 4bmi ion, o hidden ,orm ,ie#d a# o cannot 4''ort !enera# e ion trackin!. 5R4 Rewritin(: Fo4 can a''end ome e<tra data on the end o, each ="3 that identi,ie the e ion, and the erver can a ociate that e ion identi,ier with data it ha tored abo4t that e ion. %or e<am'#e, with htt':GGt4toria# 'oint.comG,i#e.htmJ e ionidK121D., the e ion identi,ier i attached a e ionidK121D. which can be acce ed at the web erver to identi,( the c#ient. ="3 rewritin! i a better wa( to maintain e ion and work ,or the brow er when the( don?t 4''ort cookie b4t here drawback i that (o4 wo4#d have !enerate ever( ="3 d(namica##( to a i!n a e ion $D tho4!h 'a!e i im'#e tatic C523 'a!e. *he %e%%i n Object: )'art ,rom the above mentioned three wa( , 6SP make 4 e o, erv#et 'rovided Ctt'Se ion $nter,ace which 'rovide a wa( to identi,( a 4 er acro more than one 'a!e re@4e t or vi it to a Web ite and to tore in,ormation abo4t that 4 er. E( de,a4#t, 6SP have e ion trackin! enab#ed and a new Ctt'Se ion ob*ect i in tantiated ,or each new c#ient a4tomatica##(. Di ab#in! e ion trackin! re@4ire e<'#icit#( t4rnin! it o,, b( ettin! the 'a!e directive e ion attrib4te to ,a# e a ,o##ow : VL^ 'a!e e ionKP,a# eP LR 5he 6SP en!ine e<'o e the Ctt'Se ion ob*ect to the 6SP a4thor thro4!h the im'#icit %e%%i n ob*ect. Since %e%%i n ob*ect i a#read( 'rovided to the 6SP 'ro!rammer, the 'ro!rammer can immediate#( be!in torin! and retrievin! data ,rom the ob*ect witho4t an( initia#iBation or !etSe ion7;. Cere i a 4mmar( o, im'ortant method avai#ab#e thro4!h e ion ob*ect: #+N+ &eth d A De%cri"ti n "ublic Object (etAttribute8#trin( na!e; 1 5hi method ret4rn the ob*ect bo4nd with the 'eci,ied name in thi e ion, or n4## i, no ob*ect i bo4nd 4nder the name. "ublic Enu!erati n (etAttributeNa!e%8; 2 5hi method ret4rn an 9n4meration o, Strin! ob*ect containin! the name o, a## the ob*ect bo4nd to thi e ion. "ublic l n( (et7reati n*i!e8; 1 5hi method ret4rn the time when thi e ion wa created, mea 4red in mi##i econd ince midni!ht 6an4ar( 1, 1-00 G25. "ublic #trin( (etId8; D 5hi method ret4rn a trin! containin! the 4ni@4e identi,ier a i!ned to thi e ion. "ublic l n( (et4a%tAcce%%ed*i!e8; . 5hi method ret4rn the #a t time the c#ient ent a re@4e t a ociated with thi e ion, a the n4mber o, mi##i econd ince midni!ht 6an4ar( 1, 1-00 G25. "ublic int (et&a,Inacti)eInter)al8; / 5hi method ret4rn the ma<im4m time interva#, in econd , that the erv#et container wi## kee' thi e ion o'en between c#ient acce e . "ublic ) id in)alidate8; 0 5hi method inva#idate thi e ion and 4nbind an( ob*ect bo4nd to it. "ublic b lean i%New8 8 5hi method ret4rn tr4e i, the c#ient doe not (et know abo4t the e ion or i, the c#ient choo e not to *oin the e ion. "ublic ) id re! )eAttribute8#trin( na!e; 5hi method remove the ob*ect bo4nd with the 'eci,ied name ,rom thi e ion. "ublic ) id %etAttribute8#trin( na!e, Object )alue; 10 5hi method bind an ob*ect to thi e ion, 4 in! the name 'eci,ied. "ublic ) id %et&a,Inacti)eInter)al8int inter)al; 11 5hi method 'eci,ie the time, in econd , between c#ient re@4e t be,ore the erv#et container wi## inva#idate thi e ion. #e%%i n *rac=in( E,a!"le: +2+ 3imited i a S4b idiar( o, 5+S 3imited 281

5hi e<am'#e de cribe how to 4 e the Ctt'Se ion ob*ect to ,ind o4t the creation time and the #a tacce ed time ,or a e ion. We wo4#d a ociate a new e ion with the re@4e t i, one doe not a#read( e<i t. VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.OP LR VL GG Get e ion creation time. Date create5ime K new Date7 e ion.!et+reation5ime7;;J GG Get #a t acce time o, thi web 'a!e. Date #a t)cce 5ime K new Date7 e ion.!et3a t)cce ed5ime7;;J Strin! tit#e K PWe#come Eack to m( web itePJ $nte!er vi it+o4nt K new $nte!er70;J Strin! vi it+o4ntHe( K new Strin!7Pvi it+o4ntP;J Strin! 4 er$DHe( K new Strin!7P4 er$DP;J Strin! 4 er$D K new Strin!7P)E+DP;J GG +heck i, thi i new comer on (o4r web 'a!e. i, 7 e ion.i New7;;N tit#e K PWe#come to m( web itePJ e ion. et)ttrib4te74 er$DHe(, 4 er$D;J e ion. et)ttrib4te7vi it+o4ntHe(, vi it+o4nt;J Q vi it+o4nt K 7$nte!er; e ion.!et)ttrib4te7vi it+o4ntHe(;J vi it+o4nt K vi it+o4nt > 1J 4 er$D K 7Strin!; e ion.!et)ttrib4te74 er$DHe(;J e ion. et)ttrib4te7vi it+o4ntHe(, vi it+o4nt;J LR Vhtm#R VheadR Vtit#eRSe ion 5rackin!VGtit#eR VGheadR Vbod(R VcenterR Vh1RSe ion 5rackin!VGh1R VGcenterR Vtab#e borderKP1P a#i!nKPcenterPR Vtr b!co#orKPb-D-D-DPR VthRSe ion in,oVGthR VthRVa#4eVGthR VGtrR VtrR VtdRidVGtdR VtdRVL o4t.'rint7 e ion.!et$d7;;J LRVGtdR VGtrR VtrR VtdR+reation 5imeVGtdR VtdRVL o4t.'rint7create5ime;J LRVGtdR VGtrR VtrR VtdR5ime o, 3a t )cce VGtdR VtdRVL o4t.'rint7#a t)cce 5ime;J LRVGtdR VGtrR VtrR VtdR= er $DVGtdR VtdRVL o4t.'rint74 er$D;J LRVGtdR VGtrR VtrR VtdRN4mber o, vi it VGtdR VtdRVL o4t.'rint7vi it+o4nt;J LRVGtdR +2+ 3imited i a S4b idiar( o, 5+S 3imited

28D

VGtrR VGtab#eR VGbod(R VGhtm#R Now '4t above code in main.* ' and tr( to acce http:>>localhost:?@?@>mai$#jsp. $t wo4#d di '#a( the ,o##owin! re 4#t when (o4 wo4#d r4n ,or the ,ir t time: Welc !e t !y web%ite #e%%i n Inf !ati n #e%%i n inf )alue id +reation 5ime 5ime o, 3a t )cce = er $D 0)919+-1%%DD91+.2.ED1.1E00) EE2D. 54e 64n 08 10:2/:D0 G25>0D:00 2010 54e 64n 08 10:2/:D0 G25>0D:00 2010 )E+D

N4mber o, vi it 0 Now tr( to r4n the ame 6SP ,or econd time, it wo4#d di '#a( ,o##owin! re 4#t. Welc !e 6ac= t !y web%ite #e%%i n Inf !ati n inf ty"e )alue id +reation 5ime 5ime o, 3a t )cce = er $D 0)919+-1%%DD91+.2.ED1.1E00) EE2D. 54e 64n 08 10:2/:D0 G25>0D:00 2010 54e 64n 08 10:2/:D0 G25>0D:00 2010 )E+D

N4mber o, vi it 1 Deletin( #e%%i n Data: When (o4 are done with a 4 er? e ion data, (o4 have evera# o'tion : Re! )e a "articular attribute: Fo4 can ca## public void remove*ttribute .tri$g $ame! method to de#ete the va#4e a ociated with a 'artic4#ar ke(. Delete the wh le %e%%i n: Fo4 can ca## public void i$validate ! method to di card an entire e ion. #ettin( #e%%i n ti!e ut: Fo4 can ca## public void setMa13$active3$terval i$t i$terval! method to et the timeo4t ,or a e ion individ4a##(. 4 ( the u%er ut: 5he erver that 4''ort erv#et 2.D, (o4 can ca## l ( ut to #o! the c#ient o4t o, the Web erver and inva#idate a## e ion be#on!in! to a## the 4 er . web+,!l 7 nfi(urati n: $, (o4 are 4 in! 5omcat, a'art ,rom the above mentioned method , (o4 can con,i!4re e ion time o4t in web.<m# ,i#e a ,o##ow . V e ion-con,i!R V e ion-timeo4tR1.VG e ion-timeo4tR VG e ion-con,i!R 5he timeo4t i e<'re ed a min4te , and override the de,a4#t timeo4t which i 10 min4te in 5omcat. 5he !et2a<$nactive$nterva#7 ; method in a erv#et ret4rn the timeo4t 'eriod ,or that e ion in econd . So i, (o4r e ion i con,i!4red in web.<m# ,or 1. min4te , !et2a<$nactive$nterva#7 ; ret4rn -00. J#P $ File 5"l adin( ) 6SP can be 4 ed with an C523 ,orm ta! to a##ow 4 er to 4'#oad ,i#e to the erver. )n 4'#oaded ,i#e co4#d be a te<t ,i#e or binar( or ima!e ,i#e or an( doc4ment. +2+ 3imited i a S4b idiar( o, 5+S 3imited 28.

7reatin( a File 5"l ad F r!: 5he ,o##owin! C52 code be#ow create an 4'#oader ,orm. %o##owin! are the im'ortant 'oint to be noted down: 5he ,orm !eth d attrib4te ho4#d be et to PO#* method and G95 method can not be 4 ed. 5he ,orm encty"e attrib4te ho4#d be et to !ulti"art/f r!$data. 5he ,orm acti n attrib4te ho4#d be et to a 6SP ,i#e which wo4#d hand#e ,i#e 4'#oadin! at backend erver. %o##owin! e<am'#e i 4 in! u"l adFile+j%" 'ro!ram ,i#e to 4'#oad ,i#e. 5o 4'#oad a in!#e ,i#e (o4 ho4#d 4 e a in!#e Vin'4t ...GR ta! with attrib4te t('eKP,i#eP. 5o a##ow m4#ti'#e ,i#e 4'#oadin!, inc#4de more than one in'4t ta! with di,,erent va#4e ,or the name attrib4te. 5he brow er a ociate a Erow e b4tton with each o, them. Vhtm#R VheadR Vtit#eR%i#e ='#oadin! %ormVGtit#eR VGheadR Vbod(R Vh1R%i#e ='#oad:VGh1R Se#ect a ,i#e to 4'#oad: Vbr GR V,orm actionKP='#oadServ#etP methodKP'o tP enct('eKPm4#ti'artG,orm-dataPR Vin'4t t('eKP,i#eP nameKP,i#eP iBeKP.0P GR Vbr GR Vin'4t t('eKP 4bmitP va#4eKP='#oad %i#eP GR VG,ormR VGbod(R VGhtm#R 5hi wi## di '#a( ,o##owin! re 4#t which wo4#d a##ow to e#ect a ,i#e ,rom #oca# P+ and when 4 er wo4#d c#ick at P='#oad %i#eP, ,orm wo4#d be 4bmitted a#on! with the e#ected ,i#e: File 5"l ad: Se#ect a ,i#e to 4'#oad:
NOTE: Above form is just dummy form and would not work, you should try above code at your machine to make it work.

Writin( 6ac=end J#P #cri"t: %ir t #et 4 de,ine a #ocation where 4'#oaded ,i#e wo4#d be tored. Fo4 can hard code thi in (o4r 'ro!ram or thi director( name co4#d a# o be added 4 in! an e<terna# con,i!4ration 4ch a a c nte,t$ "ara! e#ement in web.<m# a ,o##ow : Vweb-a''R .... Vconte<t-'aramR Vde cri'tionR3ocation to tore 4'#oaded ,i#eVGde cri'tionR V'aram-nameR,i#e-4'#oadVG'aram-nameR V'aram-va#4eR c:Ia'ache-tomcat-....2-Iweba'' IdataI VG'aram-va#4eR VGconte<t-'aramR .... VGweb-a''R %o##owin! i the o4rce code ,or ='#oad%i#e.* ' which can hand#e m4#ti'#e ,i#e 4'#oadin! at a time. Ee,ore 'roceedin! (o4 have make 4re the ,o##owin! : %o##owin! e<am'#e de'end on %i#e='#oad, o make 4re (o4 have the #ate t ver ion o, c !! n%$fileu"l ad+,+,+jar ,i#e in (o4r c#a 'ath. %i#e='#oad de'end on +ommon $&, o make 4re (o4 have the #ate t ver ion o, c !! n%$ i $,+,+jar ,i#e in (o4r c#a 'ath. Whi#e te tin! ,o##owin! e<am'#e, (o4 ho4#d 4'#oad a ,i#e which ha #e iBe than ma1File.i/e otherwi e ,i#e wo4#d not be 4'#oaded. 2ake 4re (o4 have created directorie c:Item' and c:Ia'ache-tomcat-....2-Iweba'' Idata we## in advance. VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.O, *ava<. erv#et.OP LR +2+ 3imited i a S4b idiar( o, 5+S 3imited 28/

VL^ 'a!e im'ortKP*ava<. erv#et.htt'.OP LR VL^ 'a!e im'ortKPor!.a'ache.common .,i#e4'#oad.OP LR VL^ 'a!e im'ortKPor!.a'ache.common .,i#e4'#oad.di k.OP LR VL^ 'a!e im'ortKPor!.a'ache.common .,i#e4'#oad. erv#et.OP LR VL^ 'a!e im'ortKPor!.a'ache.common .io.o4t'4t.OP LR VL %i#e ,i#e J int ma<%i#eSiBe K .000 O 102DJ int ma<2emSiBe K .000 O 102DJ Serv#et+onte<t conte<t K 'a!e+onte<t.!etServ#et+onte<t7;J Strin! ,i#ePath K conte<t.!et$nitParameter7P,i#e-4'#oadP;J GG Veri,( the content t('e Strin! content5('e K re@4e t.!et+ontent5('e7;J i, 77content5('e.inde<&,7Pm4#ti'artG,orm-dataP; RK 0;; N Di k%i#e$tem%actor( ,actor( K new Di k%i#e$tem%actor(7;J GG ma<im4m iBe that wi## be tored in memor( ,actor(. etSiBe5hre ho#d7ma<2emSiBe;J GG 3ocation to ave data that i #ar!er than ma<2emSiBe. ,actor(. et"e'o itor(7new %i#e7Pc:IItem'P;;J GG +reate a new ,i#e 4'#oad hand#er Serv#et%i#e='#oad 4'#oad K new Serv#et%i#e='#oad7,actor(;J GG ma<im4m ,i#e iBe to be 4'#oaded. 4'#oad. etSiBe2a<7 ma<%i#eSiBe ;J tr(N GG Par e the re@4e t to !et ,i#e item . 3i t ,i#e$tem K 4'#oad.'ar e"e@4e t7re@4e t;J GG Proce the 4'#oaded ,i#e item $terator i K ,i#e$tem .iterator7;J o4t.'rint#n7PVhtm#RP;J o4t.'rint#n7PVheadRP;J o4t.'rint#n7PVtit#eR6SP %i#e 4'#oadVGtit#eRP;J o4t.'rint#n7PVGheadRP;J o4t.'rint#n7PVbod(RP;J whi#e 7 i.ha Ne<t 7; ; N %i#e$tem ,i K 7%i#e$tem;i.ne<t7;J i, 7 W,i.i %orm%ie#d 7; ; N GG Get the 4'#oaded ,i#e 'arameter Strin! ,ie#dName K ,i.!et%ie#dName7;J Strin! ,i#eName K ,i.!etName7;J boo#ean i $n2emor( K ,i.i $n2emor(7;J #on! iBe$nE(te K ,i.!etSiBe7;J GG Write the ,i#e i,7 ,i#eName.#a t$nde<&,7PIIP; RK 0 ;N ,i#e K new %i#e7 ,i#ePath > ,i#eName. 4b trin!7 ,i#eName.#a t$nde<&,7PIIP;;; J Qe# eN ,i#e K new %i#e7 ,i#ePath > ,i#eName. 4b trin!7,i#eName.#a t$nde<&,7PIIP;>1;; J Q ,i.write7 ,i#e ; J o4t.'rint#n7P='#oaded %i#ename: P > ,i#ePath > +2+ 3imited i a S4b idiar( o, 5+S 3imited

280

,i#eName > PVbrRP;J Q Q o4t.'rint#n7PVGbod(RP;J o4t.'rint#n7PVGhtm#RP;J Qcatch79<ce'tion e<; N S( tem.o4t.'rint#n7e<;J Q Qe# eN o4t.'rint#n7PVhtm#RP;J o4t.'rint#n7PVheadRP;J o4t.'rint#n7PVtit#eRServ#et 4'#oadVGtit#eRP;J o4t.'rint#n7PVGheadRP;J o4t.'rint#n7PVbod(RP;J o4t.'rint#n7PV'RNo ,i#e 4'#oadedVG'RP;J o4t.'rint#n7PVGbod(RP;J o4t.'rint#n7PVGhtm#RP;J Q LR Now tr( to 4'#oad ,i#e 4 in! the C523 ,orm which (o4 created above. When (o4 wo4#d tr( htt':GG#oca#ho t:8080G='#oad%i#e.htm, it wo4#d di '#a( ,o##owin! re 4#t which wo4#d he#' (o4 4'#oadin! an( ,i#e ,rom (o4r #oca# machine. File 5"l ad: Se#ect a ,i#e to 4'#oad: $, (o4r 6SP cri't work ,ine, (o4r ,i#e ho4#d be 4'#oaded in c:Ia'ache-tomcat-....2-Iweba'' IdataI director(. J#P $ 'andlin( Date &ne o, the mo t im'ortant advanta!e o, 4 in! 6SP i that (o4 can 4 e a## the method avai#ab#e in core 6ava. 5hi t4toria# wo4#d take (o4 thro4!h 6ava 'rovided Date c#a which i avai#ab#e in ja)a+util 'acka!e, thi c#a enca' 4#ate the c4rrent date and time. 5he Date c#a 4''ort two con tr4ctor . 5he ,ir t con tr4ctor initia#iBe the ob*ect with the c4rrent date and time. Date7 ; 5he ,o##owin! con tr4ctor acce't one ar!4ment that e@4a# the n4mber o, mi##i econd that have e#a' ed ince midni!ht, 6an4ar( 1, 1-00 Date7#on! mi##i ec; &nce (o4 have a Date ob*ect avai#ab#e, (o4 can ca## an( o, the ,o##owin! 4''ort method to '#a( with date : #N &eth d% with De%cri"ti n b lean after8Date date; 1 "et4rn tr4e i, the invokin! Date ob*ect contain a date that i #ater than the one 'eci,ied b( date, otherwi e, it ret4rn ,a# e. b lean bef re8Date date; 2 "et4rn tr4e i, the invokin! Date ob*ect contain a date that i ear#ier than the one 'eci,ied b( date, otherwi e, it ret4rn ,a# e. Object cl ne8 ; 1 D4'#icate the invokin! Date ob*ect. int c !"are* 8Date date; +om'are the va#4e o, the invokin! ob*ect with that o, date. "et4rn 0 i, the va#4e are D e@4a#. "et4rn a ne!ative va#4e i, the invokin! ob*ect i ear#ier than date. "et4rn a 'o itive va#4e i, the invokin! ob*ect i #ater than date. int c !"are* 8Object bj; . &'erate identica##( to com'are5o7Date; i, ob* i o, c#a Date. &therwi e, it throw a +#a +a t9<ce'tion. / b lean e@ual%8Object date; "et4rn tr4e i, the invokin! Date ob*ect contain the ame time and date a the one +2+ 3imited i a S4b idiar( o, 5+S 3imited 288

'eci,ied b( date, otherwi e, it ret4rn ,a# e. l n( (et*i!e8 ; 0 "et4rn the n4mber o, mi##i econd that have e#a' ed ince 6an4ar( 1, 1-00. int ha%h7 de8 ; 8 "et4rn a ha h code ,or the invokin! ob*ect. ) id %et*i!e8l n( ti!e; Set the time and date a 'eci,ied b( time, which re're ent an e#a' ed time in mi##i econd ,rom midni!ht, 6an4ar( 1, 1-00 #trin( t #trin(8 ; 10 +onvert the invokin! Date ob*ect into a trin! and ret4rn the re 4#t. Cettin( 7urrent Date A *i!e 5hi i ver( ea ( to !et c4rrent date and time in 6SP 'ro!ram. Fo4 can 4 e a im'#e Date ob*ect with to.tri$g ! method to 'rint c4rrent date and time a ,o##ow : VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.O, *ava<. erv#et.OP LR Vhtm#R VheadR Vtit#eRDi '#a( +4rrent Date X 5imeVGtit#eR VGheadR Vbod(R VcenterR Vh1RDi '#a( +4rrent Date X 5imeVGh1R VGcenterR VL Date date K new Date7;J o4t.'rint7 PVh2 a#i!nKIPcenterIPRP >date.toStrin!7;>PVGh2RP;J LR VGbod(R VGhtm#R Now #et 4 kee' abo4t code in +4rrentDate.* ' and then ca## thi 6SP 4 in! ="3 htt':GG#oca#ho t:8080G+4rrentDate.* '. 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Di%"lay 7urrent Date A *i!e & n Jun -B -B:KL:K1 C&*V.K:.. -.B. 5r( to re,re h ="3 htt':GG#oca#ho t:8080G+4rrentDate.* ' and (o4 wo4#d ,ind di,,erence in econd ever(time (o4 wo4#d re,re h. Date 7 !"ari% n: ) $ mentioned above (o4 can 4 e a## the avai#ab#e 6ava method in (o4r 6SP cri't . $n ca e (o4 need to com'are two date , ,o##owin! are the method : Fo4 can 4 e !et5ime7 ; to obtain the n4mber o, mi##i econd that have e#a' ed ince midni!ht, 6an4ar( 1, 1-00, ,or both ob*ect and then com'are the e two va#4e . Fo4 can 4 e the method be,ore7 ;, a,ter7 ;, and e@4a# 7 ;. Eeca4 e the 12th o, the month come be,ore the 18th, ,or e<am'#e, new Date7--, 2, 12;.be,ore7new Date 7--, 2, 18;; ret4rn tr4e. Fo4 can 4 e the com'are5o7 ; method, which i de,ined b( the +om'arab#e inter,ace and im'#emented b( Date. Date F r!attin( u%in( #i!"leDateF r!at: Sim'#eDate%ormat i a concrete c#a ,or ,ormattin! and 'ar in! date in a #oca#e- en itive manner. Sim'#eDate%ormat a##ow (o4 to tart b( choo in! an( 4 er-de,ined 'attern ,or date-time ,ormattin!. 3et 4 modi,( above e<am'#e a ,o##ow : VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.OP LR VL^ 'a!e im'ortKP*ava<. erv#et.O,*ava.te<t.OP LR Vhtm#R VheadR Vtit#eRDi '#a( +4rrent Date X 5imeVGtit#eR VGheadR Vbod(R VcenterR Vh1RDi '#a( +4rrent Date X 5imeVGh1R VGcenterR +2+ 3imited i a S4b idiar( o, 5+S 3imited 28-

VL Date dNow K new Date7 ;J Sim'#eDate%ormat ,t K new Sim'#eDate%ormat 7P9 ((((.22.dd ?at? hh:mm: a BBBP;J o4t.'rint7 PVh2 a#i!nKIPcenterIPRP > ,t.,ormat7dNow; > PVGh2RP;J LR VGbod(R VGhtm#R +om'i#e above erv#et once a!ain and then ca## thi erv#et 4 in! ="3 htt':GG#oca#ho t:8080G+4rrentDate. 5hi wo4#d 'rod4ce ,o##owin! re 4#t: Di%"lay 7urrent Date A *i!e & n -.B.+.L+-B at B.:.L:KK P& C&*V.K:.. #i!"le DateF r!at f r!at c de%: 5o 'eci,( the time ,ormat 4 e a time 'attern trin!. $n thi 'attern, a## )S+$$ #etter are re erved a 'attern #etter , which are de,ined a the ,o##owin!: 7haract De%cri"ti n E,a!"le er G 9ra de i!nator )D ( Fear in ,o4r di!it 2001 2 2onth in (ear 64#( or 00 d Da( in month 10 h Co4r in ).2.GP.2. 71Z12; 12 C Co4r in da( 70Z21; 22 m 2in4te in ho4r 10 Second in min4te .. S 2i##i econd 21D 9 Da( in week 54e da( D Da( in (ear 1/0 % Da( o, week in month 2 7 econd Wed. in 64#(; w Week in (ear D0 W Week in month 1 a ).2.GP.2. marker P2 k Co4r in da( 71Z2D; 2D H Co4r in ).2.GP.2. 70Z11; 10 B 5ime Bone 9a tern Standard 5ime ? 9 ca'e ,or te<t De#imiter P Sin!#e @4ote \ %or a com'#ete #i t o, con tant avai#ab#e method to mani'4#ate date, (o4 can re,er to tandard 6ava doc4mentation. J#P $ Pa(e Redirectin( Pa!e redirection i !enera##( 4 ed when a doc4ment move to a new #ocation and we need to end the c#ient to thi new #ocation or ma( be beca4 e o, #oad ba#ancin!, or ,or im'#e randomiBation. 5he im'#e t wa( o, redirectin! a re@4e t to another 'a!e i 4 in! method %endRedirect8; o, re 'on e ob*ect. %o##owin! i the i!nat4re o, thi method: '4b#ic void re 'on e. end"edirect7Strin! #ocation; throw $&9<ce'tion 5hi method end back the re 'on e to the brow er a#on! with the tat4 code and new 'a!e #ocation. Fo4 can a# o 4 e etStat4 7; and etCeader7; method to!ether to achieve the ame redirection: .... Strin! ite K Phtt':GGwww.new'a!e.comP J re 'on e. etStat4 7re 'on e.S+T2&V9DT592P&")"$3F;J re 'on e. etCeader7P3ocationP, ite;J .... E,a!"le: +2+ 3imited i a S4b idiar( o, 5+S 3imited 2-0

5hi e<am'#e how how a 6SP 'er,orm 'a!e redirection to an another #ocation: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.OP LR Vhtm#R VheadR Vtit#eRPa!e "edirectionVGtit#eR VGheadR Vbod(R VcenterR Vh1RPa!e "edirectionVGh1R VGcenterR VL GG New #ocation to be redirected Strin! ite K new Strin!7Phtt':GGwww.'hoto,4ntoo .comP;J re 'on e. etStat4 7re 'on e.S+T2&V9DT592P&")"$3F;J re 'on e. etCeader7P3ocationP, ite;J LR VGbod(R VGhtm#R Now #et 4 '4t above code in Pa!e"edirect.* ' and ca## thi 6SP 4 in! ="3 htt':GG#oca#ho t:8080GPa!e"edirect.* '. 5hi wo4#d take (o4 !iven ="3 htt':GGwww.'hoto,4ntoo .com. J#P $ 'it% 7 unter ) hit co4nter te## (o4 abo4t the n4mber o, vi it on a 'artic4#ar 'a!e o, (o4r web ite. = 4a##( (o4 attach a hit co4nter with (o4r inde<.* ' 'a!e a 4min! 'eo'#e ,ir t #and on (o4r home 'a!e. 5o im'#ement a hit co4nter (o4 can make 4 e o, )''#ication $m'#icit ob*ect and a ociated method !et)ttrib4te7; and et)ttrib4te7;. 5hi ob*ect i a re're entation o, the 6SP 'a!e thro4!h it entire #i,ec(c#e. 5hi ob*ect i created when the 6SP 'a!e i initia#iBed and wi## be removed when the 6SP 'a!e i removed b( the * 'De tro(7; method. %o##owin! i the (nta< to et a variab#e at a''#ication #eve#: a''#ication. et)ttrib4te7Strin! He(, &b*ect Va#4e;J Fo4 can 4 e above method to et a hit co4nter variab#e and to re et the ame variab#e. %o##owin! i the method to read the variab#e et b( 'revio4 method: a''#ication.!et)ttrib4te7Strin! He(;J 9ver( time a 4 e acce (o4r 'a!e, (o4 can read c4rrent va#4e o, hit co4nter and increa e it b( one and a!ain et it ,or ,4t4re 4 e. E,a!"le: 5hi e<am'#e how how (o4 can 4 e 6SP to co4nt tota# n4mber o, hit on a 'artic4#ar 'a!e. $, (o4 want to co4nt tota# n4mber o, hit o, (o4r web ite then (o4 wo4#d have to inc#4de ame code in a## the 6SP 'a!e . VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.OP LR Vhtm#R VheadR Vtit#eR)''#cation ob*ect in 6SPVGtit#eR VGheadR Vbod(R VL $nte!er hit +o4nt K 7$nte!er;a''#ication.!et)ttrib4te7Phit+o4nterP;J i,7 hit +o4nt KKn4## YY hit +o4nt KK 0 ;N GO %ir t vi it OG o4t.'rint#n7PWe#come to m( web iteWP;J hit +o4nt K 1J Qe# eN GO ret4rn vi it OG o4t.'rint#n7PWe#come back to m( web iteWP;J hit +o4nt >K 1J Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 2-1

a''#ication. et)ttrib4te7Phit+o4nterP, hit +o4nt;J LR VcenterR V'R5ota# n4mber o, vi it : VLK hit +o4ntLRVG'R VGcenterR VGbod(R VGhtm#R Now #et 4 '4t above code in main.* ' and ca## thi 6SP 4 in! ="3 htt':GG#oca#ho t:8080Gmain.* '. 5hi wo4#d di '#a( hit co4nter va#4e which wo4#d increa e ever( time when (o4 re,re h the 'a!e. Fo4 can tr( to acce the 'a!e 4 in! di,,erent brow er and (o4 wi## ,ind that hit co4nter wi## kee' increa in! with ever( hit and wo4#d di '#a( re 4#t omethin! a ,o##ow : We#come back to m( web iteW 5ota# n4mber o, vi it : 12 'it 7 unter Re%et%: What abo4t i, (o4 re- tart (o4r a''#ication ie. web erver, thi wi## re et (o4r a''#ication variab#e and (o4r co4nter wi## re et to Bero. 5o avoid thi #o , (o4 can im'#ement (o4r co4nter in 'ro,e iona# wa( which i a ,o##ow : De,ine a databa e tab#e with a in!#e co4nt, #et 4 a( hitco4nt. ) i!n a Bero va#4e to it. With ever( hit, read the tab#e to !et the va#4e o, hitco4nt. $ncrea e the va#4e o, hitco4nt b( one and 4'date the tab#e with new va#4e. Di '#a( new va#4e o, hitco4nt a tota# 'a!e hit co4nt . $, (o4 want to co4nt hit ,or a## the 'a!e , im'#ement above #o!ic ,or a## the 'a!e . J#P $ Aut Refre%h +on ider a web'a!e which i di '#a(in! #ive !ame core or tock market tat4 or c4rrenc( e<chan!e ration. %or a## 4ch t('e o, 'a!e , (o4 wo4#d need to re,re h (o4r web 'a!e re!4#ar#( 4 in! re,re h or re#oad b4tton with (o4r brow er. 6SP make thi *ob ea ( b( 'rovidin! (o4 a mechani m where (o4 can make a web'a!e in 4ch a wa( that it wo4#d re,re h a4tomatica##( a,ter a !iven interva#. 5he im'#e t wa( o, re,re hin! a web 'a!e i 4 in! method %etInt'eader8; o, re 'on e ob*ect. %o##owin! i the i!nat4re o, thi method: '4b#ic void et$ntCeader7Strin! header, int headerVa#4e; 5hi method end back header P"e,re hP to the brow er a#on! with an inte!er va#4e which indicate time interva# in econd . Aut Pa(e Refre%h E,a!"le: %o##owin! e<am'#e wo4#d 4 e %etInt'eader8; method to et Refre%h header to im4#ate a di!ita# c#ock: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.OP LR Vhtm#R VheadR Vtit#eR)4to "e,re h Ceader 9<am'#eVGtit#eR VGheadR Vbod(R VcenterR Vh2R)4to "e,re h Ceader 9<am'#eVGh2R VL GG Set re,re h, a4to#oad time a . econd re 'on e. et$ntCeader7P"e,re hP, .;J GG Get c4rrent time +a#endar ca#endar K new Gre!orian+a#endar7;J Strin! amT'mJ int ho4r K ca#endar.!et7+a#endar.C&=";J int min4te K ca#endar.!et7+a#endar.2$N=59;J int econd K ca#endar.!et7+a#endar.S9+&ND;J i,7ca#endar.!et7+a#endar.)2TP2; KK 0; amT'm K P)2PJ e# e amT'm K PP2PJ Strin! +5 K ho4r>P:P> min4te >P:P> econd >P P> amT'mJ o4t.'rint#n7P+rrent 5ime: P > +5 > PInP;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 2-2

LR VGcenterR VGbod(R VGhtm#R Now '4t the above code in main.* ' and tr( to acce it. 5hi wo4#d di '#a( c4rrent ( tem time a,ter ever( . econd a ,o##ow . 64 t r4n the 6SP and wait to ee the re 4#t: Aut Refre%h 'eader E,a!"le +4rrent 5ime i : -:DD:.0 P2 5o become more com,ortab#e with other method (o4 can tr( ,ew more above #i ted method in the ame ,a hion. J#P $ #endin( E!ail 5o end an emai# 4 in! a 6SP i im'#e eno4!h b4t to tart with (o4 ho4#d have Ja)a&ail API and Ja)a Acti)ati n Fra!ew r= 8JAF; in ta##ed on (o4r machine. Fo4 can down#oad #ate t ver ion o, 6ava2ai# 7Ver ion 1.2; ,rom 6ava? tandard web ite. Fo4 can down#oad #ate t ver ion o, 6avaEean )ctivation %ramework 6)% 7Ver ion 1.0.2; ,rom 6ava? tandard web ite. Down#oad and 4nBi' the e ,i#e , in the new#( created to' #eve# directorie (o4 wi## ,ind a n4mber o, *ar ,i#e ,or both the a''#ication . Fo4 need to add !ail+jar and acti)ati n+jar ,i#e in (o4r +3)SSP)5C. #end a #i!"le E!ail: Cere i an e<am'#e to end a im'#e emai# ,rom (o4r machine. Cere it i a 4med that (o4r l calh %t i connected to the internet and ca'ab#e eno4!h to end an emai#. Same time make 4re a## the *ar ,i#e ,rom 6ava 9mai# )P$ 'acka!e and 6)% 'acka!e ara avai#ab#e in +3)SSP)5C. VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.O,*ava<.mai#.OPLR VL^ 'a!e im'ortKP*ava<.mai#.internet.O,*ava<.activation.OPLR VL^ 'a!e im'ortKP*ava<. erv#et.htt'.O,*ava<. erv#et.OP LR VL Strin! re 4#tJ GG "eci'ient? emai# $D need to be mentioned. Strin! to K Pabcd^!mai#.comPJ GG Sender? emai# $D need to be mentioned Strin! ,rom K Pmcmohd^!mai#.comPJ GG ) 4min! (o4 are endin! emai# ,rom #oca#ho t Strin! ho t K P#oca#ho tPJ GG Get ( tem 'ro'ertie ob*ect Pro'ertie 'ro'ertie K S( tem.!etPro'ertie 7;J GG Set4' mai# erver 'ro'ertie . etPro'ert(7Pmai#. mt'.ho tP, ho t;J GG Get the de,a4#t Se ion ob*ect. Se ion mai#Se ion K Se ion.!etDe,a4#t$n tance7'ro'ertie ;J tr(N GG +reate a de,a4#t 2ime2e a!e ob*ect. 2ime2e a!e me a!e K new 2ime2e a!e7mai#Se ion;J GG Set %rom: header ,ie#d o, the header. me a!e. et%rom7new $nternet)ddre 7,rom;;J GG Set 5o: header ,ie#d o, the header. me a!e.add"eci'ient72e a!e."eci'ient5('e.5&, new $nternet)ddre 7to;;J GG Set S4b*ect: header ,ie#d me a!e. etS4b*ect7P5hi i the S4b*ect 3ineWP;J GG Now et the act4a# me a!e me a!e. et5e<t7P5hi i act4a# me a!eP;J GG Send me a!e +2+ 3imited i a S4b idiar( o, 5+S 3imited

2-1

5ran 'ort. end7me a!e;J re 4#t K PSent me a!e 4cce ,4##(....PJ Qcatch 72e a!in!9<ce'tion me<; N me<.'rintStack5race7;J re 4#t K P9rror: 4nab#e to end me a!e....PJ Q LR Vhtm#R VheadR Vtit#eRSend 9mai# 4 in! 6SPVGtit#eR VGheadR Vbod(R VcenterR Vh1RSend 9mai# 4 in! 6SPVGh1R VGcenterR V' a#i!nKPcenterPR VL o4t.'rint#n7P"e 4#t: P > re 4#t > PInP;J LR VG'R VGbod(R VGhtm#R Now #et 4 '4t above code in Send9mai#.* ' ,i#e and ca## thi 6SP 4 in! ="3 htt':GG#oca#ho t:8080GSend9mai#.* ' which wo4#d end an emai# to !iven emai# $D abcdEgmail#com and wo4#d di '#a( ,o##owin! re 'on e: #end E!ail u%in( J#P "e 4#t: Sent me a!e 4cce ,4##(.... $, (o4 want to end an emai# to m4#ti'#e reci'ient then ,o##owin! method wo4#d be 4 ed to 'eci,( m4#ti'#e emai# $D : void add"eci'ient 72e a!e."eci'ient5('e t('e, )ddre 8: addre e ; throw 2e a!in!9<ce'tion Cere i the de cri'tion o, the 'arameter : ty"e: 5hi wo4#d be et to 5&, ++ or E++. Cere ++ re're ent +arbon +o'( and E++ re're ent E#ack +arbon +o'(. 9<am'#e Message#,ecipie$t(ype#(+ addre%%e%: 5hi i the arra( o, emai# $D. Fo4 wo4#d need to 4 e $nternet)ddre 7; method whi#e 'eci,(in! emai# $D #end an '*&4 E!ail: Cere i an e<am'#e to end an C523 emai# ,rom (o4r machine. Cere it i a 4med that (o4r l calh %t i connected to the internet and ca'ab#e eno4!h to end an emai#. Same time make 4re a## the *ar ,i#e ,rom 6ava 9mai# )P$ 'acka!e and 6)% 'acka!e ara avai#ab#e in +3)SSP)5C. 5hi e<am'#e i ver( imi#ar to 'revio4 one, e<ce't here we are 4 in! et+ontent7; method to et content who e econd ar!4ment i Pte<tGhtm#P to 'eci,( that the C523 content i inc#4ded in the me a!e. = in! thi e<am'#e, (o4 can end a bi! a C523 content (o4 #ike. VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.O,*ava<.mai#.OPLR VL^ 'a!e im'ortKP*ava<.mai#.internet.O,*ava<.activation.OPLR VL^ 'a!e im'ortKP*ava<. erv#et.htt'.O,*ava<. erv#et.OP LR VL Strin! re 4#tJ GG "eci'ient? emai# $D need to be mentioned. Strin! to K Pabcd^!mai#.comPJ GG Sender? emai# $D need to be mentioned Strin! ,rom K Pmcmohd^!mai#.comPJ GG ) 4min! (o4 are endin! emai# ,rom #oca#ho t +2+ 3imited i a S4b idiar( o, 5+S 3imited

2-D

Strin! ho t K P#oca#ho tPJ GG Get ( tem 'ro'ertie ob*ect Pro'ertie 'ro'ertie K S( tem.!etPro'ertie 7;J GG Set4' mai# erver 'ro'ertie . etPro'ert(7Pmai#. mt'.ho tP, ho t;J GG Get the de,a4#t Se ion ob*ect. Se ion mai#Se ion K Se ion.!etDe,a4#t$n tance7'ro'ertie ;J tr(N GG +reate a de,a4#t 2ime2e a!e ob*ect. 2ime2e a!e me a!e K new 2ime2e a!e7mai#Se GG Set %rom: header ,ie#d o, the header. me a!e. et%rom7new $nternet)ddre 7,rom;;J GG Set 5o: header ,ie#d o, the header. me a!e.add"eci'ient72e a!e."eci'ient5('e.5&, new $nternet)ddre 7to;;J GG Set S4b*ect: header ,ie#d me a!e. etS4b*ect7P5hi i the S4b*ect 3ineWP;J

ion;J

GG Send the act4a# C523 me a!e, a bi! a (o4 #ike me a!e. et+ontent7PVh1R5hi i act4a# me a!eVGh1RP, Pte<tGhtm#P ;J GG Send me a!e 5ran 'ort. end7me a!e;J re 4#t K PSent me a!e 4cce ,4##(....PJ Qcatch 72e a!in!9<ce'tion me<; N me<.'rintStack5race7;J re 4#t K P9rror: 4nab#e to end me a!e....PJ Q LR Vhtm#R VheadR Vtit#eRSend C523 9mai# 4 in! 6SPVGtit#eR VGheadR Vbod(R VcenterR Vh1RSend 9mai# 4 in! 6SPVGh1R VGcenterR V' a#i!nKPcenterPR VL o4t.'rint#n7P"e 4#t: P > re 4#t > PInP;J LR VG'R VGbod(R VGhtm#R Now tr( to 4 e above 6SP to end C523 me a!e on a !iven emai# $D. #end Attach!ent in E!ail: Cere i an e<am'#e to end an emai# with attachment ,rom (o4r machine: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.O,*ava<.mai#.OPLR VL^ 'a!e im'ortKP*ava<.mai#.internet.O,*ava<.activation.OPLR VL^ 'a!e im'ortKP*ava<. erv#et.htt'.O,*ava<. erv#et.OP LR VL Strin! re 4#tJ GG "eci'ient? emai# $D need to be mentioned. Strin! to K Pabcd^!mai#.comPJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 2-.

GG Sender? emai# $D need to be mentioned Strin! ,rom K Pmcmohd^!mai#.comPJ GG ) 4min! (o4 are endin! emai# ,rom #oca#ho t Strin! ho t K P#oca#ho tPJ GG Get ( tem 'ro'ertie ob*ect Pro'ertie 'ro'ertie K S( tem.!etPro'ertie 7;J GG Set4' mai# erver 'ro'ertie . etPro'ert(7Pmai#. mt'.ho tP, ho t;J GG Get the de,a4#t Se ion ob*ect. Se ion mai#Se ion K Se ion.!etDe,a4#t$n tance7'ro'ertie ;J tr(N GG +reate a de,a4#t 2ime2e a!e ob*ect. 2ime2e a!e me a!e K new 2ime2e a!e7mai#Se GG Set %rom: header ,ie#d o, the header. me a!e. et%rom7new $nternet)ddre 7,rom;;J GG Set 5o: header ,ie#d o, the header. me a!e.add"eci'ient72e a!e."eci'ient5('e.5&, new $nternet)ddre 7to;;J GG Set S4b*ect: header ,ie#d me a!e. etS4b*ect7P5hi i the S4b*ect 3ineWP;J GG +reate the me a!e 'art Eod(Part me a!eEod(Part K new 2imeEod(Part7;J GG %i## the me a!e me a!eEod(Part. et5e<t7P5hi i me a!e bod(P;J GG +reate a m4#ti'ar me a!e 24#ti'art m4#ti'art K new 2ime24#ti'art7;J GG Set te<t me a!e 'art m4#ti'art.addEod(Part7me a!eEod(Part;J GG Part two i attachment me a!eEod(Part K new 2imeEod(Part7;J Strin! ,i#ename K P,i#e.t<tPJ DataSo4rce o4rce K new %i#eDataSo4rce7,i#ename;J me a!eEod(Part. etDataCand#er7new DataCand#er7 o4rce;;J me a!eEod(Part. et%i#eName7,i#ename;J m4#ti'art.addEod(Part7me a!eEod(Part;J GG Send the com'#ete me a!e 'art me a!e. et+ontent7m4#ti'art ;J GG Send me a!e 5ran 'ort. end7me a!e;J Strin! tit#e K PSend 9mai#PJ re 4#t K PSent me a!e 4cce ,4##(....PJ Qcatch 72e a!in!9<ce'tion me<; N me<.'rintStack5race7;J re 4#t K P9rror: 4nab#e to end me a!e....PJ Q +2+ 3imited i a S4b idiar( o, 5+S 3imited

ion;J

2-/

LR Vhtm#R VheadR Vtit#eRSend )ttachement 9mai# 4 in! 6SPVGtit#eR VGheadR Vbod(R VcenterR Vh1RSend )ttachement 9mai# 4 in! 6SPVGh1R VGcenterR V' a#i!nKPcenterPR VL o4t.'rint#n7P"e 4#t: P > re 4#t > PInP;J LR VG'R VGbod(R VGhtm#R Now tr( to r4n above 6SP to end a ,i#e a an attachment a#on! with a me a!e on a !iven emai# $D. 5%er Authenticati n Part: $, it i re@4ired to 'rovide 4 er $D and Pa word to the emai# erver ,or a4thentication '4r'o e then (o4 can et the e 'ro'ertie a ,o##ow : 'ro' . etPro'ert(7Pmai#.4 erP, Pm(4 erP;J 'ro' . etPro'ert(7Pmai#.'a wordP, Pm('wdP;J "e t o, the emai# endin! mechani m wo4#d remain a e<'#ained above. 5%in( F r!% t %end e!ail: Fo4 can 4 e C523 ,orm to acce't emai# 'arameter and then (o4 can 4 e re@ue%t ob*ect to !et a## the in,ormation a ,o##ow : Strin! to K re@4e t.!etParameter7PtoP;J Strin! ,rom K re@4e t.!etParameter7P,romP;J Strin! 4b*ect K re@4e t.!etParameter7P 4b*ectP;J Strin! me a!e5e<t K re@4e t.!etParameter7Pbod(P;J &nce (o4 have a## the in,ormation, (o4 can 4 e above mentioned 'ro!ram to end emai#. J#P $ #tandard *a( 4ibrary 8J#*4; *ut rial 5he 6avaServer Pa!e Standard 5a! 3ibrar( 76S53; i a co##ection o, 4 e,4# 6SP ta! which enca' 4#ate core ,4nctiona#it( common to man( 6SP a''#ication . 6S53 ha 4''ort ,or common, tr4ct4ra# ta k 4ch a iteration and conditiona# , ta! ,or mani'4#atin! A23 doc4ment , internationa#iBation ta! , and S]3 ta! . $t a# o 'rovide a ,ramework ,or inte!ratin! e<i tin! c4 tom ta! with 6S53 ta! . 5he 6S53 ta! can be c#a i,ied, accordin! to their ,4nction , into ,o##owin! 6S53 ta! #ibrar( !ro4' that can be 4 ed when creatin! a 6SP 'a!e: 7 re *a(% F r!attin( ta(% #N4 ta(% 0&4 ta(% J#*4 Functi n% In%tall J#*4 4ibrary: $, (o4 are 4 in! )'ache 5omcat container then ,o##ow the ,o##owin! two im'#e te' : Down#oad the binar( di trib4tion ,rom )'ache Standard 5a!#ib and 4n'ack the com're ed ,i#e. 5o 4 e the Standard 5a!#ib ,rom it 6akarta 5a!#ib di trib4tion, im'#( co'( the 6)" ,i#e in the di trib4tion? ?#ib? director( to (o4r a''#ication? weba'' I"&&5IW9E-$N%I#ib director(. 5o 4 e an( o, the #ibrarie , (o4 m4 t inc#4de a Vta!#ibR directive at the to' o, each 6SP that 4 e the #ibrar(. 7 re *a(%: 5he core !ro4' o, ta! are the mo t ,re@4ent#( 4 ed 6S53 ta! . %o##owin! i the (nta< to inc#4de 6S53 +ore #ibrar( in (o4r 6SP: VL^ ta!#ib 're,i<KPcP 4riKPhtt':GG*ava. 4n.comG* 'G* t#GcoreP LR 5here are ,o##owin! +ore 6S53 5a! : *a( De%cri"ti n +2+ 3imited i a S4b idiar( o, 5+S 3imited 2-0

Vc:o4t R Vc: et R Vc:remove R Vc:catchR Vc:i,R Vc:choo eR Vc:whenR Vc:otherwi e R Vc:im'ortR Vc:,or9ach R

Vc:,or5oken R Vc:'aramR Vc:redirect R Vc:4r#R F r!attin( ta(%: 5he 6S53 ,ormattin! ta! are 4 ed to ,ormat and di '#a( te<t, the date, the time, and n4mber ,or internationa#iBed Web ite . %o##owin! i the (nta< to inc#4de %ormattin! #ibrar( in (o4r 6SP: VL^ ta!#ib 're,i<KP,mtP 4riKPhtt':GG*ava. 4n.comG* 'G* t#G,mtP LR %o##owin! i the #i t o, %ormattin! 6S53 5a! : *a( De%cri"ti n V,mt:,ormatN4mberR 5o render n4merica# va#4e with 'eci,ic 'reci ion or ,ormat. Par e the trin! re're entation o, a n4mber, c4rrenc(, or V,mt:'ar eN4mberR 'ercenta!e. V,mt:,ormatDateR %ormat a date andGor time 4 in! the 4''#ied t(#e and 'attern V,mt:'ar eDateR Par e the trin! re're entation o, a date andGor time V,mt:b4nd#eR 3oad a re o4rce b4nd#e to be 4 ed b( it ta! bod(. V,mt: et3oca#eR Store the !iven #oca#e in the #oca#e con,i!4ration variab#e. 3oad a re o4rce b4nd#e and tore it in the named co'ed V,mt: etE4nd#eR variab#e or the b4nd#e con,i!4ration variab#e. S'eci,ie the time Bone ,or an( time ,ormattin! or 'ar in! action V,mt:timeSoneR ne ted in it bod(. Store the !iven time Bone in the time Bone con,i!4ration V,mt: et5imeSoneR variab#e V,mt:me a!eR 5o di '#a( an internationa#iBed me a!e. V,mt:re@4e t9ncodin!R Set the re@4e t character encodin! #N4 ta(%: 5he 6S53 S]3 ta! #ibrar( 'rovide ta! ,or interactin! with re#ationa# databa e 7"DE2S ; 4ch a &rac#e, m(S]3, or 2icro o,t S]3 Server. %o##owin! i the (nta< to inc#4de 6S53 S]3 #ibrar( in (o4r 6SP: VL^ ta!#ib 're,i<KP @#P 4riKPhtt':GG*ava. 4n.comG* 'G* t#G @#P LR %o##owin! i the #i t o, S]3 6S53 5a! : *a( De%cri"ti n +2+ 3imited i a S4b idiar( o, 5+S 3imited 2-8

3ike VLK ... R, b4t ,or e<'re ion . Set the re 4#t o, an e<'re ion eva#4ation in a ? co'e? "emove a co'ed variab#e 7,rom a 'artic4#ar co'e, i, 'eci,ied;. +atche an( 5hrowab#e that occ4r in it bod( and o'tiona##( e<'o e it. Sim'#e conditiona# ta! which eva#4te it bod( i, the 4''#ied condition i tr4e. Sim'#e conditiona# ta! that e tab#i he a conte<t ,or m4t4a##( e<c#4 ive conditiona# o'eration , marked b( VwhenR and Votherwi eR S4bta! o, Vchoo eR that inc#4de it bod( i, it condition eva#4te to ?tr4e?. S4bta! o, Vchoo eR that ,o##ow VwhenR ta! and r4n on#( i, a## o, the 'rior condition eva#4ated to ?,a# e?. "etrieve an ab o#4te or re#ative ="3 and e<'o e it content to either the 'a!e, a Strin! in ?var?, or a "eader in ?var"eader?. 5he ba ic iteration ta!, acce'tin! man( di,,erent co##ection t('e and 4''ortin! 4b ettin! and other ,4nctiona#it( . $terate over token , e'arated b( the 4''#ied de#imeter . )dd a 'arameter to a containin! ?im'ort? ta!? ="3. "edirect to a new ="3. +reate a ="3 with o'tiona# @4er( 'arameter

V @#: etDataSo4rceR V @#:@4er(R V @#:4'dateR V @#:'aramR V @#:dateParamR V @#:tran action R

+reate a im'#e DataSo4rce 4itab#e on#( ,or 'rotot('in! 9<ec4te the S]3 @4er( de,ined in it bod( or thro4!h the @# attrib4te. 9<ec4te the S]3 4'date de,ined in it bod( or thro4!h the @# attrib4te. Set a 'arameter in an S]3 tatement to the 'eci,ied va#4e. Set a 'arameter in an S]3 tatement to the 'eci,ied *ava.4ti#.Date va#4e. Provide ne ted databa e action e#ement with a hared +onnection, et 4' to e<ec4te a## tatement a one tran action.

0&4 ta(%: 5he 6S53 A23 ta! 'rovide a 6SP-centric wa( o, creatin! and mani'4#atin! A23 doc4ment . %o##owin! i the (nta< to inc#4de 6S53 A23 #ibrar( in (o4r 6SP. 5he 6S53 A23 ta! #ibrar( ha c4 tom ta! ,or interactin! with A23 data. 5hi inc#4de 'ar in! A23, tran ,ormin! A23 data, and ,#ow contro# ba ed on APath e<'re ion . VL^ ta!#ib 're,i<KP<P 4riKPhtt':GG*ava. 4n.comG* 'G* t#G<m#P LR Ee,ore (o4 'roceed with the e<am'#e , (o4 wo4#d need to co'( ,o##owin! two A23 and APath re#ated #ibrarie into (o4r V5omcat $n ta##ation Director(RI#ib: 0erce%I!"l+jar: ,alan+jar: %o##owin! i the #i t o, A23 6S53 5a! : *a( De%cri"ti n V<:o4tR 3ike VLK ... R, b4t ,or APath e<'re ion . = e to 'ar e A23 data 'eci,ied either via an attrib4te or in the V<:'ar eR ta! bod(. V<: et R Set a variab#e to the va#4e o, an APath e<'re ion. 9va#4ate a te t APath e<'re ion and i, it i tr4e, it 'roce e V<:i, R it bod(. $, the te t condition i ,a# e, the bod( i i!nored. V<:,or9achR 5o #oo' over node in an A23 doc4ment. Sim'#e conditiona# ta! that e tab#i he a conte<t ,or m4t4a##( V<:choo eR e<c#4 ive conditiona# o'eration , marked b( VwhenR and Votherwi eR S4bta! o, Vchoo eR that inc#4de it bod( i, it e<'re ion V<:when R eva#4te to ?tr4e? S4bta! o, Vchoo eR that ,o##ow VwhenR ta! and r4n on#( i, a## V<:otherwi e R o, the 'rior condition eva#4ated to ?,a# e? V<:tran ,orm R )''#ie an AS3 tran ,ormation on a A23 doc4ment = e a#on! with the tran ,orm ta! to et a 'arameter in the AS35 V<:'aram R t(#e heet J#*4 Functi n%: 6S53 inc#4de a n4mber o, tandard ,4nction , mo t o, which are common trin! mani'4#ation ,4nction . %o##owin! i the (nta< to inc#4de 6S53 %4nction #ibrar( in (o4r 6SP: VL^ ta!#ib 're,i<KP,nP 4riKPhtt':GG*ava. 4n.comG* 'G* t#G,4nction P LR %o##owin! i the #i t o, 6S53 %4nction : Functi n De%cri"ti n ,n:contain 7; 5e t i, an in'4t trin! contain the 'eci,ied 4b trin!. 5e t i, an in'4t trin! contain the 'eci,ied 4b trin! in a ,n:contain $!nore+a e7; ca e in en itive wa(. ,n:end With7; 5e t i, an in'4t trin! end with the 'eci,ied 4,,i<. ,n:e ca'eAm#7; 9 ca'e character that co4#d be inter'reted a A23 mark4'. "et4rn the inde< within! a trin! o, the ,ir t occ4rrence o, a ,n:inde<&,7; 'eci,ied 4b trin!. +2+ 3imited i a S4b idiar( o, 5+S 3imited 2--

,n:*oin7; ,n:#en!th7; ,n:re'#ace7;

,n: '#it7; ,n: tart With7; ,n: 4b trin!7; ,n: 4b trin!),ter7; ,n: 4b trin!Ee,ore7; ,n:to3ower+a e7; ,n:to=''er+a e7; ,n:trim7; J#P $ Databa%e Acce%% 5hi t4toria# a 4me (o4 have !ood 4nder tandin! on how 6DE+ a''#ication work . Ee,ore tartin! with databa e acce thro4!h a 6SP, make 4re (o4 have 'ro'er 6DE+ environment et4' a#on! with a databa e. %or more detai# on how to acce databa e 4 in! 6DE+ and it environment et4' (o4 can !o thro4!h o4r 6DE+ 54toria#. 5o tart with ba ic conce't, #et 4 create a im'#e tab#e and create ,ew record in that tab#e a ,o##ow : 7reate *able 5o create the E!"l yee% tab#e in 92P databa e, 4 e the ,o##owin! te' : #te" B: &'en a 7 !!and Pr !"t and chan!e to the in ta##ation director( a ,o##ow : +:IR +:IRcd Pro!ram %i#e I2(S]3Ibin +:IPro!ram %i#e I2(S]3IbinR #te" -: 3o!in to databa e a ,o##ow +:IPro!ram %i#e I2(S]3IbinRm( @# -4 root -' 9nter 'a word: OOOOOOOO m( @#R #te" J: +reate the tab#e E!"l yee in *E#* databa e a ,o##ow : m( @#R 4 e 59S5J m( @#R create tab#e 9m'#o(ee 7 id int not n4##, a!e int not n4##, ,ir t varchar 72..;, #a t varchar 72..; ;J ]4er( &H, 0 row a,,ected 70.08 ec; m( @#R 7reate Data Rec rd% %ina##( (o4 create ,ew record in 9m'#o(ee tab#e a ,o##ow : m( @#R $NS9"5 $N5& 9m'#o(ee V)3=9S 7100, 18, ?Sara?, ?)#i?;J ]4er( &H, 1 row a,,ected 70.0. ec; m( @#R $NS9"5 $N5& 9m'#o(ee V)3=9S 7101, 2., ?2ahnaB?, ?%atma?;J ]4er( &H, 1 row a,,ected 70.00 ec; m( @#R $NS9"5 $N5& 9m'#o(ee V)3=9S 7102, 10, ?Said?, ?Hhan?;J ]4er( &H, 1 row a,,ected 70.00 ec; m( @#R $NS9"5 $N5& 9m'#o(ee V)3=9S 7101, 28, ?S4mit?, ?2itta#?;J ]4er( &H, 1 row a,,ected 70.00 ec; +2+ 3imited i a S4b idiar( o, 5+S 3imited 100

6oin a## e#ement o, an arra( into a trin!. "et4rn the n4mber o, item in a co##ection, or the n4mber o, character in a trin!. "et4rn a trin! re 4#tin! ,rom re'#acin! in an in'4t trin! a## occ4rrence with a !iven trin!. S'#it a trin! into an arra( o, 4b trin! . 5e t i, an in'4t trin! tart with the 'eci,ied 're,i<. "et4rn a 4b et o, a trin!. "et4rn a 4b et o, a trin! ,o##owin! a 'eci,ic 4b trin!. "et4rn a 4b et o, a trin! be,ore a 'eci,ic 4b trin!. +onvert a## o, the character o, a trin! to #ower ca e. +onvert a## o, the character o, a trin! to 4''er ca e. "emove white 'ace ,rom both end o, a trin!.

m( @#R #E4E7* O"erati n: %o##owin! e<am'#e how how we can e<ec4te S]3 S939+5 tatement 4 in! 65S3 in 6SP 'ro!rammin!: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.O,*ava. @#.OPLR VL^ 'a!e im'ortKP*ava<. erv#et.htt'.O,*ava<. erv#et.OP LR VL^ ta!#ib 4riKPhtt':GG*ava. 4n.comG* 'G* t#GcoreP 're,i<KPcPLR VL^ ta!#ib 4riKPhtt':GG*ava. 4n.comG* 'G* t#G @#P 're,i<KP @#PLR Vhtm#R VheadR Vtit#eRS939+5 &'erationVGtit#eR VGheadR Vbod(R V @#: etDataSo4rce varKP na' hotP driverKPcom.m( @#.*dbc.DriverP 4r#KP*dbc:m( @#:GG#oca#ho tG59S5P 4 erKProotP 'a wordKP'a 121PGR V @#:@4er( dataSo4rceKPMN na' hotQP varKPre 4#tPR S939+5 O ,rom 9m'#o(ee J VG @#:@4er(R Vtab#e borderKP1P widthKP100LPR VtrR VthR9m' $DVGthR VthR%ir t NameVGthR VthR3a t NameVGthR VthR)!eVGthR VGtrR Vc:,or9ach varKProwP item KPMNre 4#t.row QPR VtrR VtdRVc:o4t va#4eKPMNrow.idQPGRVGtdR VtdRVc:o4t va#4eKPMNrow.,ir tQPGRVGtdR VtdRVc:o4t va#4eKPMNrow.#a tQPGRVGtdR VtdRVc:o4t va#4eKPMNrow.a!eQPGRVGtdR VGtrR VGc:,or9achR VGtab#eR VGbod(R VGhtm#R Now tr( to acce E!" ID 100 101 102 101 Fir%t Na!e Sara 2ahnaB Said S4mit

above 6SP, which ho4#d di '#a( the ,o##owin! re 4#t: 4a%t Na!e )#i %atma Hhan 2itta# A(e 18 2. 10 28

IN#ER* O"erati n: %o##owin! e<am'#e how how we can e<ec4te S]3 $NS9"5 tatement 4 in! 65S3 in 6SP 'ro!rammin!: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.O,*ava. @#.OPLR VL^ 'a!e im'ortKP*ava<. erv#et.htt'.O,*ava<. erv#et.OP LR VL^ ta!#ib 4riKPhtt':GG*ava. 4n.comG* 'G* t#GcoreP 're,i<KPcPLR VL^ ta!#ib 4riKPhtt':GG*ava. 4n.comG* 'G* t#G @#P 're,i<KP @#PLR +2+ 3imited i a S4b idiar( o, 5+S 3imited

101

Vhtm#R VheadR Vtit#eR6$NS9"5 &'erationVGtit#eR VGheadR Vbod(R V @#: etDataSo4rce varKP na' hotP driverKPcom.m( @#.*dbc.DriverP 4r#KP*dbc:m( @#:GG#oca#ho tG59S5P 4 erKProotP 'a wordKP'a 121PGR V @#:4'date dataSo4rceKPMN na' hotQP varKPre 4#tPR $NS9"5 $N5& 9m'#o(ee V)3=9S 710D, 2, ?N4ha?, ?)#i?;J VG @#:4'dateR V @#:@4er( dataSo4rceKPMN na' hotQP varKPre 4#tPR S939+5 O ,rom 9m'#o(ee J VG @#:@4er(R Vtab#e borderKP1P widthKP100LPR VtrR VthR9m' $DVGthR VthR%ir t NameVGthR VthR3a t NameVGthR VthR)!eVGthR VGtrR Vc:,or9ach varKProwP item KPMNre 4#t.row QPR VtrR VtdRVc:o4t va#4eKPMNrow.idQPGRVGtdR VtdRVc:o4t va#4eKPMNrow.,ir tQPGRVGtdR VtdRVc:o4t va#4eKPMNrow.#a tQPGRVGtdR VtdRVc:o4t va#4eKPMNrow.a!eQPGRVGtdR VGtrR VGc:,or9achR VGtab#eR VGbod(R VGhtm#R Now tr( to acce E!" ID 100 101 102 101 10D Fir%t Na!e Sara 2ahnaB Said S4mit N4ha

above 6SP, which ho4#d di '#a( the ,o##owin! re 4#t: 4a%t Na!e )#i %atma Hhan 2itta# )#i A(e 18 2. 10 28 2

DE4E*E O"erati n: %o##owin! e<am'#e how how we can e<ec4te S]3 D93959 tatement 4 in! 65S3 in 6SP 'ro!rammin!: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.O,*ava. @#.OPLR VL^ 'a!e im'ortKP*ava<. erv#et.htt'.O,*ava<. erv#et.OP LR VL^ ta!#ib 4riKPhtt':GG*ava. 4n.comG* 'G* t#GcoreP 're,i<KPcPLR VL^ ta!#ib 4riKPhtt':GG*ava. 4n.comG* 'G* t#G @#P 're,i<KP @#PLR Vhtm#R +2+ 3imited i a S4b idiar( o, 5+S 3imited 102

VheadR Vtit#eRD93959 &'erationVGtit#eR VGheadR Vbod(R V @#: etDataSo4rce varKP na' hotP driverKPcom.m( @#.*dbc.DriverP 4r#KP*dbc:m( @#:GG#oca#ho tG59S5P 4 erKProotP 'a wordKP'a 121PGR Vc: et varKPem'$dP va#4eKP101PGR V @#:4'date dataSo4rceKPMN na' hotQP varKPco4ntPR D93959 %"&2 9m'#o(ee WC9"9 $d K [ V @#:'aram va#4eKPMNem'$dQP GR VG @#:4'dateR V @#:@4er( dataSo4rceKPMN na' hotQP varKPre 4#tPR S939+5 O ,rom 9m'#o(ee J VG @#:@4er(R Vtab#e borderKP1P widthKP100LPR VtrR VthR9m' $DVGthR VthR%ir t NameVGthR VthR3a t NameVGthR VthR)!eVGthR VGtrR Vc:,or9ach varKProwP item KPMNre 4#t.row QPR VtrR VtdRVc:o4t va#4eKPMNrow.idQPGRVGtdR VtdRVc:o4t va#4eKPMNrow.,ir tQPGRVGtdR VtdRVc:o4t va#4eKPMNrow.#a tQPGRVGtdR VtdRVc:o4t va#4eKPMNrow.a!eQPGRVGtdR VGtrR VGc:,or9achR VGtab#eR VGbod(R VGhtm#R Now tr( to acce E!" ID 100 101 102 Fir%t Na!e Sara 2ahnaB Said

above 6SP, which ho4#d di '#a( the ,o##owin! re 4#t: 4a%t Na!e )#i %atma Hhan A(e 18 2. 10

5PDA*E O"erati n: %o##owin! e<am'#e how how we can e<ec4te S]3 =PD)59 tatement 4 in! 65S3 in 6SP 'ro!rammin!: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.O,*ava. @#.OPLR VL^ 'a!e im'ortKP*ava<. erv#et.htt'.O,*ava<. erv#et.OP LR VL^ ta!#ib 4riKPhtt':GG*ava. 4n.comG* 'G* t#GcoreP 're,i<KPcPLR VL^ ta!#ib 4riKPhtt':GG*ava. 4n.comG* 'G* t#G @#P 're,i<KP @#PLR Vhtm#R VheadR Vtit#eRD93959 &'erationVGtit#eR VGheadR +2+ 3imited i a S4b idiar( o, 5+S 3imited 101

Vbod(R V @#: etDataSo4rce varKP na' hotP driverKPcom.m( @#.*dbc.DriverP 4r#KP*dbc:m( @#:GG#oca#ho tG59S5P 4 erKProotP 'a wordKP'a 121PGR Vc: et varKPem'$dP va#4eKP102PGR V @#:4'date dataSo4rceKPMN na' hotQP varKPco4ntPR =PD)59 9m'#o(ee S95 #a t K ?)#i? V @#:'aram va#4eKPMNem'$dQP GR VG @#:4'dateR V @#:@4er( dataSo4rceKPMN na' hotQP varKPre 4#tPR S939+5 O ,rom 9m'#o(ee J VG @#:@4er(R Vtab#e borderKP1P widthKP100LPR VtrR VthR9m' $DVGthR VthR%ir t NameVGthR VthR3a t NameVGthR VthR)!eVGthR VGtrR Vc:,or9ach varKProwP item KPMNre 4#t.row QPR VtrR VtdRVc:o4t va#4eKPMNrow.idQPGRVGtdR VtdRVc:o4t va#4eKPMNrow.,ir tQPGRVGtdR VtdRVc:o4t va#4eKPMNrow.#a tQPGRVGtdR VtdRVc:o4t va#4eKPMNrow.a!eQPGRVGtdR VGtrR VGc:,or9achR VGtab#eR VGbod(R VGhtm#R Now tr( to acce E!" ID 100 101 102 Fir%t Na!e Sara 2ahnaB Said

above 6SP, which ho4#d di '#a( the ,o##owin! re 4#t: 4a%t Na!e )#i %atma )#i A(e 18 2. 10

J#P $ 0&4 Data When (o4 end A23 data via C55P, it make en e to 4 e 6SP to hand#e incomin! and o4t!oin! A23 doc4ment ,or e<am'#e "SS doc4ment . ) an A23 doc4ment i mere#( a b4nch o, te<t, creatin! one thro4!h a 6SP i no more di,,ic4#t than creatin! an C523 doc4ment. #endin( 0&4 fr ! a J#P: Fo4 can end A23 content 4 in! 6SP the ame wa( (o4 end C523. 5he on#( di,,erence i that (o4 m4 t et the content t('e o, (o4r 'a!e to te<tG<m#. 5o et the content t('e, 4 e the VL^'a!eLR ta!, #ike thi : VL^ 'a!e content5('eKPte<tG<m#P LR %o##owin! i a im'#e e<am'#e to end A23 content to the brow er: VL^ 'a!e content5('eKPte<tG<m#P LR Vbook R VbookR VnameRPadam Ci tor(VGnameR +2+ 3imited i a S4b idiar( o, 5+S 3imited 10D

Va4thorRS)")VGa4thorR V'riceR100VG'riceR VGbookR VGbook R 5r( to acce above A23 4 in! di,,erent brow er to ee the doc4ment tree 're entation o, the above A23. Pr ce%%in( 0&4 in J#P: Ee,ore (o4 'roceed with A23 'roce in! 4 in! 6SP, (o4 wo4#d need to co'( ,o##owin! two A23 and APath re#ated #ibrarie into (o4r V5omcat $n ta##ation Director(RI#ib: 0erce%I!"l+jar: ,alan+jar: 3et 4 '4t ,o##owin! content in book .<m# ,i#e: Vbook R VbookR VnameRPadam Ci tor(VGnameR Va4thorRS)")VGa4thorR V'riceR100VG'riceR VGbookR VbookR VnameRGreat 2i tr(VGnameR Va4thorRN=C)VGa4thorR V'riceR2000VG'riceR VGbookR VGbook R Now tr( the ,o##owin! main.* ', kee'in! in the ame director(: VL^ ta!#ib 're,i<KPcP 4riKPhtt':GG*ava. 4n.comG* 'G* t#GcoreP LR VL^ ta!#ib 're,i<KP<P 4riKPhtt':GG*ava. 4n.comG* 'G* t#G<m#P LR Vhtm#R VheadR Vtit#eR6S53 <:'ar e 5a! VGtit#eR VGheadR Vbod(R Vh1REook $n,o:VGh1R Vc:im'ort varKPbook$n,oP 4r#KPhtt':GG#oca#ho t:8080Gbook .<m#PGR V<:'ar e <m#KPMNbook$n,oQP varKPo4t'4tPGR VbR5he tit#e o, the ,ir t book i VGbR: V<:o4t e#ectKPMo4t'4tGbook Gbook81:GnameP GR VbrR VbR5he 'rice o, the econd bookVGbR: V<:o4t e#ectKPMo4t'4tGbook Gbook82:G'riceP GR VGbod(R VGhtm#R Now tr( to acce above 6SP 4 in! htt':GG#oca#ho t:8080Gmain.* ', thi wo4#d 'rod4ce ,o##owin! re 4#t: =% Inf : *he title f the fir%t b = i%:Padam Ci tor( *he "rice f the %ec nd b =: 2000 F r!attin( 0&4 with J#P: +on ider the ,o##owin! AS35 t(#e heet t(#e.< #: V[<m# ver ionKP1.0P[R V< #: t(#e heet <m#n :< #K Phtt':GGwww.w1.or!G1---GAS3G5ran ,ormP ver ionKP1.0PR V< #:o4t'4t methodKPhtm#P indentKP(e PGR V< #:tem'#ate matchKPGPR +2+ 3imited i a S4b idiar( o, 5+S 3imited 10.

Vhtm#R Vbod(R V< #:a''#(-tem'#ate GR VGbod(R VGhtm#R VG< #:tem'#ateR V< #:tem'#ate matchKPbook PR Vtab#e borderKP1P widthKP100LPR V< #:,or-each e#ectKPbookPR VtrR VtdR ViRV< #:va#4e-o, e#ectKPnamePGRVGiR VGtdR VtdR V< #:va#4e-o, e#ectKPa4thorPGR VGtdR VtdR V< #:va#4e-o, e#ectKP'ricePGR VGtdR VGtrR VG< #:,or-eachR VGtab#eR VG< #:tem'#ateR VG< #: t(#e heetR Now con ider the ,o##owin! 6SP ,i#e: VL^ ta!#ib 're,i<KPcP 4riKPhtt':GG*ava. 4n.comG* 'G* t#GcoreP LR VL^ ta!#ib 're,i<KP<P 4riKPhtt':GG*ava. 4n.comG* 'G* t#G<m#P LR Vhtm#R VheadR Vtit#eR6S53 <:tran ,orm 5a! VGtit#eR VGheadR Vbod(R Vh1REook $n,o:VGh1R Vc: et varKP<m#te<tPR Vbook R VbookR VnameRPadam Ci tor(VGnameR Va4thorRS)")VGa4thorR V'riceR100VG'riceR VGbookR VbookR VnameRGreat 2i tr(VGnameR Va4thorRN=C)VGa4thorR V'riceR2000VG'riceR VGbookR VGbook R VGc: etR Vc:im'ort 4r#KPhtt':GG#oca#ho t:8080G t(#e.< #P varKP< #tPGR V<:tran ,orm <m#KPMN<m#te<tQP < #tKPMN< #tQPGR VGbod(R VGhtm#R 5hi wo4#d 'rod4ce ,o##owin! re 4#t:

+2+ 3imited i a S4b idiar( o, 5+S 3imited

10/

=% Inf : Padam History Hreat Mistry S)") 100 N=C 2000 )

%or more detai# on A23 'roce in! 4 in! 6S53, (o4 can check 6SP Standard 5a! 3ibrar(. J#P $ Ja)a6ean% ) 6avaEean i a 'ecia##( con tr4cted 6ava c#a written in the 6ava and coded accordin! to the 6avaEean )P$ 'eci,ication . %o##owin! are the 4ni@4e characteri tic that di tin!4i h a 6avaEean ,rom other 6ava c#a e : $t 'rovide a de,a4#t, no-ar!4ment con tr4ctor. $t ho4#d be eria#iBab#e and im'#ement the #eriali>able inter,ace. $t ma( have a n4mber o, 'ro'ertie which can be read or written. $t ma( have a n4mber o, P!etterP and P etterP method ,or the 'ro'ertie . Ja)a6ean% Pr "ertie%: ) 6avaEean 'ro'ert( i a named attrib4te that can be acce ed b( the 4 er o, the ob*ect. 5he attrib4te can be o, an( 6ava data t('e, inc#4din! c#a e that (o4 de,ine. ) 6avaEean 'ro'ert( ma( be read, write, read on#(, or write on#(. 6avaEean 'ro'ertie are acce ed thro4!h two method in the 6avaEean? im'#ementation c#a : &eth d De%cri"ti n %or e<am'#e, i, 'ro'ert( name i )irstName, (o4r method name !etPr "ertyNa!e7; wo4#d be !et%ir tName7; to read that 'ro'ert(. 5hi method i ca##ed acce or. %or e<am'#e, i, 'ro'ert( name i )irstName, (o4r method name etPr "ertyNa!e7; wo4#d be et%ir tName7; to write that 'ro'ert(. 5hi method i ca##ed m4tator. ) read-on#( attrib4te wi## have on#( a !etPr "ertyNa!e7; method, and a write-on#( attrib4te wi## have on#( a etPr "ertyNa!e7; method. Ja)a6ean% E,a!"le: +on ider a t4dent c#a with ,ew 'ro'ertie : 'acka!e com.t4toria# 'ointJ '4b#ic c#a St4dent Eean im'#ement *ava.io.Seria#iBab#e N 'rivate Strin! ,ir tName K n4##J 'rivate Strin! #a tName K n4##J 'rivate int a!e K 0J '4b#ic St4dent Eean7; N Q '4b#ic Strin! !et%ir tName7;N ret4rn ,ir tNameJ Q '4b#ic Strin! !et3a tName7;N ret4rn #a tNameJ Q '4b#ic int !et)!e7;N ret4rn a!eJ Q '4b#ic void et%ir tName7Strin! ,ir tName;N thi .,ir tName K ,ir tNameJ Q '4b#ic void et3a tName7Strin! #a tName;N thi .#a tName K #a tNameJ Q '4b#ic void et)!e7$nte!er a!e;N +2+ 3imited i a S4b idiar( o, 5+S 3imited 100

thi .a!e K a!eJ Q Q Acce%%in( Ja)a6ean%: 5he u%e6ean action dec#are a 6avaEean ,or 4 e in a 6SP. &nce dec#ared, the bean become a cri'tin! variab#e that can be acce ed b( both cri'tin! e#ement and other c4 tom ta! 4 ed in the 6SP. 5he ,4## (nta< ,or the 4 eEean ta! i a ,o##ow : V* ':4 eEean t('eS'ecGR Cere va#4e ,or the co'e attrib4te co4#d be 'a!e, re@4e t, e ion or a''#ication ba ed on (o4r re@4irement. 5he va#4e o, the id attrib4te ma( be an( va#4e a a #on! a it i a 4ni@4e name amon! other 4 eEean dec#aration in the ame 6SP. %o##owin! e<am'#e how it im'#e 4 a!e: Vhtm#R VheadR Vtit#eR4 eEean 9<am'#eVGtit#eR VGheadR Vbod(R V* ':4 eEean GR V'R5he dateGtime i VLK date LR VGbod(R VGhtm#R 5hi wo4#d 'rod4ce ,o##owin! re 4#t: 5he dateGtime i 5h4 Se' 10 11:18:11 GS5 2010 Acce%%in( Ja)a6ean% Pr "ertie%: )#on! with V* ':4 eEean...R, (o4 can 4 e V* ':!etPro'ert(GR action to acce !et method and V* ': etPro'ert(GR action to acce et method . Cere i the ,4## (nta<: V* ':4 eEean R V* ': etPro'ert( nameKPbean? idP 'ro'ert(KP'ro'ert( nameP va#4eKPva#4ePGR V* ':!etPro'ert( nameKPbean? idP 'ro'ert(KP'ro'ert( namePGR ........... VG* ':4 eEeanR 5he name attrib4te re,erence the id o, a 6avaEean 'revio4 #( introd4ced to the 6SP b( the 4 eEean action. 5he 'ro'ert( attrib4te i the name o, the !et or et method that ho4#d be invoked. %o##owin! i a im'#e e<am'#e to acce the data 4 in! above (nta<: Vhtm#R VheadR Vtit#eR!et and et 'ro'ertie 9<am'#eVGtit#eR VGheadR Vbod(R V* ':4 eEean R V* ': etPro'ert( nameKP t4dent P 'ro'ert(KP,ir tNameP va#4eKPSaraPGR V* ': etPro'ert( nameKP t4dent P 'ro'ert(KP#a tNameP va#4eKP)#iPGR V* ': etPro'ert( nameKP t4dent P 'ro'ert(KPa!eP va#4eKP10PGR VG* ':4 eEeanR V'RSt4dent %ir t Name: V* ':!etPro'ert( nameKP t4dent P 'ro'ert(KP,ir tNamePGR VG'R V'RSt4dent 3a t Name: V* ':!etPro'ert( nameKP t4dent P 'ro'ert(KP#a tNamePGR VG'R +2+ 3imited i a S4b idiar( o, 5+S 3imited

108

V'RSt4dent )!e: V* ':!etPro'ert( nameKP t4dent P 'ro'ert(KPa!ePGR VG'R VGbod(R VGhtm#R 3et 4 make St4dent Eean.c#a 'rod4ce ,o##owin! re 4#t: St4dent %ir t Name: Sara St4dent 3a t Name: )#i St4dent )!e: 10 J#P $ 7u%t ! *a(% ) c4 tom ta! i a 4 er-de,ined 6SP #an!4a!e e#ement. When a 6SP 'a!e containin! a c4 tom ta! i tran #ated into a erv#et, the ta! i converted to o'eration on an ob*ect ca##ed a ta! hand#er. 5he Web container then invoke tho e o'eration when the 6SP 'a!e? erv#et i e<ec4ted. 6SP ta! e<ten ion #et (o4 create new ta! that (o4 can in ert direct#( into a 6avaServer Pa!e *4 t a (o4 wo4#d the b4i#t-in ta! (o4 #earned abo4t in ear#ier cha'ter. 5he 6SP 2.0 'eci,ication introd4ced Sim'#e 5a! Cand#er ,or writin! the e c4 tom ta! . 5o write a c4 tomer tab (o4 can im'#( e<tend Sim'#e5a!S4''ort c#a and override the d *a(8; method, where (o4 can '#ace (o4r code to !enerate content ,or the ta!. 7reate H'ell H *a(: +on ider (o4 want to de,ine a c4 tom ta! named Ve<:Ce##oR and (o4 want to 4 e it in the ,o##owin! ,a hion witho4t a bod(: Ve<:Ce##o GR 5o create a c4 tom 6SP ta!, (o4 m4 t ,ir t create a 6ava c#a that act a a ta! hand#er. So #et 4 create Ce##o5a! c#a a ,o##ow : 'acka!e com.t4toria# 'ointJ im'ort *ava<. erv#et.* '.ta!e<t.OJ im'ort *ava<. erv#et.* '.OJ im'ort *ava.io.OJ '4b#ic c#a Ce##o5a! e<tend Sim'#e5a!S4''ort N

avai#ab#e in +3)SSP)5C and tr( to acce

above 6SP. 5hi wo4#d

'4b#ic void do5a!7; throw 6 '9<ce'tion, $&9<ce'tion N 6 'Writer o4t K !et6 '+onte<t7;.!et&4t7;J o4t.'rint#n7PCe##o +4 tom 5a!WP;J Q Q )bove code ha im'#e codin! where do5a!7; method take the c4rrent 6 '+onte<t ob*ect 4 in! !et6 '+onte<t7; method and 4 e it to end PCe##o +4 tom 5a!WP to the c4rrent 6 'Writer ob*ect. 3et 4 com'i#e above c#a and co'( it in a director( avai#ab#e in environment variab#e +3)SSP)5C. %ina##( create ,o##owin! ta! #ibrar( ,i#e: V5omcat-$n ta##ation-Director(Rweba'' I"&&5IW9E$N%Ic4 tom.t#d. Vta!#ibR Vt#ib-ver ionR1.0VGt#ib-ver ionR V* '-ver ionR2.0VG* '-ver ionR V hort-nameR9<am'#e 53DVG hort-nameR Vta!R VnameRCe##oVGnameR Vta!-c#a Rcom.t4toria# 'oint.Ce##o5a!VGta!-c#a R Vbod(-contentRem't(VGbod(-contentR VGta!R VGta!#ibR Now it? time to 4 e above de,ined c4 tom ta! 'ell in o4r 6SP 'ro!ram a ,o##ow : VL^ ta!#ib 're,i<KPe<P 4riKPW9E-$N%Gc4 tom.t#dPLR +2+ 3imited i a S4b idiar( o, 5+S 3imited 10-

Vhtm#R VheadR Vtit#eR) am'#e c4 tom ta!VGtit#eR VGheadR Vbod(R Ve<:Ce##oGR VGbod(R VGhtm#R 5r( to ca## above 6SP and thi ho4#d 'rod4ce ,o##owin! re 4#t: Ce##o +4 tom 5a!W Acce%%in( the *a( 6 dy: Fo4 can inc#4de a me a!e in the bod( o, the ta! a (o4 have een with tandard ta! . +on ider (o4 want to de,ine a c4 tom ta! named Ve<:Ce##oR and (o4 want to 4 e it in the ,o##owin! ,a hion with a bod(: Ve<:Ce##oR 5hi i me a!e bod( VGe<:Ce##oR 3et 4 make ,o##owin! chan!e in above o4r ta! code to 'roce the bod( o, the ta!: 'acka!e com.t4toria# 'ointJ im'ort *ava<. erv#et.* '.ta!e<t.OJ im'ort *ava<. erv#et.* '.OJ im'ort *ava.io.OJ '4b#ic c#a Ce##o5a! e<tend Sim'#e5a!S4''ort N

Strin!Writer w K new Strin!Writer7;J '4b#ic void do5a!7; throw 6 '9<ce'tion, $&9<ce'tion N !et6 'Eod(7;.invoke7 w;J !et6 '+onte<t7;.!et&4t7;.'rint#n7 w.toStrin!7;;J Q Q $n thi ca e, the o4t'4t re 4#tin! ,rom the invocation i ,ir t ca't4red into a Strin!Writer be,ore bein! written to the 6 'Writer a ociated with the ta!. Now accordin!#( we need to chan!e 53D ,i#e a ,o##ow : Vta!#ibR Vt#ib-ver ionR1.0VGt#ib-ver ionR V* '-ver ionR2.0VG* '-ver ionR V hort-nameR9<am'#e 53D with Eod(VG hort-nameR Vta!R VnameRCe##oVGnameR Vta!-c#a Rcom.t4toria# 'oint.Ce##o5a!VGta!-c#a R Vbod(-contentR cri't#e VGbod(-contentR VGta!R VGta!#ibR Now #et 4 ca## above ta! with 'ro'er bod( a ,o##ow : VL^ ta!#ib 're,i<KPe<P 4riKPW9E-$N%Gc4 tom.t#dPLR Vhtm#R VheadR Vtit#eR) am'#e c4 tom ta!VGtit#eR VGheadR Vbod(R Ve<:Ce##oR 5hi i me a!e bod( VGe<:Ce##oR VGbod(R +2+ 3imited i a S4b idiar( o, 5+S 3imited 110

VGhtm#R 5hi wi## 'rod4ce ,o##owin! re 4#t: 5hi i me a!e bod( 7u%t ! *a( Attribute%: Fo4 can 4 e vario4 attrib4te a#on! with (o4r c4 tom ta! . 5o acce't an attrib4te va#4e, a c4 tom ta! c#a need to im'#ement etter method , identica# to 6avaEean etter method a hown be#ow: 'acka!e com.t4toria# 'ointJ im'ort *ava<. erv#et.* '.ta!e<t.OJ im'ort *ava<. erv#et.* '.OJ im'ort *ava.io.OJ '4b#ic c#a Ce##o5a! e<tend Sim'#e5a!S4''ort N

'rivate Strin! me a!eJ '4b#ic void et2e a!e7Strin! m !; N thi .me a!e K m !J Q Strin!Writer w K new Strin!Writer7;J '4b#ic void do5a!7; throw 6 '9<ce'tion, $&9<ce'tion N i, 7me a!e WK n4##; N GO = e me a!e ,rom attrib4te OG 6 'Writer o4t K !et6 '+onte<t7;.!et&4t7;J o4t.'rint#n7 me a!e ;J Q e# e N GO 4 e me a!e ,rom the bod( OG !et6 'Eod(7;.invoke7 w;J !et6 '+onte<t7;.!et&4t7;.'rint#n7 w.toStrin!7;;J Q Q Q 5he attrib4te? name i Pme a!eP, o the etter method i et2e a!e7;. Now #et 4 add thi attrib4te in 53D ,i#e 4 in! Vattrib4teR e#ement a ,o##ow : Vta!#ibR Vt#ib-ver ionR1.0VGt#ib-ver ionR V* '-ver ionR2.0VG* '-ver ionR V hort-nameR9<am'#e 53D with Eod(VG hort-nameR Vta!R VnameRCe##oVGnameR Vta!-c#a Rcom.t4toria# 'oint.Ce##o5a!VGta!-c#a R Vbod(-contentR cri't#e VGbod(-contentR Vattrib4teR VnameRme a!eVGnameR VGattrib4teR VGta!R VGta!#ibR Now #et 4 tr( ,o##owin! 6SP with me a!e attrib4te a ,o##ow : VL^ ta!#ib 're,i<KPe<P 4riKPW9E-$N%Gc4 tom.t#dPLR Vhtm#R VheadR Vtit#eR) am'#e c4 tom ta!VGtit#eR VGheadR +2+ 3imited i a S4b idiar( o, 5+S 3imited 111

Vbod(R Ve<:Ce##o me a!eKP5hi i c4 tom ta!P GR VGbod(R VGhtm#R 5hi wi## 'rod4ce ,o##owin! re 4#t: 5hi i c4 tom ta! Co'e above e<am'#e make en e ,or (o4. $t wo4#d be worth to note that (o4 can inc#4de ,o##owin! 'ro'ertie ,or an attrib4te: Pr "erty Pur" %e 5he name e#ement de,ine the name o, an attrib4te. 9ach name attrib4te name m4 t be 4ni@4e ,or a 'artic4#ar ta!. 5hi 'eci,ie i, thi attrib4te i re@4ired or o'tiona#. $t wo4#d be re@4ired ,a# e ,or o'tiona#. rte<'rva#4e Dec#are i, a r4ntime e<'re ion va#4e ,or a ta! attrib4te i va#id De,ine the 6ava c#a -t('e o, thi attrib4te. E( de,a4#t it i t('e a 4med a #trin( de cri'tion $n,ormationa# de cri'tion can be 'rovided. Dec#are i, thi attrib4te va#4e ho4#d be treated a a ,ra!ment J%"Fra(!ent. %o##owin! i the e<am'#e to 'eci,( 'ro'ertie re#ated to an attrib4te: ..... Vattrib4teR VnameRattrib4teTnameVGnameR Vre@4iredR,a# eVGre@4iredR Vt('eR*ava.4ti#.DateVGt('eR V,ra!mentR,a# eVG,ra!mentR VGattrib4teR ..... $, (o4 are 4 in! two attrib4te then (o4 can modi,( (o4r 53D a ,o##ow : ..... Vattrib4teR VnameRattrib4teTname1VGnameR Vre@4iredR,a# eVGre@4iredR Vt('eR*ava.4ti#.Eoo#eanVGt('eR V,ra!mentR,a# eVG,ra!mentR VGattrib4teR Vattrib4teR VnameRattrib4teTname2VGnameR Vre@4iredRtr4eVGre@4iredR Vt('eR*ava.4ti#.DateVGt('eR VGattrib4teR ..... J#P $ E,"re%%i n 4an(ua(e 8E4; 6SP 9<'re ion 3an!4a!e 793; make it 'o ib#e to ea i#( acce a''#ication data tored in 6avaEean com'onent . 6SP 93 a##ow (o4 to create e<'re ion both 8a; arithmetic and 8b; #o!ica#. Within a 6SP 93 e<'re ion, (o4 can 4 e inte!er , ,#oatin! 'oint n4mber , trin! , the b4i#t-in con tant tr4e and ,a# e ,or boo#ean va#4e , and n4##. #i!"le #ynta,: 5('ica##(, when (o4 'eci,( an attrib4te va#4e in a 6SP ta!, (o4 im'#( 4 e a trin!. %or e<am'#e: V* ': etPro'ert( nameKPbo<P 'ro'ert(KP'erimeterP va#4eKP100PGR 6SP 93 a##ow (o4 to 'eci,( an e<'re ion ,or an( o, the e attrib4te va#4e . ) im'#e (nta< ,or 6SP 93 i a ,o##ow : MNe<'rQ Cere e,"r 'eci,ie the e<'re ion it e#,. 5he mo t common o'erator in 6SP 93 are + and 9:. 5he e two o'erator a##ow (o4 to acce vario4 attrib4te o, 6ava Eean and b4i#t-in 6SP ob*ect . +2+ 3imited i a S4b idiar( o, 5+S 3imited 112

%or e<am'#e above (nta< V* ': etPro'ert(R ta! can be written with an e<'re ion #ike: V* ': etPro'ert( nameKPbo<P 'ro'ert(KP'erimeterP va#4eKPMN2Obo<.width>2Obo<.hei!htQPGR When the 6SP com'i#er ee the MNQ ,orm in an attrib4te, it !enerate code to eva#4ate the e<'re ion and 4b tit4e the va#4e o, e<'re on. Fo4 can a# o 4 e 6SP 93 e<'re ion within tem'#ate te<t ,or a ta!. %or e<am'#e, the V* ':te<tR ta! im'#( in ert it content within the bod( o, a 6SP. 5he ,o##owin! V* ':te<tR dec#aration in ert Vh1RCe##o 6SPWVGh1R into the 6SP o4t'4t: V* ':te<tR Vh1RCe##o 6SPWVGh1R VG* ':te<tR Fo4 can inc#4de a 6SP 93 e<'re ion in the bod( o, a V* ':te<tR ta! 7or an( other ta!; with the ame MNQ (nta< (o4 4 e ,or attrib4te . %or e<am'#e: V* ':te<tR Eo< Perimeter i : MN2Obo<.width > 2Obo<.hei!htQ VG* ':te<tR 93 e<'re ion can 4 e 'arenthe e to !ro4' 4be<'re ion . %or e<am'#e, MN71 > 2; O 1Q e@4a# -, b4t MN1 > 72 O 1;Q e@4a# 0. 5o deactivate the eva#4ation o, 93 e<'re ion , we 'eci,( the i 93$!nored attrib4te o, the 'a!e directive a be#ow: VL^ 'a!e i 93$!nored KPtr4eY,a# eP LR 5he va#id va#4e o, thi attrib4te are tr4e and ,a# e. $, it i tr4e, 93 e<'re ion are i!nored when the( a''ear in tatic te<t or ta! attrib4te . $, it i ,a# e, 93 e<'re ion are eva#4ated b( the container. 6a%ic O"erat r% in E4: 6SP 9<'re ion 3an!4a!e 793; 4''ort mo t o, the arithmetic and #o!ica# o'erator 4''orted b( 6ava. Ee#ow i the #i t o, mo t ,re@4ent#( 4 ed o'erator : O"erat r De%cri"ti n . )cce a bean 'ro'ert( or 2a' entr( 8: )cce an arra( or 3i t e#ement Gro4' a 4be<'re ion to chan!e the eva#4ation 7; order > )ddition S4btraction or ne!ation o, a va#4e O 24#ti'#ication G or div Divi ion L or mod 2od4#o 7remainder; KK or e@ 5e t ,or e@4a#it( WK or ne 5e t ,or ine@4a#it( V or #t 5e t ,or #e than R or !t 5e t ,or !reater than VK or #e 5e t ,or #e than or e@4a# RK or !t 5e t ,or !reater than or e@4a# XX or and 5e t ,or #o!ica# )ND YY or or 5e t ,or #o!ica# &" W or not =nar( Eoo#ean com'#ement em't( 5e t ,or em't( variab#e va#4e Functi n% in J#P E4 : 6SP 93 a##ow (o4 to 4 e ,4nction in e<'re ion a we##. 5he e ,4nction m4 t be de,ined in c4 tom ta! #ibrarie . ) ,4nction 4 a!e ha the ,o##owin! (nta<: MNn :,4nc7'aram1, 'aram2, ...;Q Where n i the name 'ace o, the ,4nction, ,4nc i the name o, the ,4nction and 'aram1 i the ,ir t 'arameter va#4e. %or e<am'#e, the ,4nction ,n:#en!th, which i 'art o, the 6S53 #ibrar( can be 4 ed a ,o##ow to !et the the #en!th o, a trin!. +2+ 3imited i a S4b idiar( o, 5+S 3imited 111

MN,n:#en!th7PGet m( #en!thP;Q 5o 4 e a ,4nction ,rom an( ta! #ibrar( 7 tandard or c4 tom;, (o4 m4 t in ta## that #ibrar( on (o4r erver and m4 t inc#4de the #ibrar( in (o4r 6SP 4 in! Vta!#ibR directive a e<'#ained in 6S53 cha'ter. J#P E4 I!"licit Object%: 5he 6SP e<'re ion #an!4a!e 4''ort the ,o##owin! im'#icit ob*ect : I!"licit bject De%cri"ti n 'a!eSco'e Sco'ed variab#e ,rom 'a!e co'e re@4e tSco'e Sco'ed variab#e ,rom re@4e t co'e e ionSco'e Sco'ed variab#e ,rom e ion co'e a''#icationSco'e Sco'ed variab#e ,rom a''#ication co'e 'aram "e@4e t 'arameter a trin! 'aramVa#4e "e@4e t 'arameter a co##ection o, trin! header C55P re@4e t header a trin! C55P re@4e t header a co##ection o, headerVa#4e trin! initParam +onte<t-initia#iBation 'arameter cookie +ookie va#4e 5he 6SP Pa!e+onte<t ob*ect ,or the c4rrent 'a!e+onte<t 'a!e Fo4 can 4 e the e ob*ect in an e<'re ion a i, the( were variab#e . Cere are ,ew e<am'#e which wo4#d c#ear the conce't: *he "a(e7 nte,t Object: 5he 'a!e+onte<t ob*ect !ive (o4 acce to the 'a!e+onte<t 6SP ob*ect. 5hro4!h the 'a!e+onte<t ob*ect, (o4 can acce the re@4e t ob*ect. %or e<am'#e, to acce the incomin! @4er( trin! ,or a re@4e t, (o4 can 4 e the e<'re ion: MN'a!e+onte<t.re@4e t.@4er(Strin!Q *he #c "e Object%: 5he 'a!eSco'e, re@4e tSco'e, e ionSco'e, and a''#icationSco'e variab#e 'rovide acce to variab#e tored at each co'e #eve#. %or e<am'#e, $, (o4 need to e<'#icit#( acce the bo< variab#e in the a''#ication co'e, (o4 can acce thro4!h the a''#icationSco'e variab#e a a''#icationSco'e.bo<. *he "ara! and "ara!Value% Object%: 5he 'aram and 'aramVa#4e ob*ect !ive (o4 acce to the 'arameter va#4e norma##( avai#ab#e thro4!h the re@4e t.!etParameter and re@4e t.!etParameterVa#4e method . %or e<am'#e, to acce a 'arameter named order, 4 e the e<'re ion MN'aram.orderQ or M N'aram8PorderP:Q. %o##owin! i the e<am'#e to acce a re@4e t 'arameter named 4 ername: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.OP LR VL Strin! tit#e K P)cce in! "e@4e t ParamPJ LR Vhtm#R VheadR Vtit#eRVL o4t.'rint7tit#e;J LRVGtit#eR VGheadR Vbod(R VcenterR Vh1RVL o4t.'rint7tit#e;J LRVGh1R VGcenterR Vdiv a#i!nKPcenterPR V'RMN'aram8P4 ernameP:QVG'R VGdivR VGbod(R VGhtm#R +2+ 3imited i a S4b idiar( o, 5+S 3imited 11D

it

5he 'aram ob*ect ret4rn in!#e trin! va#4e , wherea the 'aramVa#4e ob*ect ret4rn trin! arra( . header and headerValue% Object%: 5he header and headerVa#4e ob*ect !ive (o4 acce to the header va#4e norma##( avai#ab#e thro4!h the re@4e t.!etCeader and re@4e t.!etCeader method . %or e<am'#e, to acce a header named 4 er-a!ent, 4 e the e<'re ion MNheader.4 er-a!entQ or M Nheader8P4 er-a!entP:Q. %o##owin! i the e<am'#e to acce a header 'arameter named 4 er-a!ent: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.OP LR VL Strin! tit#e K P= er )!ent 9<am'#ePJ LR Vhtm#R VheadR Vtit#eRVL o4t.'rint7tit#e;J LRVGtit#eR VGheadR Vbod(R VcenterR Vh1RVL o4t.'rint7tit#e;J LRVGh1R VGcenterR Vdiv a#i!nKPcenterPR V'RMNheader8P4 er-a!entP:QVG'R VGdivR VGbod(R VGhtm#R 5hi wo4#d di '#a( omethin! a ,o##ow : 5%er A(ent E,a!"le 2oBi##aGD.0 7com'atib#eJ 2S$9 8.0J Window N5 /.1J W&W/DJ 5ridentGD.0J S3++2J .N95 +3" 2.0..0020J .N95 +3" 1...1002-J .N95 +3" 1.0.1002-J 2edia +enter P+ /.0J CPN5D%J .N95D.0+J $n,oPath.2; 5he header ob*ect ret4rn in!#e trin! va#4e , wherea the headerVa#4e ob*ect ret4rn trin! arra( . J#P $ E,ce"ti n 'andlin( When (o4 are writin! 6SP code, a 'ro!rammer ma( #eave a codin! error which can occ4r at an( 'art o, the code. Fo4 can have ,o##owin! t('e o, error in (o4r 6SP code: 7hec=ed e,ce"ti n%: )checked e<ce'tion i an e<ce'tion that i t('ica##( a 4 er error or a 'rob#em that cannot be ,ore een b( the 'ro!rammer. %or e<am'#e, i, a ,i#e i to be o'ened, b4t the ,i#e cannot be ,o4nd, an e<ce'tion occ4r . 5he e e<ce'tion cannot im'#( be i!nored at the time o, com'i#ation. Runti!e e,ce"ti n%: ) r4ntime e<ce'tion i an e<ce'tion that occ4r that 'robab#( co4#d have been avoided b( the 'ro!rammer. ) o''o ed to checked e<ce'tion , r4ntime e<ce'tion are i!nored at the time o, com'#iation. Err r%: 5he e are not e<ce'tion at a##, b4t 'rob#em that ari e be(ond the contro# o, the 4 er or the 'ro!rammer. 9rror are t('ica##( i!nored in (o4r code beca4 e (o4 can rare#( do an(thin! abo4t an error. %or e<am'#e, i, a tack over,#ow occ4r , an error wi## ari e. 5he( are a# o i!nored at the time o, com'i#ation. 5hi t4toria# wi## !ive (o4 ,ew im'#e and e#e!ant wa( to hand#e r4n time e<ce'tionGerror occ4rin! in (o4r 6SP code. 5%in( E,ce"ti n Object: 5he e<ce'tion ob*ect i an in tance o, a 4bc#a o, 5hrowab#e 7e.!., *ava.#an!. N4##Pointer9<ce'tion; and i on#( avai#ab#e in error 'a!e . %o##owin! i the #i t o, im'ortant medthod avai#ab#e in the 5hrowab#e c#a . #N &eth d% with De%cri"ti n "ublic #trin( (et&e%%a(e8; 1 "et4rn a detai#ed me a!e abo4t the e<ce'tion that ha occ4rred. 5hi me a!e i initia#iBed in the 5hrowab#e con tr4ctor. "ublic *hr wable (et7au%e8; 2 "et4rn the ca4 e o, the e<ce'tion a re're ented b( a 5hrowab#e ob*ect. +2+ 3imited i a S4b idiar( o, 5+S 3imited 11.

"ublic #trin( t #trin(8; "et4rn the name o, the c#a concatenated with the re 4#t o, !et2e a!e7; "ublic ) id "rint#tac=*race8; D Print the re 4#t o, toStrin!7; a#on! with the tack trace to S( tem.err, the error o4t'4t tream. "ublic #tac=*raceEle!ent 9: (et#tac=*race8; "et4rn an arra( containin! each e#ement on the tack trace. 5he e#ement at inde< 0 . re're ent the to' o, the ca## tack, and the #a t e#ement in the arra( re're ent the method at the bottom o, the ca## tack. "ublic *hr wable fillIn#tac=*race8; / %i## the tack trace o, thi 5hrowab#e ob*ect with the c4rrent tack trace, addin! to an( 'revio4 in,ormation in the tack trace. 6SP !ive (o4 an o'tion to 'eci,( 9rror Pa!e ,or each 6SP. Whenever the 'a!e throw an e<ce'tion, the 6SP container a4tomatica##( invoke the error 'a!e. %o##owin! i an e<am'#e to 'eci,i( an error 'a!e ,or a main.* '. 5o et 4' an error 'a!e, 4 e the VL^ 'a!e errorPa!eKP<<<P LR directive. VL^ 'a!e errorPa!eKPShow9rror.* 'P LR 1 Vhtm#R VheadR Vtit#eR9rror Cand#in! 9<am'#eVGtit#eR VGheadR Vbod(R VL GG 5hrow an e<ce'tion to invoke the error 'a!e int < K 1J i, 7< KK 1; N throw new "4ntime9<ce'tion7P9rror conditionWWWP;J Q LR VGbod(R VGhtm#R Now (o4 wo4#d have to write one 9rror Cand#in! 6SP Show9rror.* ', which i !iven be#ow. Notice that the error-hand#in! 'a!e inc#4de the directive VL^ 'a!e i 9rrorPa!eKPtr4eP LR. 5hi directive ca4 e the 6SP com'i#er to !enerate the e<ce'tion in tance variab#e. VL^ 'a!e i 9rrorPa!eKPtr4eP LR Vhtm#R VheadR Vtit#eRShow 9rror Pa!eVGtit#eR VGheadR Vbod(R Vh1R&'' ...VGh1R V'RSorr(, an error occ4rred.VG'R V'RCere i the e<ce'tion tack trace: VG'R V'reR VL e<ce'tion.'rintStack5race7re 'on e.!etWriter7;;J LR VG'reR VGbod(R VGhtm#R Now tr( to acce main.* ', it ho4#d !enerate omethin! a ,o##ow : *ava.#an!."4ntime9<ce'tion: 9rror conditionWWW ...... &'' ... Sorr(, an error occ4rred. +2+ 3imited i a S4b idiar( o, 5+S 3imited 11/

Cere i the e<ce'tion tack trace: 5%in( J#*4 ta(% f r Err r Pa(e: Fo4 can make 4 e o, 6S53 ta! to write an error 'a!e Show9rror.* '. 5hi 'a!e ha a#mo t ame #o!ic which we have 4 ed in above e<am'#e, b4t it ha better tr4ct4re and it 'rovide more in,ormation: VL^ ta!#ib 're,i<KPcP 4riKPhtt':GG*ava. 4n.comG* 'G* t#GcoreP LR VL^'a!e i 9rrorPa!eKPtr4eP LR Vhtm#R VheadR Vtit#eRShow 9rror Pa!eVGtit#eR VGheadR Vbod(R Vh1R&'' ...VGh1R Vtab#e widthKP100LP borderKP1PR Vtr va#i!nKPto'PR Vtd widthKPD0LPRVbR9rror:VGbRVGtdR VtdRMN'a!e+onte<t.e<ce'tionQVGtdR VGtrR Vtr va#i!nKPto'PR VtdRVbR="$:VGbRVGtdR VtdRMN'a!e+onte<t.errorData.re@4e t="$QVGtdR VGtrR Vtr va#i!nKPto'PR VtdRVbRStat4 code:VGbRVGtdR VtdRMN'a!e+onte<t.errorData. tat4 +odeQVGtdR VGtrR Vtr va#i!nKPto'PR VtdRVbRStack trace:VGbRVGtdR VtdR Vc:,or9ach varKPtraceP item KPMN'a!e+onte<t.e<ce'tion. tack5raceQPR V'RMNtraceQVG'R VGc:,or9achR VGtdR VGtrR VGtab#eR VGbod(R VGhtm#R Now tr( to acce main.* ', it ho4#d !enerate omethin! a ,o##ow : O""%+++ Err r: 5RI: #tatu% c de: #tac= trace: *ava.#an!."4ntime9<ce'tion: 9rror conditionWWW Gmain.* ' .00 or!.a'ache.* '.mainT* '.T* 'Service7mainT* '.*ava:/.; or!.a'ache.*a 'er.r4ntime.Ctt'6 'Ea e. ervice7Ctt'6 'Ea e.*ava:/ 8; *ava<. erv#et.htt'.Ctt'Serv#et. ervice7Ctt'Serv#et.*ava:022; or!.a'ache.*a 'er. erv#et.6 'Serv#et. ervice76 'Serv#et.*ava:2/.; *ava<. erv#et.htt'.Ctt'Serv#et. ervice7Ctt'Serv#et.*ava:022; ...................

5%in( *ry+++7atch 6l c=: $, (o4 want to hand#e error with in the ame 'a!e and want to take ome action in tead o, ,irin! an error 'a!e, (o4 can make 4 e o, try++++catch b#ock. %o##owin! i a im'#e e<am'#e which how how to 4 e tr(...catch b#ock. 3et 4 '4t ,o##owin! code in main.* ': Vhtm#R VheadR +2+ 3imited i a S4b idiar( o, 5+S 3imited 110

Vtit#eR5r(...+atch 9<am'#eVGtit#eR VGheadR Vbod(R VL tr(N int i K 1J i K i G 0J o4t.'rint#n7P5he an wer i P > i;J Q catch 79<ce'tion e;N o4t.'rint#n7P)n e<ce'tion occ4rred: P > e.!et2e a!e7;;J Q LR VGbod(R VGhtm#R Now tr( to acce main.* ', it ho4#d !enerate omethin! a ,o##ow : )n e<ce'tion occ4rred: G b( Bero J#P $ Debu((in( $t i a#wa( di,,ic4#t to te tin!Gdeb4!!in! a 6SP and erv#et . 6SP and Serv#et tend to invo#ve a #ar!e amo4nt o, c#ientG erver interaction, makin! error #ike#( b4t hard to re'rod4ce. Cere are a ,ew hint and 4!!e tion that ma( aid (o4 in (o4r deb4!!in!. 5%in( #y%te!+ ut+"rintln8;: S( tem.o4t.'rint#n7; i ea ( to 4 e a a marker to te t whether a certain 'iece o, code i bein! e<ec4ted or not. We can 'rint o4t variab#e va#4e a we##. )dditiona##(: Since the S( tem ob*ect i 'art o, the core 6ava ob*ect , it can be 4 ed ever(where witho4t the need to in ta## an( e<tra c#a e . 5hi inc#4de Serv#et , 6SP, "2$, 96E? , ordinar( Eean and c#a e , and tanda#one a''#ication . +om'ared to to''in! at break'oint , writin! to S( tem.o4t doe n?t inter,ere m4ch with the norma# e<ec4tion ,#ow o, the a''#ication, which make it ver( va#4ab#e when timin! i cr4cia#. %o##owin! i the (nta< to 4 e S( tem.o4t.'rint#n7;: S( tem.o4t.'rint#n7PDeb4!!in! me a!eP;J %o##owin! i a im'#e e<am'#e o, 4 in! S( tem.o4t.'rint7;: VL^ta!#ib 're,i<KPcP 4riKPhtt':GG*ava. 4n.comG* 'G* t#GcoreP LR Vhtm#R VheadRVtit#eRS( tem.o4t.'rint#nVGtit#eRVGheadR Vbod(R Vc:,or9ach varKPco4nterP be!inKP1P endKP10P te'KP1P R Vc:o4t va#4eKPMNco4nter-.QPGRVGbrR VL S( tem.o4t.'rint#n7 Pco4nterK P > 'a!e+onte<t.,ind)ttrib4te7Pco4nterP; ;J LR VGc:,or9achR VGbod(R VGhtm#R Now i, (o4 wi## tr( to acce above 6SP, it wi## 'rod4ce ,o##owin! re 4#t at brow er: -D -1 -2 -1 0 1 2 1 D . $, (o4 are 4 in! 5omcat, (o4 wi## a# o ,ind the e #ine a''ended to the end o, tdo4t.#o! in the #o! director(. co4nterK1 co4nterK2 +2+ 3imited i a S4b idiar( o, 5+S 3imited 118

co4nterK1 co4nterKD co4nterK. co4nterK/ co4nterK0 co4nterK8 co4nterKco4nterK10 5hi wa( (o4 can 'rin! variab#e and other in,ormation into ( tem #o! which can be ana#(Bed to ,ind o4t the root ca4 e o, the 'rob#em or ,or vario4 other rea on . 5%in( the JD6 4 ((er: 5he 62S9 #o!!in! ,ramework i de i!ned to 'rovide #o!!in! ervice ,or an( c#a r4nnin! in the 6V2. So we can make 4 e o, thi ,ramework to #o! an( in,ormation. 3et 4 re-write above e<am'#e 4 in! 6DH #o!!er )P$: VL^ta!#ib 're,i<KPcP 4riKPhtt':GG*ava. 4n.comG* 'G* t#GcoreP LR VL^'a!e im'ortKP*ava.4ti#.#o!!in!.3o!!erP LR Vhtm#R VheadRVtit#eR3o!!er.in,oVGtit#eRVGheadR Vbod(R VL 3o!!er #o!!erK3o!!er.!et3o!!er7thi .!et+#a 7;.!etName7;;JLR Vc:,or9ach varKPco4nterP be!inKP1P endKP10P te'KP1P R Vc: et varKPm(+o4ntP va#4eKPMNco4nter-.QP GR Vc:o4t va#4eKPMNm(+o4ntQPGRVGbrR VL Strin! me a!e K Pco4nterKP > 'a!e+onte<t.,ind)ttrib4te7Pco4nterP; > P m(+o4ntKP > 'a!e+onte<t.,ind)ttrib4te7Pm(+o4ntP;J #o!!er.in,o7 me a!e ;J LR VGc:,or9achR VGbod(R VGhtm#R 5hi wo4#d !enerate imi#ar re 4#t at the brow er and in tdo4t.#o!, b4t (o4 wi## have additiona# in,ormation in tdo4t.#o!. Cere we are 4 in! inf method o, the #o!!er beca4 e we are #o!!in! me a!e *4 t ,or in,ormationa# '4r'o e. Cere i a na' hot o, tdo4t.#o! ,i#e: 2D-Se'-2010 21:11:11 or!.a'ache.* '.mainT* ' T* 'Service $N%&: co4nterK1 m(+o4ntK-D 2D-Se'-2010 21:11:11 or!.a'ache.* '.mainT* ' T* 'Service $N%&: co4nterK2 m(+o4ntK-1 2D-Se'-2010 21:11:11 or!.a'ache.* '.mainT* ' T* 'Service $N%&: co4nterK1 m(+o4ntK-2 2D-Se'-2010 21:11:11 or!.a'ache.* '.mainT* ' T* 'Service $N%&: co4nterKD m(+o4ntK-1 2D-Se'-2010 21:11:11 or!.a'ache.* '.mainT* ' T* 'Service $N%&: co4nterK. m(+o4ntK0 2D-Se'-2010 21:11:11 or!.a'ache.* '.mainT* ' T* 'Service $N%&: co4nterK/ m(+o4ntK1 2D-Se'-2010 21:11:11 or!.a'ache.* '.mainT* ' T* 'Service $N%&: co4nterK0 m(+o4ntK2 2D-Se'-2010 21:11:11 or!.a'ache.* '.mainT* ' T* 'Service $N%&: co4nterK8 m(+o4ntK1 2D-Se'-2010 21:11:11 or!.a'ache.* '.mainT* ' T* 'Service $N%&: co4nterK- m(+o4ntKD 2D-Se'-2010 21:11:11 or!.a'ache.* '.mainT* ' T* 'Service $N%&: co4nterK10 m(+o4ntK. 2e a!e can be ent at vario4 #eve# b( 4 in! the convenience ,4nction %e)ere8;, warnin(8;, inf 8;, c nfi(8;, fine8;, finer8;, and fine%t8;. Cere ,ine t7; method can be 4 ed to #o! ,ine t in,ormation and evere7; method can be 4 ed to #o! evere in,ormation. +2+ 3imited i a S4b idiar( o, 5+S 3imited 11-

Fo4 can 4 e 3o!D6 %ramework to #o! me a!e in di,,erent ,i#e ba ed on their everit( #eve# and im'ortance. Debu((in( * l%: NetEean i a ,ree and o'en- o4rce 6ava $nte!rated Deve#o'ment 9nvironment that 4''ort the deve#o'ment o, tanda#one 6ava a''#ication and Web a''#ication 4''ortin! the 6SP and erv#et 'eci,ication and inc#4de a 6SP deb4!!er a we##. NetEean 4''ort the ,o##owin! ba ic deb4!!in! ,4nctiona#itie : Ereak'oint Ste''in! thro4!h code Watch'oint Fo4 can re,ere to NteEean doc4mentation to 4nder tand above deb4!!in! ,4nctiona#itie . 5%in( JD6 Debu((er: Fo4 can deb4! 6SP and erv#et with the ame jdb command (o4 4 e to deb4! an a''#et or an a''#ication. 5o deb4! a 6SP or erv#et, (o4 can deb4! 4n. erv#et.htt'.Ctt'Server, then watch a Ctt'Server e<ec4tin! 6SPG erv#et in re 'on e to C55P re@4e t we make ,rom a brow er. 5hi i ver( imi#ar to how a''#et are deb4!!ed. 5he di,,erence i that with a''#et , the act4a# 'ro!ram bein! deb4!!ed i 4n.a''#et.)''#etViewer. 2o t deb4!!er hide thi detai# b( a4tomatica##( knowin! how to deb4! a''#et . =nti# the( do the ame ,or 6SP, (o4 have to he#' (o4r deb4!!er b( doin! the ,o##owin!: Set (o4r deb4!!er? c#a 'ath o that it can ,ind 4n. erv#et.htt'.Ctt'-Server and a ociated c#a e . Set (o4r deb4!!er? c#a 'ath o that it can a# o ,ind (o4r 6SP and 4''ort c#a e , t('ica##( "&&5IW9E-$N%Ic#a e . &nce (o4 have et the 'ro'er c#a 'ath, tart deb4!!in! 4n. erv#et.htt'.Ctt'Server. Fo4 can et break'oint in whatever 6SP (o4?re intere ted in deb4!!in!, then 4 e a web brow er to make a re@4e t to the Ctt'Server ,or the !iven 6SP 7htt':GG#oca#ho t:8080G6SP5oDeb4!;. Fo4 ho4#d ee e<ec4tion to' at (o4r break'oint . 5%in( 7 !!ent%: +omment in (o4r code can he#' the deb4!!in! 'roce in vario4 wa( . +omment can be 4 ed in #ot o, other wa( in the deb4!!in! 'roce . 5he 6SP 4 e 6ava comment and in!#e #ine 7GG ...; and m4#ti'#e #ine 7GO ... OG; comment can be 4 ed to tem'orari#( remove 'art o, (o4r 6ava code. $, the b4! di a''ear , take a c#o er #ook at the code (o4 *4 t commented and ,ind o4t the 'rob#em. 7lient and #er)er 'eader%: Sometime when a 6SP doe n?t behave a e<'ected, it? 4 e,4# to #ook at the raw C55P re@4e t and re 'on e. $, (o4?re ,ami#iar with the tr4ct4re o, C55P, (o4 can read the re@4e t and re 'on e and ee e<act#( what e<act#( i !oin! with tho e header . I!" rtant Debu((in( *i"%: Cere i a #i t o, ome more deb4!!in! ti' on 6SP deb4!!in!: ) k a brow er to how the raw content o, the 'a!e it i di '#a(in!. 5hi can he#' identi,( ,ormattin! 'rob#em . $t? 4 4a##( an o'tion 4nder the View men4. 2ake 4re the brow er i n?t cachin! a 'revio4 re@4e t? o4t'4t b( ,orcin! a ,4## re#oad o, the 'a!e. With Net ca'e Navi!ator, 4 e Shi,t-"e#oadJ with $nternet 9<'#orer 4 e Shi,t-"e,re h. J#P $ #ecurity 6avaServer Pa!e and erv#et make evera# mechani m avai#ab#e to Web deve#o'er to ec4re a''#ication . "e o4rce are 'rotected dec#arative#( b( identi,(in! them in the a''#ication de'#o(ment de cri'tor and a i!nin! a ro#e to them. Severa# #eve# o, a4thentication are avai#ab#e, ran!in! ,rom ba ic a4thentication 4 in! identi,ier and 'a word to o'hi ticated a4thentication 4 in! certi,icate . R le 6a%ed Authenticati n: 5he a4thentication mechani m in the erv#et 'eci,ication 4 e a techni@4e ca##ed ro#e-ba ed ec4rit(. 5he idea i that rather than re trictin! re o4rce at the 4 er #eve#, (o4 create ro#e and re trict the re o4rce b( ro#e. Fo4 can de,ine di,,erent ro#e in ,i#e tomcat-4 er .<m#, which i #ocated o,, o, 5omcat? home director( in con,. )n e<am'#e o, thi ,i#e i hown be#ow: V[<m# ver ionK?1.0? encodin!K?4t,-8?[R Vtomcat-4 er R +2+ 3imited i a S4b idiar( o, 5+S 3imited 120

Vro#e ro#enameKPtomcatPGR Vro#e ro#enameKPro#e1PGR Vro#e ro#enameKPmana!erPGR Vro#e ro#enameKPadminPGR V4 er 4 ernameKPtomcatP 'a wordKPtomcatP ro#e KPtomcatPGR V4 er 4 ernameKPro#e1P 'a wordKPtomcatP ro#e KPro#e1PGR V4 er 4 ernameKPbothP 'a wordKPtomcatP ro#e KPtomcat,ro#e1PGR V4 er 4 ernameKPadminP 'a wordKP ecretP ro#e KPadmin,mana!erPGR VGtomcat-4 er R 5hi ,i#e de,ine a im'#e ma''in! between 4 er name, 'a word, and ro#e. Notice that a !iven 4 er ma( have m4#ti'#e ro#e , ,or e<am'#e, 4 er nameKPbothP i in the PtomcatP ro#e and the Pro#e1P ro#e. &nce (o4 identi,ied and de,ined di,,erent ro#e , a ro#e-ba ed ec4rit( re triction can be '#aced on di,,erent Web )''#ication re o4rce b( 4 in! the W%ecurity$c n%traintX e#ement in web.<m# ,i#e avai#ab#e in W9E-$N% director(. %o##owin! i a am'#e entr( in web.<m#: Vweb-a''R ... V ec4rit(-con traintR Vweb-re o4rce-co##ectionR Vweb-re o4rce-nameR Sec4redEookSite VGweb-re o4rce-nameR V4r#-'atternRG ec4redGOVG4r#-'atternR Vhtt'-methodRG95VGhtt'-methodR Vhtt'-methodRP&S5VGhtt'-methodR VGweb-re o4rce-co##ectionR Va4th-con traintR Vde cri'tionR 3et on#( mana!er 4 e thi a'' VGde cri'tionR Vro#e-nameRmana!erVGro#e-nameR VGa4th-con traintR VG ec4rit(-con traintR V ec4rit(-ro#eR Vro#e-nameRmana!erVGro#e-nameR VG ec4rit(-ro#eR V#o!in-con,i!R Va4th-methodRE)S$+VGa4th-methodR VG#o!in-con,i!R ... VGweb-a''R )bove entrie wo4#d mean: )n( C55P G95 or P&S5 re@4e t to a ="3 matched b( G ec4redGO wo4#d be 4b*ect to the ec4rit( re triction. ) 'er on with mana!er ro#e i !iven acce to the ec4red re o4rce . 3a t, the #o!in-con,i! e#ement i 4 ed to de cribe the E)S$+ ,orm o, a4thentication. Now i, (o4 tr( brow in! to an( ="3 inc#4din! the G ec4rit( director(, it wo4#d di '#a( a dia#o!4e bo< a kin! ,or 4 er name and 'a word. $, (o4 'rovide a 4 er PadminP and 'a word P ecrerP then on#( (o4 wo4#d have acce on ="3 matched b( G ec4redGO beca4 e above we have de,ined 4 er admin with mana!er ro#e who i a##owed to acce thi re o4rce. F r! 6a%ed Authenticati n: When (o4 4 e the %&"2 a4thentication method, (o4 m4 t 4''#( a #o!in ,orm to 'rom't the 4 er ,or a 4 ername and 'a word. %o##owin! i a im'#e code o, #o!in.* ' to create a ,orm ,or the ame '4r'o e: Vhtm#R Vbod( b!co#orKPb,,,,,,PR V,orm methodKPP&S5P actionKP*T ec4rit(TcheckPR Vtab#e borderKP0PR VtrR +2+ 3imited i a S4b idiar( o, 5+S 3imited 121

VtdR3o!inVGtdR VtdRVin'4t t('eKPte<tP nameKP*T4 ernamePRVGtdR VGtrR VtrR VtdRPa wordVGtdR VtdRVin'4t t('eKP'a wordP nameKP*T'a wordPRVGtdR VGtrR VGtab#eR Vin'4t t('eKP 4bmitP va#4eKP3o!inWPR VGcenterR VG,ormR VGbod(R VGhtm#R Cere (o4 have to make 4re that the #o!in ,orm m4 t contain ,orm e#ement named *T4 ername and *T'a word. 5he action in the V,ormR ta! m4 t be *T ec4rit(Tcheck. P&S5 m4 t be 4 ed a the ,orm method. Same time (o4 wo4#d have to modi,( V#o!in-con,i!R ta! to 'eci,( a4th-method a %&"2: Vweb-a''R ... V ec4rit(-con traintR Vweb-re o4rce-co##ectionR Vweb-re o4rce-nameR Sec4redEookSite VGweb-re o4rce-nameR V4r#-'atternRG ec4redGOVG4r#-'atternR Vhtt'-methodRG95VGhtt'-methodR Vhtt'-methodRP&S5VGhtt'-methodR VGweb-re o4rce-co##ectionR Va4th-con traintR Vde cri'tionR 3et on#( mana!er 4 e thi a'' VGde cri'tionR Vro#e-nameRmana!erVGro#e-nameR VGa4th-con traintR VG ec4rit(-con traintR V ec4rit(-ro#eR Vro#e-nameRmana!erVGro#e-nameR VG ec4rit(-ro#eR V#o!in-con,i!R Va4th-methodR%&"2VGa4th-methodR V,orm-#o!in-con,i!R V,orm-#o!in-'a!eRG#o!in.* 'VG,orm-#o!in-'a!eR V,orm-error-'a!eRGerror.* 'VG,orm-error-'a!eR VG,orm-#o!in-con,i!R VG#o!in-con,i!R ... VGweb-a''R Now when (o4 tr( to acce an( re o4rce with ="3 G ec4redGO, it wo4#d di '#a( above ,orm a kin! ,or 4 er id and 'a word. When the container ee the P*T ec4rit(TcheckP action, it 4 e ome interna# mechani m to a4thenticate the ca##er. $, the #o!in 4cceed and the ca##er i a4thoriBed to acce the ec4red re o4rce, then the container 4 e a e ion-id to identi,( a #o!in e ion ,or the ca##er ,rom that 'oint on. 5he container maintain the #o!in e ion with a cookie containin! the e ion-id. 5he erver end the cookie back to the c#ient, and a #on! a the ca##er 're ent thi cookie with 4b e@4ent re@4e t , then the container wi## know who the ca##er i . $, the #o!in ,ai# , then the erver end back the 'a!e identi,ied b( the ,orm-error-'a!e ettin! Cere *T ec4rit(Tcheck i the action that a''#ication 4 in! ,orm ba ed #o!in have to 'eci,( ,or the #o!in ,orm. $n the ame ,orm (o4 ho4#d a# o have a te<t in'4t contro# ca##ed *T4 ername and a 'a word in'4t contro# ca##ed *T'a word. When (o4 ee thi it mean that the in,ormation contained in the ,orm +2+ 3imited i a S4b idiar( o, 5+S 3imited 122

wi## be 4bmitted to the erver, which wi## check name and 'a word. Cow thi i done i erver 'eci,ic. +heck Standard "ea#m $m'#ementation to 4nder tand how *T ec4rit(Tcheck work ,or 5omcat container. Pr (ra!!atic #ecurity in a #er)let/J#P: 5he Ctt'Serv#et"e@4e t ob*ect 'rovide the ,o##owin! method , which can be 4 ed to mine ec4rit( in,ormation at r4ntime: # &eth d and De%cri"ti n N #trin( (etAuth*y"e8; 1 5he !et)4th5('e7; method ret4rn a Strin! ob*ect that re're ent the name o, the a4thentication cheme 4 ed to 'rotect the Serv#et. b lean i%5%erInR le8ja)a+lan(+#trin( r le; 2 5he i = er$n"o#e7; method ret4rn a boo#ean va#4e: tr4e i, the 4 er i in the !iven ro#e or ,a# e i, the( are not. #trin( (etPr t c l8; 5he !etProtoco#7; method ret4rn a Strin! ob*ect re're entin! the 'rotoco# that wa 4 ed 1 to end the re@4e t. 5hi va#4e can be checked to determine i, a ec4re 'rotoco# wa 4 ed. b lean i%#ecure8; 5he i Sec4re7; method ret4rn a boo#ean va#4e re're entin! i, the re@4e t wa made D 4 in! C55PS. ) va#4e o, tr4e mean it wa and the connection i ec4re. ) va#4e o, ,a# e mean the re@4e t wa not. Princi"le (et5%erPrinci"le8; . 5he !et= erPrinci'#e7; method ret4rn a *ava. ec4rit(.Princi'#e ob*ect that contain the name o, the c4rrent a4thenticated 4 er. %or e<am'#e, a 6avaServer Pa!e that #ink to 'a!e ,or mana!er , (o4 mi!ht have the ,o##owin! code: VL i, 7re@4e t.i = er$n"o#e7Pmana!erP;; N LR Va hre,KPmana!er Gm!rre'ort.* 'PR2ana!er "e'ortVGaR Va hre,KPmana!er G'er onne#.* 'PRPer onne# "ecord VGaR VL Q LR E( checkin! the 4 er? ro#e in a 6SP or erv#et, (o4 can c4 tomiBe the Web 'a!e to how the 4 er on#( the item he can acce . $, (o4 need the 4 er? name a it wa entered in the a4thentication ,orm, (o4 can ca## !et"emote= er method in the re@4e t ob*ect. J#P $ Internati nali>ati n[ iB3n[ lB.n Ee,ore we 'roceed, #et me e<'#ain three im'ortant term : Internati nali>ati n 8iB3n;: 5hi mean enab#in! a web ite to 'rovide di,,erent ver ion o, content tran #ated into the vi itor? #an!4a!e or nationa#it(. 4 cali>ati n 8lB.n;: 5hi mean addin! re o4rce to a web ite to ada't it to a 'artic4#ar !eo!ra'hica# or c4#t4ra# re!ion ,or e<am'#e Cindi tran #ation to a web ite. l cale: 5hi i a 'artic4#ar c4#t4ra# or !eo!ra'hica# re!ion. $t i 4 4a##( re,erred to a a #an!4a!e (mbo# ,o##owed b( a co4ntr( (mbo# which are e'arated b( an 4nder core. %or e<am'#e PenT=SP re're ent en!#i h #oca#e ,or =S. 5here are n4mber o, item which ho4#d be taken care whi#e b4i#din! 4' a !#oba# web ite. 5hi t4toria# wo4#d not !ive (o4 com'#ete detai# on thi b4t it wo4#d !ive (o4 a !ood e<am'#e on how (o4 can o,,er (o4r web 'a!e in di,,erent #an!4a!e to internet comm4nit( b( di,,erentiatin! their #ocation ie. #oca#e. ) 6SP can 'ick4' a''ro'riate ver ion o, the ite ba ed on the re@4e ter? #oca#e and 'rovide a''ro'riate ite ver ion accordin! to the #oca# #an!4a!e, c4#t4re and re@4irement . %o##owin! i the method o, re@4e t ob*ect which ret4rn 3oca#e ob*ect. *ava.4ti#.3oca#e re@4e t.!et3oca#e7; Detectin( 4 cale: %o##owin! are the im'ortant #oca#e method which (o4 can 4 e to detect re@4e ter? #ocation, #an!4a!e and o, co4r e #oca#e. )## the be#ow method di '#a( co4ntr( name and #an!4a!e name et in re@4e ter? brow er. #+N+ &eth d A De%cri"ti n +2+ 3imited i a S4b idiar( o, 5+S 3imited 121

#trin( (et7 untry8; 5hi method ret4rn the co4ntr(Gre!ion code in 4''er ca e ,or thi #oca#e in $S& 11// 2#etter ,ormat. #trin( (etDi%"lay7 untry8; 2 5hi method ret4rn a name ,or the #oca#e? co4ntr( that i a''ro'riate ,or di '#a( to the 4 er. #trin( (et4an(ua(e8; 1 5hi method ret4rn the #an!4a!e code in #ower ca e ,or thi #oca#e in $S& /1- ,ormat. #trin( (etDi%"lay4an(ua(e8; D 5hi method ret4rn a name ,or the #oca#e? #an!4a!e that i a''ro'riate ,or di '#a( to the 4 er. #trin( (etI#OJ7 untry8; . 5hi method ret4rn a three-#etter abbreviation ,or thi #oca#e? co4ntr(. #trin( (etI#OJ4an(ua(e8; / 5hi method ret4rn a three-#etter abbreviation ,or thi #oca#e? #an!4a!e. E,a!"le: 5hi e<am'#e how how (o4 di '#a( a #an!4a!e and a ociated co4ntr( ,or a re@4e t in a 6SP: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.3oca#eP LR VL^ 'a!e im'ortKP*ava<. erv#et.O,*ava<. erv#et.htt'.O PLR VL GGGet the c#ient? 3oca#e 3oca#e #oca#e K re@4e t.!et3oca#e7;J Strin! #an!4a!e K #oca#e.!et3an!4a!e7;J Strin! co4ntr( K #oca#e.!et+o4ntr(7;J LR Vhtm#R VheadR Vtit#eRDetectin! 3oca#eVGtit#eR VGheadR Vbod(R VcenterR Vh1RDetectin! 3oca#eVGh1R VGcenterR V' a#i!nKPcenterPR VL o4t.'rint#n7P3an!4a!e : P > #an!4a!e > PVbr GRP;J o4t.'rint#n7P+o4ntr( : P > co4ntr( > PVbr GRP;J LR VG'R VGbod(R VGhtm#R 4an(ua(e% #ettin(: ) 6SP can o4t'4t a 'a!e written in a We tern 94ro'ean #an!4a!e 4ch a 9n!#i h, S'ani h, German, %rench, $ta#ian, D4tch etc. Cere it i im'ortant to et +ontent-3an!4a!e header to di '#a( a## the character 'ro'er#(. Second 'oint i to di '#a( a## the 'ecia# character 4 in! C523 entitie , %or e<am'#e, PXb2D1JP re're ent PeP, and PXb1/1JP re're ent PfP a ,o##ow : VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.3oca#eP LR VL^ 'a!e im'ortKP*ava<. erv#et.O,*ava<. erv#et.htt'.O PLR VL GG Set re 'on e content t('e re 'on e. et+ontent5('e7Pte<tGhtm#P;J GG Set 'ani h #an!4a!e code. re 'on e. etCeader7P+ontent-3an!4a!eP, Pe P;J Strin! tit#e K P9n 9 'aeo#PJ 1 +2+ 3imited i a S4b idiar( o, 5+S 3imited 12D

LR Vhtm#R VheadR Vtit#eRVL o4t.'rint7tit#e;J LRVGtit#eR VGheadR Vbod(R VcenterR Vh1RVL o4t.'rint7tit#e;J LRVGh1R VGcenterR Vdiv a#i!nKPcenterPR V'R9n 9 'aeo#VG'R V'RfCo#a 24ndoWVG'R VGdivR VGbod(R VGhtm#R 4 cale #"ecific Date%: Fo4 can 4 e the *ava.te<t.Date%ormat c#a and it tatic !etDate5ime$n tance7 ; method to ,ormat date and time 'eci,ic to #oca#e. %o##owin! i the e<am'#e which how how to ,ormat date 'eci,ic to a !iven #oca#e: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.3oca#eP LR VL^ 'a!e im'ortKP*ava<. erv#et.O,*ava<. erv#et.htt'.O PLR VL^ 'a!e im'ortKP*ava.te<t.Date%ormat,*ava.4ti#.DateP LR VL Strin! tit#e K P3oca#e S'eci,ic Date PJ GGGet the c#ient? 3oca#e 3oca#e #oca#e K re@4e t.!et3oca#e7 ;J Strin! date K Date%ormat.!etDate5ime$n tance7 Date%ormat.%=33, Date%ormat.SC&"5, #oca#e;.,ormat7new Date7 ;;J LR Vhtm#R VheadR Vtit#eRVL o4t.'rint7tit#e;J LRVGtit#eR VGheadR Vbod(R VcenterR Vh1RVL o4t.'rint7tit#e;J LRVGh1R VGcenterR Vdiv a#i!nKPcenterPR V'R3oca# Date: VL o4t.'rint7date;J LRVG'R VGdivR VGbod(R VGhtm#R 4 cale #"ecific 7urrency Fo4 can 4 e the *ava.t<t.N4mber%ormat c#a and it tatic !et+4rrenc($n tance7 ; method to ,ormat a n4mber, 4ch a a #on! or do4b#e t('e, in a #oca#e 'eci,ic c4rren(. %o##owin! i the e<am'#e which how how to ,ormat c4rrenc( 'eci,ic to a !iven #oca#e: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.3oca#eP LR VL^ 'a!e im'ortKP*ava<. erv#et.O,*ava<. erv#et.htt'.O PLR VL^ 'a!e im'ortKP*ava.te<t.N4mber%ormat,*ava.4ti#.DateP LR VL Strin! tit#e K P3oca#e S'eci,ic +4rrenc(PJ GGGet the c#ient? 3oca#e 3oca#e #oca#e K re@4e t.!et3oca#e7 ;J N4mber%ormat n,t K N4mber%ormat.!et+4rrenc($n tance7#oca#e;J Strin! ,ormatted+4rr K n,t.,ormat71000000;J +2+ 3imited i a S4b idiar( o, 5+S 3imited

12.

LR Vhtm#R VheadR Vtit#eRVL o4t.'rint7tit#e;J LRVGtit#eR VGheadR Vbod(R VcenterR Vh1RVL o4t.'rint7tit#e;J LRVGh1R VGcenterR Vdiv a#i!nKPcenterPR V'R%ormatted +4rrenc(: VL o4t.'rint7,ormatted+4rr;J LRVG'R VGdivR VGbod(R VGhtm#R 4 cale #"ecific Percenta(e Fo4 can 4 e the *ava.t<t.N4mber%ormat c#a and it tatic !etPercent$n tance7 ; method to !et #oca#e 'eci,ic 'ercenta!e. %o##owin! i the e<am'#e which how how to ,ormat 'ercenta!e 'eci,ic to a !iven #oca#e: VL^ 'a!e im'ortKP*ava.io.O,*ava.4ti#.3oca#eP LR VL^ 'a!e im'ortKP*ava<. erv#et.O,*ava<. erv#et.htt'.O PLR VL^ 'a!e im'ortKP*ava.te<t.N4mber%ormat,*ava.4ti#.DateP LR VL Strin! tit#e K P3oca#e S'eci,ic Percenta!ePJ GGGet the c#ient? 3oca#e 3oca#e #oca#e K re@4e t.!et3oca#e7 ;J N4mber%ormat n,t K N4mber%ormat.!etPercent$n tance7#oca#e;J Strin! ,ormattedPerc K n,t.,ormat70..1;J LR Vhtm#R VheadR Vtit#eRVL o4t.'rint7tit#e;J LRVGtit#eR VGheadR Vbod(R VcenterR Vh1RVL o4t.'rint7tit#e;J LRVGh1R VGcenterR Vdiv a#i!nKPcenterPR V'R%ormatted Percenta!e: VL o4t.'rint7,ormattedPerc;J LRVG'R VGdivR VGbod(R VGhtm#R 6a%ic &V7 Architecture &ode# View 7ontro##er or 2V+ a it i 'o'4#ar#( ca##ed, i a o,tware de i!n 'attern ,or deve#o'in! web a''#ication . ) 2ode# View +ontro##er 'attern i made 4' o, the ,o##owin! three 'art : & del - 5he #owe t #eve# o, the 'attern which i re 'on ib#e ,or maintainin! data. View - 5hi i re 'on ib#e ,or di '#a(in! a## or a 'ortion o, the data to the 4 er. 7 ntr ller - So,tware +ode that contro# the interaction between the 2ode# and View. 2V+ i 'o'4#ar a it i o#ate the a''#ication #o!ic ,rom the 4 er inter,ace #a(er and 4''ort e'aration o, concern . Cere the +ontro##er receive a## re@4e t ,or the a''#ication and then work with the 2ode# to 're'are an( data needed b( the View. 5he View then 4 e the data 're'ared b( the +ontro##er to !enerate a ,ina# 're entab#e re 'on e. 5he 2V+ ab traction can be !ra'hica##( re're ented a ,o##ow .

+2+ 3imited i a S4b idiar( o, 5+S 3imited

12/

*he ! del 5he mode# i re 'on ib#e ,or mana!in! the data o, the a''#ication. $t re 'ond to the re@4e t ,rom the view and it a# o re 'ond to in tr4ction ,rom the contro##er to 4'date it e#,. *he )iew ) 're entation o, data in a 'artic4#ar ,ormat, tri!!ered b( a contro##er? deci ion to 're ent the data. 5he( are cri't ba ed tem'#atin! ( tem #ike 6SP, )SP, PCP and ver( ea ( to inte!rate with )6)A techno#o!(. *he c ntr ller 5he contro##er i re 'on ib#e ,or re 'ondin! to 4 er in'4t and 'er,orm interaction on the data mode# ob*ect . 5he contro##er receive the in'4t, it va#idate the in'4t and then 'er,orm the b4 ine o'eration that modi,ie the tate o, the data mode#. Str4t 2 i a 2V+ ba ed ,ramework. $n the comin! cha'ter , #et 4 ee how we can 4 e the 2V+ methodo#o!( within Str4t 2. #trut% - O)er)iew Str4t 2 i 'o'4#ar and mat4re web a''#ication ,ramework ba ed on the 2V+ de i!n 'attern. Str4t 2 i not *4 t the ne<t ver ion o, Str4t 1, b4t it i a com'#ete rewrite o, the Str4t architect4re. 5he WebWork ,ramework tarted o,, with Str4t ,ramework a the ba i and it !oa# wa to o,,er an enhanced and im'roved ,ramework b4i#t on Str4t t !a=e web de)el "!ent ea%ier f r the de)el "er%. ),ter ome time, the Webwork ,ramework and the Str4t comm4nit( *oined hand to create the ,amo4 Str4t 2 ,ramework. #trut% - fra!ew r= feature%: Cere are ome o, the !reat ,eat4re that ma( ,orce (o4 to con ider Str4t 2: POJO f r!% and POJO acti n% - Str4t 2 ha done awa( with the )ction %orm that were an inte!ra# 'art o, the Str4t ,ramework. With Str4t 2, (o4 can 4 e an( P&6& to receive the ,orm in'4t. Simi#ar#(, (o4 can now ee an( P&6& a an )ction c#a . *a( %u"" rt - Str4t 2 ha im'roved the ,orm ta! and the new ta! a##ow the deve#o'er to write #e code. AJA0 %u"" rt - Str4t 2 ha reco!ni ed the take over b( Web2.0 techno#o!ie , and ha inte!rated )6)A 4''ort into the 'rod4ct b( creatin! )6)A ta! , that ,4nction ver( imi#ar to the tandard Str4t 2 ta! . Ea%y Inte(rati n - $nte!ration with other ,ramework #ike S'rin!, 5i#e and Site2e h i now ea ier with a variet( o, inte!ration avai#ab#e with Str4t 2. *e!"late #u"" rt - S4''ort ,or !eneratin! view 4 in! tem'#ate . Plu(in #u"" rt - 5he core Str4t 2 behavio4r can be enhanced and a4!mented b( the 4 e o, '#4!in . ) n4mber o, '#4!in are avai#ab#e ,or Str4t 2. +2+ 3imited i a S4b idiar( o, 5+S 3imited 120

Pr filin( - Str4t 2 o,,er inte!rated 'ro,i#in! to deb4! and 'ro,i#e the a''#ication. $n addition to thi , Str4t a# o o,,er inte!rated deb4!!in! with the he#' o, b4i#t in deb4!!in! too# . Ea%y t ! dify ta(% - 5a! mark4' in Str4t 2 can be tweaked 4 in! %reemarker tem'#ate . 5hi doe not re@4ire 6SP or *ava know#ed!e. Ea ic C523, A23 and +SS know#ed!e i eno4!h to modi,( the ta! . Pr ! te le%% c nfi(urati n - Str4t 2 'romote #e con,i!4ration with the he#' o, 4 in! de,a4#t va#4e ,or vario4 ettin! . Fo4 don?t have to con,i!4re omethin! 4n#e it deviate ,rom the de,a4#t ettin! et b( Str4t 2. View *echn l (ie%: - Str4t 2 ha a !reat 4''ort ,or m4#ti'#e view o'tion 76SP, %reemarker, Ve#ocit( and AS35; 5he above are *4 t the to' ten ,eat4re o, Str4t 2 that make it an entre'ri e read( ,ramework. #trut% - di%ad)anta(e%: 5ho4!h Str4t 2 come with a #i t o, !reat ,eat4re b4t $ wo4#d not ,or!et to mention ,ew ne!ative 'oint abo4t Str4t 2 and wo4#d need #ot o, im'rovment : 6i((er learnin( cur)e - 5o 4 e 2V+ with Str4t , (o4 have to be com,ortab#e with the tandard 6SP, Serv#et )P$ and a #ar!e X e#aborate ,ramework. P r d cu!entati n - +om'ared to the tandard erv#et and 6SP )P$ , Str4t ha ,ewer on#ine re o4rce , and man( ,ir t-time 4 er ,ind the on#ine )'ache doc4mentation con,4 in! and 'oor#( or!aniBed. 4e%% tran%"arent - With Str4t a''#ication , there i a #ot more !oin! on behind the cene than with norma# 6ava-ba ed Web a''#ication which make it di,,ic4#t to 4nder tand the ,ramework. %ina# note, ) !ood ,ramework ho4#d 'rovide !eneric behavior that man( di,,erent t('e o, a''#ication can make 4 e o, it. Str4t 2 i one o, the be t web ,ramework and bein! hi!h#( 4 ed ,or the deve#o'ment o, "ich $nternet )''#ication 7"$);. #trut% - Architecture %rom a hi!h #eve#, Str4t 2 i a '4##-2V+ 7or 2V+2; ,ramework. 5he 2ode#-View-+ontro##er 'attern in Str4t 2 i rea#iBed with ,o##owin! ,ive core com'onent : Acti n% Interce"t r% Value #tac= / OCN4 Re%ult% / Re%ult ty"e% View techn l (ie% Str4t 2 i #i!ht#( di,,erent ,rom a traditiona# 2V+ ,ramework in that the action take the ro#e o, the mode# rather than the contro##er, a#tho4!h there i ome over#a'.

+2+ 3imited i a S4b idiar( o, 5+S 3imited

128

5he above dia!ram de'ict the &ode#, View and 7ontro##er to the Str4t 2 hi!h #eve# architect4re. 5he contro##er i im'#emented with a Str4t 2 di 'atch erv#et ,i#ter a we## a interce'tor , the mode# i im'#emented with action , and the view a a combination o, re 4#t t('e and re 4#t . 5he va#4e tack and &GN3 'rovide common thread, #inkin! and enab#in! inte!ration between the other com'onent . )'art ,rom the above com'onent , there wi## be a #ot o, in,ormation that re#ate to con,i!4ration. +on,i!4ration ,or the web a''#ication, a we## a con,i!4ration ,or action , interce'tor , re 4#t , etc. 5hi i the architect4ra# overview o, the Str4t 2 2V+ 'attern. We wi## !o thro4!h each com'onent in more detai# in the 4b e@4ent cha'ter . Re@ue%t life cycle: Ea ed on the above di!ram, one can e<'#ain the 4 er? re@4e t #i,e c(c#e in Str4t 2 a ,o##ow : = er end a re@4e t to the erver ,or re@4e tin! ,or ome re o4rce 7i.e 'a!e ;. 5he %i#terDi 'atcher #ook at the re@4e t and then determine the a''ro'riate )ction. +on,i!4red interce'tor ,4nctiona#itie a''#ie 4ch a va#idation, ,i#e 4'#oad etc. Se#ected action i e<ec4ted to 'er,orm the re@4e ted o'eration. )!ain, con,i!4red interce'tor are a''#ied to do an( 'o t-'roce in! i, re@4ired. %ina##( the re 4#t i 're'ared b( the view and ret4rn the re 4#t to the 4 er. #trut% - 'ell W rld E,a!"le ) (o4 #earnt ,rom the Str4t 2 architect4re, when (o4 c#ick on a h('er#ink or 4bmit an C523 ,orm in a Str4t 2 web a''#ication, the in'4t i co##ected b( the +ontro##er which i ent to a 6ava c#a ca##ed )ction . ),ter the )ction i e<ec4ted, a "e 4#t e#ect a re o4rce to render the re 'on e. 5he re o4rce i !enera##( a 6SP, b4t it can a# o be a PD% ,i#e, an 9<ce# 'read heet, or a 6ava a''#et window. ) 4me (o4 a#read( b4i#d-4' (o4r deve#o'ment environment. Now #et 4 'roceed ,or b4i#din! o4r ,ir t 'ell W rld tr4t 2 'ro*ect. 5he aim o, thi 'ro*ect i to b4i#d a web a''#ication that co##ect the 4 er? name and di '#a( PCe##o Wor#dP ,o##owed b( the 4 er name. We wo4#d have to create ,o##owin! ,o4r com'onent ,or an( Str4t 2 'ro*ect: # 7 !" nent% A De%cri"ti n N Acti n 1 +reate an action c#a which wi## contain com'#ete b4 ine #o!ic and conro# the interaction between the 4 er, the mode#, and the view. Interce"t r% 2 +reate interce'tor i, re@4ired, or 4 e e<i tin! interce'tor . 5hi i 'art o, +ontro##er. View 1 +reate a 6SP to interact with the 4 er to take in'4t and to 're ent the ,ina# me a!e . 7 nfi(urati n File% D +reate con,i!4ration ,i#e to co4'#e the )ction, View and +ontro##er . 5he e ,i#e are tr4t .<m#, web.<m#, tr4t .'ro'ertie . $ am !oin! to 4 e 9c#i' e $D9, o a## the re@4ired com'onent wi## be created 4nder a D(namic Web Pro*ect. So #et 4 tart with creatin! D(namic Web Pro*ect. 7reate a Dyna!ic Web Pr ject: Start (o4r 9c#i' e and then !o with File X New X Dyna!ic Web Pr ject and enter 'ro*ect name a 'ell W rld#trut%- and et re t o, the o'tion a !iven in the ,o##owin! creen: Se#ect a## the de,a4#t o'tion in the ne<t creen and ,ina##( check Cenerate Web+,!l de"l y!ent de%cri"t r o'tion. 5hi wi## create a d(namic web 'ro*ect ,or (o4 in 9c#i' e. Now !o with Wind w% X #h w View X Pr ject E,"l rer, and (o4 wi## ee (o4r 'ro*ect window omethin! a be#ow: Now co'( ,o##owin! ,i#e ,rom tr4t 2 #ib ,o#der 7:Y%trut%$-+-+JYlib to o4r 'ro*ect? WE6$INFYlib ,o#der. 5o do thi , (o4 can im'#( dra! and dro' a## the ,o##owin! ,i#e into W9E-$N%I#ib ,o#der. common -,i#e4'#oad-<.(.B.*ar common -io-<.(.B.*ar common -#an!-<.(.*ar common -#o!!in!-<.(.B.*ar common -#o!!in!-a'i-<.(.*ar ,reemarker-<.(.B.*ar *ava i t-.<(.B.G) o!n#-<.(.B.*ar tr4t 2-core-<.(.B.*ar +2+ 3imited i a S4b idiar( o, 5+S 3imited 12-

<work-core.<.(.B.*ar 7reate Acti n 7la%%: )ction c#a i the ke( to Str4t 2 a''#ication and we im'#ement mo t o, the b4 ine #o!ic in action c#a . So #et 4 create a *ava ,i#e Ce##oWor#d)ction.*ava 4nder Ja)a Re% urce% X %rc with a 'acka!e name c !+tut rial%" int+%trut%- with the content !iven be#ow. 5he )ction c#a re 'ond to a 4 er action when 4 er c#ick a ="3. &ne or more o, the )ction c#a ? method are e<ec4ted and a Strin! re 4#t i ret4rned. Ea ed on the va#4e o, the re 4#t, a 'eci,ic 6SP 'a!e i rendered. 'acka!e com.t4toria# 'oint. tr4t 2J '4b#ic c#a Ce##oWor#d)ctionN 'rivate Strin! nameJ '4b#ic Strin! e<ec4te7; throw 9<ce'tion N ret4rn P 4cce PJ Q '4b#ic Strin! !etName7; N ret4rn nameJ Q '4b#ic void etName7Strin! name; N thi .name K nameJ Q Q 5hi i a ver( im'#e c#a with one 'ro'ert( ca##ed PnameP. We have tandard !etter and etter method ,or the PnameP 'ro'ert( and an e<ec4te method that ret4rn the trin! P 4cce P. 5he Str4t 2 ,ramework wi## create an ob*ect o, the Ce##oWor#d)ction c#a and ca## the e<ec4te method in re 'on e to a 4 er? action. Fo4 '4t (o4r b4 ine #o!ic in ide e<ec4te method and ,ina##( ret4rn the Strin! con tant. Sim'#( a(in! ,or ,or each ="3, (o4 wo4#d have to im'#ement one action c#a and either (o4 can 4 e that c#a name direct#( a (o4r action name or (o4 can ma' to ome other name 4 in! tr4t .<m# ,i#e a hown be#ow. 7reate a View We need a 6SP to 're ent the ,ina# me a!e, thi 'a!e wi## be ca##ed b( Str4t 2 ,ramework when a 'rede,ined action wi## ha''en and thi ma''in! wi## be de,ined in tr4t .<m# ,i#e. So #et 4 create the be#ow * ' ,i#e 'ell W rld+j%" in the Web+ontent ,o#der in (o4r ec#i' e 'ro*ect. 5o do thi , ri!ht c#ick on the Web+ontent ,o#der in the 'ro*ect e<'#orer and e#ect New XJ#P File. VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8P LR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! P LR Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR VGheadR Vbod(R Ce##o Wor#d, V :'ro'ert( va#4eKPnamePGR VGbod(R VGhtm#R 5he ta!#ib directive te## the Serv#et container that thi 'a!e wi## be 4 in! the Str4t 2 ta! and that the e ta! wi## be 'receded b( . 5he :'ro'ert( ta! di '#a( the va#4e o, action c#a 'ro'ert( PnameR which i ret4rned b( the method (etNa!e8; o, the Ce##oWor#d)ction c#a . 7reate !ain "a(e: We a# o need to create inde,+j%" in the Web+ontent ,o#der. 5hi ,i#e wi## erve a the initia# action ="3 where a 4 er can c#ick to te## the Str4t 2 ,ramework to ca## the a de,ined method o, the Ce##oWor#d)ction c#a and render the Ce##oWor#d.* ' view. VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR +2+ 3imited i a S4b idiar( o, 5+S 3imited 110

Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR VGheadR Vbod(R Vh1RCe##o Wor#d %rom Str4t 2VGh1R V,orm actionKPhe##oPR V#abe# ,orKPnamePRP#ea e enter (o4r nameVG#abe#RVbrGR Vin'4t t('eKPte<tP nameKPnamePGR Vin'4t t('eKP 4bmitP va#4eKPSa( Ce##oPGR VG,ormR VGbod(R VGhtm#R 5he hell action de,ined in the above view ,i#e wi## be ma''ed to the Ce##oWor#d)ction c#a and it e<ec4te method 4 in! tr4t .<m# ,i#e. When a 4 er c#ick on the S4bmit b4tton it wi## ca4 e the Str4t 2 ,ramework to r4n the e<ec4te method de,ined in the Ce##oWor#d)ction c#a and ba ed on the ret4rned va#4e o, the method, an a''ro'riate view wi## be e#ected and rendered a a re 'on e. 7 nfi(urati n File% We need a ma''in! to tie the ="3, the Ce##oWor#d)ction c#a 72ode#;, and the Ce##oWor#d.* ' 7the view; to!ether. 5he ma''in! te## the Str4t 2 ,ramework which c#a wi## re 'ond to the 4 er? action 7the ="3;, which method o, that c#a wi## be e<ec4ted, and what view to render ba ed on the Strin! re 4#t that method ret4rn . So #et 4 create a ,i#e ca##ed %trut%+,!l. Since Str4t 2 re@4ire tr4t .<m# to be 're ent in c#a e ,o#der. So create tr4t .<m# ,i#e 4nder the Web+ontentGW9E-$N%Gc#a e ,o#der. 9c#i' e doe not create the Pc#a e P ,o#der b( de,a4#t, o (o4 need to do thi (o4r e#,. 5o do thi , ri!ht c#ick on the W9E-$N% ,o#der in the 'ro*ect e<'#orer and e#ect New X F lder. Fo4r tr4t .<m# ho4#d #ook #ike: V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKPhe##oP methodKPe<ec4tePR Vre 4#t nameKP 4cce PRGCe##oWor#d.* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R %ew word abo4t the above con,i!4ration ,i#e. Cere we et the con tant %trut%+de)& de to true, beca4 e we are workin! in deve#o'ment environment and we need to ee ome 4 e,4# #o! me a!e . 5hen, we de,ined a 'acka!e ca##ed hell w rld. +reatin! a 'acka!e i 4 e,4# when (o4 want to !ro4' (o4r action to!ether. $n o4r e<am'#e, we named o4r action a Phe##oP which i corre 'ondin! to the ="3 /hell +acti n and i backed 4' b( the 'ell W rldActi n+cla%%. 5he e,ecute method o, 'ell W rldActi n+cla%% i the method that i r4n when the ="3 /hell +acti n i invoked. $, the o4tcome o, the e,ecute method ret4rn P 4cce P, then we take the 4 er to 'ell W rld+j%". Ne<t te' i to create a web+,!l ,i#e which i an entr( 'oint ,or an( re@4e t to Str4t 2. 5he entr( 'oint o, Str4t 2 a''#ication wi## be a ,i#ter de,ined in de'#o(ment de cri'tor 7web.<m#;. Cence we wi## de,ine an entr( o,or!.a'ache. tr4t 2.di 'atcher.%i#terDi 'atcher c#a in web.<m#. 5he web.<m# ,i#e need to be created 4nder the W9E-$N% ,o#der 4nder Web+ontent. 9c#i' e had a#read( created a ke#ton web.<m# ,i#e ,or (o4 when (o4 created the 'ro*ect. So, #et *4 t modi,( it a ,o##ow : V[<m# ver ionKP1.0P encodin!KP=5%-8P[R Vweb-a'' <m#n :< iKPhtt':GGwww.w1.or!G2001GA23Schema-in tanceP <m#n KPhtt':GG*ava. 4n.comG<m#Gn G*avaeeP <m#n :webKPhtt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T2T..< dP < i: chema3ocationKPhtt':GG*ava. 4n.comG<m#Gn G*avaee htt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T1T0.< dP +2+ 3imited i a S4b idiar( o, 5+S 3imited 111

R Vdi '#a(-nameRStr4t 2VGdi '#a(-nameR Vwe#come-,i#e-#i tR Vwe#come-,i#eRinde<.* 'VGwe#come-,i#eR VGwe#come-,i#e-#i tR V,i#terR V,i#ter-nameR tr4t 2VG,i#ter-nameR V,i#ter-c#a R or!.a'ache. tr4t 2.di 'atcher.%i#terDi 'atcher VG,i#ter-c#a R VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR tr4t 2VG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R VGweb-a''R We have 'eci,ied inde<.* ' to be o4r we#come ,i#e. 5hen we have con,i!4red the Str4t 2 ,i#ter to r4n on a## 4r# 7i.e, an( 4r# that match the 'attern GO; Enable Detailed 4 (: Fo4 can enab#ed com'#ete #o!!in! ,4nctiona#it( whi#e workin! with Str4t 2 b( creatin! l ((in(+"r "ertie% ,i#e 4nder WE6$INF/cla%%e% ,o#der. Hee' the ,o##owin! two #ine in (o4r 'ro'ert( ,i#e: or!.a'ache.cata#ina.core.+ontainerEa e.8+ata#ina:.#eve# K $N%& or!.a'ache.cata#ina.core.+ontainerEa e.8+ata#ina:.hand#er K I *ava.4ti#.#o!!in!.+on o#eCand#er 5he de,a4#t #o!!in!.'ro'ertie 'eci,ie a +on o#eCand#er ,or ro4tin! #o!!in! to tdo4t and a# o a %i#eCand#er. ) hand#er? #o! #eve# thre ho#d can be et 4 in! S9V9"9, W)"N$NG, $N%&, +&N%$G, %$N9, %$N9", %$N9S5 or )33. 5hat? it. We are read( to r4n o4r Ce##o Wor#d a''#ication 4 in! Str4t 2 ,ramework. E,ecute the A""licati n "i!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Ginde<.* '. 5hi wi## !ive (o4 ,o##owin! creen: 9nter a va#4e PStr4t 2P and 4bmit the 'a!e. Fo4 ho4#d ee the ne<t 'a!e Note that (o4 can de,ine inde, a an action in tr4t .<m# ,i#e and in that ca e (o4 can ca## inde< 'a!e a htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Ginde<.action. +heck be#ow how (o4 can de,ine inde< a an action: V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKPinde<PR Vre 4#t RGinde<.* 'VGre 4#tR VGactionR Vaction nameKPhe##oP methodKPe<ec4tePR Vre 4#t nameKP 4cce PRGCe##oWor#d.* 'VGre 4#tR VGactionR VG'acka!eR +2+ 3imited i a S4b idiar( o, 5+S 3imited 112

VG tr4t R #trut% - 7 nfi(urati n File% 5hi cha'ter wi## take (o4 thro4!h ba ic con,i!4ration re@4ired ,or a Str4t 2 a''#ication. Cere we wi## ee what wi## be con,i!4red in ,ew im'ortant con,i!4ration ,i#e : web+,!l, %trut%+,!l, %trut%$ c nfi(+,!l and %trut%+"r "ertie% Cone t#( 'eakin! (o4 can 4rvive 4 in! web.<m# and tr4t .<m# con,i!4ration ,i#e and (o4 have een in 'revio4 cha'ter that o4r e<am'#e worked 4 in! the e two ,i#e , b4t ,or (o4r know#ed!e #et me e<'#ain other ,i#e a we##. *he web+,!l file: 5he web.<m# con,i!4ration ,i#e i a 6299 con,i!4ration ,i#e that determine how e#ement o, the C55P re@4e t are 'roce ed b( the erv#et container. $t i not trict#( a Str4t 2 con,i!4ration ,i#e, b4t it i a ,i#e that need to be con,i!4red ,or Str4t 2 to work. ) di c4 ed ear#ier, thi ,i#e 'rovide an entr( 'oint ,or an( web a''#ication. 5he entr( 'oint o, Str4t 2 a''#ication wi## be a ,i#ter de,ined in de'#o(ment de cri'tor 7web.<m#;. Cence we wi## de,ine an entr( o, Filter9ispatcher c#a in web.<m#. 5he web.<m# ,i#e need to be created 4nder the ,o#der Web7 ntent/WE6$INF. 5hi i the ,ir t con,i!4ration ,i#e (o4 wi## need to con,i!4re i, (o4 are tartin! witho4t the aid o, a tem'#ate or too# that !enerate it 7 4ch a 9c#i' e or 2aven2;. %o##owin! i the content o, web.<m# ,i#e which we 4 ed in o4r #a t e<am'#e. V[<m# ver ionKP1.0P encodin!KP=5%-8P[R Vweb-a'' <m#n :< iKPhtt':GGwww.w1.or!G2001GA23Schema-in tanceP <m#n KPhtt':GG*ava. 4n.comG<m#Gn G*avaeeP <m#n :webKPhtt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T2T..< dP < i: chema3ocationKPhtt':GG*ava. 4n.comG<m#Gn G*avaee htt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T1T0.< dP R Vdi '#a(-nameRStr4t 2VGdi '#a(-nameR Vwe#come-,i#e-#i tR Vwe#come-,i#eRinde<.* 'VGwe#come-,i#eR VGwe#come-,i#e-#i tR V,i#terR V,i#ter-nameR tr4t 2VG,i#ter-nameR V,i#ter-c#a R or!.a'ache. tr4t 2.di 'atcher.%i#terDi 'atcher VG,i#ter-c#a R VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR tr4t 2VG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R VGweb-a''R Note that we ma' the Str4t 2 ,i#ter to /U, and not to /U+acti n which mean that a## 4r# wi## be 'ar ed b( the tr4t ,i#ter. We wi## cover thi when we wi## !o thro4!h the )nnotation cha'ter. *he %trut%+,!l file: 5he %trut%+,!l ,i#e contain the con,i!4ration in,ormation that (o4 wi## be modi,(in! a action are deve#o'ed. 5hi ,i#e can be 4 ed to override de,a4#t ettin! ,or an a''#ication, ,or e<am'#e struts#devMode B )alse and other ettin! which are de,ined in 'ro'ert( ,i#e. 5hi ,i#e can be created 4nder the ,o#der WE6$INF/cla%%e%. 3et 4 have a #ook at the tr4t .<m# ,i#e we created in the Ce##o Wor#d e<am'#e e<'#ained in 'revio4 cha'ter. V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R +2+ 3imited i a S4b idiar( o, 5+S 3imited 111

Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKPhe##oP methodKPe<ec4tePR Vre 4#t nameKP 4cce PRGCe##oWor#d.* 'VGre 4#tR VGactionR V-- more action can be #i ted here --R VG'acka!eR V-- more 'acka!e can be #i ted here --R VG tr4t R 5he ,ir t thin! to note i the DO7*EPE. )## tr4t con,i!4ration ,i#e need to have the correct doct('e a hown in o4r #itt#e e<am'#e. V tr4t R i the root ta! e#ement, 4nder which we dec#are di,,erent 'acka!e 4 in! V'acka!eR ta! . Cere V'acka!eR a##ow e'aration and mod4#ariBation o, the con,i!4ration. 5hi i ver( 4 e,4# when (o4 have a #ar!e 'ro*ect and 'ro*ect i divided into di,,erent mod4#e . Sa(, i, (o4r 'ro*ect ha three domain - b4 ine Ta''#icaiton, c4 tomerTa''#ication and ta,,Ta''#ication, (o4 co4#d create three 'acka!e and tore a ociated action in the a''ro'riate 'acka!e. 5he 'acka!e ta! ha the ,o##owin! attrib4te : Attribute De%cri"ti n name 7re@4ired; 5he 4ni@4e identi,ier ,or the 'acka!e Which 'acka!e doe thi 'acka!e e<tend ,rom[ E( de,a4#t, we 4 e e<tend tr4t -de,a4#t a the ba e 'acka!e. $, marked tr4e, the 'acka!e i not avai#ab#e ,or end 4 er ab tract con 4m'tion. name a'ce =ni@4e name 'ace ,or the action 5he c n%tant ta! a#on! with name and va#4e attrib4te wi## be 4 ed to override an( o, the ,o##owin! 'ro'ertie de,ined in default+"r "ertie%, #ike we *4 t et %trut%+de)& de 'ro'ert(. Settin! %trut%+de)& de 'ro'ert( a##ow 4 to ee more deb4! me a!e in the #o! ,i#e. We de,ine acti n ta! corre 'ond to ever( ="3 we want to acce and we de,ine a c#a with e<ec4te7; method which wi## be acce ed whenever we wi## acce corre 'ondin! ="3. "e 4#t determine what !et ret4rned to the brow er a,ter an action i e<ec4ted. 5he trin! ret4rned ,rom the action ho4#d be the name o, a re 4#t. "e 4#t are con,i!4red 'er-action a above, or a a P!#oba#P re 4#t, avai#ab#e to ever( action in a 'acka!e. "e 4#t have o'tiona# na!e and ty"e attrib4te . 5he de,a4#t name va#4e i P 4cce P. Str4t .<m# ,i#e can !row bi! over time and o breakin! it b( 'acka!e i one wa( o, mod4#ariBin! it, b4t tr4t o,,er another wa( to mod4#ariBe the tr4t .<m# ,i#e. Fo4 co4#d '#it the ,i#e into m4#ti'#e <m# ,i#e and im'ort them in the ,o##owin! ,a hion. V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vinc#4de ,i#eKPm(- tr4t 1.<m#PGR Vinc#4de ,i#eKPm(- tr4t 2.<m#PGR VG tr4t R 5he other con,i!4ration ,i#e that we haven?t covered i the tr4t -de,a4#t.<m#. 5hi ,i#e contain the tandard con,i!4ration ettin! ,or Str4t and (o4 wo4#d not have to to4ch the e ettin! ,or --.--L o, (o4r 'ro*ect . %or thi rea on, we are not !oin! into too m4ch detai# on thi ,i#e. $, (o4 are intere ted, take a #ook into the at the default+"r "ertie% ,i#e avai#ab#e in tr4t 2-core-2.2.1.*ar ,i#e. 5he tr4t -con,i!.<m# ,i#e:5he tr4t -con,i!.<m# con,i!4ration ,i#e i a #ink between the View and 2ode# com'onent in the Web +#ient b4t (o4 wo4#d not have to to4ch the e ettin! ,or --.--L o, (o4r 'ro*ect . 5he con,i!4ration ,i#e ba ica##( contain ,o##owin! main e#ement : # Interce"t r A De%cri"ti n N +2+ 3imited i a S4b idiar( o, 5+S 3imited 11D

%trut%$c nfi( 5hi i the root node o, the con,i!4ration ,i#e. f r!$bean% 5hi i where (o4 ma' (o4r )ction%orm 4bc#a to a name. Fo4 4 e thi name a an 2 a#ia ,or (o4r )ction%orm thro4!ho4t the re t o, the tr4t -con,i!.<m# ,i#e, and even on (o4r 6SP 'a!e . (l bal f rward% 1 5hi ection ma' a 'a!e on (o4r weba'' to a name. Fo4 can 4 e thi name to re,er to the act4a# 'a!e. 5hi avoid hardcodin! ="3 on (o4r web 'a!e . acti n$!a""in(% D 5hi i where (o4 dec#are ,orm hand#er and the( are a# o known a acti n !a""in(%. c ntr ller . 5hi ection con,i!4re Str4t interna# and rare#( 4 ed in 'ractica# it4ation . "lu($in / 5hi ection te## Str4t where to ,ind (o4r 'ro'ertie ,i#e , which contain 'rom't and error me a!e %o##owin! i the am'#e tr4t -con,i!.<m# ,i#e: V[<m# ver ionKP1.0P encodin!KP$S&-88.--1P [R VWD&+5FP9 tr4t -con,i! P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 1.0GG9NP Phtt':GG*akarta.a'ache.or!G tr4t Gdtd G tr4t -con,i!T1T0.dtdPR 1 V tr4t -con,i!R VW-- KKKKKKKKKK %orm Eean De,inition KKKKKKKKKKKK --R V,orm-bean R V,orm-bean nameKP#o!inP t('eKPte t. tr4t .3o!in%ormP GR VG,orm-bean R VW-- KKKKKKKKKK G#oba# %orward De,inition KKKKKKKKK --R V!#oba#-,orward R VG!#oba#-,orward R VW-- KKKKKKKKKK )ction 2a''in! De,inition KKKKKKKK --R Vaction-ma''in! R Vaction 'athKPG#o!inP t('eKPte t. tr4t .3o!in)ctionP R V,orward nameKPva#idP 'athKPG* 'G2ain2en4.* 'P GR V,orward nameKPinva#idP 'athKPG* 'G3o!inView.* 'P GR VGactionR VGaction-ma''in! R VW-- KKKKKKKKKK +ontro##er De,inition KKKKKKKK --R Vcontro##er content5('eKPte<tGhtm#Jchar etK=5%-8P deb4!KP1P ma<%i#eSiBeKP1./182P #oca#eKPtr4eP nocacheKPtr4ePGR VG tr4t -con,i!R %or more detai# on tr4t -con,i!.<m# ,i#e, kind#( check (o4r tr4t doc4mentation. *he %trut%+"r "ertie% file +2+ 3imited i a S4b idiar( o, 5+S 3imited 11.

5hi con,i!4ration ,i#e 'rovide a mechani m to chan!e the de,a4#t behavior o, the ,ramework. )ct4a##( a## o, the 'ro'ertie contained within the %trut%+"r "ertie% con,i!4ration ,i#e can a# o be con,i!4red in the web+,!l 4 in! the init$"ara!, a we## 4 in! the c n%tant ta! in the %trut%+,!l con,i!4ration ,i#e. E4t i, (o4 #ike to kee' the thin! e'arate and more tr4t 'eci,ic then (o4 can create thi ,i#e 4nder the ,o#der WE6$INF/cla%%e%. 5he va#4e con,i!4red in thi ,i#e wi## override the de,a4#t va#4e con,i!4red in default+"r "ertie% which i contained in the tr4t 2-core-<.(.B.*ar di trib4tion. 5here are a co4'#e o, 'ro'ertie that (o4 mi!ht con ider chan!in! 4 in! %trut%+"r "ertie% ,i#e: bbb When et to tr4e, Str4t wi## act m4ch more ,riend#( ,or deve#o'er tr4t .dev2ode K tr4e bbb 9nab#e re#oadin! o, internationa#iBation ,i#e tr4t .i18n.re#oad K tr4e bbb 9nab#e re#oadin! o, A23 con,i!4ration ,i#e tr4t .con,i!4ration.<m#.re#oad K tr4e bbb Set the 'ort that the erver i r4n on tr4t .4r#.htt'.'ort K 8080 Cere an( #ine tartin! with ha h 7b; wi## be a 4med a a comment and it wi## be i!nored b( Str4t 2. #trut% - Acti n% )ction are the core o, the Str4t 2 ,ramework, a the( are ,or an( 2V+ 72ode# View +ontro##er; ,ramework. 9ach ="3 i ma''ed to a 'eci,ic action, which 'rovide the 'roce in! #o!ic nece ar( to ervice the re@4e t ,rom the 4 er. E4t the action a# o erve in two other im'ortant ca'acitie . %ir t, the action '#a( an im'ortant ro#e in the tran ,er o, data ,rom the re@4e t thro4!h to the view, whether it a 6SP or other t('e o, re 4#t. Second, the action m4 t a i t the ,ramework in determinin! which re 4#t ho4#d render the view that wi## be ret4rned in the re 'on e to the re@4e t. 7reate Acti n: 5he on#( re@4irement ,or action in Str4t 2 i that there m4 t be one no-ar!4ment method that ret4rn either a Strin! or "e 4#t ob*ect and m4 t be a P&6&. $, the no-ar!4ment method i not 'eci,ied, the de,a4#t behavior i to 4 e the e<ec4te7; method. &'tiona##( (o4 can e<tend the Acti n#u"" rt c#a which im'#ement i< inter,ace inc#4din! Acti n inter,ace. 5he )ction inter,ace i a ,o##ow : '4b#ic inter,ace )ction N '4b#ic tatic ,ina# Strin! S=++9SS K P 4cce PJ '4b#ic tatic ,ina# Strin! N&N9 K PnonePJ '4b#ic tatic ,ina# Strin! 9""&" K PerrorPJ '4b#ic tatic ,ina# Strin! $NP=5 K Pin'4tPJ '4b#ic tatic ,ina# Strin! 3&G$N K P#o!inPJ '4b#ic Strin! e<ec4te7; throw 9<ce'tionJ Q 3et 4 take a #ook at the action method in the Ce##o Wor#d e<am'#e: 'acka!e com.t4toria# 'oint. tr4t 2J '4b#ic c#a Ce##oWor#d)ctionN 'rivate Strin! nameJ '4b#ic Strin! e<ec4te7; throw 9<ce'tion N ret4rn P 4cce PJ Q '4b#ic Strin! !etName7; N ret4rn nameJ Q '4b#ic void etName7Strin! name; N thi .name K nameJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 11/

Q Q 5o i##4 trate the 'oint that the action method contro# the view, #et 4 make the ,o##owin! chan!e to the e,ecute method and e<tend the c#a )ctionS4''ort a ,o##ow : 'acka!e com.t4toria# 'oint. tr4t 2J im'ort com.o'en (m'hon(.<work2.)ctionS4''ortJ '4b#ic c#a Ce##oWor#d)ction e<tend )ctionS4''ortN 'rivate Strin! nameJ '4b#ic Strin! e<ec4te7; throw 9<ce'tion N i, 7PS9+"95P.e@4a# 7name;; N ret4rn S=++9SSJ Qe# eN ret4rn 9""&"J Q Q '4b#ic Strin! !etName7; N ret4rn nameJ Q '4b#ic void etName7Strin! name; N thi .name K nameJ Q Q $n thi e<am'#e, we have ome #o!ic in the e<ec4te method to #ook at the name attrib4te. $, the attrib4te e@4a# to the trin! PS9+"95P, we ret4rn S=++9SS a the re 4#t otherwi e we ret4rn 9""&" a the re 4#t. Eeca4 e we have e<tended )ctionS4''ort, o we can 4 e Strin! con tant S=++9SS and 9""&". Now, #et 4 modi,( o4r tr4t .<m# ,i#e a ,o##ow : V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKPhe##oP methodKPe<ec4tePR Vre 4#t nameKP 4cce PRGCe##oWor#d.* 'VGre 4#tR Vre 4#t nameKPerrorPRG)cce Denied.* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R 7reate a View 3et 4 create the be#ow * ' ,i#e 'ell W rld+j%" in the Web+ontent ,o#der in (o4r ec#i' e 'ro*ect. 5o do thi , ri!ht c#ick on the Web+ontent ,o#der in the 'ro*ect e<'#orer and e#ect New XJ#P File. 5hi ,i#e wi## be ca##ed in ca e ret4rn re 4#t i S=++9SS which i a Strin! con tant P 4cce P a de,ined in )ction inter,ace: VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8P LR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! P LR Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR VGheadR Vbod(R +2+ 3imited i a S4b idiar( o, 5+S 3imited 110

Ce##o Wor#d, V :'ro'ert( va#4eKPnamePGR VGbod(R VGhtm#R %o##owin! i the ,i#e which wi## be invoked b( the ,ramework in ca e action re 4#t i 9""&" which i e@4a# to Strin! con tant PerrorP. %o##owin! i the content o, Acce%%Denied+j%" VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8P LR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! P LR Vhtm#R VheadR Vtit#eR)cce DeniedVGtit#eR VGheadR Vbod(R Fo4 are not a4thoriBed to view thi 'a!e. VGbod(R VGhtm#R We a# o need to create inde,+j%" in the Web+ontent ,o#der. 5hi ,i#e wi## erve a the initia# action ="3 where the 4 er can c#ick to te## the Str4t 2 ,ramework to ca## the e,ecute method o, the Ce##oWor#d)ction c#a and render the Ce##oWor#d.* ' view. VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR VGheadR Vbod(R Vh1RCe##o Wor#d %rom Str4t 2VGh1R V,orm actionKPhe##oPR V#abe# ,orKPnamePRP#ea e enter (o4r nameVG#abe#RVbrGR Vin'4t t('eKPte<tP nameKPnamePGR Vin'4t t('eKP 4bmitP va#4eKPSa( Ce##oPGR VG,ormR VGbod(R VGhtm#R 5hat? it, there i no chan!e re@4ired web.<m# ,i#e, o #et 4 4 e ame web.<m# which we had created in 9<am'#e cha'ter. Now we are read( to r4n o4r Ce##o Wor#d a''#ication 4 in! Str4t 2 ,ramework. E,ecute the A""licati n "i!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Ginde<.* '. 5hi wi## !ive (o4 ,o##owin! creen: 3et 4 enter a word a PS9+"95P and (o4 ho4#d ee the ,o##owin! 'a!e: Now enter an( word other than PS9+"95P and (o4 ho4#d ee the ,o##owin! 'a!e: 7reate &ulti"le Acti n%: Fo4 wi## ,re@4ent#( de,ine more than one action to hand#e di,,erent re@4e t and to 'rovide di,,erent ="3 to the 4 er , accordin!#( (o4 wi## de,ine di,,erent c#a e a de,ined be#ow: 'acka!e com.t4toria# 'oint. tr4t 2J im'ort com.o'en (m'hon(.<work2.)ctionS4''ortJ c#a 2()ction e<tend )ctionS4''ortN '4b#ic tatic Strin! G&&D K S=++9SSJ '4b#ic tatic Strin! E)D K 9""&"J Q '4b#ic c#a Ce##oWor#d e<tend )ctionS4''ortN ... '4b#ic Strin! e<ec4te7; N +2+ 3imited i a S4b idiar( o, 5+S 3imited

118

i, 7PS9+"95P.e@4a# 7name;; ret4rn 2()ction.G&&DJ ret4rn 2()ction.E)DJ Q ... Q '4b#ic c#a Some&ther+#a e<tend )ctionS4''ortN ... '4b#ic Strin! e<ec4te7; N ret4rn 2()ction.G&&DJ Q ... Q Fo4 wi## con,i!4re the e action in tr4t .<m# ,i#e a ,o##ow : V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKPhe##oP methodKPe<ec4tePR Vre 4#t nameKP 4cce PRGCe##oWor#d.* 'VGre 4#tR Vre 4#t nameKPerrorPRG)cce Denied.* 'VGre 4#tR VGactionR Vaction nameKP omethin!P methodKPe<ec4tePR Vre 4#t nameKP 4cce PRGSomethin!.* 'VGre 4#tR Vre 4#t nameKPerrorPRG)cce Denied.* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R ) (o4 can ee in the above h('othetica# e<am'#e, the action re 4#t S=++9SS and 9""&" are d4'#icated. 5o !et aro4nd thi i 4e, it i 4!!e ted that (o4 create a c#a that contain the re 4#t o4tcome . #trut% - Interce"t r% $nterce'tor are conce't4a##( the ame a erv#et ,i#ter or the 6DH Pro<( c#a . $nterce'tor a##ow ,or cro c4ttin! ,4nctiona#it( to be im'#emented e'arate#( ,rom the action a we## a the ,ramework. Fo4 can achieve the ,o##owin! 4 in! interce'tor : Providin! 're'roce in! #o!ic be,ore the action i ca##ed. Providin! 'o t'roce in! #o!ic a,ter the action i ca##ed. +atchin! e<ce'tion o that a#ternate 'roce in! can be 'er,ormed. 2an( o, the ,eat4re 'rovided in the Str4t 2 ,ramework are im'#emented 4 in! interce'tor J e<am'#e inc#4de e<ce'tion hand#in!, ,i#e 4'#oadin!, #i,ec(c#e ca##back and va#idation etc. $n ,act, a Str4t 2 ba e m4ch o, it ,4nctiona#it( on interce'tor , it i not 4n#ike#( to have 0 or 8 interce'tor a i!ned 'er action. #trut%- Fra!ew r= Interce"t r%: Str4t 2 ,ramework 'rovide a !ood #i t o, o4t-o,-the-bo< interce'tor that come 'recon,i!4red and read( to 4 e. %ew o, the im'ortant interce'tor are #i ted be#ow: # Interce"t r A De%cri"ti n N alia% 1 )##ow 'arameter to have di,,erent name a#ia e acro re@4e t . 2 chec=b , +2+ 3imited i a S4b idiar( o, 5+S 3imited 11-

) i t in mana!in! check bo<e b( addin! a 'arameter va#4e o, ,a# e ,or check bo<e that are not checked. c n)er%i nErr r 1 P#ace error in,ormation ,rom convertin! trin! to 'arameter t('e into the action? ,ie#d error . create#e%%i n D )4tomatica##( create an C55P e ion i, one doe not a#read( e<i t. debu((in( . Provide evera# di,,erent deb4!!in! creen to the deve#o'er. e,ecAndWait / Send the 4 er to an intermediar( waitin! 'a!e whi#e the action e<ec4te in the back!ro4nd. e,ce"ti n 0 2a' e<ce'tion that are thrown ,rom an action to a re 4#t, a##owin! a4tomatic e<ce'tion hand#in! via redirection. file5"l ad 8 %aci#itate ea ( ,i#e 4'#oadin!. iB3n Hee' track o, the e#ected #oca#e d4rin! a 4 er? e ion. l ((er 10 Provide im'#e #o!!in! b( o4t'4ttin! the name o, the action bein! e<ec4ted. "ara!% 11 Set the re@4e t 'arameter on the action. "re"are 12 5hi i t('ica##( 4 ed to do 're-'roce in! work, 4ch a et4' databa e connection . "r file 11 )##ow im'#e 'ro,i#in! in,ormation to be #o!!ed ,or action . %c "e 1D Store and retrieve the action? tate in the e ion or a''#ication co'e. #er)let7 nfi( 1. Provide the action with acce to vario4 erv#et-ba ed in,ormation. ti!er 1/ Provide im'#e 'ro,i#in! in,ormation in the ,orm o, how #on! the action take to e<ec4te. t =en 10 +heck the action ,or a va#id token to 'revent d4'#icate ,orm 4bmi ion. )alidati n 18 Provide va#idation 4''ort ,or action P#ea e #oook into Str4t 2 doc4mentation ,or com'#ete detai# on above mentioned interce'tor . E4t $ wi## how (o4 how to 4 e an interce'tor in !enera# in (o4r Str4t a''#ication. ' w t u%e Interce"t r%? 3et 4 ee how to 4 e an a#read( e<i tin! interce'tor to o4r PCe##o Wor#dP 'ro!ram. We wi## 4 e the ti!er interce'tor who e '4r'o e i to mea 4re how #on! it took to e<ec4te an action method. Same time $?m 4 in! "ara!% interce'tor who e '4r'o e i to end the re@4e t 'arameter to the action. Fo4 can tr( (o4r e<am'#e witho4t 4 in! thi interce'tor and (o4 wi## ,ind that na!e 'ro'ert( i not bein! et beca4 e 'arameter i not ab#e to reach to the action. We wi## kee' Ce##oWor#d)ction.*ava, web.<m#, Ce##oWor#d.* ' and inde<.* ' ,i#e a the( have been created in 9<am'#e cha'ter b4t #et 4 modi,( the %trut%+,!l ,i#e to add an interce'tor a ,o##ow V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR +2+ 3imited i a S4b idiar( o, 5+S 3imited

1D0

V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKPhe##oP methodKPe<ec4tePR Vinterce'tor-re, nameKP'aram PGR Vinterce'tor-re, nameKPtimerP GR Vre 4#t nameKP 4cce PRGCe##oWor#d.* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R "i!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Ginde<.* '. 5hi wi## !ive (o4 ,o##owin! creen: Now enter an( word in the !iven te<t bo< and c#ick Sa( Ce##o b4tton to e<ec4te the de,ined action. Now i, (o4 wi## check the #o! !enerated, (o4 wi## ,ind ,o##owin! te<t: $N%&: Server tart4' in 1.1- m 20G08G2011 8:D0:.1 P2 com.o'en (m'hon(.<work2.4ti#.#o!!in!.common .+ommon 3o!!er in,o $N%&: 9<ec4ted action 8GGhe##oWe<ec4te: took 10- m . Cere bottom #ine i bein! !enerated beca4 e o, ti!er interce'tor which i te##in! that action took tota# 10-m to be e<ec4ted. 7reate 7u%t ! Interce"t r% = in! c4 tom interce'tor in (o4r a''#ication i an e#e!ant wa( to 'rovide cro -c4ttin! a''#ication ,eat4re . +reatin! a c4 tom interce'tor i ea (J the inter,ace that need to be e<tended i the ,o##owin! Interce"t r inter,ace: '4b#ic inter,ace $nterce'tor e<tend Seria#iBab#eN void de tro(7;J void init7;J Strin! interce't7)ction$nvocation invocation; throw 9<ce'tionJ Q ) the name 4!!e t, the init7; method 'rovide a wa( to initia#iBe the interce'tor, and the de tro(7; method 'rovide a ,aci#it( ,or interce'tor c#ean4'. =n#ike action , interce'tor are re4 ed acro re@4e t and need to be thread- a,e, e 'ecia##( the interce't7; method. 5he Acti nIn) cati n ob*ect 'rovide acce to the r4ntime environment. $t a##ow acce to the action it e#, and method to invoke the action and determine whether the action ha a#read( been invoked. $, (o4 have no need ,or initia#iBation or c#ean4' code, the Ab%tractInterce"t r c#a can be e<tended. 5hi 'rovide a de,a4#t no-o'eration im'#ementation o, the init7; and de tro(7; method . 7reate Interce"t r 7la%%: 3et 4 create ,o##owin! &yInterce"t r+ja)a in Ja)a Re% urce% X %rc ,o#der: 'acka!e com.t4toria# 'oint. tr4t 2J im'ort *ava.4ti#.OJ im'ort com.o'en (m'hon(.<work2.)ction$nvocationJ im'ort com.o'en (m'hon(.<work2.interce'tor.)b tract$nterce'torJ '4b#ic c#a 2($nterce'tor e<tend )b tract$nterce'tor N

'4b#ic Strin! interce't7)ction$nvocation invocation;throw 9<ce'tionN GO #et 4 do ome 're-'roce in! OG Strin! o4t'4t K PPre-Proce in!PJ S( tem.o4t.'rint#n7o4t'4t;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D1

GO #et 4 ca## action or ne<t interce'tor OG Strin! re 4#t K invocation.invoke7;J GO #et 4 do ome 'o t-'roce in! OG o4t'4t K PPo t-Proce in!PJ S( tem.o4t.'rint#n7o4t'4t;J ret4rn re 4#tJ Q Q ) (o4 notice, act4a# action wi## be e<ec4ted 4 in! the interce'tor b( in) cati n+in) =e8; ca##. So (o4 can do ome 're-'roce in! and ome 'o t-'roce in! ba ed on (o4r re@4irement. 5he ,ramework it e#, tart the 'roce b( makin! the ,ir t ca## to the )ction$nvocation ob*ect? invoke7;. 9ach time in) =e8; i ca##ed, )ction$nvocation con 4#t it tate and e<ec4te whichever interce'tor come ne<t. When a## o, the con,i!4red interce'tor have been invoked, the invoke7; method wi## ca4 e the action it e#, to be e<ec4ted. %o##owin! di!ram how the ame conce't thro4!h a re@4e t ,#ow: 7reate Acti n 7la%%: 3et 4 create a *ava ,i#e Ce##oWor#d)ction.*ava 4nder Ja)a Re% urce% X %rc with a 'acka!e name c !+tut rial%" int+%trut%- with the content !iven be#ow. 'acka!e com.t4toria# 'oint. tr4t 2J im'ort com.o'en (m'hon(.<work2.)ctionS4''ortJ '4b#ic c#a Ce##oWor#d)ction e<tend )ctionS4''ortN 'rivate Strin! nameJ '4b#ic Strin! e<ec4te7; throw 9<ce'tion N S( tem.o4t.'rint#n7P$n ide action....P;J ret4rn P 4cce PJ Q '4b#ic Strin! !etName7; N ret4rn nameJ Q '4b#ic void etName7Strin! name; N thi .name K nameJ Q Q 5hi i a ame c#a which we have een in 'revio4 e<am'#e . We have tandard !etter and etter method ,or the PnameP 'ro'ert( and an e<ec4te method that ret4rn the trin! P 4cce P. 7reate a View 3et 4 create the be#ow * ' ,i#e 'ell W rld+j%" in the Web+ontent ,o#der in (o4r ec#i' e 'ro*ect. VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8P LR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! P LR Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR VGheadR Vbod(R Ce##o Wor#d, V :'ro'ert( va#4eKPnamePGR VGbod(R VGhtm#R 7reate !ain "a(e: We a# o need to create inde,+j%" in the Web+ontent ,o#der. 5hi ,i#e wi## erve a the initia# action ="3 where a 4 er can c#ick to te## the Str4t 2 ,ramework to ca## the a de,ined method o, the Ce##oWor#d)ction c#a and render the Ce##oWor#d.* ' view. VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D2

'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR VGheadR Vbod(R Vh1RCe##o Wor#d %rom Str4t 2VGh1R V,orm actionKPhe##oPR V#abe# ,orKPnamePRP#ea e enter (o4r nameVG#abe#RVbrGR Vin'4t t('eKPte<tP nameKPnamePGR Vin'4t t('eKP 4bmitP va#4eKPSa( Ce##oPGR VG,ormR VGbod(R VGhtm#R 5he hell action de,ined in the above view ,i#e wi## be ma''ed to the Ce##oWor#d)ction c#a and it e<ec4te method 4 in! tr4t .<m# ,i#e. 7 nfi(urati n File% Now we need to re!i ter o4r interce'tor and then ca## it a we had ca##ed de,a4#t interce'tor in 'revio4 e<am'#e. 5o re!i ter a new#( de,ined interce'tor, the Vinterce'tor R...VGinterce'tor R ta! are '#aced direct#( 4nder the V'acka!eR ta! in %trut%+,!l ,i#e. Fo4 can ki' thi te' ,or a de,a4#t interce'tor a we did in o4r 'revio4 e<am'#e. E4t here #et 4 re!i ter and 4 e it a ,o##ow : V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vinterce'tor R Vinterce'tor nameKPm(interce'torP GR VGinterce'tor R Vaction nameKPhe##oP methodKPe<ec4tePR Vinterce'tor-re, nameKP'aram PGR Vinterce'tor-re, nameKPm(interce'torP GR Vre 4#t nameKP 4cce PRGCe##oWor#d.* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R $t ho4#d be noted that (o4 can re!i ter more than one interce'tor in ide W"ac=a(eX ta! and ame time (o4 can ca## more than one interce'tor in ide the Wacti nX ta!. Fo4 can ca## ame interce'tor with the di,,erent action . 5he web.<m# ,i#e need to be created 4nder the W9E-$N% ,o#der 4nder Web+ontent a ,o##ow : V[<m# ver ionKP1.0P encodin!KP=5%-8P[R Vweb-a'' <m#n :< iKPhtt':GGwww.w1.or!G2001GA23Schema-in tanceP <m#n KPhtt':GG*ava. 4n.comG<m#Gn G*avaeeP <m#n :webKPhtt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T2T..< dP < i: chema3ocationKPhtt':GG*ava. 4n.comG<m#Gn G*avaee htt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T1T0.< dP R +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D1

Vdi '#a(-nameRStr4t 2VGdi '#a(-nameR Vwe#come-,i#e-#i tR Vwe#come-,i#eRinde<.* 'VGwe#come-,i#eR VGwe#come-,i#e-#i tR V,i#terR V,i#ter-nameR tr4t 2VG,i#ter-nameR V,i#ter-c#a R or!.a'ache. tr4t 2.di 'atcher.%i#terDi 'atcher VG,i#ter-c#a R VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR tr4t 2VG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R VGweb-a''R "i!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Ginde<.* '. 5hi wi## !ive (o4 ,o##owin! creen: Now enter an( word in the !iven te<t bo< and c#ick Sa( Ce##o b4tton to e<ec4te the de,ined action. Now i, (o4 wi## check the #o! !enerated, (o4 wi## ,ind ,o##owin! te<t at the bottom: Pre-Proce in! $n ide action.... Po t-Proce in! #tac=in( !ulti"le Interce"t r%: ) (o4 can ima!ine, havin! to con,i!4re m4#ti'#e interce'tor ,or each action wo4#d @4ick#( become e<treme#( 4nmana!eab#e. %or thi rea on, interce'tor are mana!ed with interce'tor tack . Cere i an e<am'#e, direct#( ,rom the tr4t -de,a4#t.<m# ,i#e: Vinterce'tor- tack nameKPba icStackPR Vinterce'tor-re, nameKPe<ce'tionPGR Vinterce'tor-re, nameKP erv#et-con,i!PGR Vinterce'tor-re, nameKP're'arePGR Vinterce'tor-re, nameKPcheckbo<PGR Vinterce'tor-re, nameKP'aram PGR Vinterce'tor-re, nameKPconver ion9rrorPGR VGinterce'tor- tackR 5he above take i ca##ed ba%ic#tac= and can be 4 ed in (o4r con,i!4ration a hown be#ow. 5hi con,i!4ration node i '#aced 4nder the V'acka!e ...GR node. 9ach Vinterce'tor-re, ...GR ta! re,erence either an interce'tor or an interce'tor tack that ha been con,i!4red be,ore the c4rrent interce'tor tack. $t i there,ore ver( im'ortant to en 4re that the name i 4ni@4e acro a## interce'tor and interce'tor tack con,i!4ration when con,i!4rin! the initia# interce'tor and interce'tor tack . We have a#read( een how to a''#( interce'tor to the action, a''#(in! interce'tor tack i no di,,erent. $n ,act, we 4 e e<act#( the ame ta!: Vaction nameKPhe##oP R Vinterce'tor-re, nameKPba icStackPGR Vre 4#tRview.* 'VGre 4#tR VGaction 5he above re!i tration o, Pba icStackP wi## re!i ter com'#ete take o, a## the i< interce'tor with he##o action. 5hi ho4#d be noted that interce'tor are e<ec4ted in the order, in which the( have been con,i!4red. %or e<am'#e, in above ca e, e<ce'tion wi## be e<ec4ted ,ir t, econd wo4#d be erv#etcon,i! and o on. #trut% - Re%ult% and Re%ult *y"e% ) mentioned 'revio4 #(, the Wre%ult%X ta! '#a( the ro#e o, a )iew in the Str4t 2 2V+ ,ramework. 5he action i re 'on ib#e ,or e<ec4tin! the b4 ine #o!ic. 5he ne<t te' a,ter e<ec4tin! the b4 ine #o!ic i to di '#a( the view 4 in! the Wre%ult%X ta!. +2+ 3imited i a S4b idiar( o, 5+S 3imited 1DD

&,ten there i ome navi!ation r4#e attached with the re 4#t . %or e<am'#e, i, the action method i to a4thenticate a 4 er, there are three 'o ib#e o4tcome . 7a; S4cce ,4# 3o!in 7b; =n 4cce ,4# 3o!in $ncorrect 4 ername or 'a word 7c; )cco4nt 3ocked. $n thi cenario, the action method wi## be con,i!4red with the three 'o ib#e o4tcome trin! and three di,,erent view to render the o4tcome. We have a#read( een thi in the 'revio4 e<am'#e . E4t, Str4t 2 doe not tie (o4 4' with 4 in! 6SP a the view techno#o!(. ),tera## the who#e '4r'o e o, the 2V+ 'aradi!m i to kee' the #a(er e'arate and hi!h#( con,i!4rab#e. %or e<am'#e, ,or a Web2.0 c#ient, (o4 ma( want to ret4rn A23 or 6S&N a the o4t'4t. $n thi ca e, (o4 co4#d create a new re 4#t t('e ,or A23 or 6S&N and achieve thi . Str4t come with a n4mber o, 'rede,ined re%ult ty"e% and whatever we?ve a#read( een that wa the de,a4#t re 4#t t('e di%"atcher, which i 4 ed to di 'atch to 6SP 'a!e . Str4t a##ow (o4 to 4 e other mark4' #an!4a!e ,or the view techno#o!( to 're ent the re 4#t and 'o'4#ar choice inc#4de Vel city, Free!a=er, 0#4* and *ile%. *he di%"atcher re%ult ty"e: 5he di%"atcher re 4#t t('e i the de,a4#t t('e, and i 4 ed i, no other re 4#t t('e i 'eci,ied. $t? 4 ed to ,orward to a erv#et, 6SP, C523 'a!e, and o on, on the erver. $t 4 e the ,e6uest9ispatcher#)or2ard ! method. We aw the P horthandP ver ion in o4r ear#ier e<am'#e , where we 'rovided a 6SP 'ath a the bod( o, the re 4#t ta!. Vre 4#t nameKP 4cce PR GCe##oWor#d.* ' VGre 4#tR We can a# o 'eci,( the 6SP ,i#e 4 in! a V'aram nameKP#ocationPR ta! within the Vre 4#t...R e#ement a ,o##ow : Vre 4#t nameKP 4cce P t('eKPdi 'atcherPR V'aram nameKP#ocationPR GCe##oWor#d.* ' VG'aram R VGre 4#tR We can a# o 4''#( a "ar%e 'arameter, which i tr4e b( de,a4#t. 5he 'ar e 'arameter determine whether or not the #ocation 'arameter wi## be 'ar ed ,or &GN3 e<'re ion . *he Free&a=er re%ult ty"e: $n thi e<am'#e we are !oin! to ee how we can 4 e Free&a=er a the view techno#o!(. %reemaker i a 'o'4#ar tem'#atin! en!ine that i 4 ed to !enerate o4t'4t 4 in! 'rede,ined tem'#ate . 3et 4 create a %reemaker tem'#ate ,i#e ca##ed hell +f! with the ,o##owin! content : Ce##o Wor#d MNnameQ Cere above ,i#e i a tem'#ate where na!e i a 'aramter which wi## be 'a ed ,rom o4t ide 4 in! the de,ined action. Fo4 wi## kee' thi ,i#e in (o4r +3)SSP)5C. Ne<t, #et 4 modi,( the %trut%+,!l to 'eci,( the re 4#t a ,o##ow : V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKPhe##oP methodKPe<ec4tePR Vre 4#t nameKP 4cce P t('eKP,reemarkerPR V'aram nameKP#ocationPRGhe##o.,mVG'aramR VGre 4#tR VGactionR VG'acka!eR VG tr4t R +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D.

3et 4 kee' o4r Ce##oWor#d)ction.*ava, Ce##oWor#d)ction.* ' and inde<.* ' ,i#e a we have created them in e<am'#e cha'ter. Now "i!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Ginde<.* '. 5hi wi## !ive (o4 ,o##owin! creen: 9nter a va#4e PStr4t 2P and 4bmit the 'a!e. Fo4 ho4#d ee the ne<t 'a!e ) (o4 can ee, thi i e<act#( ame a the 6SP view e<ce't that we are not tied to 4 in! 6SP a the view techno#o!(. We have 4 ed %reemaker in thi e<am'#e. *he redirect re%ult ty"e: 5he redirect re 4#t t('e ca## the tandard respo$se#se$d,edirect ! method, ca4 in! the brow er to create a new re@4e t to the !iven #ocation. We can 'rovide the #ocation either in the bod( o, the Vre 4#t...R e#ement or a a V'aram nameKP#ocationPR e#ement. "edirect a# o 4''ort the "ar%e 'arameter. Cere? an e<am'#e con,i!4red 4 in! A23: Vaction nameKPhe##oP methodKPe<ec4tePR Vre 4#t nameKP 4cce P t('eKPredirectPR V'aram nameKP#ocationPR GNewWor#d.* ' VG'aram R VGre 4#tR VGactionR So *4 t modi,( (o4r tr4t .<m# ,i#e to de,ine redirect t('e a mentioned above and create a new ,i#e NewWor#d.*'! where (o4 wi## be redirected whenever he##o action wi## ret4rn 4cce . Fo4 can check Str4t 2 "edirect )ction e<am'#e ,or better 4nder tandin!. #trut% - Value #tac=/OCN4 *he Value #tac=: 5he va#4e tack i a et o, evera# ob*ect which kee' the ,o##owin! ob*ect in the 'rovided order: # Object% A De%cri"ti n N *e!" rary Object% 1 5here are vario4 tem'orar( ob*ect which are created d4rin! e<ec4tion o, a 'a!e. %or e<am'#e the c4rrent iteration va#4e ,or a co##ection bein! #oo'ed over in a 6SP ta!. *he & del Object 2 $, (o4 are 4 in! mode# ob*ect in (o4r tr4t a''#ication, the c4rrent mode# ob*ect i '#aced be,ore the action on the va#4e tack *he Acti n Object 1 5hi wi## be the c4rrent action ob*ect which i bein! e<ec4ted. Na!ed Object% D 5he e ob*ect inc#4de ba''#ication, b e ion, bre@4e t, battr and b'arameter and re,er to the corre 'ondin! erv#et co'e 5he va#4e tack can be acce ed via the ta! 'rovided ,or 6SP, Ve#ocit( or %reemarker. 5here are vario4 ta! which we wi## t4d( in e'arate cha'ter , are 4 ed to !et and et tr4t 2.0 va#4e tack. Fo4 can !et va#4eStack ob*ect in ide (o4r action a ,o##ow : )ction+onte<t.!et+onte<t7;.!etVa#4eStack7; &nce (o4 have a Va#4eStack ob*ect, (o4 can 4 e ,o##owin! method to mani'4#ate that ob*ect: # Value#tac= &eth d% A De%cri"ti n N Object findValue8#trin( e,"r; 1 %ind a va#4e b( eva#4atin! the !iven e<'re ion a!ain t the tack in the de,a4#t earch order. 7 !" undR t (etR t8; 2 Get the +om'o4nd"oot which ho#d the ob*ect '4 hed onto the tack. Object "ee=8; 1 Get the ob*ect on the to' o, the tack witho4t chan!in! the tack. +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D/

Object " "8; Get the ob*ect on the to' o, the tack and remove it ,rom the tack. ) id "u%h8Object ; . P4t thi ob*ect onto the to' o, the tack. ) id %et8#trin( =ey, Object ; / Set an ob*ect on the tack with the !iven ke( o it i retrievab#e b( ,indVa#4e7ke(,...; ) id %etDefault*y"e87la%% default*y"e; 0 Set the de,a4#t t('e to convert to i, no t('e i 'rovided when !ettin! a va#4e. ) id %etValue8#trin( e,"r, Object )alue; 8 )ttem't to et a 'ro'ert( on a bean in the tack with the !iven e<'re ion 4 in! the de,a4#t earch order. int %i>e8; Get the n4mber o, ob*ect in the tack. *he OCN4: *he Object$Cra"h Na)i(ati n 4an(ua(e 7&GN3; i a 'ower,4# e<'re ion #an!4a!e that i 4 ed to re,erence and mani'4#ate data on the Va#4eStack. &GN3 a# o he#' in data tran ,er and t('e conver ion. 5he &GN3 i ver( imi#ar to the 6SP 9<'re ion 3an!4a!e. &GN3 i ba ed on the idea o, havin! a root or de,a4#t ob*ect within the conte<t. 5he 'ro'ertie o, the de,a4#t or root ob*ect can be re,erenced 4 in! the mark4' notation, which i the 'o4nd (mbo#. ) mentioned ear#ier, &GN3 i ba ed on a conte<t and Str4t b4i#d an )ction+onte<t ma' ,or 4 e with &GN3. 5he )ction+onte<t ma' con i t o, the ,o##owin!: a""licati n - a''#ication co'ed variab#e %e%%i n - e ion co'ed variab#e r t / )alue %tac= - a## (o4r action variab#e are tored here re@ue%t - re@4e t co'ed variab#e "ara!eter% - re@4e t 'arameter atribute% - the attrib4te tored in 'a!e, re@4e t, e ion and a''#ication co'e $t i im'ortant to 4nder tand that the )ction ob*ect i a#wa( avai#ab#e in the va#4e tack. So, there,ore i, (o4r )ction ob*ect ha 'ro'ertie < and ( there are readi#( avai#ab#e ,or (o4 to 4 e. &b*ect in the )ction+onte<t are re,erred 4 in! the 'o4nd (mbo#, however, the ob*ect in the va#4e tack can be direct#( re,erenced, ,or e<am'#e i, e!"l yee i a 'ro'ert( o, an action c#a then it can !e re,erenced a ,o##ow : V :'ro'ert( va#4eKPnamePGR in tead o, V :'ro'ert( va#4eKPbnamePGR $, (o4 have an attrib4te in e ion ca##ed P#o!inP (o4 can retrieve it a ,o##ow : V :'ro'ert( va#4eKPb e ion.#o!inPGR &GN3 a# o 4''ort dea#in! with co##ection - name#( 2a', 3i t and Set. %or e<am'#e to di '#a( a dro'down #i t o, co#or , (o4 co4#d do: V : e#ect nameKPco#orP #i tKPN?red?,?(e##ow?,?!reen?QP GR 5he &GN3 e<'re ion i c#ever to inter'ret the PredP,P(e##owP,P!reenP a co#o4r and b4i#d a #i t ba ed on that. 5he &GN3 e<'re ion wi## be 4 ed e<ten ive#( in the ne<t cha'ter when we wi## t4d( di,,erent ta! . So rather than #ookin! at them in i o#ation, #et 4 #ook at it 4 in! ome e<am'#e in the %orm 5a! G +ontro# 5a! G Data 5a! and )*a< 5a! ection. Value#tac=/OCN4 E,a!"le: 7reate Acti n: 3et 4 con ider ,o##owin! action c#a where we are acce in! va#4eStack and then ettin! ,ew ke( which we wi## acce 4 in! &GN3 in o4r view ie. 6SP 'a!e. 'acka!e com.t4toria# 'oint. tr4t 2J D im'ort *ava.4ti#.OJ im'ort com.o'en (m'hon(.<work2.4ti#.Va#4eStackJ im'ort com.o'en (m'hon(.<work2.)ction+onte<tJ +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D0

im'ort com.o'en (m'hon(.<work2.)ctionS4''ortJ '4b#ic c#a Ce##oWor#d)ction e<tend )ctionS4''ortN 'rivate Strin! nameJ '4b#ic Strin! e<ec4te7; throw 9<ce'tion N Va#4eStack tack K )ction+onte<t.!et+onte<t7;.!etVa#4eStack7;J 2a'VStrin!, &b*ectR conte<t K new Ca h2a'VStrin!, &b*ectR7;J conte<t.'4t7Pke(1P, new Strin!7P5hi i ke(1P;;J conte<t.'4t7Pke(2P, new Strin!7P5hi i ke(2P;;J tack.'4 h7conte<t;J S( tem.o4t.'rint#n7PSiBe o, the va#4eStack: P > tack. iBe7;;J ret4rn P 4cce PJ Q '4b#ic Strin! !etName7; N ret4rn nameJ Q '4b#ic void etName7Strin! name; N thi .name K nameJ Q Q )ct4a##(, Str4t 2 add (o4r action to the to' o, the va#4eStack when e<ec4ted. So, the 4 4a# wa( to '4t t4,, on the Va#4e Stack i to add !etter G etter ,or the va#4e to (o4r )ction c#a and then 4 e V :'ro'ert(R ta! to acce the va#4e . E4t $?m howin! (o4 how e<act#( )ction+onte<t and Va#4eStack work in tr4t . 7reate View% 3et 4 create the be#ow * ' ,i#e 'ell W rld+j%" in the Web+ontent ,o#der in (o4r ec#i' e 'ro*ect. 5hi view wi## be di '#a(ed in ca e action ret4rn 4cce : VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8P LR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! P LR Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR VGheadR Vbod(R 9ntered va#4e : V :'ro'ert( va#4eKPnamePGRVbrGR Va#4e o, ke( 1 : V :'ro'ert( va#4eKPke(1P GRVbrGR Va#4e o, ke( 2 : V :'ro'ert( va#4eKPke(2P GR VbrGR VGbod(R VGhtm#R We a# o need to create inde,+j%" in the Web+ontent ,o#der who e content i a ,o##ow : VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR VGheadR Vbod(R Vh1RCe##o Wor#d %rom Str4t 2VGh1R V,orm actionKPhe##oPR V#abe# ,orKPnamePRP#ea e enter (o4r nameVG#abe#RVbrGR Vin'4t t('eKPte<tP nameKPnamePGR +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D8

Vin'4t t('eKP 4bmitP va#4eKPSa( Ce##oPGR VG,ormR VGbod(R VGhtm#R 7 nfi(urati n File% %o##owin! i the content o, %trut%+,!l ,i#e: V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKPhe##oP methodKPe<ec4tePR Vre 4#t nameKP 4cce PRGCe##oWor#d.* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R %o##owin! i the content o, web+,!l ,i#e: V[<m# ver ionKP1.0P encodin!KP=5%-8P[R Vweb-a'' <m#n :< iKPhtt':GGwww.w1.or!G2001GA23Schema-in tanceP <m#n KPhtt':GG*ava. 4n.comG<m#Gn G*avaeeP <m#n :webKPhtt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T2T..< dP < i: chema3ocationKPhtt':GG*ava. 4n.comG<m#Gn G*avaee htt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T1T0.< dP R Vdi '#a(-nameRStr4t 2VGdi '#a(-nameR Vwe#come-,i#e-#i tR Vwe#come-,i#eRinde<.* 'VGwe#come-,i#eR VGwe#come-,i#e-#i tR V,i#terR V,i#ter-nameR tr4t 2VG,i#ter-nameR V,i#ter-c#a R or!.a'ache. tr4t 2.di 'atcher.%i#terDi 'atcher VG,i#ter-c#a R VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR tr4t 2VG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R VGweb-a''R "i!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Ginde<.* '. 5hi wi## !ive (o4 ,o##owin! creen: Now enter an( word in the !iven te<t bo< and c#ick PSa( Ce##oP b4tton to e<ec4te the de,ined action. Now i, (o4 wi## check the #o! !enerated, (o4 wi## ,ind ,o##owin! te<t at the bottom: SiBe o, the va#4eStack: 1 +2+ 3imited i a S4b idiar( o, 5+S 3imited 1D-

and thi wi## di '#a( ,o##owin! creen, which wi## di '#a( whatever va#4e (o4 wi## enter and va#4e o, ke(1 and ke(2 which we had '4t on Va#4eStack. #trut% - File 5"l ad% 5he Str4t 2 ,ramework 'rovide b4i#t-in 4''ort ,or 'roce in! ,i#e 4'#oad 4 in! P%orm-ba ed %i#e ='#oad in C523P. When a ,i#e i 4'#oaded it wi## t('ica##( be tored in a tem'orar( director( and the( ho4#d be 'roce ed or moved b( (o4r )ction c#a to a 'ermanent director( to en 4re the data i not #o t. N te that %er)er% !ay ha)e a %ecurity " licy in "lace that "r hibit% y u fr ! writin( t direct rie% ther than the te!" rary direct ry and the direct rie% that bel n( t y ur web a""licati n+ %i#e 4'#oadin! in Str4t i 'o ib#e thro4!h a 're-de,ined interce'tor ca##ed File5"l ad interce'tor which i avai#ab#e thro4!h the or!.a'ache. tr4t 2.interce'tor.%i#e='#oad$nterce'tor c#a and inc#4ded a 'art o, the default#tac=. Sti## (o4 can 4 e that in (o4r tr4t .<m# to et vario4 'aramter a we wi## ee be#ow. 7reate )iew file%: 3et 4 tart with creatin! o4r view which wi## be re@4ired to brow e and 4'#oad a e#ected ,i#e. So #et 4 create a inde,+j%" with '#ain C523 4'#oad ,orm that a##ow the 4 er to 4'#oad a ,i#e: VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eR%i#e ='#oadVGtit#eR VGheadR Vbod(R V,orm actionKP4'#oadP methodKP'o tP enct('eKPm4#ti'artG,orm-dataPR V#abe# ,orKPm(%i#ePR='#oad (o4r ,i#eVG#abe#R Vin'4t t('eKP,i#eP nameKPm(%i#eP GR Vin'4t t('eKP 4bmitP va#4eKP='#oadPGR VG,ormR VGbod(R VGhtm#R 5here are co4'#e o, 'oint worth notin! in the above e<am'#e. %ir t o, a##, the ,orm? enct('e i et to !ulti"art/f r!$data. 5hi ho4#d be et o that ,i#e 4'#oad are hand#ed 4cce ,4##( b( the ,i#e 4'#oad interce'tor. 5he ne<t 'oint notin! i the ,orm? action method u"l ad and the name o, the ,i#e 4'#oad ,ie#d - which i !yFile. We need thi in,ormation to create the action method and the tr4t con,i!4ration. Ne<t #et 4 create a im'#e * ' ,i#e %ucce%%+j%" to di '#a( the o4tcome o, o4r ,i#e 4'#oad in ca e it become 4cce . VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8P LR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! P LR Vhtm#R VheadR Vtit#eR%i#e ='#oad S4cce VGtit#eR VGheadR Vbod(R Fo4 have 4cce ,4##( 4'#oaded V :'ro'ert( va#4eKPm(%i#e%i#eNamePGR VGbod(R VGhtm#R %o##owin! wi## be the re 4#t ,i#e err r+j%" in ca e there i ome error in 4'#oadin! the ,i#e: VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8P LR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! P LR Vhtm#R VheadR Vtit#eR%i#e ='#oad 9rrorVGtit#eR VGheadR +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.0

Vbod(R 5here ha been an error in 4'#oadin! the ,i#e. VGbod(R VGhtm#R 7reate acti n cla%%: Ne<t #et 4 create a 6ava c#a ca##ed u"l adFile+ja)a which wi## take care o, 4'#oadin! ,i#e and torin! that ,i#e at a ec4re #ocation: 'acka!e com.t4toria# 'oint. tr4t 2J im'ort *ava.io.%i#eJ im'ort or!.a'ache.common .io.%i#e=ti# J im'ort *ava.io.$&9<ce'tionJ im'ort com.o'en (m'hon(.<work2.)ctionS4''ortJ '4b#ic c#a 4'#oad%i#e e<tend )ctionS4''ortN 'rivate %i#e m(%i#eJ 'rivate Strin! m(%i#e+ontent5('eJ 'rivate Strin! m(%i#e%i#eNameJ 'rivate Strin! de tPathJ '4b#ic Strin! e<ec4te7; N GO +o'( ,i#e to a a,e #ocation OG de tPath K P+:Ga'ache-tomcat-/.0.11GworkGPJ tr(N S( tem.o4t.'rint#n7PSrc %i#e name: P > m(%i#e;J S( tem.o4t.'rint#n7PD t %i#e name: P > m(%i#e%i#eName;J %i#e de t%i#e K new %i#e7de tPath, m(%i#e%i#eName;J %i#e=ti# .co'(%i#e7m(%i#e, de t%i#e;J Qcatch7$&9<ce'tion e;N e.'rintStack5race7;J ret4rn 9""&"J Q ret4rn S=++9SSJ Q '4b#ic %i#e !et2(%i#e7; N ret4rn m(%i#eJ Q '4b#ic void et2(%i#e7%i#e m(%i#e; N thi .m(%i#e K m(%i#eJ Q '4b#ic Strin! !et2(%i#e+ontent5('e7; N ret4rn m(%i#e+ontent5('eJ Q '4b#ic void et2(%i#e+ontent5('e7Strin! m(%i#e+ontent5('e; N thi .m(%i#e+ontent5('e K m(%i#e+ontent5('eJ Q '4b#ic Strin! !et2(%i#e%i#eName7; N ret4rn m(%i#e%i#eNameJ Q '4b#ic void et2(%i#e%i#eName7Strin! m(%i#e%i#eName; N thi .m(%i#e%i#eName K m(%i#e%i#eNameJ Q Q +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.1

5he u"l adFile+ja)a i a ver( im'#e c#a . 5he im'ortant thin! to note i that the %i#e='#oad interce'tor a#on! with the Parameter $ntercet'or doe a## the heav( #i,tin! ,or 4 . 5he %i#e='#oad interce'tor make three 'arameter avai#ab#e ,or (o4 b( de,a4#t. 5he( are named in the ,o##owin! 'attern: 9y ur file na!e "ara!eter: - 5hi i the act4a# ,i#e that the 4 er ha 4'#oaded. $n thi e<am'#e it wi## be Pm(%i#eP 9y ur file na!e "ara!eter:7 ntent*y"e - 5hi i the content t('e o, the ,i#e that wa 4'#oaded. $n thi e<am'#e it wi## be Pm(%i#e+ontent5('eP 9y ur file na!e "ara!eter:FileNa!e - 5hi i the name o, the ,i#e that wa 4'#oaded. $n thi e<am'#e it wi## be Pm(%i#e%i#eNameP 5he three 'arameter are avai#ab#e ,or 4 , thank to the Str4t $nterce'tor . )## we have to do i to create three 'arameter with the correct name in o4r )ction c#a and a4tomaica##( the e variab#e are a4to wired ,or 4 . So, in the above e<am'#e, we have three 'arameter and an action method that im'#( ret4rn P 4cce P i, ever(thin! !oe ,ine otherwi e it ret4rn PerrorP. 7 nfi(urati n file%: %o##owin! are the Str4t 2 con,i!4ration 'ro'ertie that contro# ,i#e 4'#oadin! 'roce : # Pr "ertie% A De%cri"ti n N %trut%+!ulti"art+!a,#i>e 1 5he ma<im4m iBe 7in b(te ; o, a ,i#e to be acce'ted a a ,i#e 4'#oad. De,a4#t i 2.02. %trut%+!ulti"art+"ar%er 2 5he #ibrar( 4 ed to 4'#oad the m4#ti'art ,orm. E( de,a4#t i ja=arta %trut%+!ulti"art+%a)eDir 1 5he #ocation to tore the tem'orar( ,i#e. E( de,a4#t i *ava<. erv#et.conte<t.tem'dir. $n order to chan!e an( o, the e ettin! (o4 can 4 e c n%tant ta! in (o4r a''#ication tr4t .<m# ,i#e, a $ did to chan!e the ma<im4m iBe o, a ,i#e to be 4'#oaded. 3et 4 have o4r %trut%+,!l a ,o##ow : V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR Vcon tant nameKP tr4t .m4#ti'art.ma<SiBeP va#4eKP1000000P GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKP4'#oadP R Vre 4#t nameKP 4cce PRG 4cce .* 'VGre 4#tR Vre 4#t nameKPerrorPRGerror.* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R Eeca4 e File5"l ad interce'tor i a 'art o, the de,a4#tStack o, interce'tor , we do not need to con,i!4re it e<'#icit(. E4t (o4 can add Vinterce'tor-re,R ta! in ide VactionR. 5he ,i#e='#oad interce'tor take two 'arameter 8a; !a,i!u!#i>e and 8b; all wed*y"e%. 5he ma<im4mSiBe 'arameter et the ma<im4m ,i#e iBe a##owed 7the de,a4#t i a''ro<imate#( 22E;.5he a##owed5('e 'arameter i a comma- e'arated #i t o, acce'ted content 72$29; t('e a hown be#ow: Vaction nameKP4'#oadP R Vinterce'tor-re, nameKPba icStackPR Vinterce'tor-re, nameKP,i#e='#oadPR V'aram nameKPa##owed5('e PRima!eG*'e!,ima!eG!i,VG'aramR VGinterce'tor-re,R Vre 4#t nameKP 4cce PRG 4cce .* 'VGre 4#tR Vre 4#t nameKPerrorPRGerror.* 'VGre 4#tR VGactionR +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.2

%o##owin! i the content o, web+,!l ,i#e: V[<m# ver ionKP1.0P encodin!KP=5%-8P[R Vweb-a'' <m#n :< iKPhtt':GGwww.w1.or!G2001GA23Schema-in tanceP <m#n KPhtt':GG*ava. 4n.comG<m#Gn G*avaeeP <m#n :webKPhtt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T2T..< dP < i: chema3ocationKPhtt':GG*ava. 4n.comG<m#Gn G*avaee htt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T1T0.< dP R Vdi '#a(-nameRStr4t 2VGdi '#a(-nameR Vwe#come-,i#e-#i tR Vwe#come-,i#eRinde<.* 'VGwe#come-,i#eR VGwe#come-,i#e-#i tR V,i#terR V,i#ter-nameR tr4t 2VG,i#ter-nameR V,i#ter-c#a R or!.a'ache. tr4t 2.di 'atcher.%i#terDi 'atcher VG,i#ter-c#a R VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR tr4t 2VG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R VGweb-a''R Now "i!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2G4'#oad.* '. 5hi wi## !ive (o4 ,o##owin! creen: Now e#ect a ,i#e P+ontact .t<tP 4 in! Erow e b4tton and c#ick 4'#oad b4tton which wi## 4'#oad the ,i#e on (o4r erve and (o4 ho4#d ee the net 'a!e. Fo4 can check the 4'#oaded ,i#e ho4#d be aved in +:Ia'ache-tomcat-/.0.11Iwork. Note that the %i#e='#oad $nterce'tor de#ete the 4'#oaded ,i#e a4tomatica##( o (o4 wo4#d have to ave 4'#oaded ,i#e 'ro!ramatica##( at ome #ocation be,ore it !et de#eted. Err r &e%%a(e%: 5he ,i#e='#aod interce'tor 4 e evera# de,a4#t error me a!e ke( : # Err r &e%%a(e <ey A De%cri"ti n N %trut%+!e%%a(e%+err r+u"l adin( 1 ) !enera# error that occ4r when the ,i#e co4#d not be 4'#oaded. %trut%+!e%%a(e%+err r+file+t +lar(e 2 &cc4r when the 4'#oaded ,i#e i too #ar!e a 'eci,ied b( ma<im4mSiBe. %trut%+!e%%a(e%+err r+c ntent+ty"e+n t+all wed 1 &cc4r when the 4'#oaded ,i#e doe not match the e<'ected content t('e 'eci,ied. Fo4 can override the te<t o, the e me a!e in Web7 ntent/WE6$INF/cla%%e%/!e%%a(e%+"r "ertie% re o4rce ,i#e . #trut% - Databa%e Acce%% 5hi cha'ter wi## teah (o4 how to acce a databa e 4 in! Str4t 2 in im'#e te' . Str4t i a 2V+ ,ramework and not a databa e ,ramework b4t it 'rovide e<ce##ent 4''ort ,or 6P)GCibernate inte!ration. We ha## #ook at the hibernate inte!ration in a #ater cha'ter, b4t in thi cha'ter we ha## 4 e '#ain o#d 6DE+ to acce the databa e. 5he ,ir t te' in thi cha'ter i to et4' and 'rime o4r databa e. $ am 4 in! 2(S]3 a m( databa e ,or thi e<am'#e. $ have 2(S]3 in ta##ed on m( machine and $ have created a new databa e ca##ed +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.1

P tr4t Tt4toria#P. $ have created a tab#e ca##ed l (in and 'o'4#ated it with ome va#4e . Ee#ow i the cri't $ 4 ed to create and 'o'4#ate the tab#e. 2( 2FS]3 databa e ha the de,a4#t 4 ername ProotP and Proot121P 'a word +"9)59 5)E39 \ tr4t Tt4toria#\.\#o!in\ 7 \4 er\ V)"+C)"7 10 ; N&5 N=33 , \'a word\ V)"+C)"7 10 ; N&5 N=33 , \name\ V)"+C)"7 20 ; N&5 N=33 , P"$2)"F H9F 7 \4 er\ ; ; 9NG$N9 K $nnoDEJ $NS9"5 $N5& \ tr4t Tt4toria#\.\#o!in\ 7\4 er\, \'a word\, \name\; V)3=9S 7? cott?, ?nav(?, ?Scott E4r!emott?;J Ne<t te' i to down#oad the 2(S]3 +onnector *ar ,i#e and '#acin! thi ,i#e in the W9E-$N%I#ib ,o#der o, (o4r 'ro*ect. ),ter we have done thi , we are now read( to create the action c#a . 7reate Acti n: 5he action c#a ha the 'ro'ertie corre 'ondin! to the co#4mn in the databa e tab#e. We have u%er, "a%%w rd and na!e a Strin! attrib4e . $n the action method, we 4 e the 4 er and 'a word 'arameter to check i, the 4 er e<i t , i, o , we di '#a( the 4 er name in the ne<t creen. $, the 4 er ha entered wron! in,ormation, we end them to the #o!in creen a!ain. %o##owin! i the content o, 4 (inActi n+ja)a ,i#e: 'acka!e com.t4toria# 'oint. tr4t 2J im'ort *ava. im'ort *ava. im'ort *ava. im'ort *ava. @#.+onnectionJ @#.Driver2ana!erJ @#.Pre'aredStatementJ @#."e 4#tSetJ

im'ort com.o'en (m'hon(.<work2.)ctionS4''ortJ '4b#ic c#a 3o!in)ction e<tend )ctionS4''ort N

'rivate Strin! 4 erJ 'rivate Strin! 'a wordJ 'rivate Strin! nameJ '4b#ic Strin! e<ec4te7; N Strin! ret K 9""&"J +onnection conn K n4##J tr( N Strin! ="3 K P*dbc:m( @#:GG#oca#ho tG tr4t Tt4toria#PJ +#a .,orName7Pcom.m( @#.*dbc.DriverP;J conn K Driver2ana!er.!et+onnection7="3, ProotP, Proot121P;J Strin! @# K PS939+5 name %"&2 #o!in WC9"9PJ @#>KP 4 er K [ )ND 'a word K [PJ Pre'aredStatement ' K conn.'re'areStatement7 @#;J ' . etStrin!71, 4 er;J ' . etStrin!72, 'a word;J "e 4#tSet r K ' .e<ec4te]4er(7;J whi#e 7r .ne<t7;; N name K r .!etStrin!71;J ret K S=++9SSJ Q Q catch 79<ce'tion e; N ret K 9""&"J Q ,ina##( N i, 7conn WK n4##; N tr( N conn.c#o e7;J +2+ 3imited i a S4b idiar( o, 5+S 3imited

1.D

Q catch 79<ce'tion e; N Q Q Q ret4rn retJ Q '4b#ic Strin! !et= er7; N ret4rn 4 erJ Q '4b#ic void et= er7Strin! 4 er; N thi .4 er K 4 erJ Q '4b#ic Strin! !etPa ret4rn 'a wordJ Q word7; N

'4b#ic void etPa word7Strin! 'a thi .'a word K 'a wordJ Q '4b#ic Strin! !etName7; N ret4rn nameJ Q '4b#ic void etName7Strin! name; N thi .name K nameJ Q

word; N

Q 7reate !ain "a(e: Now, #et 4 create a 6SP ,i#e inde,+j%" to co##ect the 4 ername and 'a word. 5hi 4 ername and 'a word wi## be checked a!ain t the databa e. VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eR3o!inVGtit#eR VGheadR Vbod(R V,orm actionKP#o!inactionP methodKP'o tPR = er:VbrGRVin'4t t('eKPte<tP nameKP4 erPGRVbrGR Pa word:VbrGRVin'4t t('eKP'a wordP nameKP'a wordPGRVbrGR Vin'4t t('eKP 4bmitP va#4eKP3o!inPGR VG,ormR VGbod(R VGhtm#R 7reate View%: Now #et 4 create %ucce%%+j%" ,i#e which wi## be invoked in ca e action ret4rn S=++9SS, b4t we wi## have another view ,i#e in ca e o, an 9""&" i ret4rned ,rom the action. VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8P LR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! P LR Vhtm#R VheadR Vtit#eRS4cce ,4# 3o!inVGtit#eR +2+ 3imited i a S4b idiar( o, 5+S 3imited 1..

VGheadR Vbod(R Ce##o Wor#d, V :'ro'ert( va#4eKPnamePGR VGbod(R VGhtm#R %o##owin! wi## be the view ,i#e err r+j%" in ca e o, an 9""&" i ret4rned ,rom the action. VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8P LR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! P LR Vhtm#R VheadR Vtit#eR$nva#id = er Name or Pa wordVGtit#eR VGheadR Vbod(R Wron! 4 er name or 'a word 'rovided. VGbod(R VGhtm#R 7 nfi(urati n File%: %ina##(, #et 4 '4t ever(thin! to!ether 4 in! the tr4t .<m# con,i!4ration ,i#e a ,o##ow : V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKP#o!inactionP methodKPe<ec4tePR Vre 4#t nameKP 4cce PRG 4cce .* 'VGre 4#tR Vre 4#t nameKPerrorPRGerror.* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R %o##owin! i the content o, web+,!l ,i#e: V[<m# ver ionKP1.0P encodin!KP=5%-8P[R Vweb-a'' <m#n :< iKPhtt':GGwww.w1.or!G2001GA23Schema-in tanceP <m#n KPhtt':GG*ava. 4n.comG<m#Gn G*avaeeP <m#n :webKPhtt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T2T..< dP < i: chema3ocationKPhtt':GG*ava. 4n.comG<m#Gn G*avaee htt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T1T0.< dP R Vdi '#a(-nameRStr4t 2VGdi '#a(-nameR Vwe#come-,i#e-#i tR Vwe#come-,i#eRinde<.* 'VGwe#come-,i#eR VGwe#come-,i#e-#i tR V,i#terR V,i#ter-nameR tr4t 2VG,i#ter-nameR V,i#ter-c#a R or!.a'ache. tr4t 2.di 'atcher.%i#terDi 'atcher VG,i#ter-c#a R VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR tr4t 2VG,i#ter-nameR +2+ 3imited i a S4b idiar( o, 5+S 3imited 1./

V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R VGweb-a''R Now, ri!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Ginde<.* '. 5hi wi## !ive (o4 ,o##owin! creen: 9nter a wron! 4 er name and 'a word. Fo4 ho4#d ee the net 'a!e Now enter %c tt a 4 er name and na)y a 'a word. Fo4 ho4#d ee the net 'a!e #trut% - #endin( E!ail 5hi cha'ter wi## teach (o4 how (Fo4 can end an emai# 4 in! (o4r Str4t 2 a''#ication. %or thi e<erci e, (o4 need to down#oad and in ta## the mai#.*ar ,rom 6ava2ai# )P$ 1.D.D and '#ace the !ail+jar ,i#e in (o4r W9E-$N%I#ib ,o#der and then 'roceed to ,o##ow the tandard te' o, creatin! action, view and con,i!4ration ,i#e . 7reate Acti n: 5he ne<t te' i to create an )ction method that take care o, endin! the emai#. 3et 4 create a new c#a ca##ed E!ailer+ja)a with the ,o##owin! content . 'acka!e com.t4toria# 'oint. tr4t 2J im'ort *ava.4ti#.Pro'ertie J im'ort *ava<.mai#.2e a!eJ im'ort *ava<.mai#.Pa word)4thenticationJ im'ort *ava<.mai#.Se ionJ im'ort *ava<.mai#.5ran 'ortJ im'ort *ava<.mai#.internet.$nternet)ddre J im'ort *ava<.mai#.internet.2ime2e a!eJ im'ort com.o'en (m'hon(.<work2.)ctionS4''ortJ '4b#ic c#a 9mai#er e<tend )ctionS4''ort N

'rivate Strin! ,romJ 'rivate Strin! 'a wordJ 'rivate Strin! toJ 'rivate Strin! 4b*ectJ 'rivate Strin! bod(J tatic Pro'ertie 'ro'ertie K new Pro'ertie 7;J tatic N 'ro'ertie .'4t7Pmai#. mt'.ho tP, P mt'.!mai#.comP;J 'ro'ertie .'4t7Pmai#. mt'. ocket%actor(.'ortP, PD/.P;J 'ro'ertie .'4t7Pmai#. mt'. ocket%actor(.c#a P, P*ava<.net. #.SS3Socket%actor(P;J 'ro'ertie .'4t7Pmai#. mt'.a4thP, Ptr4eP;J 'ro'ertie .'4t7Pmai#. mt'.'ortP, PD/.P;J Q '4b#ic Strin! e<ec4te7; N Strin! ret K S=++9SSJ tr( N Se ion e ion K Se ion.!etDe,a4#t$n tance7'ro'ertie , new *ava<.mai#.)4thenticator7; N 'rotected Pa word)4thentication !etPa word)4thentication7; N ret4rn new Pa word)4thentication7,rom, 'a word;J +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.0

QQ;J 2e a!e me a!e K new 2ime2e a!e7 e ion;J me a!e. et%rom7new $nternet)ddre 7,rom;;J me a!e. et"eci'ient 72e a!e."eci'ient5('e.5&, $nternet)ddre .'ar e7to;;J me a!e. etS4b*ect7 4b*ect;J me a!e. et5e<t7bod(;J 5ran 'ort. end7me a!e;J Q catch79<ce'tion e; N ret K 9""&"J e.'rintStack5race7;J Q ret4rn retJ Q '4b#ic Strin! !et%rom7; N ret4rn ,romJ Q '4b#ic void et%rom7Strin! ,rom; N thi .,rom K ,romJ Q '4b#ic Strin! !etPa ret4rn 'a wordJ Q word7; N

'4b#ic void etPa word7Strin! 'a thi .'a word K 'a wordJ Q '4b#ic Strin! !et5o7; N ret4rn toJ Q '4b#ic void et5o7Strin! to; N thi .to K toJ Q '4b#ic Strin! !etS4b*ect7; N ret4rn 4b*ectJ Q

word; N

'4b#ic void etS4b*ect7Strin! 4b*ect; N thi . 4b*ect K 4b*ectJ Q '4b#ic Strin! !etEod(7; N ret4rn bod(J Q '4b#ic void etEod(7Strin! bod(; N thi .bod( K bod(J Q '4b#ic tatic Pro'ertie !etPro'ertie 7; N +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.8

ret4rn 'ro'ertie J Q '4b#ic tatic void etPro'ertie 7Pro'ertie 'ro'ertie ; N 9mai#er.'ro'ertie K 'ro'ertie J Q Q ) een in the o4rce code above, the E!ailer+ja)a ha 'ro'ertie that corre 'ond to the ,orm attrib4te in the emai#.* ' 'a!e !iven be#ow. 5he e attrib4te are fr ! - 5he emai# addre o, the ender. ) we are 4 in! Goo!#e? S25P, we need a va#id !ta#k id "a%%w rd - 5he 'a word o, the above acco4nt t - Who to end the emai# to[ #ubject - 4b*ect o, the emai# b dy - 5he act4a# emai# me a!e We have not con idered an( va#idation on the above ,ie#d , va#idation wi## be added in the ne<t cha'ter. 3et 4 now #ook at the e<ec4te7; method. 5he e<ec4te7; method 4 e the *ava< 2ai# #ibrar( to end an emai# 4 in! the 4''#ied 'arameter . $, the mai# i ent 4cce ,4#(, action ret4rn S=++9SS, otherwi e it ret4rn 9""&". 7reate !ain "a(e: 3et 4 write main 'a!e 6SP ,i#e inde,+j%", which wi## be 4 ed to co##ect emai# re#ated in,ormation mentioned above: VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eR9mai# %ormVGtit#eR VGheadR Vbod(R VemR5he ,orm be#ow 4 e Goo!#e? S25P erver. So (o4 need to enter a !mai# 4 ername and 'a word VGemR V,orm actionKPemai#erP methodKP'o tPR V#abe# ,orKP,romPR%romVG#abe#RVbrGR Vin'4t t('eKPte<tP nameKP,romPGRVbrGR V#abe# ,orKP'a wordPRPa wordVG#abe#RVbrGR Vin'4t t('eKP'a wordP nameKP'a wordPGRVbrGR V#abe# ,orKPtoPR5oVG#abe#RVbrGR Vin'4t t('eKPte<tP nameKPtoPGRVbrGR V#abe# ,orKP 4b*ectPRS4b*ectVG#abe#RVbrGR Vin'4t t('eKPte<tP nameKP 4b*ectPGRVbrGR V#abe# ,orKPbod(PREod(VG#abe#RVbrGR Vin'4t t('eKPte<tP nameKPbod(PGRVbrGR Vin'4t t('eKP 4bmitP va#4eKPSend 9mai#PGR VG,ormR VGbod(R VGhtm#R 7reate View%: We wi## 4 e 6SP ,i#e %ucce%%+j%" which wi## be invoked in ca e action ret4rn S=++9SS, b4t we wi## have another view ,i#e in ca e o, an 9""&" i ret4rned ,rom the action. VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR +2+ 3imited i a S4b idiar( o, 5+S 3imited 1.-

Vhtm#R VheadR Vtit#eR9mai# S4cce VGtit#eR VGheadR Vbod(R Fo4r emai# to V :'ro'ert( va#4eKPtoPGR wa ent 4cce ,4##(. VGbod(R VGhtm#R %o##owin! wi## be the view ,i#e err r+j%" in ca e o, an 9""&" i ret4rned ,rom the action. VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eR9mai# 9rrorVGtit#eR VGheadR Vbod(R 5here i a 'rob#em endin! (o4r emai# to V :'ro'ert( va#4eKPtoPGR. VGbod(R VGhtm#R 7 nfi(urati n File%: Now #et 4 '4t ever(thin! to!ether 4 in! the tr4t .<m# con,i!4ration ,i#e a ,o##ow : V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKPemai#erP methodKPe<ec4tePR Vre 4#t nameKP 4cce PRG 4cce .* 'VGre 4#tR Vre 4#t nameKPerrorPRGerror.* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R %o##owin! i the content o, web+,!l ,i#e: V[<m# ver ionKP1.0P encodin!KP=5%-8P[R Vweb-a'' <m#n :< iKPhtt':GGwww.w1.or!G2001GA23Schema-in tanceP <m#n KPhtt':GG*ava. 4n.comG<m#Gn G*avaeeP <m#n :webKPhtt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T2T..< dP < i: chema3ocationKPhtt':GG*ava. 4n.comG<m#Gn G*avaee htt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T1T0.< dP R Vdi '#a(-nameRStr4t 2VGdi '#a(-nameR Vwe#come-,i#e-#i tR Vwe#come-,i#eRinde<.* 'VGwe#come-,i#eR VGwe#come-,i#e-#i tR V,i#terR +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/0

V,i#ter-nameR tr4t 2VG,i#ter-nameR V,i#ter-c#a R or!.a'ache. tr4t 2.di 'atcher.%i#terDi 'atcher VG,i#ter-c#a R VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR tr4t 2VG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R VGweb-a''R Now, ri!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Ginde<.* '. 5hi wi## !ive (o4 ,o##owin! creen: 9nter the re@4ired in,ormation and c#ick #end E!ail b4tton. $, ever(thin! !oe ,ine then (o4 ho4#d ee the ,o##owin! 'a!e #trut% - Validati n% Fra!ew r= Now we wi## #ook into how Str4t ? va#idation ,ramework. )t Str4t ? core, we have the va#idation ,ramework that a i t the a''#ication to r4n the r4#e to 'er,orm va#idation be,ore the action method i e<ec4ted. +#ient ide va#idation i 4 4a##( achieved 4 in! 6ava cri't. E4t one ho4#d not re#( 4'on c#ient ide va#idation a#one. Ee t 'racti e 4!!e t that the va#idation ho4#d be introd4ced at a## #eve# o, (o4r a''#ication ,ramework. Now #et 4 #ook at two wa( o, addin! va#idation to o4r Str4t 'ro*ect. Cere we wi## take an e<am'#e o, E!"l yee who e name and a!e wo4#d be ca't4red 4 in! a im'#e 'a!e and we wi## '4t two va#idation to make 4re that 4 e a#wa( enter a name and a!e ho4#d be in between 28 and /.. So #et 4 tart with the main 6SP 'a!e o, the e<am'#e. 7reate !ain "a(e: 3et 4 write main 'a!e 6SP ,i#e inde,+j%", which wi## be 4 ed to co##ect 9m'#o(ee re#ated in,ormation mentioned above. VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eR9m'#o(ee %ormVGtit#eR VGheadR Vbod(R V :,orm actionKPem'in,oP methodKP'o tPR V :te<t,ie#d nameKPnameP #abe#KPNameP iBeKP20P GR V :te<t,ie#d nameKPa!eP #abe#KP)!eP iBeKP20P GR V : 4bmit nameKP 4bmitP #abe#KPS4bmitP a#i!nKPcenterP GR VG :,ormR VGbod(R VGhtm#R 5he inde<.* ' make 4 e o, Str4t ta!, which we have not covered (et b4t we wi## t4d( them in ta! re#ated cha'ter . E4t ,or now, *4 t a 4me that the :te<t,ie#d ta! 'rint a in'4t ,ie#d, and the : 4bmit 'rint a 4bmit b4tton. We have 4 ed #abe# 'ro'ert( ,or each ta! which create #abe# ,or each ta!. 7reate View%: We wi## 4 e 6SP ,i#e %ucce%%+j%" which wi## be invoked in ca e de,ined action ret4rn S=++9SS. VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/1

VheadR Vtit#eRS4cce VGtit#eR VGheadR Vbod(R 9m'#o(ee $n,ormation i ca't4red 4cce ,4##(. VGbod(R VGhtm#R 7reate Acti n: So #et 4 de,ine a ma## action c#a E!"l yee, and then add a method ca##ed )alidate8; a hown be#ow in E!"l yee+ja)a ,i#e. 2ake 4re that (o4r action c#a e<tend the Acti n#u"" rt c#a , otherwi e (o4r va#idate method wi## not be e<ec4ted. 'acka!e com.t4toria# 'oint. tr4t 2J im'ort com.o'en (m'hon(.<work2.)ctionS4''ortJ '4b#ic c#a 9m'#o(ee e<tend )ctionS4''ortN 'rivate Strin! nameJ 'rivate int a!eJ '4b#ic Strin! e<ec4te7; N ret4rn S=++9SSJ Q '4b#ic Strin! !etName7; N ret4rn nameJ Q '4b#ic void etName7Strin! name; N thi .name K nameJ Q '4b#ic int !et)!e7; N ret4rn a!eJ Q '4b#ic void et)!e7int a!e; N thi .a!e K a!eJ Q '4b#ic void va#idate7; N i, 7name KK n4## YY name.trim7;.e@4a# 7PP;; N add%ie#d9rror7PnameP,P5he name i re@4iredP;J Q i, 7a!e V 28 YY a!e R /.; N add%ie#d9rror7Pa!eP,P)!e m4 t be in between 28 and /.P;J Q Q Q ) hown in the above e<am'#e, the va#idation method check whether the ?Name? ,ie#d ha a va#4e or not. $, no va#4e ha been 4''#ied, we add a ,ie#d error ,or the ?Name? ,ie#d with a c4 tom error me a!e. Second#(, we check i, entered va#4e ,or ?)!e? ,ie#d i in between 28 and /. or not, i, thi condition doe not meet we add an error above the va#idated ,ie#d. 7 nfi(urati n File%: %ina##(, #et 4 '4t ever(thin! to!ether 4 in! the %trut%+,!l con,i!4ration ,i#e a ,o##ow : V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/2

V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKPem'in,oP methodKPe<ec4tePR Vre 4#t nameKPin'4tPRGinde<.* 'VGre 4#tR Vre 4#t nameKP 4cce PRG 4cce .* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R %o##owin! i the content o, web+,!l ,i#e: V[<m# ver ionKP1.0P encodin!KP=5%-8P[R Vweb-a'' <m#n :< iKPhtt':GGwww.w1.or!G2001GA23Schema-in tanceP <m#n KPhtt':GG*ava. 4n.comG<m#Gn G*avaeeP <m#n :webKPhtt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T2T..< dP < i: chema3ocationKPhtt':GG*ava. 4n.comG<m#Gn G*avaee htt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T1T0.< dP R Vdi '#a(-nameRStr4t 2VGdi '#a(-nameR Vwe#come-,i#e-#i tR Vwe#come-,i#eRinde<.* 'VGwe#come-,i#eR VGwe#come-,i#e-#i tR V,i#terR V,i#ter-nameR tr4t 2VG,i#ter-nameR V,i#ter-c#a R or!.a'ache. tr4t 2.di 'atcher.%i#terDi 'atcher VG,i#ter-c#a R VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR tr4t 2VG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R VGweb-a''R Now, ri!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Ginde<.* '. 5hi wi## !ive (o4 ,o##owin! creen: Now do not enter an( re@4ired in,ormation, *4 t c#ick on #ub!it b4tton. Fo4 wi## ee ,o##owin! re 4#t: 9nter the re@4ired in,ormation b4t enter a wron! %rom ,ie#d, #et 4 a( name a Pte tP and a!e a 10, and ,ina##( c#ick on #ub!it b4tton. Fo4 wi## ee ,o##owin! re 4#t: ' w thi% )alidati n w r=%? When the 4 er 're e the 4bmit b4tton, Str4t 2 wi## a4tomatica##( e<ec4te the va#idate method and i, an( o, the i, tatement #i ted in ide the method are tr4e, Str4t 2 wi## ca## it add%ie#d9rror method. $, an( error have been added then Str4t 2 wi## not 'roceed to ca## the e<ec4te method. "ather the Str4t 2 ,ramework wi## ret4rn in"ut a the re 4#t o, ca##in! the action. So when va#idation ,ai# and Str4t 2 ret4rn in"ut, the Str4t 2 ,ramework wi## redi '#a( the inde<.* ' ,i#e. Since we 4 ed Str4t 2 ,orm ta! , Str4t 2 wi## a4tomatica##( add the error me a!e *4 t above the ,orm ,i#ed. 5he e error me a!e are the one we 'eci,ied in the add%ie#d9rror method ca##. 5he add%ie#d9rror method take two ar!4ment . 5he ,ir t i the f r! ,ie#d name to which the error a''#ie and the econd i the error me a!e to di '#a( above that ,orm ,ie#d. add%ie#d9rror7PnameP,P5he name i re@4iredP;J 5o hand#e the ret4rn va#4e o, in"ut we need to add the ,o##owin! re 4#t to o4r action node in %trut%+,!l. +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/1

Vre 4#t nameKPin'4tPRGinde<.* 'VGre 4#tR 0&4 6a%ed Validati n: 5he econd method o, doin! va#idation i b( '#acin! an <m# ,i#e ne<t to the action c#a . Str4t 2 A23 ba ed va#idation 'rovide more o'tion o, va#idation #ike emai# va#idation, inte!er ran!e va#idation, ,orm va#idation ,ie#d, e<'re ion va#idation, re!e< va#idation, re@4ired va#idation, re@4ired trin! va#idation, trin!#en!th va#idation and etc. 5he <m# ,i#e need to be named I9acti n$cla%%:I$)alidati n+,!l. So, in o4r ca e we create a ,i#e ca##ed E!"l yee$)alidati n+,!l with the ,o##owin! content : VWD&+5FP9 va#idator P=E3$+ P-GG&'enS(m'hon( Gro4'GGAWork Va#idator 1.0.2GG9NP Phtt':GGwww.o'en (m'hon(.comG<workG<work-va#idator-1.0.2.dtdPR Vva#idator R V,ie#d nameKPnamePR V,ie#d-va#idator t('eKPre@4iredPR Vme a!eR 5he name i re@4ired. VGme a!eR VG,ie#d-va#idatorR VG,ie#dR V,ie#d nameKPa!ePR V,ie#d-va#idator t('eKPintPR V'aram nameKPminPR2-VG'aramR V'aram nameKPma<PR/DVG'aramR Vme a!eR )!e m4 t be in between 28 and /. VGme a!eR VG,ie#d-va#idatorR VG,ie#dR VGva#idator R )bove A23 ,i#e wo4#d be ke't in (o4r +3)SSP)5C idea# #trut%- 4 cali>ati n, internati nali>ati n 8iB3n; $nternationa#iBation 7i18n; i the 'roce o, '#annin! and im'#ementin! 'rod4ct and ervice o that the( can ea i#( be ada'ted to 'eci,ic #oca# #an!4a!e and c4#t4re , a 'roce ca##ed #oca#iBation. 5he internationa#iBation 'roce i ometime ca##ed tran #ation or #oca#iBation enab#ement. $nternationa#iBation i abbreviated iB3n beca4 e the word tart with an i and end with an n, and there are 18 character between the ,ir t i and the #a t n. Str4t 2 'rovide #oca#iBation ie. internationa#iBation 7i18n; 4''ort thro4!h re o4rce b4nd#e , interce'tor and ta! #ibrarie in the ,o##owin! '#ace : 5he =$ 5a! 2e a!e and 9rror . Within action c#a e . Re% urce 6undle%: Str4t 2 4 e re o4rce b4nd#e to 'rovide m4#ti'#e #an!4a!e and #oca#e o'tion to the 4 er o, the web a''#ication. Fo4 don?t need to worr( abo4t writin! 'a!e in di,,erent #an!4a!e . )## (o4 have to do i to create a re o4rce b4nd#e ,or each #an!4a!e that (o4 want. 5he re o4rce b4nd#e wi## contain tit#e , me a!e , and other te<t in the #an!4a!e o, (o4r 4 er. "e o4rce b4nd#e are the ,i#e that contain the ke(Gva#4e 'air ,or the de,a4#t #an!4a!e o, (o4r a''#ication. 5he im'#e t namin! ,ormat ,or a re o4rce ,i#e i : b4nd#enameT#an!4a!eTco4ntr(.'ro'ertie Cere bundlena!e co4#d be )ction+#a , $nter,ace, S4'er+#a , 2ode#, Packa!e, G#oba# re o4rce 'ro'ertie . Ne<t 'art lan(ua(eRc untry re're ent the co4ntr( #oca#e ,or e<am'#e S'ani h 7S'ain; #oca#e i re're ented b( e T9S and 9n!#i h 7=nited State ; #oca#e i re're ented b( enT=S etc. Cere (o4 can ki' co4ntr( 'art which i o'tiona#. When (o4 re,erence a me a!e e#ement b( it ke(, Str4t ,ramework earche ,or a corre 'ondin! me a!e b4nd#e in the ,o##owin! order: )ction+#a .'ro'ertie +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/D

$nter,ace.'ro'ertie S4'er+#a .'ro'ertie mode#.'ro'ertie 'acka!e.'ro'ertie tr4t .'ro'ertie !#oba#.'ro'ertie 5o deve#o' (o4r a''#ication in m4#ti'#e #an!4a!e , (o4 wo4#d have to maintain m4#ti'#e 'ro'ert( ,i#e corre 'ondin! to tho e #an!4a!e G#oca#e and de,ine a## the content in term o, ke(Gva#4e 'air . %or e<am'#e i, (o4 are !oin! to deve#o' (o4r a''#ication ,or =S 9n!#i h 7De,a4#t;, S'ani h, and %ranch the (o4 wo4#d have to create three 'ro'ertie ,i#e . Cere $ wi## 4 e (l bal+"r "ertie% ,i#e on#(, (o4 can make 4 e o, di,,erent 'ro'ert( ,i#e to e!re!ate di,,erent t('e o, me a!e . (l bal+"r "ertie%: E( de,a4#t 9n!#i h 7=nited State ; wi## be a''#ied (l balRfr+"r "ertie%: 5hi wi## be 4 ed ,or %ranch #oca#e. (l balRe%+"r "ertie%: 5hi wi## be 4 ed ,or S'ani h #oca#e. Acce%% the !e%%a(e%: 5here are evera# wa( to acce the me a!e re o4rce , inc#4din! !et5e<t, the te<t ta!, ke( attrib4te o, =$ ta! , and the i18n ta!. 3et 4 ee them in brie,: 5o di '#a( iB3n te<t, 4 e a ca## to (et*e,t in the 'ro'ert( ta!, or an( other ta!, 4ch a the =$ ta! a ,o##ow : V :'ro'ert( va#4eKP!et5e<t7? ome.ke(?;P GR 5he te,t ta( retrieve a me a!e ,rom the de,a4#t re o4rce b4nd#e ie. tr4t .'ro'ertie V :te<t nameKP ome.ke(P GR 5he iB3n ta( '4 he an arbitrar( re o4rce b4nd#e on to the va#4e tack. &ther ta! within the co'e o, the i18n ta! can di '#a( me a!e ,rom that re o4rce b4nd#e: V :i18n nameKP ome.'acka!e.b4nd#ePR V :te<t nameKP ome.ke(P GR VG :i18nR 5he =ey attrib4te o, mo t =$ ta! can be 4 ed to retrieve a me a!e ,rom a re o4rce b4nd#e: V :te<t,ie#d ke(KP ome.ke(P nameKPte<t,ie#dNamePGR 4 cali>ati n E,a!"le: 3et 4 tar!et to create inde,+j%" ,rom the 'revio4 cha'ter in m4#ti'#e #an!4a!e . Same ,i#e wo4#d be written a ,o##ow : VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eR9m'#o(ee %orm with 24#ti#in!4a# S4''ortVGtit#eR VGhead #trut% - *y"e 7 n)er%i n 9ver(thin! on a C55P re@4e t i treated a a #trin( b( the 'rotoco#. 5hi inc#4de n4mber , boo#ean , inte!er , date , decima# and ever(thin! e# e. 9ver( thin! i a trin! accordin! to C55P. Cowever, in the Str4t c#a , (o4 co4#d have 'ro'ertie o, an( data t('e . Cowe doe Str4t a4towire the 'ro'ertie ,or (o4 [ Str4t 4 e a variet( o, t('e converter 4nder the cover to do the heav( #i,tin!. %or e<am'#e, i, (o4 have an inte!er attrib4te in (o4r )ction c#a , Str4t a4tomatica##( convert the re@4e t 'arameter to the inte!er attrib4te witho4t (o4 doin! an(thin!. E( de,a4#t, Str4t come with a n4mber o, t('e converter . Some o, them are #i ted be#ow and i, (o4 are 4 in! an( o, them then (o4 have nothin! to worr( abo4t: $nte!er, %#oat, Do4b#e, Decima# Date and Datetime )rra( and +o##ection 9n4meration Eoo#ean Ei!Decima#

+2+ 3imited i a S4b idiar( o, 5+S 3imited

1/.

Some time when (o4 are 4 in! (o4r own data t('e, it i nece ar( to add (o4r own converter to make Str4t aware how to convert tho e va#4e be,ore di '#a(in!. +on ider the ,o##owin! P&6& c#a En)ir n!ent+ja)a. 'acka!e com.t4toria# 'oint. tr4t 2J '4b#ic c#a 9nvironment N 'rivate Strin! nameJ '4b#ic 9nvironment7Strin! name; N thi .name K nameJ Q '4b#ic Strin! !etName7; N ret4rn nameJ Q '4b#ic void etName7Strin! name; N thi .name K nameJ Q Q 5hi i a ver( im'#e c#a that ha an attrib4te ca##ed na!e, o nothin! 'ecia# abo4t thi c#a . 3et 4 create another c#a that contain in,ormation abo4t the ( tem - #y%te!Detail%+ja)a. %or the '4r'o e o, thi e<ercice , $ have hardcoded the 9nvironment to PDeve#o'mentP and the &'eratin! S( tem to PWindow AP SP1P. $n a rea# 'ro*ect, (o4 wo4#d !et thi in,ormation ,rom the ( tem con,i!4ration. So #et 4 have ,o##owin! action c#a : 'acka!e com.t4toria# 'oint. tr4t 2J im'ort com.o'en (m'hon(.<work2.)ctionS4''ortJ '4b#ic c#a S( temDetai# e<tend )ctionS4''ort N 'rivate 9nvironment environment K new 9nvironment7PDeve#o'mentP;J 'rivate Strin! o'eratin!S( tem K PWindow AP SP1PJ '4b#ic Strin! e<ec4te7; N ret4rn S=++9SSJ Q '4b#ic 9nvironment !et9nvironment7; N ret4rn environmentJ Q '4b#ic void et9nvironment79nvironment environment; N thi .environment K environmentJ Q '4b#ic Strin! !et&'eratin!S( tem7; N ret4rn o'eratin!S( temJ Q '4b#ic void et&'eratin!S( tem7Strin! o'eratin!S( tem; N thi .o'eratin!S( tem K o'eratin!S( temJ Q Q Ne<t #et 4 create a im'#e 6SP ,i#e #y%te!+j%" to di '#a( the 9nvironment and the &'eratin! S( tem in,ormation. VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eRS( tem Detai# VGtit#eR VGheadR Vbod(R +2+ 3imited i a S4b idiar( o, 5+S 3imited 1//

9nvironment: V :'ro'ert( va#4eKPenvironmentPGRVbrGR &'eratin! S( tem:V :'ro'ert( va#4eKPo'eratin!S( temPGR VGbod(R VGhtm#R 3et 4 wire the %y%te!+j%" and the #y%te!Detail%+ja)a c#a to!ether 4 in! %trut%+,!l. 5he S( temDetai# c#a ha a im'#e e<ec4te7; method that ret4rn the trin! PS=++9SSP. V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKP ( temP methodKPe<ec4tePR Vre 4#t nameKP 4cce PRGS( tem.* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R "i!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2G ( tem.action. 5hi wi## !ive (o4 ,o##owin! creen: What i wron! with the above o4t'4t[ Str4t know how to di '#a( and convert the trin! PWindow AP SP1P and other b4i#t-in data t('e , b4t it doe not know what to do with the the 'ro'ert( o, En)ir n!ent t('e. So, it im'#( ca##ed the t #trin(8; method on the c#a . 5o re o#ve thi 'rob#em, #et 4 now create and re!i ter a im'#e *y"e7 n)erter ,or the 9nvironment c#a . +reate a c#a ca##ed En)ir n!ent7 n)erter+ja)a with the ,o##owin!. 'acka!e com.t4toria# 'oint. tr4t 2J im'ort *ava.4ti#.2a'J im'ort or!.a'ache. tr4t 2.4ti#.Str4t 5('e+onverterJ '4b#ic c#a 9nvironment+onverter e<tend Str4t 5('e+onverter N ^&verride '4b#ic &b*ect convert%romStrin!72a' conte<t, Strin!8: va#4e , +#a c#aBB; N 9nvironment env K new 9nvironment7va#4e 80:;J ret4rn envJ Q ^&verride '4b#ic Strin! convert5oStrin!72a' conte<t, &b*ect va#4e; N 9nvironment env K 79nvironment; va#4eJ ret4rn env KK n4## [ n4## : env.!etName7;J Q Q 5he En)ir n!ent7 n)erter e<tend the #trut%*y"e7 n)erter c#a and te## Str4t how to convert 9nvironment to a Strin! and vice ver a b( overridin! two method c n)ertFr !#trin(8; and c n)ert* #trin(8;. 3et 4 now re!i ter thi converter be,ore we 4 it in o4r a''#ication. 5here are two wa( to re!i ter a converter. $, the converter wi## be 4 ed on#( in a 'artic4#ar action, then (o4 wo4#d have to create a 'ro'ert( ,i#e need to be named a I9acti n$cla%%:I$c n)er%ti n+"r "ertie%, So, in o4r ca e we create a ,i#e ca##ed #y%te!Detail%$c n)er%ti n+"r "ertie% with the ,o##owin! re!i tration enter(: environmentKcom.t4toria# 'oint. tr4t 2.9nvironment+onverter +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/0

$n the above e<am'#e, PenvironmentP i the name o, the 'ro'ert( in the #y%te!Detail%+ja)a c#a and we are te##in! Str4t to 4 e the En)ir n!ent7 n)erter ,or convertin! to and ,rom thi 'ro'ert(. Cowever, we are not !oin! to do thi , $n tead we are !oin! to re!i ter thi converter !#oba##( o that it can be 4 ed thro4!ho4t the a''#ication. 5o do thi , create a 'ro'ert( ,i#e ca##ed ,w r=$ c n)er%i n+"r "ertie% in the WE6$INF/cla%%e% ,o#der with the ,o##owin! #ine: com.t4toria# 'oint. tr4t 2.9nvironment K I com.t4toria# 'oint. tr4t 2.9nvironment+onverter 5hi im'#( re!i ter the converter !#oba##(, o that Str4t can a4tomatica##( do the conver ion ever( time it enco4nter an ob*ect o, t('e En)ir n!ent. Now, i, (o4 re-com'i#e and re-r4n the 'ro!ram, (o4 wi## !et a better o4t'4t a ,o##ow : &bvio4 #(, now re 4#t i better which mean o4r Str4t convertor i workin! ,ine. 5hi i how (o4 can create m4#ti'#e convertor and re!i ter them to 4 e a 'er (o4r re@4irement . #trut%- *he!e% and *e!"late% Ee,ore tartin! act4a# t4toria# ,or thi cha'ter, #et 4 #ook into ,ew de,inition a !iven b( http:>>struts#apache#org: *er! De%cri"ti n ) ma## 'iece o, code e<ec4ted ,rom within 6SP, %ree2arker, or ta( Ve#ocit(. ) bit o, code, 4 4a##( written in %ree2arker, that can be rendered b( te!"late certain ta! 7C523 ta! ;. ) co##ection o, tem'#ate 'acka!ed to!ether to 'rovide common the!e ,4nctiona#it(. $ wo4#d a# o 4!!e t to !o thro4!h the Str4t 2 3oca#iBation cha'ter beca4 e we wi## take ame e<am'#e once a!ain to 'er,orm o4r e<cerci e. When (o4 4 e a Str4t 2 ta! 4ch a V : 4bmit...R, V :te<t,ie#d...R etc in (o4r web 'a!e, the Str4t 2 ,ramework !enerate C523 code with a 'recon,i!4red t(#e and #a(o4t. Str4t 2 come with three b4i#tin theme : *he!e De%cri"ti n ) minima# theme with no Pbe## and whi t#e P. %or e<am'#e, the %i!"le the!e te<t,ie#d ta! render the C523 Vin'4tGR ta! witho4t a #abe#, va#idation, error re'ortin!, or an( other ,ormattin! or ,4nctiona#it(. 5hi i the de,a4#t theme 4 ed b( Str4t 2 and 'rovide a## the ba ic that the im'#e theme 'rovide and add evera# ,eat4re #ike ,ht!l the!e tandard two-co#4mn tab#e #a(o4t ,or the C523, 3abe# ,or each o, the C523, Va#idation and error re'ortin! etc. 5hi theme 'rovide a## the ba ic that the im'#e theme 'rovide and add evera# ,eat4re #ike tandard two-co#4mn +SS-ba ed c%%R,ht!l the!e #a(o4t, 4 in! VdivR ,or the C523 Str4t 5a! , 3abe# ,or each o, the C523 Str4t 5a! , '#aced accordin! to the +SS t(#e heet. ) mentioned above, i, (o4 don? t 'eci,( a theme, then Str4t 2 wi## 4 e the <htm# theme b( de,a4#t. %or e<am'#e thi Str4t 2 e#ect ta!: V :te<t,ie#d nameKPnameP #abe#KPNameP GR !enerate ,o##owin! C523 mark4': VtrR Vtd R V#abe# ,orKPem'in,oTnameP RName:VG#abe#R VGtdRVtdR Vin'4t t('eKPte<tP nameKPnameP va#4eKPP GR VGtdR VGtrR Cere e!"inf i the action name de,ined in tr4t .<m# ,i#e. #electin( the!e%: Fo4 can 'eci,( the theme on a 'er Str4t 2 ta! ba i or (o4 can 4 e one o, the ,o##owin! method to 'eci,( what theme Str4t 2 ho4#d 4 e: +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/8

5he theme attrib4te on the 'eci,ic ta! 5he theme attrib4te on a ta!? 4rro4ndin! ,orm ta! 5he 'a!e- co'ed attrib4te named PthemeP 5he re@4e t- co'ed attrib4te named PthemeP 5he e ion- co'ed attrib4te named PthemeP 5he a''#ication- co'ed attrib4te named PthemeP 5he tr4t .4i.theme 'ro'ert( in tr4t .'ro'ertie 7de,a4#t to <htm#; %o##owin! i the (nta< to 'eci,( a them at ta! #eve# i, (o4 are wi##in! to 4 e di,,erent theme ,or di,,erent ta! : V :te<t,ie#d nameKPnameP #abe#KPNameP themeKP<htm#PGR Eeca4 e it i not ver( m4ch 'ractica# to 4 e theme on 'er ta! ba i , o im'#( we can 'eci,( the r4#e in %trut%+"r "ertie% ,i#e 4 in! the ,o##owin! ta! : b Standard =$ theme tr4t .4i.themeK<htm# b Director( where theme tem'#ate re ide tr4t .4i.tem'#ateDirKtem'#ate b Set the de,a4#t tem'#ate t('e. 9ither ,t#, vm, or * ' tr4t .4i.tem'#ateS4,,i<K,t# %o##owin! i the re 4#t we 'icked 4' ,rom #oca#iBation cha'ter where we 4 ed de,a4#t theme with a ettin! %trut%+ui+the!eS,ht!l in %trut%$default+"r "ertie% ,i#e which come b( de,a4#t in tr4t 2core.<(.B.*ar ,i#e. ' w a the!e w r=%? %or a !iven theme, ever( tr4t ta! ha an a ociated tem'#ate #ike %:te,tfield $X te,t+ftl and %:"a%%w rd $X "a%%w rd+ftl etc. 5he e tem'#ate ,i#e come Bi''ed in tr4t 2-core.<(.B.*ar ,i#e. 5he e tem'#ate ,i#e kee' a 're-de,ined C523 #a(o4t ,or each ta!. So Str4t 2 ,ramework !enerate ,ina# C523 mark4' code 4 in! St4rt ta! and a ociated tem'#ate . Str4t 2 ta! > ) ociated tem'#ate ,i#e K %ina# C523 mark4' code. De,a4#t tem'#ate have been written in %ree2arker and the( have e<ten ion +ftl. Fo4 can de i!n (o4r tem'#ate 4 in! ve#ocit( or 6SP and accordin!#( et the con,i!4ration in tr4t .'ro'ertie 4 in! %trut%+ui+te!"late#uffi, and %trut%+ui+te!"lateDir. 7reatin( new the!e%: 5he im'#e t wa( to create a new theme i to co'( an( o, the e<i tin! themeGtem'#ate ,i#e and do re@4ired modi,ication . So #et 4 tart with creatin! a ,o#der ca##ed te!"late in WebCo$te$t>W%<; 3NF>classes and a 4b-,o#der with the name o, o4r new theme, ,or e<am'#e WebCo$te$t>W%<; 3NF>classes>template>mytheme. %rom here, (o4 can tart b4i#din! tem'#ate ,rom cratch, or (o4 can co'( the tem'#ate ,rom the Str4t 2 di trib4tion and modi,( them a needed. We are !oin! to modi,( e<i tin! de,a4#t tem'#ate ,ht!l ,or #earnin! '4r'o e. So now #et 4 co'( the content ,rom strutsI;core;1#y#/#jar>template>1html to o4r theme director( and modi,( on#( WebCo$te$t>W%<;3NF>classes>template>mytheme>co$trol#)tl ,i#e. When we o'en c ntr l+ftl it wi## have ,o##owin! #ine : Vtab#e VbrtGR Vbi, 'arameter .c St(#e[[R t(#eKPMN'arameter .c St(#e[htm#QPVbrtGR VGbi,R R 3et 4 chan!e above ,i#e c ntr l+ftl to have the ,o##owin! content: Vtab#e t(#eKPborder:1'< o#id b#ackJPR $, (o4 wi## check f r!+ftl then (o4 wi## ,ind that c ntr l+ftl i bein! 4 ed in thi ,i#e, b4t ,orm.,t# i re,erin! thi ,i#e ,rom <htm# theme. So #et 4 chan!e it a ,o##ow : Vbinc#4de PGMN'arameter .tem'#ateDirQG<htm#G,orm-va#idate.,t#P GR Vbinc#4de PGMN'arameter .tem'#ateDirQG im'#eG,orm-common.,t#P GR Vbi, 7'arameter .va#idate[de,a4#t7,a# e;;R onre etKPMN'arameter .onre et[de,a4#t7?c#ear9rror2e a!e 7thi ;JI c#ear9rror3abe# 7thi ;J?;QP Vbe# eR Vbi, 'arameter .onre et[[R onre etKPMN'arameter .onre et[htm#QP VGbi,R VGbi,R +2+ 3imited i a S4b idiar( o, 5+S 3imited 1/-

R W\include H/]F"ara!eter%+te!"lateDirG/!ythe!e/c ntr l+ftlH /X $ a 4me (o4 wo4#d not have m4ch 4nder tandin! o, the Free&ar=er tem'#ate #an!4a!e, ti## (o4 can !et a 'rett( !ood idea o, what need to be done b( #ookin! at the.,t# ,i#e . Cowever, #et 4 ave above chan!e , and !o back to o4r #oca#iBation e<am'#e and create Web7 ntent/WE6$ INF/cla%%e%/%trut%+"r "ertie% ,i#e with the ,o##owin! content: b +4 tomiBed them tr4t .4i.themeKm(theme b Director( where theme tem'#ate re ide tr4t .4i.tem'#ateDirKtem'#ate b Set the tem'#ate t('e to ,t#. tr4t .4i.tem'#ateS4,,i<K,t# Now a,ter thi chan!e, ri!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2. 5hi wi## !ive (o4 ,o##owin! creen: Fo4 can ee a border aro4nd the ,orm com'onent which i a re 4#t o, the chan!e we did in o4t theme a,ter co'(in! it ,rom <htm# theme. $, (o4 '4t #itt#e e,,ort in #earnin! %ree2arker, then (o4 wi## be ab#e to create or modi,( (o4r theme ver( ea i#(. )t#ea t now (o4 m4 t have a ba ic 4nder tandin! on St4rt 2 theme and tem'#ate , i n?t it[ #trut% - E,ce"ti n 'andlin( Str4t 'rovide an ea ier wa( to hand#e 4nca4!ht e<ce'tion and redirect 4 er to a dedicated error 'a!e. Fo4 can ea i#( con,i!4re Str4t to have di,,erent error 'a!e ,or di,,erent e<ece'tion . Str4t make the e<ce'tion hand#in! ea ( b( the 4 e o, the Pe<ce'tionP interce'tor. 5he Pe<ce'tionP interce'tor i inc#4ded a 'art o, the de,a4#t tack, o (o4 don?t have to do an(thin! e<tra to con,i!4re it. $t i avai#ab#e o4t-o,-the-bo< read( ,or (o4 to 4 e. 3et 4 ee a im'#e Ce##o Wor#d e<am'#e with ome modi,ication in 'ell W rldActi n+ja)a ,i#e. Cere we '4r'o e#( introd4ced a NullP inter 9<ce'tion in o4r 'ell W rldActi n action code. 'acka!e com.t4toria# 'oint. tr4t 2J im'ort com.o'en (m'hon(.<work2.)ctionS4''ortJ '4b#ic c#a Ce##oWor#d)ction e<tend )ctionS4''ortN 'rivate Strin! nameJ '4b#ic Strin! e<ec4te7;N Strin! < K n4##J < K <. 4b trin!70;J ret4rn S=++9SSJ Q '4b#ic Strin! !etName7; N ret4rn nameJ Q '4b#ic void etName7Strin! name; N thi .name K nameJ Q Q 3et 4 kee' the content o, 'ell W rld+j%" a ,o##ow : VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8P LR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! P LR Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR VGheadR Vbod(R +2+ 3imited i a S4b idiar( o, 5+S 3imited

100

Ce##o Wor#d, V :'ro'ert( va#4eKPnamePGR VGbod(R VGhtm#R %o##owin! i the content o, inde,+j%": VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR VGheadR Vbod(R Vh1RCe##o Wor#d %rom Str4t 2VGh1R V,orm actionKPhe##oPR V#abe# ,orKPnamePRP#ea e enter (o4r nameVG#abe#RVbrGR Vin'4t t('eKPte<tP nameKPnamePGR Vin'4t t('eKP 4bmitP va#4eKPSa( Ce##oPGR VG,ormR VGbod(R VGhtm#R Fo4r %trut%+,!l ho4#d #ook #ike: V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKPhe##oP methodKPe<ec4tePR Vre 4#t nameKP 4cce PRGCe##oWor#d.* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R Now ri!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Ginde<.* '. 5hi wi## !ive (o4 ,o##owin! creen: 9nter a va#4e PStr4t 2P and 4bmit the 'a!e. Fo4 ho4#d ee the ,o##owin! 'a!e: ) hown in the above e<am'#e, the de,a4#t e<ce'tion interce'tor doe a !reat *ob o, hand#in! the e<ce'tion. 3et 4 now create a dedicated error 'a!e ,or o4r 9<ce'tion. +reate a ,i#e ca##ed Err r+j%" with the ,o##owin! content : VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR +2+ 3imited i a S4b idiar( o, 5+S 3imited 101

Vtit#eRVGtit#eR VGheadR Vbod(R 5hi i m( c4 tom error 'a!e VGbod(R VGhtm#R 3et 4 now con,i!4re Str4t to 4 e thi thi error 'a!e in ca e o, an e<ce'tion. 3et 4 modi,( the %trut%+,!l a ,o##ow : V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR Vaction nameKPhe##oP methodKPe<ec4tePR Ve<ce'tion-ma''in! e<ce'tionKP*ava.#an!.N4##Pointer9<ce'tionP re 4#tKPerrorP GR Vre 4#t nameKP 4cce PRGCe##oWor#d.* 'VGre 4#tR Vre 4#t nameKPerrorPRG9rror.* 'VGre 4#tR VGactionR VG'acka!eR VG tr4t R ) hown in the e<am'#e above, now we have con,i!4red Str4t to 4 e the dedicated 9rror.* ' ,or the N4##Pointer9<ce'tion. $, (o4 rer4n the 'ro!ram now, (o4 ha## now ee the ,o##owin! o4t'4t: $n addition to thi , Str4t 2 ,ramework come with a P#o!!in!P interce'tor to #o! the e<ce'tion . E( enab#in! the #o!!er to #o! the 4nca4!ht e<ce'tion , we can ea i#( #ook at the tack trace and work o4t what went wron!. Cl bal E,ce"ti n &a""in(%: We have een how we can hand#e action 'eci,ic e<ce'tion. We can et an e<ce'tion !#oba##( which wi## a''#( to a## the action . %or e<am'#e, to catch the ame N4##Pointer9<ce'tion e<ce'tion , we co4#d add W(l bal$e,ce"ti n$!a""in(%+++X ta! in ide V'acka!e...R ta! and it Vre 4#t...R ta! ho4#d be added in ide the Vaction...R ta! in tr4t .<m# ,i#e a ,o##ow : V[<m# ver ionKP1.0P encodin!KP=5%-8P[R VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R Vcon tant nameKP tr4t .dev2odeP va#4eKPtr4eP GR V'acka!e nameKPhe##owor#dP e<tend KP tr4t -de,a4#tPR V!#oba#-e<ce'tion-ma''in! R Ve<ce'tion-ma''in! e<ce'tionKP*ava.#an!.N4##Pointer9<ce'tionP re 4#tKPerrorP GR VG!#oba#-e<ce'tion-ma''in! R Vaction nameKPhe##oP methodKPe<ec4tePR Vre 4#t nameKP 4cce PRGCe##oWor#d.* 'VGre 4#tR Vre 4#t nameKPerrorPRG9rror.* 'VGre 4#tR VGactionR +2+ 3imited i a S4b idiar( o, 5+S 3imited 102

VG'acka!eR VG tr4t R #trut% - Ann tati n% ) mentioned 'revio4 #(, Str4t 'rovide two ,orm o, con,i!4ration. 5he traditiona# wa( i to 4 e the %trut%+,!l ,i#e ,or a## the con,i!4ration . We have een o man( e<am'#e o, that in the t4torai# o ,ar. 5he other wa( o, con,i!4rin! Str4t i b( 4 in! the 6ava . )nnotation ,eat4re. = in! the tr4t annotation , we can achieve Der 7 nfi(urati n. 5o tart 4 in! annotation in (o4r 'ro*ect, make 4re (o4 have inc#4ded ,o##owin! *ar ,i#e in (o4r Web7 ntent/WE6$INF/lib ,o#der: tr4t 2-convention-'#4!in-<.(.B.*ar a m-<.(.*ar ant#r-<.(.B.*ar common -,i#e4'#oad-<.(.B.*ar common -io-<.(.B.*ar common -#an!-<.(.*ar common -#o!!in!-<.(.B.*ar common -#o!!in!-a'i-<.(.*ar ,reemarker-<.(.B.*ar *ava i t-.<(.B.G) o!n#-<.(.B.*ar tr4t 2-core-<.(.B.*ar <work-core.<.(.B.*ar Now #et 4 ee how (o4 can do awa( with the con,i!4ration avai#ab#e in the %trut%+,!l ,i#e and re'#ace it with annotaion . 5o e<'#ain the conce't o, )nnotation in Str4t 2, we wo4#d have to recon ider o4r va#idation e<am'#e e<'#ained in Str4t 2 Va#idation cha'ter. Cere we wi## take an e<am'#e o, E!"l yee who e name and a!e wo4#d be ca't4red 4 in! a im'#e 'a!e and we wi## '4t two va#idation to make 4re that 4 e a#wa( enter a name and a!e ho4#d be in between 28 and /.. So #et 4 tart with the main 6SP 'a!e o, the e<am'#e. 7reate !ain "a(e: 3et 4 write main 'a!e 6SP ,i#e inde,+j%", which wi## be 4 ed to co##ect 9m'#o(ee re#ated in,ormation mentioned above. VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eR9m'#o(ee %ormVGtit#eR VGheadR Vbod(R V :,orm actionKPem'in,oP methodKP'o tPR V :te<t,ie#d nameKPnameP #abe#KPNameP iBeKP20P GR V :te<t,ie#d nameKPa!eP #abe#KP)!eP iBeKP20P GR V : 4bmit nameKP 4bmitP #abe#KPS4bmitP a#i!nKPcenterP GR VG :,ormR VGbod(R VGhtm#R 5he inde<.* ' make 4 e o, Str4t ta!, which we have not covered (et b4t we wi## t4d( them in ta! re#ated cha'ter . E4t ,or now, *4 t a 4me that the :te<t,ie#d ta! 'rint a in'4t ,ie#d, and the : 4bmit 'rint a 4bmit b4tton. We have 4 ed #abe# 'ro'ert( ,or each ta! which create #abe# ,or each ta!. 7reate View%: We wi## 4 e 6SP ,i#e %ucce%%+j%" which wi## be invoked in ca e de,ined action ret4rn S=++9SS. VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P +2+ 3imited i a S4b idiar( o, 5+S 3imited 101

'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vtit#eRS4cce VGtit#eR VGheadR Vbod(R 9m'#o(ee $n,ormation i ca't4red 4cce ,4##(. VGbod(R VGhtm#R 7reate Acti n: 5hi i the '#ace where annotation wi## be 4 ed. 3et 4 re-de,ine action c#a E!"l yee with annotation, and then add a method ca##ed )alidate8; a hown be#ow in E!"l yee+ja)a ,i#e. 2ake 4re that (o4r action c#a e<tend the Acti n#u"" rt c#a , otherwi e (o4r va#idate method wi## not be e<ec4ted. 'acka!e com.t4toria# 'oint. tr4t 2J im'ort com.o'en (m'hon(.<work2.)ctionS4''ortJ im'ort or!.a'ache. tr4t 2.convention.annotation.)ctionJ im'ort or!.a'ache. tr4t 2.convention.annotation."e 4#tJ im'ort or!.a'ache. tr4t 2.convention.annotation."e 4#t J im'ort com.o'en (m'hon(.<work2.va#idator.annotation .OJ ^"e 4#t 7N ^"e 4#t7nameKP 4cce P, #ocationKPG 4cce .* 'P;, ^"e 4#t7nameKPin'4tP, #ocationKPGinde<.* 'P; Q; '4b#ic c#a 9m'#o(ee e<tend )ctionS4''ortN 'rivate Strin! nameJ 'rivate int a!eJ ^)ction7va#4eKPGem'in,oP; '4b#ic Strin! e<ec4te7; N ret4rn S=++9SSJ Q ^"e@4ired%ie#dVa#idator7 me a!e K P5he name i re@4iredP ; '4b#ic Strin! !etName7; N ret4rn nameJ Q '4b#ic void etName7Strin! name; N thi .name K nameJ Q ^$nt"an!e%ie#dVa#idator7me a!e K P)!e m4 t be in between 28 and /.P, min K P2-P, ma< K P/.P; '4b#ic int !et)!e7; N ret4rn a!eJ Q '4b#ic void et)!e7int a!e; N thi .a!e K a!eJ Q Q We have 4 ed ,ew annotation in thi e<am'#e. 3et me !o thro4!h them one b( one: +2+ 3imited i a S4b idiar( o, 5+S 3imited 10D

%ir t, we have inc#4ded the Re%ult% annotation. ) "e 4#t annotation i a co##ection o, re 4#t . =nder the re 4#t annotation, we have two re 4#t annotation . 5he re 4#t annotation have the na!e that corre 'ond to the o4tcome o, the e<ec4te method. 5he( a# o contain a #ocation a to which view ho4#d be erved corre 'ondin! to ret4rn va#4e ,rom e<ec4te7;. 5he ne<t annotation i the Acti n annotation. 5hi i 4 ed to decorate the e<ec4te7; method. 5he )ction method a# o take in a va#4e which i the ="3 on which the action i invoked. %ina##(, $ have 4 ed two )alidati n annotation . $ have con,i!4red the re@4ired ,ie#d va#idator on na!e ,ie#d and the inte!er ran!e va#idator on the a(e ,ie#d. $ have a# o 'eci,ied a c4 tom me a!e ,or the va#idation . 7 nfi(urati n File%: We rea##( do not need %trut%+,!l con,i!4ration ,i#e, o #et 4 remove thi ,i#e and #et 4 check the content o, web+,!l ,i#e: V[<m# ver ionKP1.0P encodin!KP=5%-8P[R Vweb-a'' <m#n :< iKPhtt':GGwww.w1.or!G2001GA23Schema-in tanceP <m#n KPhtt':GG*ava. 4n.comG<m#Gn G*avaeeP <m#n :webKPhtt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T2T..< dP < i: chema3ocationKPhtt':GG*ava. 4n.comG<m#Gn G*avaee htt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T1T0.< dP R Vdi '#a(-nameRStr4t 2VGdi '#a(-nameR Vwe#come-,i#e-#i tR Vwe#come-,i#eRinde<.* 'VGwe#come-,i#eR VGwe#come-,i#e-#i tR V,i#terR V,i#ter-nameR tr4t 2VG,i#ter-nameR V,i#ter-c#a R or!.a'ache. tr4t 2.di 'atcher.%i#terDi 'atcher VG,i#ter-c#a R Vinit-'aramR V'aram-nameR tr4t .dev2odeVG'aram-nameR V'aram-va#4eRtr4eVG'aram-va#4eR VGinit-'aramR VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR tr4t 2VG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R VGweb-a''R Now, ri!ht c#ick on the 'ro*ect name and c#ick E," rt X WAR File to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Ginde<.* '. 5hi wi## !ive (o4 ,o##owin! creen: Now do not enter an( re@4ired in,ormation, *4 t c#ick on #ub!it b4tton. Fo4 wi## ee ,o##owin! re 4#t: 9nter the re@4ired in,ormation b4t enter a wron! %rom ,ie#d, #et 4 a( name a Pte tP and a!e a 10, and ,ina##( c#ick on #ub!it b4tton. Fo4 wi## ee ,o##owin! re 4#t: #trut% - Ann tati n% *y"e%: Str4t 2 a''#ication can 4 e 6ava . annotation a an a#ternative to A23 and 6ava 'ro'ertie con,i!4ration. Fo4 can check the #i t o, mo t im'ortant annotation re#ated to di,,erent cate!orie : #trut% - 7 ntr l *a(% 5he Str4t 2 ta! have a et o, ta! that make it ea ( to contro# the ,#ow o, 'a!e e<ec4tion. %o##owin! i the #i t o, im'ortant Str4t 2 +ontro# 5a! : *he if and el%e ta(%: 5he e ta! 'er,orm ba ic condition ,#ow ,o4nd in ever( #an!4a!e. ?$,? ta! co4#d be 4 ed b( it e#, or with ?9# e $,? 5a! andGor in!#eGm4#ti'#e ?9# e? 5a! a hown be#ow: V :i, te tKPLN,a# eQPR VdivRWi## Not Ee 9<ec4tedVGdivR +2+ 3imited i a S4b idiar( o, 5+S 3imited 10.

VG :i,R V :e# ei, te tKPLNtr4eQPR VdivRWi## Ee 9<ec4tedVGdivR VG :e# ei,R V :e# eR VdivRWi## Not Ee 9<ec4tedVGdivR VG :e# eR *he iterat r ta(%: 5he e iterat r wi## iterate over a va#4e. )n iterab#e va#4e can be an( o,: *ava.4ti#.+o##ection, *ava.4ti#.$terator. Whi#e iteratin! over an iterator, (o4 can 4 e # rt ta! to ort the re 4#t or #ub#et ta! to to !et a 4b et o, the #i t or arra(. 5he ,o##owin! e<am'#e retrieve the va#4e o, the !etDa( 7; method o, the c4rrent ob*ect on the va#4e tack and 4 e it to iterate over. 5he V :'ro'ert(GR ta! 'rint o4t the c4rrent va#4e o, the iterator. V :iterator va#4eKPda( PR V'Rda( i : V :'ro'ert(GRVG'R VG :iteratorR *he !er(e ta(: 5he e !er(e ta! take two or more #i t a 'arameter and mer!e them a## to!ether a hown be#ow: V :mer!e varKPm(2er!ed$teratorPR V :'aram va#4eKPLNm(3i t1QP GR V :'aram va#4eKPLNm(3i t2QP GR V :'aram va#4eKPLNm(3i t1QP GR VG :mer!eR V :iterator va#4eKPLNbm(2er!ed$teratorQPR V :'ro'ert( GR VG :iteratorR *he a""end ta(: 5he e a""end ta! take two or more #i t a 'arameter and a''end them a## to!ether a hown be#ow: V :a''end varKPm()''end$teratorPR V :'aram va#4eKPLNm(3i t1QP GR V :'aram va#4eKPLNm(3i t2QP GR V :'aram va#4eKPLNm(3i t1QP GR VG :a''endR V :iterator va#4eKPLNbm()''end$teratorQPR V :'ro'ert( GR VG :iteratorR *he (enerat r ta(: 5he e (enerat r ta! !enerate an iterator ba ed on the va# attrib4te 4''#ied. %o##owin! !enerator ta! !enerate an iterator and 'rint it o4t 4 in! the iterator ta!. V :!enerator va#KPLN?aaa,bbb,ccc,ddd,eee?QPR V :iteratorR V :'ro'ert( GRVbrGR VG :iteratorR VG :!eneratorR #trut% - Data *a(% 5he Str4t 2 data ta(% are 'rimari#( 4 ed to mani'4#ate the data di '#a(ed on a 'a!e. 3i ted be#ow are the im'ortant data ta! : *he acti n ta(: 5hi ta! enab#e deve#o'er to ca## action direct#( ,rom a 6SP 'a!e b( 'eci,(in! the action name and an o'tiona# name 'ace. 5he bod( content o, the ta! i 4 ed to render the re 4#t ,rom the )ction. )n( re 4#t 'roce or de,ined ,or thi action in tr4t .<m# wi## be i!nored, 4n#e the e<ec4te"e 4#t 'arameter i 'eci,ied. VdivR5a! to e<ec4te the actionVGdivR Vbr GR V :action nameKPaction5a!)ctionP e<ec4te"e 4#tKPtr4eP GR Vbr GR VdivR5o invoke 'ecia# method in action c#a VGdivR Vbr GR +2+ 3imited i a S4b idiar( o, 5+S 3imited 10/

V :action nameKPaction5a!)ctionW 'ecia#2ethodP e<ec4te"e 4#tKPtr4eP GR *he include ta(: 5he e include wi## be 4 ed to inc#4de a 6SP ,i#e in another 6SP 'a!e. V-- %ir t S(nta< --R V :inc#4de va#4eKPm(6 '.* 'P GR V-- Second S(nta< --R V :inc#4de va#4eKPm(6 '.* 'PR V :'aram nameKP'aram1P va#4eKPva#4e2P GR V :'aram nameKP'aram2P va#4eKPva#4e2P GR VG :inc#4deR V-- 5hird S(nta< --R V :inc#4de va#4eKPm(6 '.* 'PR V :'aram nameKP'aram1PRva#4e1VG :'aramR V :'aram nameKP'aram2PRva#4e2VG :'aramR VG :inc#4deR *he bean ta(: 5he e bean ta! in tantiate a c#a that con,orm to the 6avaEean 'eci,ication. 5hi ta! ha a bod( which can contain a n4mber o, Param e#ement to et an( m4tator method on that c#a . $, the var attrib4te i et on the Eean5a!, it wi## '#ace the in tantiated bean into the tack? +onte<t. V :bean nameKPor!.a'ache. tr4t 2.4ti#.+o4nterP varKPco4nterPR V :'aram nameKP,ir tP va#4eKP20PGR V :'aram nameKP#a tP va#4eKP2.P GR VG :beanR *he date ta(: 5he e date ta! wi## a##ow (o4 to ,ormat a Date in a @4ick and ea ( wa(. Fo4 can 'eci,( a c4 tom ,ormat 7e!. PddG22G(((( hh:mmP;, (o4 can !enerate ea ( readab#e notation 7#ike Pin 2 ho4r , 1D min4te P;, or (o4 can *4 t ,a## back on a 'rede,ined ,ormat with ke( ? tr4t .date.,ormat? in (o4r 'ro'ertie ,i#e. V :date nameKP'er on.birthda(P ,ormatKPddG22G((((P GR V :date nameKP'er on.birthda(P ,ormatKPLN!et5e<t7? ome.i18n.ke(?;QP GR V :date nameKP'er on.birthda(P niceKPtr4eP GR V :date nameKP'er on.birthda(P GR *he "ara! ta(: 5he e "ara! ta! can be 4 ed to 'arameteriBe other ta! . 5hi ta! ha the ,o##owin! two 'aramter . name 7Strin!; - the name o, the 'arameter va#4e 7&b*ect; - the va#4e o, the 'arameter V'reR V4i:com'onentR V4i:'aram nameKPke(P va#4eKP80:PGR V4i:'aram nameKPva#4eP va#4eKP81:PGR V4i:'aram nameKPconte<tP va#4eKP82:PGR VG4i:com'onentR VG'reR *he "r "erty ta(: 5he e "r "erty ta! i 4 ed to !et the 'ro'ert( o, a va#4e, which wi## de,a4#t to the to' o, the tack i, none i 'eci,ied. V :'4 h va#4eKPm(EeanPR VW-- 9<am'#e 1: --R V :'ro'ert( va#4eKPm(EeanPro'ert(P GR VW-- 9<am'#e 2: --R5e<t=ti# V :'ro'ert( va#4eKPm(EeanPro'ert(P de,a4#tKPa de,a4#t va#4eP GR VG :'4 hR *he "u%h ta(: 5he e "u%h ta! i 4 ed to '4 h va#4e on tack ,or im'#i,ied 4 a!e. V :'4 h va#4eKP4 erPR +2+ 3imited i a S4b idiar( o, 5+S 3imited 100

V :'ro'er( va#4eKP,ir tNameP GR V :'ro'er( va#4eKP#a tNameP GR VG :'4 hR *he %et ta(: 5he e %et ta! a i!n a va#4e to a variab#e in a 'eci,ied co'e. $t i 4 e,4# when (o4 wi h to a i!n a variab#e to a com'#e< e<'re ion and then im'#( re,erence that variab#e each time rather than the com'#e< e<'re ion. 5he co'e avai#ab#e are a""licati n, %e%%i n, re@ue%t, "a(e and acti n. V : et nameKPm(envP va#4eKPenvironment.namePGR V :'ro'ert( va#4eKPm(envPGR *he te,t ta(: 5he e te,t ta! i 4 ed to render a $18n te<t me a!e. VW-- %ir t 9<am'#e --R V :i18n nameKP tr4t .action.te t.i18n.Sho'PR V :te<t nameKPmain.tit#ePGR VG :i18nR VW-- Second 9<am'#e --R V :te<t nameKPmain.tit#eP GR VW-- 5hird 9<am#'e --R V :te<t nameKPi18n.#abe#.!reetin! PR V :'aram R2r SmithVG :'aramR VG :te<tR *he url ta(: 5he e url ta! i 4 ed to create a ="3. V-- 9<am'#e 1 --R V :4r# va#4eKPeditGad!et.actionPR V :'aram nameKPidP va#4eKPLN e#ectedQP GR VG :4r#R V-- 9<am'#e 2 --R V :4r# actionKPeditGad!etPR V :'aram nameKPidP va#4eKPLN e#ectedQP GR VG :4r#R V-- 9<am'#e 1--R V :4r# inc#4deParam KP!etPR V :'aram nameKPidP va#4eKPLN?22?QP GR VG :4r#R #trut% - $ *he F r! *a(% 5he #i t o, f r! ta! i a 4b et o, Str4t =$ 5a! . 5he e ta! he#' in the renderin! o, the 4 er inter,ace re@4ired ,or the Str4t web a''#ication and can be cate!ori ed into three cate!orie . 5hi cha'ter wi## take (o4 thor4!h a## the three t('e o, =$ ta! : #i!"le 5I *a(%: We have 4 ed the e ta! in o4r e<am'#e a#read(, we wi## br4 h them in thi cha'ter. 3et 4 #ook a im'#e view 'a!e e!ail+j%" with evera# im'#e =$ ta! : VL^ 'a!e #an!4a!eKP*avaP content5('eKPte<tGhtm#J char etK$S&-88.--1P 'a!e9ncodin!KP$S&-88.--1PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VWD&+5FP9 htm# P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR V :headGR Vtit#eRCe##o Wor#dVGtit#eR VGheadR Vbod(R V :divR9mai# %ormVG :divR +2+ 3imited i a S4b idiar( o, 5+S 3imited 108

V :te<t nameKPP#ea e ,i## in the ,orm be#ow:P GR V :,orm actionKPhe##oP methodKP'o tP enct('eKPm4#ti'artG,orm-dataPR V :hidden nameKP ecretP va#4eKPabracadabraPGR V :te<t,ie#d ke(KPemai#.,romP nameKP,romP GR V :'a word ke(KPemai#.'a wordP nameKP'a wordP GR V :te<t,ie#d ke(KPemai#.toP nameKPtoP GR V :te<t,ie#d ke(KPemai#. 4b*ectP nameKP 4b*ectP GR V :te<tarea ke(KPemai#.bod(P nameKPemai#.bod(P GR V :#abe# ,orKPattachmentP va#4eKP)ttachmentPGR V :,i#e nameKPattachmentP acce'tKPte<tGhtm#,te<tG'#ainP GR V :token GR V : 4bmit ke(KP 4bmitP GR VG :,ormR VGbod(R VGhtm#R $, (o4 are aware o, C523 then a## the ta! 4 ed are ver( common C523 ta! with an additiona# 're,i< %: a#on!with each ta! and di,,erent attrib4te . When we e<ec4te the above 'ro!ram, we !et the ,o##owin! 4 er inter,ace 'rovided (o4 have et4' 'ro'er ma''in! ,or a## the ke( 4 ed. ) hown, the :head !enerate the *ava cri't and t(#e heet e#ement re@4ired ,or the Str4t 2 a''#ication. Ne<t, we have the :div and :te<t e#ement . 5he :div i 4 ed to render a C523 Div e#ement. 5hi i 4 e,4# ,or 'eo'#e who do not #ike to mi< C523 and Str4t ta! to!ether. %or tho e 'eo'#e, the( have the choice to 4 e :div to render a div. 5he :te<t a hown i 4 ed to render a te<t on the creen. Ne<t we have the ,amii#ar :,orm ta!. 5he :,orm ta! ha an action attrib4te that determine where to 4bmit the ,orm. Eeca4 e we have a ,i#e 4'#oad e#ement in the ,orm, we have to et the enct('e to m4#ti'art. &therwi e, we can #eave thi b#ank. )t the end o, the ,orm ta!, we have the : 4bmit ta!. 5hi i 4 ed to 4bmit the ,orm. When the ,orm i 4bmitted, a## the ,orm va#4e are 4bmitted to the the action 'eci,ied in the :,orm ta! $n ide the :,orm, we have a hidden attrib4te ca##ed ecret. 5hi render a hidden e#ement in the C523. $n o4r ca e, the P ecretP e#ement ha the va#4e PabracadabraP. 5hi e#ement i not vi ib#e to the end 4 er and i 4 ed to carr( the tate ,rom one view to another. Ne<t we have the :#abe#, :te<t,ie#d, :'a word and :te<tarea ta! . 5he e are 4 ed to render the #abe#, in'4t ,ie#d, 'a word and the te<t area re 'ective#(. We have een the e in action in the PStr4t Sendin! 9mai#P e<am'#e. 5he im'ortant thin! to note here i the 4 e o, Pke(P attrib4te. 5he Pke(P attrib4te i 4 ed to ,etch the #abe# ,or the e contro# ,rom the 'ro'ert( ,i#e. We have a#read( covered thi ,eat4re in the Str4t -$nterna#iBation cha'ter. 5hen, we have the :,i#e ta! which render a in'4t ,i#e 4'#oad com'onent. 5hi com'onent a##ow the 4 er to 4'#oad ,i#e . $n thi e<am'#e, we have 4 ed the Pacce'tP 'aramter o, the :,i#e ta! to 'eci,( which ,i#e t('e are a##owed to be 4'#oaded. %ina##( we have the :token ta!. 5he token ta! !enerate an 4ni@4e token which i 4 ed to ,ind o4t whether a ,orm ha been do4b#e 4bmitted. When the ,orm i rendered, a hidden variab#e i '#aced a the token va#4e. 3et 4 a(, ,or e<am'#e that the token i P)E+P. When thi ,orm i 4bmitted, the Str4t %it#er check the token a!ain t the token tored in the e ion. $, it matche , it remove the token ,rom the e ion. Now, i, the ,orm i accidenta##( re 4bmitted 7either b( re,re hin! or b( hittin! the brow er back b4tton;, the ,orm wi## be re 4bmitted with P)E+P a the token. $n thi ca e, the ,i#ter check the token a!ain t the token tored in the e ion a!ain. E4t beca4 e the token P)E+P ha been removed ,rom the e ion, it wi## not match and the Str4t ,i#ter wi## re*ect the re@4e t. Cr u" 5I *a(%: 5he !ro4' =$ ta! are 4 ed to create radio b4tton and the checkbo<. 3et 4 #ook a im'#e view 'a!e 'ell W rld+j%" with check bo< and radio b4tton ta! : VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR V :head GR VGheadR +2+ 3imited i a S4b idiar( o, 5+S 3imited 10-

Vbod(R V :,orm actionKPhe##o.actionPR V :radio #abe#KPGenderP nameKP!enderP #i tKPN?ma#e?,?,ema#e?QP GR V :checkbo<#i t #abe#KPCobbie P nameKPhobbie P #i tKPN? 'ort ?,?tv?,? ho''in!?QP GR VG :,ormR VGbod(R VGhtm#R When we e<ec4te the above 'ro!ram o4r o4t'4t wi## #ook imi#ar to the ,o##owin!: 3et 4 #ook at the e<am'#e now. $n the ,ir t e<am'#e, we are creatin! a im'#e radiob4tton with the #abe# PGenderP. 5he name attrib4te i mandator( ,or the radiob4tton ta!, o we 'eci,( a name which i P!enderP. We then 4''#( a #i t to the !ender. 5he #i t i 'o'4#ated with the va#4e Pma#eP and P,ema#eP. 5here,ore, in the o4t'4t we !et a radiob4tton with two va#4e in it. $n the econd e<am'#e, we are creatin! a checkbo< #i t. 5hi i to !ather the 4 er? hobbie . 5he 4 er can have more than one hobb( and there,ore we are 4 in! the checkbo< in tead o, the radiob4tton. 5he checkbo< i 'o'4#ated with the #i t P 'ort P, P5vP and PSho''in!P. 5hi 're ent the hobbie a a checkbo< #i t. #elect 5I *a(%: 3et 4 e<'#ore the di,,erent variation o, the Se#ect 5a! o,,ered b( Str4t . 3et 4 #ook a im'#e view 'a!e 'ell W rld+j%" with e#ect ta! : VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR V :head GR VGheadR Vbod(R V :,orm actionKP#o!in.actionPR V : e#ect nameKP4 ernameP #abe#KP= ernameP #i tKPN?2ike?,?6ohn?,?Smith?QP GR V : e#ect #abe#KP+om'an( &,,iceP nameKPm(Se#ectionP va#4eKPLN?)merica?QP #i tKPLNbN?)merica?:?)merica?QQPR V :o't!ro4' #abe#KP) iaP #i tKPLNbN?$ndia?:?$ndia?,?+hina?:?+hina?QQP GR V :o't!ro4' #abe#KP94ro'eP #i tKPLNbN?=H?:?=H?,?Sweden?:?Sweden?,?$ta#(?:?$ta#(?QQP GR VG : e#ectR V :combobo< #abe#KP2( Si!nP nameKPm(Si!nP #i tKPbN?arie ?:?arie ?,?ca'ricorn?:?ca'ricorn?QP headerHe(KP-1P headerVa#4eKP--- P#ea e Se#ect ---P em't(&'tionKPtr4eP va#4eKPca'ricornP GR V :do4b#e e#ect #abe#KP&cc4'ationP nameKPocc4'ationP #i tKPN?5echnica#?,?&ther?QP do4b#eNameKPocc4'ation 2P do4b#e3i tKPto' KK ?5echnica#? [ N?$.5?, ?Cardware?Q : N?)cco4ntin!?, ?C."?QP GR VG :,ormR VGbod(R VGhtm#R When we e<ec4te the above 'ro!ram o4r o4t'4t wi## #ook imi#ar to the ,o##owin!: 3et 4 now !o thro4!h the individ4a# ca e , one b( one.

+2+ 3imited i a S4b idiar( o, 5+S 3imited

180

%ir t, the e#ect ta! render the C523 e#ect bo<. $n the ,ir t e<am'#e, we are creatin! a im'#e e#ect bo< with name P4 ernameP and the #abe# P4 ernameP. 5he e#ect bo< wi## be 'o'4#ated with a #i t that contain the name 2ike, 6ohn and Smith. $n the econd e<am'#e, o4r com'an( ha head o,,ice in )merica. $t a# o ha !#oba# o,,ice in ) ia and 94ro'e. We want to di '#a( the o,,ice in a e#ect bo< b4t we want to !ro4' the !#oba# o,,ice b( the name o, the continent. 5hi i where the o't!ro4' come in hand(. We 4 e the :o't!ro4' ta! to create a new !ro4'. We !ive the !ro4' a #abe# and a e'arate #i t. $n the third e<am'#e, the combobo< i 4 ed. ) combo bo< i a combination o, an in'4t ,ie#d and a e#ect bo<. 5he 4 er can either e#ect a va#4e ,rom the e#ect bo< in which ca e the in'4t ,ie#d i a4tomatica##( ,i##ed in with the va#4e the 4 er ha e#ected. Sho4#d the 4 er to enter a va#4e direct#( then no va#4e ,rom the e#ect bo< wi## be e#ected. $n o4r e<am'#e we have the combobo< #i tin! the 4n i!n . 5he e#ectbo< #i t on#( ,o4r entrie a##owin! the 4 er to t('e in hi 4n i!n i, it i not in the #i t. We a# o add a header entr( to the e#ect bo<. 5he headerentr( i the one that i di '#a(ed at the to' o, the e#ect bo<. $n o4r ca e we want to di '#a( PP#ea e Se#ectP. $, the 4 er doe not e#ect an(thin!, then we a 4me -1 a the va#4e. $n ome ca e , we do not want the 4 er to e#ect an em't( va#4e. $n tho e condition , one wo4#d et the Pem't(&'tionP 'ro'ert( to ,a# e. %ina##(, in o4r e<am'#e we 4''#( Pca'ricornP a the de,a4#t va#4e ,or the combobo<. $n the ,o4rth e<am'#e, we have a do4b#e e#ect. ) do4b#e e#ect i 4 ed when (o4 want to di '#a( two e#ect bo<e . 5he va#4e e#ected in the ,ir t e#ect bo< determine what a''ear in the econd e#ect bo<. $n o4r e<am'#e the ,ir t e#ect bo< di '#a( P5echnica#P and P&therP. $, the 4 er e#ect 5echnica#, we wi## di '#a( $5 and Cardware in the econd e#ect bo<. &therwi e we wi## di '#a( )cco4ntin! and C". 5hi i 'o ib#e 4 in! the P#i tP and Pdo4b#e3i tP atrrib4te a hown in the e<am'#e. $n the above e<am'#e, we do a com'ari on to ee i, the to' e#ect bo< e@4a# 5echica#. $, it doe , then we di '#a( $5 and Cardware. We a# o need to !ive a name ,or the to' bo< 7PnameK?&cc4'ation ?; and the bottom bo< 7do4b#eNameK?occ4'ation 2?; #trut% - $ *he Aja, *a(% Str4t 4 e the D&6& ,ramework ,or the )6)A ta! im'#ementation. %ir t o, a##, to 'roceed with thi e<am'#e, (o4 need to add tr4t 2-do*o-'#4!in-2.2.1.*ar to (o4r c#a 'ath. Fo4 can !et thi ,i#e ,rom the #ib ,o#der o, (o4r tr4t 2 down#oad 7+:I tr4t -2.2.1-a##I tr4t -2.2.1I#ibI tr4t 2-do*o-'#4!in-2.2.1.*ar; %or thi e<ercie , #et 4 modi,( 'ell W rld+j%" a ,o##ow : VL^ 'a!e content5('eKPte<tGhtm#J char etK=5%-8PLR VL^ ta!#ib 're,i<KP P 4riKPG tr4t -ta! PLR VL^ ta!#ib 're,i<KP <P 4riKPG tr4t -do*o-ta! PLR Vhtm#R VheadR Vtit#eRCe##o Wor#dVGtit#eR V :head GR V <:head GR VGheadR Vbod(R V :,ormR V <:a4tocom'#eter #abe#KP%avo4rite +o#o4rP #i tKPN?red?,?!reen?,?b#4e?QP GR Vbr GR V <:datetime'icker nameKPde#iver(dateP #abe#KPDe#iver( DateP di '#a(%ormatKPddG22G((((P GR Vbr GR V :4r# GR V <:div hre,KPLNb4r#QP de#a(KP2000PR $nitia# +ontent VG <:divR VbrGR V <:tabbed'ane# R V <:div #abe#KP5ab 1PR5ab 1VG <:divR V <:div #abe#KP5ab 2PR5ab 2VG <:divR VG <:tabbed'ane#R +2+ 3imited i a S4b idiar( o, 5+S 3imited 181

VG :,ormR VGbod(R VGhtm#R When we r4n the above e<am'#e, we !et the ,o##owin! o4t'4t: 3et 4 now !o thro4!h thi e<am'#e one te' at a time. %ir t thin! to notice i the addition o, a new ta! #ibrar( with the 're,i< <. 5hi 7 tr4t -do*o-ta! ; i the ta! #ibrar( 'eci,ica##( created ,or the a*a< inte!ration. 5hen in ide the C523 head we ca## the <:head. 5hi initia#iBe the do*o ,ramework and make it read( ,or a## )6)A invocation within the 'a!e. 5hi te' i im'ortant - (o4r a*a< ca## wi## not work witho4t the <:head bein! initia#iBed. %ir t we have the a4tocom'#eter ta!. 5he a4tocom'#eter ta! #ook 'rett( m4ch #ike a e#ect bo<. $t i 'o'4#ated with the va#4e red, !reen and b#4e. E4t the di,,erent between a e#ect bo< and thi one i that it a4to com'#ete . 5hat i , i, (o4 tart t('in! in !r, it wi## ,i## it with P!reenP. &ther than that thi ta! i ver( m4ch imi#ar to the : e#ect ta! which we covered ear#ier. Ne<t, we have a date time 'icker. 5hi ta! create an in'4t ,ie#d with a b4tton ne<t to it. When the b4tton i 're ed, a 'o'4' date time 'icker i di '#a(ed. When the 4 er e#ect a date, the date i ,i##ed into the in'4t te<t in the ,ormat that i 'eci,ied in the ta! attrib4te. $n o4r e<am'#e, we have 'eci,ied ddG22G(((( a the ,ormat ,or the date. Ne<t we create a 4r# ta! to the ( tem.action ,i#e which we created in the ear#ier e<erci e . $t doe n?t have to be the ( tem.action - it co4#d be an( action ,i#e that (o4 created ear#ier. 5hen we have a div with the h('er#ink et to the 4r# and de#a( et to 2 econd . What ha''en when (o4 r4n thi i , the P$nitia# +ontentP wi## be di '#a(ed ,or 2 econd , then the div? content wi## be re'#aced with the content ,rom the hell +acti n e<ec4tion. %ina##( we have a im'#e tab 'ane# with two tab . 5he tab are div them e#eve with the #abe# 5ab 1 and 5ab2. $t ho4#d be worth notin! that the )6)A ta! inte!ration in Str4t i ti## a work in 'ro!re and the mat4rit( o, thi inte!ration i #ow#( increa in! with ever( re#ea e. #trut% - and *ile% Inte(rati n $n thi cha'ter, #et 4 !o thro4!h the te' invo#ved in inte!ratin! the 5i#e ,ramework with Str4t 2. )'ache 5i#e i a tem'#atin! ,ramework b4i#t to im'#i,( the deve#o'ment o, web a''#ication 4 er inter,ace . %ir t o, a## we need to down#oad the ti#e *ar ,i#e ,rom the )'ache 5i#e web ite. Fo4 need to add the ,o##owin! *ar ,i#e to the 'ro*ect? c#a 'ath. ti#e -a'i-<.(.B.*ar ti#e -com'at-<.(.B.*ar ti#e -core-<.(.B.*ar ti#e -* '-<.(.B.*ar ti#e - erv#et-<.(.B.*ar $n addition to the above, we have to co'( the ,o##owin! *ar ,i#e ,rom the tr4t 2 down#oad in (o4r WE6$ INF/lib. common -bean4ti# -<.(.B*ar common -di!e ter-<.(.*ar tr4t 2-ti#e -'#4!in-<.(.B.*ar Now #et 4 et4' the web+,!l ,or the Str4t -5i#e inte!ration a !iven be#ow. 5here are two im'ortant 'oint to note here. %ir t, we need to te## ti#e , where to ,ind ti#e con,i!4ration ,i#e tile%+,!l. $n o4r ca e, it wi## be 4nder /WE6$INF ,o#der. Ne<t we need to initi#iaBe the 5i#e #i tener that come with Str4t 2 down#oad. V[<m# ver ionKP1.0P encodin!KP=5%-8P[R Vweb-a'' <m#n :< iKPhtt':GGwww.w1.or!G2001GA23Schema-in tanceP <m#n KPhtt':GG*ava. 4n.comG<m#Gn G*avaeeP <m#n :webKPhtt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T2T..< dP < i: chema3ocationKPhtt':GG*ava. 4n.comG<m#Gn G*avaee htt':GG*ava. 4n.comG<m#Gn G*avaeeGweb-a''T2T..< dP R Vdi '#a(-nameRStr4t 29<am'#e1.VGdi '#a(-nameR Vconte<t-'aramR V'aram-nameR +2+ 3imited i a S4b idiar( o, 5+S 3imited 182

or!.a'ache.ti#e .im'#.Ea ic5i#e +ontainer.D9%$N$5$&NST+&N%$G VG'aram-nameR V'aram-va#4eR GW9E-$N%Gti#e .<m# VG'aram-va#4eR VGconte<t-'aramR V#i tenerR V#i tener-c#a R or!.a'ache. tr4t 2.ti#e .Str4t 5i#e 3i tener VG#i tener-c#a R VG#i tenerR V,i#terR V,i#ter-nameR tr4t 2VG,i#ter-nameR V,i#ter-c#a R or!.a'ache. tr4t 2.di 'atcher.n!.,i#ter.Str4t Pre'are)nd9<ec4te%i#ter VG,i#ter-c#a R VG,i#terR V,i#ter-ma''in!R V,i#ter-nameR tr4t 2VG,i#ter-nameR V4r#-'atternRGOVG4r#-'atternR VG,i#ter-ma''in!R Vwe#come-,i#e-#i tR Vwe#come-,i#eRinde<.* 'VGwe#come-,i#eR VGwe#come-,i#e-#i tR VGweb-a''R Ne<t #et 4 create tile%+,!l 4nder GW9E-$N% ,o#der with the ,o##owin! content : V[<m# ver ionKP1.0P encodin!KP=5%-8P [R VWD&+5FP9 ti#e -de,inition P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D 5i#e +on,i!4ration 2.0GG9NP Phtt':GGti#e .a'ache.or!Gdtd Gti#e -con,i!T2T0.dtdPR Vti#e -de,inition R Vde,inition nameKPba e3a(o4tP tem'#ateKPGba e3a(o4t.* 'PR V'4t-attrib4te nameKPtit#eP va#4eKP5em'#atePGR V'4t-attrib4te nameKPbannerP va#4eKPGbanner.* 'PGR V'4t-attrib4te nameKPmen4P va#4eKPGmen4.* 'PGR V'4t-attrib4te nameKPbod(P va#4eKPGbod(.* 'PGR V'4t-attrib4te nameKP,ooterP va#4eKPG,ooter.* 'PGR VGde,initionR Vde,inition nameKPti!erP e<tend KPba e3a(o4tPR V'4t-attrib4te nameKPtit#eP va#4eKP5i!erPGR V'4t-attrib4te nameKPbod(P va#4eKPGti!er.* 'PGR VGde,initionR Vde,inition nameKP#ionP e<tend KPba e3a(o4tPR V'4t-attrib4te nameKPtit#eP va#4eKP3ionPGR V'4t-attrib4te nameKPbod(P va#4eKPG#ion.* 'PGR VGde,initionR VGti#e -de,inition R 2e<t. we de,ine a ba ic ke#eton #a(o4t in the ba%e4ay ut+j%". $t ha ,ive re4 ab#e G overridab#e area . Name#( title, banner, !enu, b dy and f ter. We 'rovide the de,a4#t va#4e ,or the ba e3a(o4t and +2+ 3imited i a S4b idiar( o, 5+S 3imited 181

then we create two c4 tomiBation that e<tend ,rom the de,a4#t #a(o4t. 5he ti!er #a(o4t i imi#ar to the ba ic #a(o4t, e<ce't it 4 e the ti(er+j%" a it bod( and the te<t P5i!erP a the tit#e. Simi#ar#(, the #ion #a(o4t i imi#ar to the ba ic #a(o4t, e<ce't it 4 e the li n+j%" a it bod( and the te<t P3ionP a the tit#e. 3et 4 have a #ook at the individ4a# * ' ,i#e . %o##owin! i the content o, ba%e4ay ut+j%" ,i#e: VL^ ta!#ib 4riKPhtt':GGti#e .a'ache.or!Gta! -ti#e P 're,i<KPti#e PLR VWD&+5FP9 C523 P=E3$+ P-GGW1+GGD5D C523 D.01 5ran itiona#GG9NP Phtt':GGwww.w1.or!G5"Ghtm#DG#oo e.dtdPR Vhtm#R VheadR Vmeta htt'-e@4ivKP+ontent-5('eP contentKPte<tGhtm#J char etK=5%-8PR Vtit#eRVti#e :in ert)ttrib4te nameKPtit#eP i!noreKPtr4eP GR VGtit#eR VGheadR Vbod(R Vti#e :in ert)ttrib4te nameKPbannerP GRVbrGR VhrGR Vti#e :in ert)ttrib4te nameKPmen4P GRVbrGR VhrGR Vti#e :in ert)ttrib4te nameKPbod(P GRVbrGR VhrGR Vti#e :in ert)ttrib4te nameKP,ooterP GRVbrGR VGbod(R VGhtm#R Cere we *4 t '4t to!ether a ba ic C523 'a!e that ha the ti#e attrib4te . We in ert the ti#e attrib4te in the '#ace where we need them to be. Ne<t, #et 4 create banner+j%" ,i#e with the ,o##owin! content: Vim! rcKP ima!e Gt'-#o!o.!i,PGR 5he !enu+j%" ,i#e wi## have ,o##owin! #ine which are the #ink - to the 5i!er2en4.action and the 3ion2en4.action tr4t action . VL^ta!#ib 4riKPG tr4t -ta! P 're,i<KP PLR Va hre,KPV :4r# actionKPti!er2en4PGRP 5i!erVGaRVbrR Va hre,KPV :4r# actionKP#ion2en4PGRP 3ionVGaRVbrR 5he li n+j%" ,i#e wi## have ,o##owin! content: Vim! rcKPhtt':GG4'#oad.wikimedia.or!Gwiki'ediaGcommon GdGd2G3ion.*'!PGR 5he #ion 5he ti(er+j%" ,i#e wi## have ,o##owin! content: Vim! rcKPhtt':GGwww.,reeweb .comGti!ero,dart Gti!er.*'!PGR 5he ti!er Ne<t, #et 4 create the action c#a ,i#e &enuActi n+ja)a which contain the ,o##owin!: 'acka!e com.t4toria# 'oint. tr4t 2J im'ort com.o'en (m'hon(.<work2.)ctionS4''ortJ '4b#ic c#a 2en4)ction e<tend )ctionS4''ort N '4b#ic Strin! ti!er7; N ret4rn Pti!erPJ Q '4b#ic Strin! #ion7; N ret4rn P#ionPJ Q Q 5hi i a 'rett( trai!ht ,orward c#a . We dec#ared two method ti!er7; and #ion7; that ret4rn ti!er and #ion a o4tcome re 'ective#(. 3et 4 '4t it a## to!ether in the %trut%+,!l ,i#e: VWD&+5FP9 tr4t P=E3$+ P-GG)'ache So,tware %o4ndationGGD5D Str4t +on,i!4ration 2.0GG9NP Phtt':GG tr4t .a'ache.or!Gdtd G tr4t -2.0.dtdPR V tr4t R V'acka!e nameKPde,a4#tP e<tend KP tr4t -de,a4#tPR Vre 4#t-t('e R +2+ 3imited i a S4b idiar( o, 5+S 3imited

18D

Vre 4#t-t('e nameKPti#e P GR VGre 4#t-t('e R Vaction nameKPO2en4P methodKPN1QP R Vre 4#t nameKPti!erP t('eKPti#e PRti!erVGre 4#tR Vre 4#t nameKP#ionP t('eKPti#e PR#ionVGre 4#tR VGactionR VG'acka!eR VG tr4t R 3et 4 check what we did in above ,i#e. %ir t o, a##, we dec#ared a new re 4#t t('e ca##ed Pti#e P a we are now 4 in! ti#e in tead o, '#ain * ' ,or the view techno#o!(. Str4t 2 ha it 4''ort ,or the 5i#e View re 4#t t('e, o we create the re 4#t t('e Pti#e P to be o, the Por!.a'ache. tr4t 2.view.ti#e .5i#e "e 4#tP c#a . Ne<t, we want to a( i, the re@4e t i ,or Gti!er2en4.action take the 4 er to the ti!er ti#e 'a!e and i, the re@4e t i ,or G#ion2en4.action take the 4 er to the #ion ti#e 'a!e. We achieve thi 4 in! a bit o, re!4#ar e<'re ion. $n o4r action de,inition, we a( an(thin! that matche the 'attern PO2en4P wi## be hand#ed b( thi action. 5he matchin! method wi## be invoked in the 2en4)ction c#a . 5hat i , ti!er2en4.action wi## invoke ti!er7; and #ion2en4.action wi## invoke #ion7;. We then need to ma' the o4tcome o, the re 4#t to the a''ro'riate ti#e 'a!e . Now ri!ht c#ick on the 'ro*ect name and c#ick 9<'ort R W)" %i#e to create a War ,i#e. 5hen de'#o( thi W)" in the 5omcat? weba'' director(. %ina##(, tart 5omcat erver and tr( to acce ="3 htt':GG#oca#ho t:8080GCe##oWor#dStr4t 2Gti!er2en4.* '. 5hi wi## !ive (o4 ,o##owin! creen: Simi#ar#(, i, (o4 !oto the #ion2en4.action 'a!e, (o4 wi## ee the #ion 'a!e which 4 e the ame ti#e #a(o4t.

+2+ 3imited i a S4b idiar( o, 5+S 3imited

18.

You might also like