英文技术文献 jsp文档格式.docx
《英文技术文献 jsp文档格式.docx》由会员分享,可在线阅读,更多相关《英文技术文献 jsp文档格式.docx(15页珍藏版)》请在冰点文库上搜索。
O’ReillyMedia,Inc.
Source:
.
Chapter18.WebApplicationModels
PartIIofthisbookdescribeshowyoucancreatemanydifferenttypesofapplicationsusingonlyJSPpageswithgenericcomponents—JSTLactions,customactionsandbeans—toaccessdatabases,presentcontentindifferentlanguages,protectpages,andsoforth—allwithoutknowingmuchaboutJavaprogramming.Thisapproachworksfineformanytypesofwebapplications,suchasemployeeregisters,productcatalogs,andconferenceroomreservationsystems.Butforapplicationswithcomplicatedschemas,intricatebusinessrules,andtrickycontrolflows,thegenericcomponentsjustdon'
tcutit,andyousuddenlyfindthatyouneedamorepowerfulwaytohandletherequestprocessingandthebusinesslogic.
AsImentionedinChapter3,JSPcanbecombinedwithotherJavatechnologiessuchasservletsandEJBinmorecomplexapplications.Inthischapter,welookathowJSPfitsintothislargerpicture.Afterthebriefdescriptionofthemostcommonapplicationmodelsinthischapter,Chapter19describesthecombinationofservletsandJSPindetail.
ThematerialpresentedinthispartofthebookisgearedtowardsJavaprogrammers.Ifyou'
renotaprogrammer,youmaystillwanttobrowsethroughthisparttogetafeelforthepossibilities,butdon'
texpecttounderstandeverything.Toreallyappreciatethetechniquesdescribedinthispartofthebook,youshouldhaveexperiencewithJavaprogrammingingeneral.FamiliaritywithJavaservletsalsohelps,andashortintroductionisincludedinChapter19forthosewhoarenewtothistechnology.
18.1TheJava2EnterpriseEditionModel
AttheJavaOneconferenceinSanFranciscoinJune1999,SunMicrosystemsannouncedanewarchitectureforJava,withseparateeditionsfordifferenttypesofapplications:
theJava2StandardEdition(J2SE)fordesktopandworkstationdevices;
theJava2MicroEdition(J2ME)forsmalldevicessuchascellphones,pagers,andPDAs;
andtheJava2EnterpriseEdition(J2EE)forserver-basedapplications.
J2EEisacompilationofvariousJavaAPIsthathavepreviouslybeenofferedasseparatepackages;
anApplicationProgrammingModel(APM)(alsoknownastheJ2EEBlueprints)thatdescribeshowtheycanallbecombined;
andatestsuiteJ2EEvendorscanusetotesttheirproductsforcompatibility.J2EEhasgonethroughanumberofrevisionssince1999,andthelatestversion(J2EE1.4)includesthefollowingenterprise-specificAPIsamongothers:
∙JavaServerPages(JSP)
∙JavaServlet
∙EnterpriseJavaBeans(EJB)
∙JavaDatabaseConnection(JDBC)
∙JavaTransactionAPI(JTA)andJavaTransactionService(JTS)
∙JavaNamingandDirectoryInterface(JNDI)
∙JavaMessageService(JMS)
∙JavaIDLandRemoteMethodInvocation(RMI)
∙JavaAPIforXMLParsing(JAXP),JavaAPIforXML-basedRPC(JAX-RPC),SOAPwithAttachmentsAPIforJava(SAAJ),andJavaAPIforXMLRegistries(JAXR)
∙JavaMailandJavaBeansActivationFramework(JAF)
∙J2EEConnectorArchitecture(JCX)
∙JavaAuthenticationandAuthorizationService(JAAS)
∙JavaManagementExtensions(JMX)
Inaddition,alltheJ2SEAPIscanbeusedwhendevelopingaJ2EEapplication.TheseAPIscanbeusedinnumerouscombinations.ThefirstthreeJ2EEAPIs—EJB,JSP,andservlets—representdifferentcomponenttechnologies,managedbywhattheJ2EEdocumentscallcontainers.AwebcontainerprovidestheruntimeenvironmentforservletsandJSPcomponents,translatingrequestsandresponsesintostandardJavaobjects.EJBcomponentsaresimilarlyhandledbyanEJBcontainer.Don'
tbefooledbythenamesimilaritybetweenJavaBeansandEnterpriseJavaBeans(EJB);
theyarecompletelydifferentanimals.AJavaBeanscomponentisaregularJavaclass,followingafewsimplenamingconventions,whichcanbeusedbyanyotherJavaclass.AnEnterpriseJavaBeancomponent,ontheotherhand,mustbedevelopedincompliancewithawholesetofstrictrulesandworksonlyintheenvironmentprovidedbyanEJBcontainer.
ComponentsinthetwotypesofcontainerscanusetheotherJ2EEAPIstoaccessdatabases(JDBCandJTA/JTS)andotherEIStierapplications(JCX),authenticateusersandcontrolaccess(JAAS),locatevariousresources(JNDI),andcommunicatewithotherserverresources(JavaMail/JAF,JMS,JavaIDL,RMI,JAXP,JAX-RPC,SAAJandJAXR).Enterpriseapplicationsareoftendividedintoasetoftiers,andJ2EEidentifiesthree:
theclienttier,themiddletier,andtheEnterpriseInformationSystem(EIS)tier.ThemiddletiercanbefurtherdividedintothewebtierandtheEJBtier.Thislogicalseparation,withwell-definedinterfaces,makesitpossibletobuildscalableapplications.Initiallyoneormoretierscanberunningonthesamephysicalserver.Withincreaseddemands,thetierscanbeseparatedanddistributedovermultipleserverswithoutmodifyingthecode,justbychangingtheconfiguration.
TheclienttiercontainsbrowsersaswellasregularGUIapplications.AbrowserusesHTTPtocommunicatewiththewebcontainer.AstandaloneapplicationcanalsouseHTTPorcommunicatedirectlywiththeEJBcontainerusingRMIorIIOP(aCORBAprotocol).Anothertypeofclientthat'
sbecomingmoreandmorepopularistheextremelythinclient,suchasacellphoneorPDA.ThistypeofclienttypicallyusestheWirelessAccessProtocol(WAP),typicallyconvertedintoHTTPviaagateway,tocommunicatewiththewebcontainer.
ThemiddletierprovidesclientservicesthroughthewebcontainerandtheEJBcontainer.AclientthatcommunicateswiththeserverthroughHTTPusescomponentsinthewebcontainer,suchasservletsandJSPpages,asentrypointstotheapplication.Manyapplicationscanbeimplementedsolelyaswebcontainercomponents.Inotherapplications,thewebcomponentsjustactasaninterfacetotheapplicationlogicimplementedbyEJBcomponents.Astandaloneapplication,writteninJavaoranyotherprogramminglanguage,canalsocommunicatedirectlywiththeEJBcomponents.Generalguidelinesforwhentousethedifferentapproachesarediscussedlaterinthischapter.ComponentsinthistiercanaccessdatabasesandcommunicatewithotherserverapplicationsusingalltheotherJ2EEAPIs.
TheEnterpriseInformationSystem(EIS)tierholdstheapplication'
sbusinessdata.Typically,itconsistsofoneormorerelationaldatabasemanagementservers,butothertypesofdatabasessuchasIMSdatabases;
legacyapplicationssuchasEnterpriseResourcePlanning(ERP);
andmainframetransactionprocessingsystemssuchasCICS,arealsoincludedinthistier.ThemiddletierusesJ2EEAPIssuchasJDBC,JTA/JTS,andtheJ2EEConnectorArchitecture(JCX)tointeractwiththeEIStier.
18.2TheMVCDesignModel
Inadditiontotheseparationofresponsibilitiesintodifferenttiers,J2EEalsoencouragestheuseoftheModel-View-Controller(MVC)designmodel,brieflyintroducedinChapter3,whendesigningapplications.
TheMVCmodelwasfirstdescribedbyXeroxinanumberofpaperspublishedinthelate1980sinconjunctionwiththeSmalltalklanguage.ThismodelhassincebeenusedforGUIapplicationsdevelopedinallpopularprogramminglanguages.Let'
sreview:
thebasicideaistoseparatetheapplicationdataandbusinesslogic,thepresentationofthedata,andtheinteractionwiththedataintodistinctentitieslabeledtheModel,theView,andtheController,respectively.
TheModelrepresentspurebusinessdataandtherulesforhowtousethisdata;
itknowsnothingabouthowthedataisdisplayedortheuserinterfaceformodifyingthedata.TheView,ontheotherhand,knowsallabouttheuserinterfacedetails.ItalsoknowsaboutthepublicModelinterfaceforreadingitsdata,sothatitcanrenderitcorrectly,anditknowsabouttheControllerinterface,soitcanasktheControllertomodifytheModel.
Usinganemployeeregistryapplicationasanexample,anEmployeeclassmayrepresentaModel.Itholdsinformationaboutanemployee:
name,employmentdate,vacationdays,salary,etc.Italsoholdsrulesforhowthisinformationcanbechanged;
forinstance,thenumberofvacationdaysmaybelimitedbasedontheemploymenttime.TheuserinterfacethatshowstheinformationisaView.ItgetsholdofanEmployeeobjectbyaskingtheControllerforitandthenrenderstheinformationbyaskingtheEmployeeobjectforitspropertyvalues.TheViewalsorenderscontrolsthatallowtheusertomodifytheinformation.TheViewssendsthemodificationrequesttotheController,whichupdatestheEmployeeobjectandthentellstheViewthattheModelhasbeenmodified.TheView,finally,updatestheuserinterfacetodisplaytheupdatedvalues.
UsingtheMVCdesignmodelmakesforaflexibleapplicationarchitecture,inwhichmultiplepresentations(Views)canbeprovidedandeasilymodified,andchangesinthebusinessrulesorphysicalrepresentationofthedata(theModel)canbemadewithouttouchinganyoftheuserinterfacecode.
EventhoughthemodelwasoriginallydevelopedforstandaloneGUIapplications,ittranslatesfairlywellintothemultitierapplicationdomainofJ2EE.TheuserinteractswiththeControllertoaskforthingstobedone,andtheControllerrelaystheserequeststotheModelinaclient-typeindependentway.Say,forinstance,thatyouhavetwotypesofclients:
anHTTPclientsuchasabrowserandaGUIclientapplicationusingIIOPtotalktotheserver.InthisscenarioyoucanhaveoneControllerforeachprotocolthatreceivestherequestsandextractstherequestinformationinaprotocol-dependentmanner.BothControllersthencalltheModelthesameway;
theModeldoesn'
tneedtoknowwhatkindof