The Java EE 5 Tutorial

The Java EE 5 Tutorial

-

English
1084 Pages
Read
Download
Downloading requires you to have access to the YouScribe library
Learn all about the services we offer

Description

TheJavaEE5Tutorial
ForSunJavaSystemApplicationServer9.1
PartNo:821–1743–10
September2010(originallypublished2007) Copyright©2010,Oracleand/oritsaffiliates.Allrightsreserved.
Thissoftwareandrelateddocumentationareprovidedunderalicenseagreementcontainingrestrictionsonuseanddisclosureandareprotectedbyintellectual
propertylaws.Exceptasexpresslypermittedinyourlicenseagreementorallowedbylaw,youmaynotuse,copy,reproduce,translate,broadcast,modify,license,
transmit,distribute,exhibit,perform,publish,ordisplayanypart,inanyform,orbyanymeans.Reverseengineering,disassembly,ordecompilationofthissoftware,
unlessrequiredbylawforinteroperability,isprohibited.
Theinformationcontainedhereinissubjecttochangewithoutnoticeandisnotwarrantedtobeerror-free.Ifyoufindanyerrors,pleasereportthemtousinwriting.
IfthisissoftwareorrelatedsoftwaredocumentationthatisdeliveredtotheU.S.GovernmentoranyonelicensingitonbehalfoftheU.S.Government,thefollowing
noticeisapplicable:
U.S.GOVERNMENTRIGHTSPrograms,software,databases,andrelateddocumentationandtechnicaldatadeliveredtoU.S.Governmentcustomersare
“commercialcomputersoftware”or“commercialtechnicaldata”pursuanttotheapplicableFederalAcquisitionRegulationandagency-specificsupplemental
regulations.Assuch,theuse,duplication,disclosure,modification,andadaptationshallbesubjecttotherestrictionsandlicensetermssetforthintheapplicable
Governmentcontract,and,totheextentapplicablebythetermsoftheGovernmentcontract,theadditionalrightssetforthinFAR52.227-19 ...

Subjects

Informations

