Upload
phani
View
214
Download
0
Embed Size (px)
DESCRIPTION
02_Type of Applications
Citation preview
APPLICATIONType of applications: ---------- [ST VI]You can develop various types of application based on your requirements Stand alone application Client-server application Web-based application istributed applicationStandaloneapplication: ------------!"enyou"avestandaloneapplicationyoumust install t"at all t"emac"ine one copy for one user# In t"is data entered by one user !ill be stored in one mac"ine ot"er user areunable to access t"is# Client-serverapplication:------------ tosolve t"e problems $ata-s"earin%&of standalone applicationClient-server application are desi%ned # in t"is one centrali'ed mac"ine !ill be t"ere !"ic" is called server#(ultiple clients !ill beconnectedandrela)es t"e server# T"is arc"itecture !ill %iveyoumaintenanceproblem# *ecause entire application is divided into t!o parts and partial placed in server and partial placed inclient #!"en client feature !ill en"anced t"en you re-install all t"e soft!are to all t"e mac"ine !"ic" !ill %iveyou maintenanceproblems#Web-based application: -------- to solve t"e problem !it" client-server application Web-based application isintroduced !"ic" is run in WWW# In t"is entire application !ill be placed in !eb-sever and can be access byany client at any place !it" internet connection# T"is +ind of application is limited to sin%le or%ani'ation i#e#one company can develop application and e)pose to customer but unable to access ot"er application belon%sto ot"er company#Distributed application: ------------- istributed application is current trend in company !"ic" allo!s t"ebusiness partner to s"ear t"e information amon% t"em#Tecnolo!y and Server re"uired for various applicationsDifference #$W Web Server and Application Server Web Server Web server "ave !eb container Web server supportonly !eb based application Web container provides t"e support for !eb components !"ic" is implementin% !it" !eb tec"nolo%y li+e Servlet, -S.#Application Server /pplication server "ave !eb container and 0-* container # /pplication server Web based application and istributed application # Web container support !eb components and 0-* container support 0-* components#Stand alone application:-----------Tec"nolo%y required :-------- C, 11 , 2ava, VC11 , V* etcServer required: -------- no special server required Client-server application :-----------Tec"nolo%y required :-------- soc+et pro%rammin% !it"C, C11 , 2avaetcNOTE :--- in 2ava you can implements Client 3server application usin% 2ava#net pac+a%e Server required: -------- no special server required Web-based application :------------Tec"nolo%y required :-------- Servlet ,-S.,/S.,/S.#40T,.5.,C6I Server required: -------- /pac"e-tomcat ,any Web serveravailable in mar+et#Distributed application :----------Tec"nolo%y required --------7(I , 0-*,C87*/,C8( etc#Server required: -------- /ny /pplicationserveravailable in mar+et#%& [ST VII]Client S$W%'Server S$WCentrali(ed location Proble) :--- data searin!proble) :------ )aintenance proble)Stand alone application Client-server applicationWeb server Web client Web client *o(ila Web based application distributed application Web container free services: -------------- 4et!or+in% I8 Stream (ulti t"readin% 9ife cycle mana%ement 7esource mana%ement -S. support eclaratin%security::eclaration security /pplicationserver !eb container !ill do t"e seven free services#+,# container provide te follo-in! free services :---------- 4et!or+in% I8 Stream (ulti t"read 7esource mana%ement 9ife cycle mana%ement .ersistence mana%ement Transaction mana%ement Security mana%ement $more po!erful t"an!eb container services& (assa%in% service Timer service Web-services support(ainly t"is isdivided into t"ree types :--------- 9o! level:----- 4et!or+in%, I8 stream , (ulti t"read (iddle level :---- all remainin% 5i%" level:-------business and lo%ic service Servlet lifecycle met"ods :---------------- init$&Server C;CServer C; C service$& destroy$&2ava)#servlet pac+a%e :------------interface servlet? [ST VIII]void init$servlet confi%&t"ro!s servlet0)ception servlet confi% %etServletConfi%$&@void service$Servlet7equest,Servlet7esponse& t"ro!s Servlet0)ception,I80)ceptionStrin% %etservletinfo$&@Void destroy$&@AServlet conte.t and servlet confi!: ------------ pro!ra) /Servlet life cycleW"enyoustart t"eserver after developin%anddeployin%int"eserver ,t"efollo!in%t"in%s!ill be"appened !"en server startup :--------- Containerinitiali'et"e!eb#)ml filei#e# containerreadst"edatafrom!eb#)ml andplacein2avaob2ects #to read t"e data from )ml container use S/B $simple /.I for B(9& parser Container creates servlet conte)t ob2ect andinitiali'et"econte)t ob2ect !it"conte)t parametersspecified in t"e !eb#)ml# /ll t"e filters placed in t"e application !ill be instantiated$ob2ect !ill create & and initiali'e by callin%init$& met"od of t"e filter# Some servlets !ill be loaded, instantiated and initiali'e by callin% init$& met"od of t"e servlets !"oseservletConfi%uration contains C load-on-startupD ta%# T"read pool !ill be created by t"e container # assume t"at yoou "ave !ritten re%ister#"tmlre%isterservlet deployed in t"e server !eb#)ml is confi%ure and you started t"e server #Wen you open te follo-in!%0L in te -eb bro-ser -at -ill appen 1"ttp:::local"ost:EFEF:re%ister#"tml !"enuser"itst"eaboveG7l,one5ttp7equest !ill%otot"eserver,serverdele%atet"erequestto!ebcontainer t"en !eb container verifies t"e !"et"er requested G79 :file is available t"en transfer to t"e client ifnot error messa%e H7equest resource is not availableI !ill be send to t"e client # -en client clic2 on te re!ister sub)it button after enterin! all te data $value in te 3T*L for)-at -ill appen 14ote:-----assume t"at t"is is t"e ;st request to re%ister servletI/ Web server receive t"e request from !eb client and it dele%ates itto !eb container#II/ Web container collects t"e request G7I$value of action attribute in form ta%& from t"e incomin% requestand verifies any servlet is confi%ure !it" t"e G79pattern similar to t"is #III/ If t"e request G7I is not found !it" any servlet t"enerror messa%e H t"e requested resource$:re%ister#2lc&is not available !it" t"e status code =F=#I4/ If t"e requested G7I is found !it" any servlet t"en servlet name !ill be ta+en !it" t"e name servletclass !ill be quantified#4/ Container trys to locate t"e servlet class if it is not found # t"en error messa%eH2ava#lan%#class4otJoundIcom#2lc#7e%isterServlet !it" t"e status code KFF4I/ If t"e servlet class is found t"en servlet class !ill be loaded into t"e mainmemory 4II/ Servlet instance !ill be created by callin% t"e default constructor#4III/ Container create t"e servlet confi% ob2ectand initiali'et"e confi% ob2ect !it" init$& parametersspecifyin !eb#)ml related to re%ister servlet #I5/ Container initiali'es servlet confi%ob2ect !it" servlet confi% ob2ect 5/ Container calls init$& met"odby passin% servlet confi% as parameter #5I/ Container ta+es one t"read from t"read pool !"ic" is created server startup 6ST I57Class servlet confi% implements sc?/rray$&@servletConte)t scLnull@void set ServletConte)t$servletConte)t sc&?t"is#scLsc@Asc #%etServletConte)t$&@?7eturn sc@A5II/ T"read!ill start t"eprocessint"erequest int"is;st5ttpServlet7equest and5ttpServlet7esponseob2ect s"ould be readed#5III/ 7equest ob2ect !ill be initiali'ed !it"incomin% data $request parameters,request "eader& etc5I4/ Service met"od !ill be called by passin% request and response ob2ect as parameter#54/ 8nce t"e service met"od e)ecution is completed t"e stream ob2ect !"ic" is assopciated !it" responseob2ect !ill be flus"ed to t"e client mac"ine #When you send the request to the register servlet which is all ready instantiated then following things will happened :------------(second request onwards) I/ Step ;II/ Step I4/ Step =4/ Container +no!s t"atcall ready t"is servlet class is instantiated4I/ Step ;;4II/ Step ;I5/ Step ;=5/ Step ;KWen you sutdo-n te server follo-in! tin!s -ill appened 1 T"read pool !ill be destroyed Servlet conte)t ob2ect !ill be destroyed /ll t"e filter !ill be destroyed$container call destroy on every filter instance& /ll t"e servlet instances !ill be destroyed one by one $container call a destroy met"od in every servletinstance&Difference #$W sin!le tread )odel4$S )ulti tread )odel If you !ant to implement sin%le t"read model t"en you !rite your servlet as follo!s .ublic class 5elloServlet e)tends 5ttpServletImplements sin%leT"read(odel?------------ ------------ A W"en you implement sin%le t"read model mar+er interfacecontainer creates multiple instances for t"e servlet across multiple request# *ecause of multiple instances for a sin%le servlet it is over"ead for t"e container to If you !ant to implement multit"read model t"en you !rite your servlet as follo!s .ublic class 5elloServlet e)tends 5ttpServlet?------------ ------------ A T"is is a default model !"ic" is for!ard by t"e container in t"is container creates sin%le instance for t"e servlet and multiple t"read is created to servemultiple request In multi t"read model container life is easy because sin%le instanceare usin% only#create and destroyed t"e instances every time# Sin%le t"read model is not advisable (ulti t"read is better and it is advisable al!ays#Note: ---- sin!le tread )odel interface is deprecated in ,'++ &/8 version/9uestions6ST 57Servlet interfaceServlet interface is t"e root interface for all t"e servlet# Servlet "as t"ree life cyclemet"od Init:servlet confi!; Service:Servlet0e"uestPublic void service:Servlet0e"uest re" -------------- --------------?6eneric servlet is protocol independent i#e you can send t"e request to 6eneric servlet usin% nayprotocol$5ttp, JT., S(..T"ere is one direct subclass for 6eneric servlet called 5TT. Servlet# 5TT. servlet is overridin% t"e 6enericservice met"od of 6eneric Servlet#4o abstract met"od is available in 5TT. servlet still 5TT. servlet is declare as abstract #3ttp servletisprotocol dependent you can send a re"uest to 3TTP servlet usin! 3TTP protocol only/ Can I !rite constructor inside t"e servlet classM---Can I !rite ar%umented constructor Ninside t"e servlet classM ---Can I create instance of a servletM--- Can I create instance of a servlet usin% ar%umented constructorM---Can I call life cycle met"od for a servletM ----If I !rite a servlet class !it"out e)tendin% 5ttp servlet !"at !ill "appenM -----Can I call service met"od inside t"e init$& met"odM-----Can I call service met"od inside t"e destroy$& met"odM-----Can I call init$& met"od inside t"e service$& met"odM-----Can I call init$& met"od inside t"e destroy$& met"odM -----Can I call destroy$& met"od inside t"e init$& met"odM----Can I call destroy$& met"od inside t"e service$& met"odM---- NOT+: ----- -en you -rite your servlet by e.tendin! =eneric servlet or 3TTP servlet or -en you override any )etod in your servlet al-ays container invo2es =eneric service )etod only/ Note: ----------To force te developer -rite a subclass for 3TTP servlet and to stop teInterface creation of 3TTP Servlet< sun as defined 3TTP servlet as abstract/Case I:------- I "ave !ritten 5elloServlet by e)tendin% 5ttpServletand I "ave overridden 6eneric serviceinside t"e 5elloServlet !"at !ill "appen after deployin% t"e 5elloServlet inside t"e containerM.ublic abstract class 5ttpServlet e)tends 6eneric Servlet? 6ST5I7.ublic void service $S#7equest res,S#7esponse res&? --------------- --------------- AA.ublic class 5elloServlet e)tends 5ttpServlet?.ublic void service$S#7equest req,S#7esponse res&?8ut#println$H5ello 6uysI&@AAClass clsLclass#for4ame$SC& :: SCservlet class5ttpServlet "sL$5elloServlet&cls#ne! instance$&"s#service$Servlet7equest,Servlet7esponse&@In t"is case container create 5elloServlet class ob2ect and stored in 5ttpServlet7esponse variable#8nce 6eneric service met"od is called invo+es 6eneric service !"ic" is implement in 5elloServlet class only#Case II:------ I"ave!ritten 5elloServletbye)tendin% 5ttpServlet and I"aveoverridden 5ttp serviceinside t"e 5elloServlet , !"at !ill "appen !"en I send a request to5elloServlet M.ublic abstract class 5ttpServlet e)tends 6eneric servlet?.ublic void service$S#7equest req,S#7esponse res&?5ttpServlet7equest "sreqL$5ttpServlet7equest&res@T"is#service$"sreq,"sres&@A.ublic void service$S#7equest req,S#7esponse res&?-------------- ------------- AA.ublic class 5elloServlet e)tends 5ttpServlet?.ublic void service$5#S#7equest req,5#S#7esponse res&?8ut#println$H5ello 6uysI&@A5sL $5elloServlet&cls#ne! instance$&@5s#service$Servlet 7equest,Servlet 7esponse&@-------------- ------------ AA In t"is ;st container calls 6eneric service# 6eneric service of 5ttpServlet !ill be invo+e# 6eneric service of 5ttpServlet invo+es 5ttp service met"od# 5ttp service of 5elloServlet !ill be invo+e because ob2ect belon%s to 5elloServlet#6ST 5II7CaseIII:---------- I "ave!ritten5elloServlet bye)tendin%5ttpServlet andI "aveoverridden6enericservice and 5ttp service# W"en I send a request to 5elloServlet !"at !ill "appenM.ublic abstract class 5ttpServlet e)tends 6eneric servlet?.ublic void service$S#7equest req,S#7esponse res&?5ttpServlet7equest "sreqL$5ttpServlet7equest&res@T"is#service$"sreq,"sres&@A.ublic void service$S#7equest req,S#7esponse res&?-------------- ------------- AA.ublic class 5elloServlet e)tends 5ttpServlet?.ublic void service$5#S#7equest req,5#S#7esponse res&?8ut#println$HI am 5ttpServiceI&@A.ublic void service$5#S#7equest req,5#S#7esponse res&?8ut#println$HI am 6eneric SeviceI&@AAContainer invo+es 6eneric service #in t"is scenario 6eneric service is available in super class and subclassbecause of 5elloServlet ob2ect6eneric service of 5elloServlet !ill only call#Note:------ in t"is no c"ance of callin% 5ttp Service of 5elloServlet unless you invo+e 5ttp service from6eneric service of 5elloServlet#Case I4:---------- I "ave !ritten 5elloServlet by e)tendin% 5ttpServlet and I "ave overridden do6et met"od!"en I send a request to 5elloServlet!"at !ill "appen MT"e 5ttp met"od is 60T#.ublic abstract class 5ttpServlet e)tends 6eneric service?.ublic void service$S7eq,Sres&?5#S#7esponse "resL$5#S#7&S#7@-------------------------- T"is#service$"req,"res&@A.ublic void service$5S7eq,5S7es&?(et"od L"res#%etmet"od$&@If$met"od equals$H6etI&@Cform action LI Hmet"odLI H5ere met"od can be 6et .ost.ut 5ead elete 8ption /ce?T"is#do6et$"req,"res&@A0lse if$met"od equals$H.ostI&@?T"is#do.ost$"req,"res&@6ST 5III7A---------------- .ut-------- --------------- 5ead ------ ----------- elete --------- ---------- 8ption ---------------- /ce ------------AAClass 5elloServlet e)tends 5ttpServlet?.ublic void do6et$5S7eq,5S7es&?8ut#println$HI am 5elloServletI&@AAContainer invo+es 6eneric service t"en 6eneric service of 5ttpServlet !ill be call#6eneric service of 5ttp Servlet invo+es 5ttp service and 5ttp service of 5ttpServlet !ill call#5ttp service of 5ttpServlet verifies t"e incomin% request 5ttp met"od based on t"e correspondin% met"od !illinvo+e# In t"is scenario t"e incomin% request met"od is 60T# So do6et !ill be called# You "ave do6et in5elloServlet and 5ttpServlet #do6et of 5elloServlet !ill be invo+e#Case 4: -------- I "ave !ritten 5elloServlet by e)tendin% 5ttpServletand I "ave overriddendo.ost met"od5ttp met"od of incomin% request is 6et, !"at !ill "appen !"en I send a request to5elloServlet MWe !ill %et t"e follo!in% error for t"is scenario H5ttp status =FK ----- 5ttp met"od 60T is not supported byt"is G79ICase 4I: ------- I "ave !ritten 5elloServlet by e)tendin% 5ttpServlet and I "ave overridden do6et O do .ostmet"od in 5elloServlet#5ttp met"od of incomin% request is .ost, !"at !ill "appen !"en I send a request to5elloServletMIn t"is scenario 5ttp Service of 5elloServlet !ill be invo+e# If you !ant to invo+e do6et and do.ost e)plicitlyyou can do it from 5ttp service of 5elloServlet# 0)ample of 5ttpServlet7equest met"od: ------------*odel vie- controller desi!n pattern -------------*4CIntroduction -it ,SP:@ava server pa!e ;: ----- 2sp is a server side tec"nolo%y from sun !"ic" is used to develop server side components#-S. is similar to servlet !"ic" contains "tml ta%s and -S. elements# You need to place -S.Ps in root folder you can send t"e request to 2sp as follo!s: -----ttp:$$localost:ABAB$ello/@sp3ello/@spCDEpa!e i)port FG@ava/util/HGDIC"tmlDCbodyDC";Dintroduction to -S. !orld CbrDCQ out#println$H5elloI&@8ut#println$HI am a scriptletI&@ate dLne! ate$&@I)portin! pac2a!e as follo-s: --------CDEpa!e i)port FG@ava/util/HGDJ---------J -----------< ----------- JI8ut#printl$d&@QDC:bodyDC:"tmlD6ST 5I47*odel &arcitecture: -------Inmodel ;servlet component isresponsibletoperformcontroller lo%ic,persistence lo%ic , business lo%ic and presentation lo%ic "avin% all t"e code in one component %ives you noclarity and also t"e follo!in% problems :------------ 6enerally presentation lo%ic !ill be c"an%e frequently !"en c"an%e presentation lo%ic you need torecompile and restart t"e server *ecause of !ritin% business lo%ic and persistence lo%ic in t"e servlet you %et t"e code duplicationproblem because if you !ant to do t"e same business lo%ic and persistence lo%ic many time you needto !rite t"e same in all t"e servlet component !"ic" %ives you code duplication#*odel ' arcitecture: -------in (VC t"ere is a clean separation amon% all t"e lo%ic# (VC solve t"e problems!it" model ; arc"itecture# In (VCpresentation lo%ic !e are separatin% and !ritin% in 2sp !"ic" does not require anyrecompilation and restart t"e server# In (VC persistence lo%ic O business lo%ic !ill placed separately in a model component !"ic" youcan re use any !"ere# Controller lo%ic !ill be "andlin% by servlet component# W"en you follo! (VC desi%n pattern :arc"itecture for a sin%le request !e need to !rite one servlet,one or more -S. and one or more model components W"en you "avemany requirements of your application# You need to !rite more servlets "avin% many servlet$many entry points for t"e application& you can %et t"e follo!in% problem: ------------------- In any!eb-basedapplicationyouneed toperform some common tas+li+e doin%securityc"ec+s,lo%in t"e client information, validatin% t"e session, applyin% encryption al%orit"m etc# !"en you manyentry point for t"e application you need to !rite t"e code related to t"e above tas+ in all t"e servlet#W"ic"%ivesyout"ecodeduplicationandreducest"esystemfle)ibility#somaintenance!ill beincreased#Solution for te proble): --------- Gse front controller desi%n pattern, accordin% to front desi%n pattern youneed to !rite a sin%le servlet !"ic" is responsible to receive all t"e incomin% request #because of "avin% asin%le servlet you can !rite t"e code related to commonly required tas+ as one place #t"is reduces t"e codeduplication and increases t"e system fle)ibility#
4alidations: --- W"en user sends t"e data to t"e application, before processin% t"e request you need to verify!"et"er user "as %iven all t"e information required, %iven data in t"e correct format etc#Validations are monetary for t"e application data#You can do t"e validation in t!o !ays: ----------- Client side validation Server side validation Client side validation can be done !it" t"e 2ava script#Server side validation can be implements !it" simple 2ava class# You can reuse t"e validation code across various requirements by centrali'in% it in one place instead of!ritin% t"e validation code at many places#0eco))endation: -------Writin% one utility class and implement all t"e validation required for yourapplication !"en you can reuse it#DonKt for!et to do tis :-------------I*PO0TANT---------------LO0 S+04L+T P0O=0A**IN= Copy tools#2ar from C:N2