Vous êtes sur la page 1sur 13

MCA - Semester 4 MC0078 - 1ava Programming

Assignment Set - 2 Roll No.



1

MAY 2011
Master of Computer AppIication (MCA) - Semester 4
MC0078 - Java Programming - 4 Credits
(Book ID: B0831 & B0832)
Assignment Set - 2 (40 Marks)
Answer aII Questions Each Question carries five marks
Book ID: B0831
1 Wr|te a program to demonstrate the concept of I]C streams |n Iava
Ans 1
lmporL [avalo*
// ulsplays conLenLs of a flle
//(eg [ava 1ype applnl)
publlc class 1ype

publlc sLaLlc vold maln(
SLrlng args) Lhrows LxcepLlon

// Cpen lnpuL/ouLpuL and seLup varlables
llle8eader fr new llle8eader(args0)
rlnLWrlLer pw new rlnLWrlLer(
SysLemouL Lrue)
char c new char4096
lnL read 0
// 8ead (and prlnL) Llll end of flle
whlle ((read frread(c)) ! 1)
pwwrlLe(c 0 read)
// Close shop
frclose()
pwclose()


MCA - Semester 4 MC0078 - 1ava Programming
Assignment Set - 2 Roll No.

2

now do you |mp|ements |nher|tance |n [ava?
Ans
Imp|ement|ng Inher|tance |n Iava
1he exteoJs eyword ls used Lo derlve a class from a superclass or ln oLher words exLend Lhe
funcLlonallLy of a superclass
yntax
publlc class subclass_nameexLendssuperclass_name
Lxamp|e
publlc class conflrmed exLends LlceL


ku|es for Cverr|d|ng Methods
r 1he meLhod name and Lhe order of argumenLs should be ldenLlcal Lo LhaL of Lhe superclass
meLhod
r 1he reLurn Lype of boLh Lhe meLhods musL be Lhe same
r 1he overrldlng meLhod cannoL be less accesslble Lhan Lhe meLhod lL overrldes lor example lf Lhe
meLhod Lo overrlde ls declared as publlc ln Lhe superclass you cannoL overrlde lL wlLh Lhe prlvaLe
eyword ln Lhe subclass
r An overrldlng meLhod cannoL ralse more excepLlons Lhan Lhose ralsed by Lhe superclass
Lxamp|e
// CreaLe a superclass
class A
lnL l [
vold showl[()
SysLemouLprlnLln(l and [ + l + + [)


// CreaLe a subclass by exLendlng class A
class 8 exLends A
lnL
vold show()
SysLemouLprlnLln( + )

MCA - Semester 4 MC0078 - 1ava Programming
Assignment Set - 2 Roll No.

3

vold sum()
SysLemouLprlnLln(l+[+ + (l+[+))


class SlmplelnherlLance
publlc sLaLlc vold maln(SLrlng args)
A superCb new A()
8 subCb new 8()
// 1he superclass may be used by lLself
superCbl 10
superCb[ 20
SysLemouLprlnLln(ConLenLs of superCb )
superCbshowl[()
SysLemouLprlnLln()
/* 1he subclass has access Lo all publlc members of
lLs superclass */
subCbl 7
subCb[ 8
subCb 9
SysLemouLprlnLln(ConLenLs of subCb )
subCbshowl[()
subCbshow()
SysLemouLprlnLln()
SysLemouLprlnLln(Sum of l [ and ln subCb)
subCbsum()


1he ouLpuL from Lhls program ls shown here
ConLenLs of superCb
l and [ 10 20
ConLenLs of subCb
l and [ 7 8
9
Sum of l [ and ln subCb
MCA - Semester 4 MC0078 - 1ava Programming
Assignment Set - 2 Roll No.

4

l+[+ 24
As you can see Lhe subclass lncludes all of Lhe members of lLs superclass A 1hls ls why subCb can
access | and [ and call show|[ ( ) Also lnslde sum ( ) | and [ can be referred Lo dlrecLly as lf Lhey
were parL of
Lven Lhough A ls a superclass for lL ls also a compleLely lndependenL sLandalone class 8elng a
superclass for a subclass does noL mean LhaL Lhe superclass cannoL be used by lLself lurLher a
subclass can be a superclass for anoLher subclass
1he general form of a c|ass declaraLlon LhaL lnherlLs a superclass ls shown here
class sobclossoome exLends sopetclossoome
// body of class

?ou can only speclfy one superclass for any subclass LhaL you creaLe !ava does noL supporL Lhe
lnherlLance of mulLlple superclasses lnLo a slngle subclass (1hls dlffers from C++ ln whlch you can
lnherlL mulLlple base classes) ?ou can as sLaLed creaLe a hlerarchy of lnherlLance ln whlch a
subclass becomes a superclass of anoLher subclass
Powever no class can be a superclass of lLself

3 Lxp|a|n t|er and Nt|er arch|tecture??
Ans 3

@|er Arch|tecture
1woLler archlLecLure ls a cllenL/server archlLecLure where a requesL Lo do some Las ls senL
Lo Lhe server and Lhe server responds by performlng Lhe Laswhere as a LhreeLler or a mulLl Ller
archlLecLure has cllenL server and daLabase Where Lhe cllenL requesL ls senL Lo Lhe server and Lhe
server ln Lurn sends Lhe requesL Lo Lhe daLabase 1he daLabase sends bac Lhe lnformaLlon/daLa
requlred Lo Lhe server whlch lnLurn sends lL Lo Lhe cllenL

1hls ls an example of a 2Ller archlLecLure 1he Lwo Llers are
1 uaLa server Lhe daLabase serves up daLa based on SCL querles submlLLed by Lhe appllcaLlon
CllenL appllcaLlon Lhe appllcaLlon on Lhe cllenL compuLer consumes Lhe daLa and presenLs lL ln
a readable formaL Lo Lhe sLudenL
ln 1wo Ller Lhere ls presenL only one cllenL and anoLher one ls Server 8uL ln Lhree Ller appllcaLlons
Lhere are 3 layers
1 resentat|on Layer
u|sness |ayer
3 erv|ce Layer
MCA - Semester 4 MC0078 - 1ava Programming
Assignment Set - 2 Roll No.

3



Nt|er arch|tecture
%ier and ExampIe
Dsually n1ler ArchlLecLure beglns as a 31ler model and ls expanded lL provldes flner granularlLy
CranularlLy ls Lhe ablllLy of a sysLem ln Lhls case an appllcaLlon Lo be broen down lnLo smaller
componenLs or granules 1he flner Lhe granularlLy Lhe greaLer Lhe flexlblllLy of a sysLem lL can also
be referred Lo as a sysLem's modularlLy 1herefore lL refers Lo Lhe pulllng aparL of an appllcaLlon lnLo
separaLe layers or flner gralns


Cne of Lhe besL examples of n1ler ArchlLecLure ln web appllcaLlons ls Lhe popular shopplngcarL
web appllcaLlon 1he cllenL Ller lnLeracLs wlLh Lhe user Lhrough CDls (Craphlc Dser lnLerfaces) and
wlLh Lhe appllcaLlon and Lhe appllcaLlon server ln web appllcaLlons Lhls cllenL Ller ls a web browser
ln addlLlon Lo lnlLlaLlng Lhe requesL Lhe web browser also recelves and dlsplays code ln dynamlc
P1ML (PyperLexL Marup Language) Lhe prlmary language of Lhe World Wlde Web ln a shopplng
carL web appllcaLlon Lhe presenLaLlon Ller dlsplays lnformaLlon relaLed Lo such servlces as browslng
merchandlse purchaslng and shopplng carL conLenLs lL communlcaLes wlLh oLher Llers by
ouLpuLLlng resulLs Lo Lhe browser/cllenL Ller and all oLher Llers ln Lhe neLwor 1hls layer calls cusLom
Lags LhroughouL Lhe neLwor and Lo oLher neLwors lL also calls daLabase sLored procedures
and web services all ln Lhe goal of provldlng a more sophlsLlcaLed response 1hls layer glues Lhe
whole appllcaLlon LogeLher and allows dlfferenL nodes Lo communlcaLe wlLh each oLher and be
dlsplayed Lo Lhe user Lhrough Lhe browser lL ls locaLed ln Lhe appllcaLlon server
MCA - Semester 4 MC0078 - 1ava Programming
Assignment Set - 2 Roll No.

6


ln n1ler ArchlLecLure Lhe buslness loglc Ller ls pulled ouL from Lhe presenLaLlon Ller and as lLs own
layer lL conLrols an appllcaLlon's funcLlonallLy by performlng deLalled processlng lor example ln our
shopplng carL example Lhls Ller compleLes and calculaLes Lhlngs lle shlpplng cosLs and sales Lax
1he Lools used Lo encapsulaLe an appllcaLlon's buslness loglc lnLo lLs own layer lnclude web servlces
cusLom Lags and sLored procedures



4 What are the d|fference between an |nterface and an abstract c|ass?
Interfaces verses abstract c|asses
Whlle an lnLerface ls used Lo speclfy Lhe form LhaL someLhlng musL have lL does noL acLually provlde
Lhe lmplemenLaLlon for lL ln Lhls sense an lnLerface ls a llLLle lle an absLracL class LhaL musL be
exLended ln exacLly Lhe manner LhaL lLs absLracL meLhods speclfy

r An absLracL class ls an lncompleLe class LhaL requlres furLher speclallzaLlon An lnLerface ls [usL a
speclflcaLlon or prescrlpLlon for behavlor
r An lnLerface does noL have any overLones of speclallzaLlon LhaL are presenL wlLh lnherlLance
r A class can lmplemenL several lnLerfaces aL once whereas a class can exLend only one parenL class
r lnLerfaces can be used Lo supporL callbacs (lnherlLance does noL help wlLh Lhls) 1hls ls a
MCA - Semester 4 MC0078 - 1ava Programming
Assignment Set - 2 Roll No.

7

slgnlflcanL codlng ldlom lL essenLlally provldes a polnLer Lo a funcLlon buL ln a Lypesafe way
Callbac ls explalned ln Lhe nexL secLlon
@he abstract keyword
1he absLracL eyword ls used Lo declare classes LhaL deflne common properLles and behavlor of
oLher classes An absLracL ls used as a base class Lo derlve speclflc classes of Lhe same lnd

ook ID 3
S Lxp|a|n the work|ng of struts w|th an examp|e?
Work|ng of struts
SLruLs ls based on Lhe Llmeproven ModelvlewConLroller (MvC) deslgn paLLern 1he MvC paLLern ls
wldely recognlzed as belng among Lhe mosL welldeveloped and maLure deslgn paLLerns ln use 8y
uslng Lhe MvC deslgn paLLern processlng ls broen lnLo Lhree dlsLlncL secLlons apLly named Lhe
Model Lhe vlew and Lhe ConLroller 1hese are descrlbed ln Lhe followlng subsecLlons

Mode| Components
Model componenLs provlde a model of Lhe buslness loglc or daLa behlnd a SLruLs program lor
example ln a SLruLs appllcaLlon LhaL manages cusLomer lnformaLlon lL may be approprlaLe Lo have a
CusLomer Model componenL LhaL provldes program access Lo lnformaLlon abouL cusLomers
lLs very common for Model componenLs Lo provlde lnLerfaces Lo daLabases or bacend sysLems lor
example lf a SLruLs appllcaLlon needs Lo access employee lnformaLlon LhaL ls epL ln an enLerprlse
P8 lnformaLlon sysLem lL mlghL be approprlaLe Lo deslgn an Lmployee Model componenL LhaL acLs
as an lnLerface beLween Lhe SLruLs appllcaLlon and Lhe P8 lnformaLlon sysLem
Model componenLs are generally sLandard !ava classes 1here ls no speclflcally requlred formaL for a
Model componenL so lL may be posslble Lo reuse !ava code wrlLLen for oLher pro[ecLs
V|ew Components
vlew componenLs are Lhose pleces of an appllcaLlon LhaL presenL lnformaLlon Lo users and accepL
lnpuL ln SLruLs appllcaLlons Lhese correspond Lo Web pages
vlew componenLs are used Lo dlsplay Lhe lnformaLlon provlded by Model componenLs lor example
Lhe CusLomer Model componenL dlscussed above would need a vlew componenL Lo dlsplay lLs
lnformaLlon Dsually Lhere wlll one or more vlew componenLs for each Web page ln a SLruLs
appllcaLlon
vlew componenLs are generally bullL uslng !avaServer age (!S) flles SLruLs provldes a large number
of !S CusLom 1ags (someLlmes referred Lo as SLruLs 1ags) whlch exLend Lhe normal capablllLles of
!S and slmpllfy Lhe developmenL of vlew componenLs
Contro||er Components
ConLroller componenLs coordlnaLe acLlvlLles ln Lhe appllcaLlon 1hls may mean Lalng daLa from Lhe
user and updaLlng a daLabase Lhrough a Model componenL or lL may mean deLecLlng an error
condlLlon wlLh a bacend sysLem and dlrecLlng Lhe user Lhrough speclal error processlng ConLroller
componenLs accepL daLa from Lhe users declde whlch Model componenLs need Lo be updaLed and
Lhen declde whlch vlew componenL needs Lo be called Lo dlsplay Lhe resulLs

Cne of Lhe ma[or conLrlbuLlons of ConLroller componenLs ls LhaL Lhey allow Lhe developer Lo remove
much of Lhe error handllng loglc from Lhe !S pages ln Lhelr appllcaLlon (AfLer all lf errors ln
processlng occur Lhe ConLroller componenL forwards Lo an errorprocesslng vlew componenL noL
MCA - Semester 4 MC0078 - 1ava Programming
Assignment Set - 2 Roll No.

8

Lhe prlmary resulLs vlew componenL) 1hls can slgnlflcanLly slmpllfy Lhe loglc ln Lhe pages and mae
Lhem easler Lo develop and malnLaln
ConLroller componenLs ln SLruLs are !ava classes and musL be bullL uslng speclflc rules 1hey are
usually referred Lo as AcLlon classes

ottom L|ne enef|ts of MVC
8emember Lhe earller deflnlLlon LhaL descrlbed a lramewor as A seL of assumpLlons concepLs
values and pracLlces LhaL consLlLuLes a way of vlewlng reallLy? 1hls ls one of Lhe mosL powerful
beneflLs of Lhe MvC paLLern lL allows developers Lo Lhln abouL (and deslgn) complex appllcaLlons
as a serles of relaLlvely slmple Model vlew and ConLroller componenLs 1hls leads Lo beLLer more
conslsLenL and more easlly malnLalnable deslgns ln addlLlon lL helps avold Lhe common plLfall of
havlng each developer on a pro[ecL choose a dlfferenL approach for Lhelr wor

Lxamp|e k|ght nand Manager oftware
When uennls uoubleday and oLhers on hls Leam aL 8lghL Pand Manager SofLware
(hLLp//wwwrlghLhandmanagercom) were evaluaLlng how Lhey wanLed Lo do developmenL Lhey
declded rlghL away LhaL Lhey wanLed Lo use a framewor LhaL was based on Lhe MvC deslgn paLLern
1hls declslon came afLer havlng bullL a number of appllcaLlons based on Lhe !2LL plaLform Cn
prevlous pro[ecLs uennls says We spenL a loL of Llme developlng Lhe reusable framewor and noL
our appllcaLlon
ln oLher words Lhey wanLed a framewor LhaL allowed Lhem Lo focus on bulldlng Lhelr appllcaLlon
wlLhouL spendlng a loL of Llme wrlLlng Lhe code LhaL organlzed and coordlnaLed processlng AfLer
revlewlng a number of oLher compeLlng framewors Lhey seLLled on SLruLs for a number of reasons
some Lechnlcal and some noL 1he SLruLs Lechnlcal feaLures LhaL were lmporLanL Lo Lhem were
r SLruLs performs well
r SLruLs has a sound archlLecLural model wlLh a modesL learnlng curve
r SLruLs ls very solld and sLable
r SLruLs has a sLrong seL of cusLom Lag llbrarles LhaL slmpllfy !S developmenL
Also accordlng Lo uennls SLruLs ls very compeLlLlve Lechnlcally buL Lo my mlnd Lhe blggesL
advanLages SLruLs has over compeLlng Lechnologles are pracLlcal 1hese feaLures are
r ongolng developmenL by a large number of commlLLed users/developers
r nowledgeable and responslve pro[ecL leadershlp
r generally qulc (and someLlmes nearlnsLanL) problem resoluLlon vla Lhe SLruLs malllng llsL or
archlves
r access Lo source code
r sLrong connecLlon Lo and commlLmenL Lo fuLure lnLegraLlon wlLh forwardloolng Lechnologles lle
!S1L and !ava Server laces
r lncreaslng mlnd share LhaL we expecL wlll mae lL easler Lo hlre new developers already famlllar
wlLh Lhe framewor
Pow have Lhlngs wored ouL for Lhem? Agaln accordlng Lo uennls We are very happy wlLh Lhe way
lL ls worlng ouL so far 1here ls very llLLle burden on us Lo undersLand or lmplemenL Lhe plumblng of
MCA - Semester 4 MC0078 - 1ava Programming
Assignment Set - 2 Roll No.

9

our appllcaLlon we slmply creaLe our AcLlon and lorm class exLenslons and our appllcaLlon Model
beans

1helr resulLs have been fasLer more conslsLenL developmenL and have excellenL supporL lus Lhe
plaLform Lhey are bulldlng on ls sLable and consLanLly belng lmproved Chand by Lhe waylLs free
and you can geL all Lhe source code lf you wanL lL

6 What |s the d|fference between @C and UD?
Ans 6
%ere are two types oI internet protocol (IP) traIIic, and bot ave very diIIerent
uses.

@C(@ransm|ss|on Contro| rotoco|)
1C ls a connecLlonorlenLed proLocol a connecLlon can be made from cllenL Lo server and from
Lhen on any daLa can be senL along LhaL connecLlon

ke||ab|e
when you send a message along a 1C soceL you now lL wlll geL Lhere unless Lhe connecLlon falls
compleLely lf lL geLs losL along Lhe way Lhe server wlll rerequesL Lhe losL parL 1hls means compleLe
lnLegrlLy Lhlngs donL geL corrupLed

Crdered
lf you send Lwo messages along a connecLlon one afLer Lhe oLher you now Lhe flrsL message wlll
geL Lhere flrsL ?ou donL have Lo worry abouL daLa arrlvlng ln Lhe wrong order

neavywe|ght
Wwhen Lhe low level parLs of Lhe 1C sLream arrlve ln Lhe wrong order resend requesLs have Lo
be senL and all Lhe ouL of sequence parLs have Lo be puL bac LogeLher so requlres a blL of wor Lo
plece LogeLher

UD(User Datagram rotoco|)
A slmpler messagebased connecLlonless proLocol WlLh Du you send messages(paceLs) across Lhe
neLwor ln chuns
4 Unre||ab|e When you send a message you donL now lf lLll geL Lhere lL could geL
losL on Lhe way
4 Not ordered lf you send Lwo messages ouL you donL now whaL order Lheyll
arrlve ln
4 L|ghtwe|ghL no orderlng of messages no Lraclng connecLlons eLc lLs [usL flre and
forgeL! 1hls means lLs a loL qulcer and Lhe neLwor card / CS have Lo do very llLLle
wor Lo LranslaLe Lhe daLa bac from Lhe paceLs



MCA - Semester 4 MC0078 - 1ava Programming
Assignment Set - 2 Roll No.

10

Lxp|a|n the ||fe cyc|e of a erv|et?
Ans
erv|et L|fe Cyc|e
now LhaL you have seen Lhe baslc sLrucLure of a servleL leL's revlew Lhe process by whlch a server
lnvoes a servleL 1hls process can be broen down lnLo Lhe nlne sLeps as follows
1 1he server loads Lhe servleL when lL ls flrsL requesLed by Lhe cllenL or lf conflgured Lo do so aL
server sLarLup 1he servleL may be loaded from elLher a local or a remoLe locaLlon uslng Lhe
sLandard !ava class loadlng faclllLy
1hls sLep ls equlvalenL Lo Lhe followlng code
C|ass cC|assforName("comsourcestreamMyerv|et")
lL should be noLed LhaL when referrlng Lo servleLs Lhe Lerm load ofLen refers Lo Lhe process of boLh
loadlng and lnsLanLlaLlng Lhe servleL
2 1he server creaLes one or more lnsLances of Lhe servleL class uependlng on lmplemenLaLlon 1he
server may creaLe a slngle lnsLance LhaL servlces all requesLs Lhrough mulLlple Lhreads or creaLe a
pool of lnsLances from whlch one chosen Lo servlce each new requesL 1hls sLep ls equlvalenL Lo Lhe
followlng !ava code
erv|et s(erv|et) cnewInstance () where 'c' ls Lhe same Class ob[ecL creaLed ln prevlous sLep
3 1he server consLrucLs a ServerConflg ob[ecL LhaL provldes lnlLlallzaLlon lnformaLlon Lo Lhe servleL
4 1he server calls Lhe servleL's lnlL () meLhod passlng Lhe ob[ecL consLrucLed ln sLep 3 as a
parameLer 1he lnlL () meLhod ls guaranLeed Lo flnlsh execuLlon prlor Lo Lhe servleL processlng Lhe
flrsL requesL lf Lhe server has creaLed mulLlple servleL lnsLances (sLep 2) Lhe lnlL () meLhod ls called
one Llme for each lnsLance
3 1he server consLrucLs a ServleL8equesL or PLLpServleL8equesL ob[ecL from Lhe daLa lncluded ln Lhe
cllenL's requesL lL also consLrucLs a ServleL8esponse or PLLpServleL8esponse ob[ecL LhaL provldes
meLhods for cusLomlzlng Lhe server's response 1he Lype of ob[ecL passed ln Lhese Lwo parameLers
depends on wheLher Lhe servleL exLends Lhe CenerlcServleL class or Lhe PLLpServleL class
respecLlvely
6 1he server calls Lhe servleL's servlce() meLhod passlng Lhe ob[ecLs consLrucLed ln sLep 3 as
parameLers When concurrenL requesLs arrlve mulLlple servlce() meLhods can run ln separaLe
Lhreads
7 1he servlce () meLhod processes Lhe cllenL requesL by evaluaLlng Lhe ServleL8equesL or
PLLpServleL8equesL ob[ecL and responds uslng ServleL8esponse or PLLpServleL8esponse ob[ecL
8 lf Lhe server recelves anoLher requesL for Lhls servleL Lhe process beglns agaln aL sLep 3
9 When lnsLrucLed Lo unload Lhe servleL perhaps by Lhe server admlnlsLraLor or programmaLlcally
by Lhe servleL lLself Lhe server calls Lhe servleL's desLroy() meLhod 1he servleL ls Lhen ellglble for
garbage collecLlon
1he above menLloned nlne sLeps lllusLraLe Lhe enLlre llfecycle of a servleL 1he followlng flgure shows
Lhe flow of Lhe servleL llfecycle
MCA - Semester 4 MC0078 - 1ava Programming
Assignment Set - 2 Roll No.

11


LkVLL@ LIIL CCLL

Lxp|a|n the |mportance app||cat|ons and work|ng of Iava truts
SLruLs ls an appllcaLlon developmenL framewor LhaL ls deslgned for and used wlLh Lhe popular !2LL
(!ava 2 LnLerprlse LdlLlon) plaLform lL cuLs Llme ouL of Lhe developmenL process and maes
developers more producLlve by provldlng Lhem a serles of Lools and componenLs Lo bulld
appllcaLlons wlLh lL ls nonproprleLary and wors wlLh vlrLually any !2LLcompllanL appllcaLlon
server SLruLs falls under Lhe !aarLa subpro[ecL of Lhe Apache SofLware loundaLlon and comes wlLh
an Cpen Source llcense (meanlng lL has no cosL and lLs users have free access Lo all lLs lnLernal
source code)
ln addlLlon Lo helplng you wor fasLer and havlng no cosL SLruLs also helps mae your end wor
producLs beLLer 1he maln reason for Lhls Lo quoLe from Lrlc 8aymonds 1he CaLhedral and Lhe
8azaar (a classlc boo on open source developmenL) ls LhaL (g)lven a large enough beLaLesLer and
codeveloper base almosL every problem wlll be characLerlzed qulcly and Lhe flx obvlous Lo
someone ln oLher words so many people are uslng and developlng SLruLs LhaL bugs are found and
geL flxed qulcly


MCA - Semester 4 MC0078 - 1ava Programming
Assignment Set - 2 Roll No.

12

truts |s a Web App||cat|on Iramework?
1he dlcLlonary calls a framewor A sLrucLure for supporLlng or encloslng someLhlng else especlally
a seleLal supporL used as Lhe basls for someLhlng belng consLrucLed 1hls perfecLly descrlbes
SLruLsa collecLlon of !ava code deslgned Lo help you bulld solld appllcaLlons whlle savlng Llme lL
provldes Lhe baslc seleLon and plumblng you focus on Lhe layouL and loo of each room
lnLeresLlngly Lhe dlcLlonary offers an alLernaLlve deflnlLlon of a framewor A seL of assumpLlons
concepLs values and pracLlces LhaL consLlLuLes a way of vlewlng reallLy 1hls descrlbes SLruLs as
welllLs a way of loolng aL Lhlngs SLruLs saves you Llme by allowlng you Lo vlew complex
appllcaLlons as a serles of baslc componenLs vlews AcLlon Classes and Model componenLs

And Irameworks Are Important ecause?
Dslng a framewor means LhaL you donL have Lo spend Llme bulldlng your enLlre appllcaLlon ?ou
can focus on codlng Lhe buslness loglc and Lhe presenLaLlon layer of Lhe appllcaLlonnoL Lhe
overhead pleces lle flgurlng ouL how Lo capLure user lnpuL or flgurlng ouL how Lo generaLe drop
down boxes on a Web page
Dslng a framewor also helps you encode besL pracLlces 1he framewor developers have puL a loL of
LhoughL lnLo Lhe besL approaches Lo appllcaLlon bulldlng why relnvenL Lhls yourself?
AnoLher beneflL of uslng a framewor ls LhaL lL allows your code (aL leasL ln Lhe case of SLruLs) Lo be
hlghly plaLform lndependenL lor example Lhe same SLruLs code should wor under 1omcaL on an
old Wlndows machlne as runs uslng Webloglc on Llnux or Solarls ln producLlon And Lhls can be
accompllshed wlLhouL even recomplllng ln many casesLhe same Web appllcaLlon (or war flle)
can slmply be copled from one server Lo anoLher
AnoLher exLremely lmporLanL beneflLespeclally lf youre relaLlvely new Lo Web developmenLls LhaL
lL glves you a place Lo sLarL Any developer wlll Lell you lLs easler Lo Lae a baslc appllcaLlon and
modlfy lL Lhan lL ls Lo bulld someLhlng from scraLch 1hls feaLure of SLruLs can save you days or wees
of plannlng and developmenL
1oday l creaLe vlrLually noLhlng from scraLch AlmosL no one who ls an experlenced developer does
ln facL some of Lhe greaLesL successes ln sofLware developmenL were based on Lhls exacL ldea lor
example ln 1991 when Llnus 1orvalds began bulldlng Lhe operaLlng sysLem LhaL Loday ls Llnux he
began wlLh Lhe operaLlng sysLem Mlnlx Pe goL a copy of Lhe Mlnlx source code looed lL over ln
deLall and used lL as Lhe basls for Llnux And whlle Lhe flrsL launch of Llnux conLalned none of Lhe
orlglnal Mlnlx code Llnus surely wenL furLher fasLer because he had lL Lo sLarL wlLh
Cb[ect|ves
ln Lhls chapLer you wlll learn abouL Lhe
r ueflnlLlon of !ava SLruLs
r Pow does !ava SLruLs Wor
truts Work|ng
SLruLs ls based on Lhe Llmeproven ModelvlewConLroller (MvC) deslgn paLLern 1he MvC paLLern ls
wldely recognlzed as belng among Lhe mosL welldeveloped and maLure deslgn paLLerns ln use 8y
uslng Lhe MvC deslgn paLLern processlng ls broen lnLo Lhree dlsLlncL secLlons apLly named Lhe
Model Lhe vlew and Lhe ConLroller 1hese are descrlbed ln Lhe followlng subsecLlons
Mode| Components
Model componenLs provlde a model of Lhe buslness loglc or daLa behlnd a SLruLs program lor
example ln a SLruLs appllcaLlon LhaL manages cusLomer lnformaLlon lL may be approprlaLe Lo have a
CusLomer Model componenL LhaL provldes program access Lo lnformaLlon abouL cusLomers
lLs very common for Model componenLs Lo provlde lnLerfaces Lo daLabases or bacend sysLems lor
example lf a SLruLs appllcaLlon needs Lo access employee lnformaLlon LhaL ls epL ln an enLerprlse
MCA - Semester 4 MC0078 - 1ava Programming
Assignment Set - 2 Roll No.

13

P8 lnformaLlon sysLem lL mlghL be approprlaLe Lo deslgn an Lmployee Model componenL LhaL acLs
as an lnLerface beLween Lhe SLruLs appllcaLlon and Lhe P8 lnformaLlon sysLem
Model componenLs are generally sLandard !ava classes 1here ls no speclflcally requlred formaL for a
Model componenL so lL may be posslble Lo reuse !ava code wrlLLen for oLher pro[ecLs
V|ew Components
vlew componenLs are Lhose pleces of an appllcaLlon LhaL presenL lnformaLlon Lo users and accepL
lnpuL ln SLruLs appllcaLlons Lhese correspond Lo Web pages
vlew componenLs are used Lo dlsplay Lhe lnformaLlon provlded by Model componenLs lor example
Lhe CusLomer Model componenL dlscussed above would need a vlew componenL Lo dlsplay lLs
lnformaLlon Dsually Lhere wlll one or more vlew componenLs for each Web page ln a SLruLs
appllcaLlon
vlew componenLs are generally bullL uslng !avaServer age (!S) flles SLruLs provldes a large number
of !S CusLom 1ags (someLlmes referred Lo as SLruLs 1ags) whlch exLend Lhe normal capablllLles of
!S and slmpllfy Lhe developmenL of vlew componenLs

Contro||er Components
ConLroller componenLs coordlnaLe acLlvlLles ln Lhe appllcaLlon 1hls may mean Lalng daLa from Lhe
user and updaLlng a daLabase Lhrough a Model componenL or lL may mean deLecLlng an error
condlLlon wlLh a bacend sysLem and dlrecLlng Lhe user Lhrough speclal error processlng ConLroller
componenLs accepL daLa from Lhe users declde whlch Model componenLs need Lo be updaLed and
Lhen declde whlch vlew componenL needs Lo be called Lo dlsplay Lhe resulLs
Cne of Lhe ma[or conLrlbuLlons of ConLroller componenLs ls LhaL Lhey allow Lhe developer Lo remove
much of Lhe error handllng loglc from Lhe !S pages ln Lhelr appllcaLlon (AfLer all lf errors ln
processlng occur Lhe ConLroller componenL forwards Lo an errorprocesslng vlew componenL noL
Lhe prlmary resulLs vlew componenL) 1hls can slgnlflcanLly slmpllfy Lhe loglc ln Lhe pages and mae
Lhem easler Lo develop and malnLaln
ConLroller componenLs ln SLruLs are !ava classes and musL be bullL uslng speclflc rules 1hey are
usually referred Lo as AcLlon classes
ottom L|ne enef|ts of MVC

Vous aimerez peut-être aussi