Published by
Reads 73
Language English
Document size 9 MB
Report a problem
TheJavaEE5Tutorial ForSunJavaSystemApplicationServer9.1 PartNo:821–1743–10 September2010(originallypublished2007) Copyright©2010,Oracleand/oritsaffiliates.Allrightsreserved. Thissoftwareandrelateddocumentationareprovidedunderalicenseagreementcontainingrestrictionsonuseanddisclosureandareprotectedbyintellectual propertylaws.Exceptasexpresslypermittedinyourlicenseagreementorallowedbylaw,youmaynotuse,copy,reproduce,translate,broadcast,modify,license, transmit,distribute,exhibit,perform,publish,ordisplayanypart,inanyform,orbyanymeans.Reverseengineering,disassembly,ordecompilationofthissoftware, unlessrequiredbylawforinteroperability,isprohibited. Theinformationcontainedhereinissubjecttochangewithoutnoticeandisnotwarrantedtobeerror-free.Ifyoufindanyerrors,pleasereportthemtousinwriting. IfthisissoftwareorrelatedsoftwaredocumentationthatisdeliveredtotheU.S.GovernmentoranyonelicensingitonbehalfoftheU.S.Government,thefollowing noticeisapplicable: U.S.GOVERNMENTRIGHTSPrograms,software,databases,andrelateddocumentationandtechnicaldatadeliveredtoU.S.Governmentcustomersare “commercialcomputersoftware”or“commercialtechnicaldata”pursuanttotheapplicableFederalAcquisitionRegulationandagency-specificsupplemental regulations.Assuch,theuse,duplication,disclosure,modification,andadaptationshallbesubjecttotherestrictionsandlicensetermssetforthintheapplicable Governmentcontract,and,totheextentapplicablebythetermsoftheGovernmentcontract,theadditionalrightssetforthinFAR52.227-19,Commercial ComputerSoftwareLicense(December2007).OracleAmerica,Inc.,500OracleParkway,RedwoodCity,CA94065. Thissoftwareorhardwareisdevelopedforgeneraluseinavarietyofinformationmanagementapplications.Itisnotdevelopedorintendedforuseinanyinherently dangerousapplications,includingapplicationswhichmaycreateariskofpersonalinjury.Ifyouusethissoftwareorhardwareindangerousapplications,thenyou shallberesponsibletotakeallappropriatefail-safe,backup,redundancy,andothermeasurestoensureitssafeuse.OracleCorporationanditsaffiliatesdisclaimany liabilityforanydamagescausedbyuseofthissoftwareorhardwareindangerousapplications. OracleandJavaareregisteredtrademarksofOracleand/oritsaffiliates.Othernamesmaybetrademarksoftheirrespectiveowners. AMD,Opteron,theAMDlogo,andtheAMDOpteronlogoaretrademarksorregisteredtrademarksofAdvancedMicroDevices.IntelandIntelXeonare trademarksorregisteredtrademarksofIntelCorporation.AllSPARCtrademarksareusedunderlicenseandaretrademarksorregisteredtrademarksofSPARC International,Inc.UNIXisaregisteredtrademarklicensedthroughX/OpenCompany,Ltd. Thissoftwareorhardwareanddocumentationmayprovideaccesstoorinformationoncontent,products,andservicesfromthirdparties.OracleCorporationand itsaffiliatesarenotresponsibleforandexpresslydisclaimallwarrantiesofanykindwithrespecttothird-partycontent,products,andservices.OracleCorporation anditsaffiliateswillnotberesponsibleforanyloss,costs,ordamagesincurredduetoyouraccesstooruseofthird-partycontent,products,orservices. 100921@24661 Contents Preface...................................................................................................................................................29 PartI Introduction..........37 1 Overview................39 JavaEEApplicationModel .................................................................................................................40 DistributedMultitieredApplications ...............................................................................................40 Security..........................................................................................................................................41 JavaEEComponents ...................................................................................................................42 JavaEEClients..............................................................................................................................42 WebComponents......44 BusinessC .................................................................................................................45 EnterpriseInformationSystemTier..........................................................................................46 JavaEEContainers...............46 ContainerServices .......................................................................................................................47 CTypes...........................................................................................................................47 WebServicesSupport............49 XML ...............................................................................................................................................49 SOAPTransportProtocol ...........................................................................................................50 WSDLStandardFormat.50 UDDIandebXMLStandardFormats .......................................................................................50 JavaEEApplicationAssemblyandDeployment .............................................................................50 PackagingApplications............51 DevelopmentRoles .............................................................................................................................52 JavaEEProductProvider..53 ToolProvider................................................................................................................................53 ApplicationComponentProvider .............................................................................................53 3 Contents ApplicationAssembler ................................................................................................................54 ADeployerandAdministrator.................................................................................54 JavaEE5APIs ......................................................................................................................................55 EnterpriseJavaBeansTechnology..............................................................................................55 JavaServletTechnology ..............................................................................................................56 JavaServerPagesTechnology .....................................................................................................56 JPagesStandardTagLibrary .....................................................................................56 JavaServerFaces ...........................................................................................................................57 JavaMessageServiceAPI..57 JavaTransactionAPI ...................................................................................................................57 JavaMailAPI.............57 JavaBeansActivationFramework ..............................................................................................58 JavaAPIforXMLProcessing58 JavaAPIforXMLWebServices(JAX-WS) ..............................................................................58 JavaArchitectureforXMLBinding(JAXB) .............................................................................59 SOAPwithAttachmentsAPIforJava........................................................................................59 JavaAPIforXMLRegistries .......................................................................................................59 J2EEConnectorArchitecture.....................................................................................................59 JavaDatabaseConnectivityAPI.................................................................................................60 JavaPersistenceAPI.....................................................................................................................60 JavaNamingandDirectoryInterface60 JavaAuthenticationandAuthorizationService .......................................................................61 SimplifiedSystemsIntegration...................................................................................................61 SunJavaSystemApplicationServer9.1 ............................................................................................62 Tools ..............................................................................................................................................62 2 UsingtheTutorialExamples ..............................................................................................................65 RequiredSoftware ...............................................................................................................................65 TutorialBundle ............................................................................................................................65 JavaPlatform,StandardEdition.................................................................................................66 SunJavaSystemApplicationServer9.1.....................................................................................66 NetBeansIDE ...............................................................................................................................67 ApacheAnt ...................................................................................................................................67 StartingandStoppingtheApplicationServer..................................................................................68theAdminConsole...............................................................................................................69 4 TheJavaEE5Tutorial • September2010(originallypublished2007) Contents StartingandStoppingtheJavaDBDatabaseServer........................................................................69 BuildingtheExamples ........................................................................................................................69 BuildingtheExamplesUsingNetBeansIDE ............................................................................69 BuildingtheontheCommand-LineUsingAnt .....................................................70 TutorialExampleDirectoryStructure ..............................................................................................71 DebuggingJavaEEApplications .......................................................................................................72 UsingtheServerLog ....................................................................................................................72 UsingaDebugger.........................................................................................................................72 PartII TheWebTier .........................................................................................................................................75 3 GettingStartedwithWebApplications...........................................................................................77 WebApplications ................................................................................................................................77 WebApplicationLifeCycle................................................................................................................80 WebModules .......................................................................................................................................81 PackagingWebModules .............................................................................................................83 DeployingaWARFile .................................................................................................................84 TestingDeployedWebModules ................................................................................................85 ListingDeployedWebModules .................................................................................................86 UpdatingWebModules ..............................................................................................................86 UndeployingWebModules88 ConfiguringWebApplications..........................................................................................................89 MappingURLstoWebComponents ........................................................................................89 DeclaringWelcomeFiles ............................................................................................................91 SettingInitializationParameters92 MappingErrorstoErrorScreens ...............................................................................................93 DeclaringResourceReferences ..................................................................................................94 Duke’sBookstoreExamples ...............................................................................................................96 AccessingDatabasesfromWebApplications ..................................................................................97 PopulatingtheExampleDatabase .............................................................................................97 CreatingaDataSourceintheApplicationServer ....................................................................97 FurtherInformationaboutWebApplications.................................................................................98 5 Contents 4 JavaServletTechnology .....................................................................................................................99 WhatIsaServlet?.................................................................................................................................99 TheExampleServlets ........................................................................................................................100 TroubleshootingDuke'sBookstoreDatabaseProblems.......................................................102 ServletLifeCycle................................................................................................................................102 HandlingServletLife-CycleEvents .........................................................................................103 HandlingServletErrors.............................................................................................................105 SharingInformation............105 UsingScopeObjects ..................................................................................................................105 ControllingConcurrentAccesstoSharedResources ...........................................................106 AccessingDatabases ..................................................................................................................107 InitializingaServlet ...........................................................................................................................109 WritingServiceMethods ..................................................................................................................109 GettingInformationfromRequests ........................................................................................110 ConstructingResponses..111 FilteringRequestsandResponses....................................................................................................114 ProgrammingFilters.....115 PCustomizedRequestsandResponses ............................................................116 SpecifyingFilterMappings .......................................................................................................118 InvokingOtherWebResources.......................................................................................................121 IncludingOtherintheResponse ...........................................................................121 TransferringControltoAnotherWebComponent..............................................................123 AccessingtheWebContext..............................................................................................................123 MaintainingClientState..........124 AccessingaSession ....................................................................................................................124 AssociatingObjectswithaSession ..........................................................................................124 SessionManagement...125 SessionTracking ........................................................................................................................126 FinalizingaServlet.............................................................................................................................127 TrackingServiceRequests ........................................................................................................127 NotifyingMethodstoShutDown ............................................................................................128 CreatingPoliteLong-RunningMethods.................................................................................129 FurtherInformationaboutJavaServletTechnology ....................................................................129 6 TheJavaEE5Tutorial • September2010(originallypublished2007) Contents 5 JavaServerPagesTechnology .........................................................................................................131 WhatIsaJSPPage?............................................................................................................................131 ASimpleJSPPageExample ......................................................................................................132 TheExampleJSPPages .....................................................................................................................134 TheLifeCycleofaJSPPage.....140 TranslationandCompilation ...................................................................................................140 Execution ....................................................................................................................................141 CreatingStaticContent ....................................................................................................................142 ResponseandPageEncoding143 CreatingDynamicContent..............................................................................................................143 UsingObjectswithinJSPPages................................................................................................143 UnifiedExpressionLanguage ..........................................................................................................144 ImmediateandDeferredEvaluationSyntax...........................................................................146 ValueandMethodExpressions147 DefiningaTagAttributeType..................................................................................................153 DeactivatingExpressionEvaluation........................................................................................154 LiteralExpressions.....................................................................................................................155 Resolving ...............................................................................................................156 ImplicitObjects ..........................................................................................................................158 Operators ....................................................................................................................................159 ReservedWords .........................................................................................................................160 ExamplesofELExpressions .....................................................................................................160 Functions..............161 JavaBeansComponents ....................................................................................................................163 JavaBeansComponentDesignConventions..........................................................................163 CreatingandUsingaJavaBeansComponent.........................................................................164 SettingJavaBeansComponentProperties ..............................................................................165 RetrievingJavaBeansCProperties ........................................................................167 UsingCustomTags ...........................................................................................................................168 DeclaringTagLibraries .............................................................................................................168 IncludingtheTagLibraryImplementation ............................................................................170 ReusingContentinJSPPages ..........................................................................................................171 TransferringControltoAnotherWebComponent .....................................................................172 jsp:paramElement....................................................................................................................172 IncludinganApplet...........................................................................................................................172 SettingPropertiesforGroupsofJSPPages.....................................................................................175 7 Contents DeactivatingELExpressionEvaluation ..................................................................................176 FurtherInformationaboutJavaServerPagesTechnology ...........................................................179 6 JavaServerPagesDocuments .........................................................................................................181 TheExampleJSPDocument ............................................................................................................181 CreatingaJSPDocument .................................................................................................................184 DeclaringTagLibraries...186 IncludingDirectivesinaJSPDocument .................................................................................187 CreatingStaticandDynamicContent ....................................................................................189 Usingthejsp:rootElement ....................................................................................................191 Usingthejsp:outputElement ................................................................................................192 IdentifyingtheJSPDocumenttotheContainer............................................................................196 7 JavaServerPagesStandardTagLibrary ........................................................................................197 TheExampleJSPPages .....................................................................................................................197 UsingJSTL..........................................................................................................................................199 TagCollaboration ......................................................................................................................200 CoreTagLibrary ...............................................................................................................................201 VariableSupportTags ...............................................................................................................201 FlowControlTagsintheCoreTagLibrary ............................................................................202 URLTags.....................................................................................................................................205 MiscellaneousTags ....................................................................................................................206 XMLTagLibrary ...............................................................................................................................207 CoreTags ....................................................................................................................................208 XMLFlowControlTags209 TransformationTags .................................................................................................................210 InternationalizationTagLibrary .....................................................................................................211 SettingtheLocale .......................................................................................................................211 MessagingTags........212 FormattingTags .........................................................................................................................212 SQLTagLibrary...............213 queryTagResultInterface........................................................................................................215 JSTLFunctions ..................................................................................................................................216 FurtherInformationaboutJSTL .....................................................................................................218 8 TheJavaEE5Tutorial • September2010(originallypublished2007) Contents 8 CustomTagsinJSPPages.................................................................................................................219 WhatIsaCustomTag? .....................................................................................................................220 TheExampleJSPPages .....................................................................................................................220 TypesofTags......................................................................................................................................223 TagswithAttributes......223 TagswithBodies.......226 TagsThatDefineVariables.......................................................................................................226 CommunicationbetweenTags ................................................................................................226 EncapsulatingReusableContentUsingTagFiles .........................................................................227 TagFileLocation........................................................................................................................228 TagFileDirectives......................................................................................................................229 EvaluatingFragmentsPassedtoTagFiles...............................................................................236 CustomTagExamples.236 TagLibraryDescriptors....................................................................................................................240 Top-LevelTagLibraryDescriptorElements ..........................................................................241 DeclaringTagFiles......242 DTagHandlers ............................................................................................................244 DeclaringTagAttributesforTagHandlers ............................................................................245 DTagVariablesforTagHandlers..............................................................................246 ProgrammingSimpleTagHandlers ...............................................................................................248 IncludingTagHandlersinWebApplications ........................................................................248 HowIsaSimpleTagHandlerInvoked? ..................................................................................249 TagHandlersforBasicTags .....................................................................................................249 TagHandlersforTagswithAttributes ....................................................................................249 TagHandlersforTagswithBodies ..........................................................................................252 TagHandlersforTagsThatDefineVariables253 CooperatingTags.......................................................................................................................256 TagHandlerExamples ..............................................................................................................257 9 ScriptinginJSPPages .......................................................................................................................265 TheExampleJSPPages........265 UsingScripting ..................................................................................................................................267 DisablingScripting...............267 JSPDeclarations ................................................................................................................................268 InitializingandFinalizingaJSPPage.......................................................................................268 9 Contents JSPScriptlets ......................................................................................................................................269 JSPExpressions..................................................................................................................................269 ProgrammingTagsThatAcceptScriptingElements....................................................................270 TLDElements.............................................................................................................................270 TagHandlers..........270 TagswithBodies.......272 CooperatingTags.......................................................................................................................274 TagsThatDefineVariables.......................................................................................................275 10 JavaServerFacesTechnology ..........................................................................................................277 JavaServerFacesTechnologyUserInterface..................................................................................277 JFacesBenefits ............................................................................................278 WhatIsaJavaServerFacesApplication?.........................................................................................279 ASimpleJFacesApplication...........................................................................................279 StepsintheDevelopmentProcess280 MappingtheFacesServletInstance ......................................................................................281 CreatingthePages......................................................................................................................282 DefiningPageNavigation .........................................................................................................287 ConfiguringErrorMessages .....................................................................................................288 DevelopingtheBeans ................................................................................................................289 AddingManagedBeanDeclarations .......................................................................................290 UserInterfaceComponentModel ..................................................................................................290 UserInterfaceComponentClasses..........................................................................................291 ComponentRenderingModel .................................................................................................293 ConversionModel .....................................................................................................................296 EventandListenerModel .........................................................................................................297 ValidationModel.......298 NavigationModel..............299 BackingBeans ....................................................................................................................................301 CreatingaBackingBeanClass301 TheLifeCycleofaJavaServerFacesPage .......................................................................................305 RestoreViewPhase ....................................................................................................................306 FurtherInformationaboutJavaServerFacesTechnology............................................................310 10 TheJavaEE5Tutorial • September2010(originallypublished2007)