Sun JavaEE5 Tutorial for Application Server 9.1

需积分: 10 3 下载量 193 浏览量 更新于2024-07-31 收藏 9.55MB PDF 举报
"TheJavaEE5Tutorial对于SunJavaSystemApplicationServer9.1的官方技术文档,出版于2010年,最初发布于2007年。由Oracle及其关联公司版权所有,包含对使用和披露的限制,并受知识产权法律保护。" 这篇文档详细介绍了Java Enterprise Edition(JavaEE)5版本的相关知识,特别是针对Sun Java System Application Server 9.1的教程。JavaEE是用于构建企业级Web应用程序的框架,它提供了一系列的API和服务,包括但不限于: 1. **Servlet和JSP**:Servlet是Java EE中的核心组件,用于处理HTTP请求,而JSP(JavaServer Pages)则是一种用于创建动态网页的技术,将业务逻辑与表现层分离。 2. **JNDI (Java Naming and Directory Interface)**:用于在分布式环境中查找和管理资源,比如数据库连接、EJB等。 3. **EJB (Enterprise JavaBeans)**:JavaEE的重要组成部分,提供了服务器端组件模型,支持事务、安全性和并发性管理,分为Session Beans(处理业务逻辑)、Message-driven Beans(处理消息)和Entity Beans(表示持久化数据)。 4. **JMS (Java Message Service)**:允许应用程序之间通过消息传递进行异步通信。 5. **JTA (Java Transaction API)**:提供了跨多个资源的事务管理,确保了在分布式环境中的数据一致性。 6. **JPA (Java Persistence API)**:简化了对象关系映射,使得Java对象可以透明地操作关系数据库。 7. **JPA提供商如Hibernate**:在实际应用中,JPA通常会依赖于像Hibernate这样的ORM工具来实现具体的数据访问功能。 8. **JSF (JavaServer Faces)**:一种用户界面组件模型,用于构建可重用的UI组件,简化了Web应用程序的开发。 9. **Web服务支持(JAX-WS和JAX-RS)**:JAX-WS用于创建基于SOAP的Web服务,而JAX-RS则是用于创建RESTful Web服务的API。 10. **CDI (Contexts and Dependency Injection)**:提供依赖注入和上下文管理,简化了组件的配置和管理。 11. **JAF (Java Activation Framework)**:用于处理MIME类型的Java对象,常用于电子邮件和消息传递系统。 12. **安全管理**:JavaEE提供了一套完整的安全模型,包括角色、权限、认证和授权。 这个教程不仅涵盖了这些核心技术的理论知识,还可能包含实际操作指南、示例代码以及最佳实践。对于学习和理解JavaEE 5开发者来说,这是一个宝贵的资源,可以帮助他们熟练掌握构建和部署企业级应用所需的各种技能。
2009-07-16 上传
Sun 官方 J2ee 5.0 教程 The Java EE 5Tutorial For Sun Java System Application Server 9.1 Contents Preface ..................................................................................................................................................29 Part I Introduction ........................................................................................................................................39 1 Overview ..............................................................................................................................................41 Java EE ApplicationModel ................................................................................................................. 42 DistributedMultitiered Applications ............................................................................................... 42 Security ......................................................................................................................................... 43 Java EE Components ................................................................................................................... 44 Java EE Clients .............................................................................................................................. 44 Web Components ........................................................................................................................ 46 Business Components ................................................................................................................. 47 Enterprise Information System Tier .......................................................................................... 48 Java EE Containers .............................................................................................................................. 48 Container Services ....................................................................................................................... 49 Container Types ........................................................................................................................... 49 Web Services Support ......................................................................................................................... 51 XML .............................................................................................................................................. 51 SOAP Transport Protocol ........................................................................................................... 52 WSDL Standard Format .............................................................................................................. 52 UDDI and ebXML Standard Formats ....................................................................................... 52 Java EE Application Assembly andDeployment ............................................................................. 52 Packaging Applications ...................................................................................................................... 53 Development Roles ............................................................................................................................. 54 Java EE Product Provider ............................................................................................................ 55 Tool Provider ............................................................................................................................... 55 Application Component Provider ............................................................................................. 55 3 Application Assembler ................................................................................................................ 56 ApplicationDeployer and Administrator ................................................................................. 56 Java EE 5 APIs ..................................................................................................................................... 57 Enterprise JavaBeans Technology .............................................................................................. 57 Java Servlet Technology .............................................................................................................. 58 JavaServer Pages Technology ..................................................................................................... 58 JavaServer Pages Standard Tag Library ..................................................................................... 58 JavaServer Faces ........................................................................................................................... 58 JavaMessage Service API ............................................................................................................ 59 Java Transaction API ................................................................................................................... 59 JavaMail API ................................................................................................................................ 59 JavaBeans Activation Framework .............................................................................................. 59 Java API for XML Processing ..................................................................................................... 60 Java API for XML Web Services (JAX-WS) .............................................................................. 60 Java Architecture for XML Binding (JAXB) ............................................................................. 60 SOAP with Attachments API for Java ........................................................................................ 60 Java API for XML Registries ....................................................................................................... 61 J2EE Connector Architecture ..................................................................................................... 61 JavaDatabase Connectivity API ................................................................................................. 61 Java Persistence API ..................................................................................................................... 62 JavaNaming and Directory Interface ........................................................................................ 62 Java Authentication and Authorization Service ....................................................................... 62 Simplified Systems Integration ................................................................................................... 63 Sun Java System Application Server Platform Edition 9 ................................................................. 63 Tools ............................................................................................................................................. 63 2 Using theTutorial Examples ..............................................................................................................65 Required Software ............................................................................................................................... 65 Tutorial Bundle ............................................................................................................................ 65 Java Platform, Standard Edition ................................................................................................. 66 Sun Java System Application Server 9.1 ..................................................................................... 66 NetBeans IDE ............................................................................................................................... 67 Apache Ant .................................................................................................................................. 68 Starting and Stopping the Application Server .................................................................................. 68 Starting the Admin Console ............................................................................................................... 69 Contents 4 The Java EE 5Tutorial • September 2007 Starting and Stopping the JavaDBDatabase Server ........................................................................ 69 Building the Examples ........................................................................................................................ 70 Building the Examples UsingNetBeans IDE ............................................................................ 70 Building the Examples on the Command-Line Using Ant ..................................................... 70 Tutorial Example Directory Structure .............................................................................................. 71 Debugging Java EE Applications ....................................................................................................... 72 Using the Server Log .................................................................................................................... 72 Using aDebugger ......................................................................................................................... 73 Part II TheWebTier ........................................................................................................................................ 75 3 Getting Started withWeb Applications ........................................................................................... 77 Web Applications ............................................................................................................................... 77 Web Application Life Cycle ................................................................................................................ 80 WebModules ...................................................................................................................................... 81 Packaging WebModules ............................................................................................................. 83 Deploying aWARFile ................................................................................................................. 84 TestingDeployed WebModules ................................................................................................ 85 ListingDeployed WebModules ................................................................................................. 86 Updating WebModules .............................................................................................................. 86 Undeploying WebModules ........................................................................................................ 88 Configuring Web Applications .......................................................................................................... 89 Mapping URLs to Web Components ........................................................................................ 89 Declaring Welcome Files ............................................................................................................ 91 Setting Initialization Parameters ................................................................................................ 92 Mapping Errors to Error Screens ............................................................................................... 93 Declaring Resource References .................................................................................................. 94 Duke’s Bookstore Examples ............................................................................................................... 96 AccessingDatabases from Web Applications .................................................................................. 97 Populating the ExampleDatabase ............................................................................................. 97 Creating aData Source in the Application Server .................................................................... 98 Further Information about Web Applications ................................................................................. 98 Contents 5 4 Java ServletTechnology .....................................................................................................................99 What Is a Servlet? ................................................................................................................................ 99 The Example Servlets ........................................................................................................................ 100 Troubleshooting Duke's BookstoreDatabase Problems ....................................................... 102 Servlet Life Cycle ............................................................................................................................... 102 Handling Servlet Life-Cycle Events ......................................................................................... 103 Handling Servlet Errors ............................................................................................................. 105 Sharing Information ......................................................................................................................... 105 Using Scope Objects .................................................................................................................. 105 Controlling Concurrent Access to Shared Resources ........................................................... 106 AccessingDatabases .................................................................................................................. 107 Initializing a Servlet ........................................................................................................................... 109 Writing ServiceMethods .................................................................................................................. 110 Getting Information from Requests ........................................................................................ 110 Constructing Responses ............................................................................................................ 112 Filtering Requests and Responses .................................................................................................... 114 Programming Filters .................................................................................................................. 115 Programming Customized Requests and Responses ............................................................ 117 Specifying FilterMappings ....................................................................................................... 119 Invoking Other Web Resources ....................................................................................................... 122 Including Other Resources in the Response ........................................................................... 122 Transferring Control to Another Web Component .............................................................. 124 Accessing the Web Context .............................................................................................................. 124 Maintaining Client State ................................................................................................................... 125 Accessing a Session .................................................................................................................... 125 Associating Objects with a Session .......................................................................................... 126 SessionManagement ................................................................................................................. 126 Session Tracking ........................................................................................................................ 127 Finalizing a Servlet ............................................................................................................................. 128 Tracking Service Requests ........................................................................................................ 129 NotifyingMethods to ShutDown ............................................................................................ 129 Creating Polite Long-RunningMethods ................................................................................. 130 Further Information about Java Servlet Technology .................................................................... 131 Contents 6 The Java EE 5Tutorial • September 2007 5 JavaServer PagesTechnology .........................................................................................................133 What Is a JSP Page? ............................................................................................................................ 133 A Simple JSP Page Example ...................................................................................................... 134 The Example JSP Pages ..................................................................................................................... 136 The Life Cycle of a JSP Page .............................................................................................................. 142 Translation and Compilation ................................................................................................... 142 Execution ................................................................................................................................... 143 Creating Static Content .................................................................................................................... 144 Response and Page Encoding ................................................................................................... 145 CreatingDynamic Content .............................................................................................................. 145 Using Objects within JSP Pages ................................................................................................ 145 Unified Expression Language .......................................................................................................... 146 Immediate andDeferred Evaluation Syntax ........................................................................... 148 Value andMethod Expressions ................................................................................................ 150 Defining a Tag Attribute Type .................................................................................................. 156 Deactivating Expression Evaluation ........................................................................................ 157 Literal Expressions ..................................................................................................................... 158 Resolving Expressions ............................................................................................................... 160 Implicit Objects .......................................................................................................................... 162 Operators ................................................................................................................................... 163 Reserved Words ......................................................................................................................... 163 Examples of EL Expressions ..................................................................................................... 164 Functions ................................................................................................................................... 165 JavaBeans Components .................................................................................................................... 167 JavaBeans ComponentDesign Conventions .......................................................................... 167 Creating and Using a JavaBeans Component ......................................................................... 168 Setting JavaBeans Component Properties .............................................................................. 169 Retrieving JavaBeans Component Properties ........................................................................ 171 Using Custom Tags ........................................................................................................................... 172 Declaring Tag Libraries ............................................................................................................. 172 Including the Tag Library Implementation ............................................................................ 174 Reusing Content in JSP Pages .......................................................................................................... 175 Transferring Control to Another Web Component ..................................................................... 176 jsp:param Element .................................................................................................................... 176 Including an Applet ........................................................................................................................... 176 Setting Properties for Groups of JSP Pages ..................................................................................... 179 Contents 7 Deactivating EL Expression Evaluation .................................................................................. 180 Further Information about JavaServer Pages Technology ........................................................... 183 6 JavaServer Pages Documents .........................................................................................................185 The Example JSPDocument ............................................................................................................ 185 Creating a JSPDocument ................................................................................................................. 188 Declaring Tag Libraries ............................................................................................................. 190 Including Directives in a JSPDocument ................................................................................. 191 Creating Static andDynamic Content .................................................................................... 193 Using the jsp:root Element .................................................................................................... 196 Using the jsp:output Element ................................................................................................ 196 Identifying the JSPDocument to the Container ............................................................................ 200 7 JavaServer Pages StandardTag Library ........................................................................................201 The Example JSP Pages ..................................................................................................................... 201 Using JSTL ......................................................................................................................................... 203 Tag Collaboration ...................................................................................................................... 204 Core Tag Library ............................................................................................................................... 205 Variable Support Tags ............................................................................................................... 205 Flow Control Tags ...................................................................................................................... 206 URL Tags .................................................................................................................................... 210 Miscellaneous Tags .................................................................................................................... 211 XML Tag Library ............................................................................................................................... 211 Core Tags ................................................................................................................................... 213 Flow Control Tags ...................................................................................................................... 214 Transformation Tags ................................................................................................................. 215 Internationalization Tag Library ..................................................................................................... 215 Setting the Locale ....................................................................................................................... 216 Messaging Tags .......................................................................................................................... 216 Formatting Tags ......................................................................................................................... 217 SQL Tag Library ................................................................................................................................ 218 query Tag Result Interface ........................................................................................................ 220 JSTL Functions ................................................................................................................................. 222 Further Information about JSTL ..................................................................................................... 223 Contents 8 The Java EE 5Tutorial • September 2007 8 CustomTags in JSP Pages .................................................................................................................225 What Is a Custom Tag? ..................................................................................................................... 226 The Example JSP Pages ..................................................................................................................... 226 Types of Tags ..................................................................................................................................... 229 Tags with Attributes ................................................................................................................... 229 Tags with Bodies ........................................................................................................................ 232 Tags ThatDefine Variables ....................................................................................................... 232 Communication between Tags ................................................................................................ 233 Encapsulating Reusable Content Using Tag Files ......................................................................... 233 Tag File Location ........................................................................................................................ 235 Tag File Directives ...................................................................................................................... 235 Evaluating Fragments Passed to Tag Files ............................................................................... 242 Custom Tag Examples ............................................................................................................... 243 Tag LibraryDescriptors .................................................................................................................... 247 Top-Level Tag LibraryDescriptor Elements .......................................................................... 248 Declaring Tag Files .................................................................................................................... 249 Declaring TagHandlers ............................................................................................................ 251 Declaring Tag Attributes for TagHandlers ............................................................................ 252 Declaring Tag Variables for TagHandlers .............................................................................. 254 Programming Simple TagHandlers ............................................................................................... 256 Including TagHandlers in Web Applications ........................................................................ 256 How Is a Simple TagHandler Invoked? .................................................................................. 256 TagHandlers for Basic Tags ..................................................................................................... 257 TagHandlers for Tags with Attributes .................................................................................... 257 TagHandlers for Tags with Bodies .......................................................................................... 260 TagHandlers for Tags ThatDefine Variables ........................................................................ 261 Cooperating Tags ....................................................................................................................... 263 TagHandler Examples .............................................................................................................. 265 9 Scripting in JSP Pages .......................................................................................................................273 The Example JSP Pages ..................................................................................................................... 273 Using Scripting ................................................................................................................................. 275 Disabling Scripting ............................................................................................................................ 275 JSPDeclarations ............................................................................................................................... 276 Initializing and Finalizing a JSP Page ....................................................................................... 276 Contents 9 JSP Scriptlets ..................................................................................................................................... 277 JSP Expressions ................................................................................................................................. 277 Programming Tags That Accept Scripting Elements .................................................................... 278 TLD Elements ............................................................................................................................. 278 TagHandlers .............................................................................................................................. 278 Tags with Bodies ........................................................................................................................ 280 Cooperating Tags ....................................................................................................................... 282 Tags ThatDefine Variables ....................................................................................................... 284 10 JavaServer FacesTechnology ..........................................................................................................285 JavaServer Faces Technology User Interface .................................................................................. 285 JavaServer Faces Technology Benefits ............................................................................................ 286 What Is a JavaServer Faces Application? ......................................................................................... 287 A Simple JavaServer Faces Application ........................................................................................... 287 Steps in theDevelopment Process ........................................................................................... 288 Mapping the FacesServlet Instance ...................................................................................... 289 Creating the Pages ...................................................................................................................... 290 Defining PageNavigation ......................................................................................................... 296 Configuring ErrorMessages ..................................................................................................... 297 Developing the Beans ................................................................................................................ 298 AddingManaged BeanDeclarations ....................................................................................... 298 User Interface ComponentModel .................................................................................................. 299 User Interface Component Classes .......................................................................................... 300 Component RenderingModel ................................................................................................. 301 ConversionModel ..................................................................................................................... 304 Event and ListenerModel ......................................................................................................... 305 ValidationModel ....................................................................................................................... 307 NavigationModel .............................................................................................................................. 307 Backing Beans ................................................................................................................................... 309 Creating a Backing Bean Class ................................................................................................. 309 The Life Cycle of a JavaServer Faces Page ....................................................................................... 313 Restore View Phase .................................................................................................................... 315 Further Information about JavaServer Faces Technology ............................................................ 318 Contents 10 The Java EE 5Tutorial • September 2007 11 Using JavaServer FacesTechnology in JSP Pages ........................................................................ 319 The Example JavaServer Faces Application .................................................................................... 319 Setting Up a Page ............................................................................................................................... 322 Using the Core Tags .......................................................................................................................... 325 Adding UI Components to a Page Using the HTML Component Tags ..................................... 327 UI Component Tag Attributes ................................................................................................. 327 Adding a Form Component ..................................................................................................... 329 Using Text Components ........................................................................................................... 330 Using Command Components for Performing Actions andNavigation ........................... 335 UsingData-Bound Table Components .................................................................................. 337 Adding Graphics and Images with the graphicImage Tag ................................................... 340 Laying Out Components with the UIPanel Component ...................................................... 341 Rendering Components for Selecting One Value .................................................................. 343 Rendering Components for SelectingMultiple Values ......................................................... 345 The UISelectItem, UISelectItems, and UISelectItemGroup Components ................... 346 Displaying ErrorMessages with the message and messages Tags ....................................... 349 Using LocalizedData ........................................................................................................................ 350 Loading a Resource Bundle ...................................................................................................... 351 Referencing Localized StaticData ............................................................................................ 352 Referencing ErrorMessages ..................................................................................................... 352 Using the Standard Converters ........................................................................................................ 354 Converting a Component’s Value ............................................................................................ 355 Using DateTimeConverter ....................................................................................................... 356 Using NumberConverter ........................................................................................................... 357 Registering Listeners on Components ............................................................................................ 359 Registering a Value-Change Listener on a Component ........................................................ 359 Registering an Action Listener on a Component ................................................................... 360 Using the Standard Validators ......................................................................................................... 361 Validating a Component’s Value ............................................................................................. 362 Using the LongRangeValidator .............................................................................................. 363 Binding Component Values and Instances to ExternalData Sources ........................................ 364 Binding a Component Value to a Property ............................................................................. 365 Binding a Component Value to an Implicit Object ............................................................... 366 Binding a Component Instance to a Bean Property .............................................................. 368 Binding Converters, Listeners, and Validators to Backing Bean Properties .............................. 369 Referencing a Backing BeanMethod .............................................................................................. 370 Contents 11 Referencing aMethod That PerformsNavigation ................................................................. 371 Referencing aMethod ThatHandles an Action Event .......................................................... 371 Referencing aMethod That Performs Validation .................................................................. 372 Referencing aMethod ThatHandles a Value-change Event ................................................ 372 Using Custom Objects ...................................................................................................................... 373 Using a Custom Converter ....................................................................................................... 374 Using a Custom Validator ......................................................................................................... 375 Using a Custom Component .................................................................................................... 376 12 Developing with JavaServer FacesTechnology ...........................................................................379 Writing Bean Properties ................................................................................................................... 379 Writing Properties Bound to Component Values ................................................................. 380 Writing Properties Bound to Component Instances ............................................................. 388 Writing Properties Bound to Converters, Listeners, or Validators ..................................... 389 Performing Localization ................................................................................................................... 390 Creating a Resource Bundle ...................................................................................................... 390 LocalizingDynamicData .......................................................................................................... 390 LocalizingMessages ................................................................................................................... 391 Creating a Custom Converter .......................................................................................................... 393 Implementing an Event Listener ..................................................................................................... 395 Implementing Value-Change Listeners .................................................................................. 396 Implementing Action Listeners ............................................................................................... 397 Creating a Custom Validator ........................................................................................................... 398 Implementing the Validator Interface ..................................................................................... 399 Creating a Custom Tag .............................................................................................................. 402 Writing Backing BeanMethods ...................................................................................................... 404 Writing aMethod toHandleNavigation ................................................................................ 404 Writing aMethod toHandle an Action Event ........................................................................ 406 Writing aMethod to Perform Validation ............................................................................... 406 Writing aMethod toHandle a Value-Change Event ............................................................. 407 13 Creating CustomUI Components ...................................................................................................409 Determining Whether YouNeed a Custom Component or Renderer ....................................... 410 When to Use a Custom Component ........................................................................................ 410 When to Use a Custom Renderer ............................................................................................. 411 Contents 12 The Java EE 5Tutorial • September 2007 Component, Renderer, and Tag Combinations ..................................................................... 412 Understanding the ImageMap Example ........................................................................................ 413 Why Use JavaServer Faces Technology to Implement an ImageMap? ............................... 413 Understanding the Rendered HTML ...................................................................................... 413 Understanding the JSP Page ..................................................................................................... 414 ConfiguringModelData ........................................................................................................... 416 Summary of the Application Classes ....................................................................................... 417 Steps for Creating a Custom Component ....................................................................................... 418 Creating Custom Component Classes ............................................................................................ 419 Specifying the Component Family .......................................................................................... 421 Performing Encoding ................................................................................................................ 422 PerformingDecoding ................................................................................................................ 424 Enabling Component Properties to Accept Expressions ...................................................... 424 Saving and Restoring State ........................................................................................................ 426 Delegating Rendering to a Renderer ............................................................................................... 427 Creating the Renderer Class ..................................................................................................... 427 Identifying the Renderer Type ................................................................................................. 429 Handling Events for Custom Components .................................................................................... 429 Creating the Component TagHandler ........................................................................................... 430 Retrieving the Component Type .............................................................................................. 431 Setting Component Property Values ....................................................................................... 431 Providing the Renderer Type ................................................................................................... 433 Releasing Resources ................................................................................................................... 434 Defining the Custom Component Tag in a Tag LibraryDescriptor ........................................... 434 14 Configuring JavaServer Faces Applications ..................................................................................437 Application Configuration Resource File ....................................................................................... 437 Configuring Beans ............................................................................................................................. 439 Using the managed-bean Element ............................................................................................ 439 Initializing Properties Using the managed-property Element ............................................ 441 InitializingMaps and Lists ........................................................................................................ 447 Registering Custom ErrorMessages ............................................................................................... 448 Registering Custom Localized Static Text ...................................................................................... 449 Registering a Custom Validator ....................................................................................................... 450 Registering a Custom Converter ..................................................................................................... 451 Contents 13 ConfiguringNavigation Rules ......................................................................................................... 451 Registering a Custom Renderer with a Render Kit ........................................................................ 455 Registering a Custom Component .................................................................................................. 457 Basic Requirements of a JavaServer Faces Application ................................................................. 458 Configuring an Application with aDeploymentDescriptor ................................................ 459 Including the Required JAR Files ............................................................................................. 466 Including the Classes, Pages, and Other Resources ............................................................... 466 15 Internationalizing and LocalizingWeb Applications ..................................................................467 Java Platform Localization Classes .................................................................................................. 467 Providing LocalizedMessages and Labels ...................................................................................... 468 Establishing the Locale .............................................................................................................. 468 Setting the Resource Bundle ..................................................................................................... 469 Retrieving LocalizedMessages ................................................................................................. 470 Date andNumber Formatting ......................................................................................................... 471 Character Sets and Encodings .......................................................................................................... 472 Character Sets ............................................................................................................................. 472 Character Encoding ................................................................................................................... 473 Further Information about Internationalizing Web Applications .............................................. 475 Part III Web Services .....................................................................................................................................477 16 BuildingWeb Services with JAX-WS ...............................................................................................479 Setting the Port .................................................................................................................................. 480 Creating a Simple Web Service and Client with JAX-WS ............................................................ 480 Requirements of a JAX-WS Endpoint ..................................................................................... 481 Coding the Service Endpoint Implementation Class ............................................................ 482 Building, Packaging, andDeploying the Service .................................................................... 482 Testing the Service without a Client ........................................................................................ 484 A Simple JAX-WS Client ........................................................................................................... 484 Types Supported by JAX-WS ........................................................................................................... 486 Web Services Interoperability and JAX-WS .................................................................................. 487 Further Information about JAX-WS ............................................................................................... 487 Contents 14 The Java EE 5Tutorial • September 2007 17 Binding between XML Schema and Java Classes ......................................................................... 489 JAXB Architecture ............................................................................................................................. 489 ArchitecturalOverview ............................................................................................................. 489 The JAXB Binding Process ........................................................................................................ 490 More about Unmarshalling ...................................................................................................... 492 More aboutMarshalling ............................................................................................................ 492 More about Validation .............................................................................................................. 492 Representing XML Content ............................................................................................................. 492 Java Representation of XML Schema ....................................................................................... 492 Binding XML Schemas ..................................................................................................................... 493 Simple TypeDefinitions ............................................................................................................ 493 DefaultData Type Bindings ..................................................................................................... 493 Customizing Generated Classes and Java Program Elements ..................................................... 495 Schema-to-Java .......................................................................................................................... 495 Java-to-Schema .......................................................................................................................... 496 JAXB Examples ................................................................................................................................. 501 JAXB Compiler Options ........................................................................................................... 503 JAXB Schema Generator Option ............................................................................................. 505 About the Schema-to-Java Bindings ........................................................................................ 505 Schema-Derived JAXB Classes ................................................................................................. 508 Basic JAXB Examples ........................................................................................................................ 511 ModifyMarshal Example .......................................................................................................... 511 Unmarshal Validate Example ................................................................................................... 512 Customizing JAXB Bindings ............................................................................................................ 514 Why Customize? ........................................................................................................................ 515 CustomizationOverview .......................................................................................................... 515 Customize Inline Example ........................................................................................................ 526 Datatype Converter Example ................................................................................................... 531 BindingDeclaration Files .......................................................................................................... 533 External Customize Example ................................................................................................... 536 Java-to-Schema Examples ................................................................................................................ 536 CreateMarshal Example ........................................................................................................... 537 XmlAccessorOrder Example .................................................................................................... 538 XmlAdapter Field Example ...................................................................................................... 540 XmlAttribute Field Example ..................................................................................................... 543 XmlRootElement Example ....................................................................................................... 544 Contents 15 XmlSchemaType Class Example .............................................................................................. 545 XmlType Example ..................................................................................................................... 546 Further Information about JAXB .................................................................................................... 548 18 Streaming API for XML ......................................................................................................................549 Why StAX? ........................................................................................................................................ 549 Streaming versusDOM ............................................................................................................. 549 Pull Parsing versus Push Parsing .............................................................................................. 550 StAX Use Cases .......................................................................................................................... 551 Comparing StAX to Other JAXP APIs .................................................................................... 551 StAX API ........................................................................................................................................... 552 Cursor API ................................................................................................................................. 553 Iterator API ................................................................................................................................ 553 Choosing between Cursor and Iterator APIs .......................................................................... 557 Using StAX ........................................................................................................................................ 559 StAX Factory Classes ................................................................................................................. 559 Resources,Namespaces, and Errors ........................................................................................ 561 Reading XML Streams ............................................................................................................... 561 Writing XML Streams ............................................................................................................... 564 Sun’s Streaming XML Parser Implementation .............................................................................. 566 Reporting CDATA Events ......................................................................................................... 566 Streaming XML Parser Factories Implementation ................................................................ 566 Example Code ................................................................................................................................... 567 Example Code Organization .................................................................................................... 567 Example XMLDocument ......................................................................................................... 568 Cursor Example .......................................................................................................................... 568 Cursor-to-Event Example ......................................................................................................... 571 Event Example ............................................................................................................................ 573 Filter Example ............................................................................................................................ 575 Read-and-Write Example ......................................................................................................... 578 Writer Example .......................................................................................................................... 580 Further Information about StAX ..................................................................................................... 583 19 SOAP with Attachments API for Java ............................................................................................. 585 Overview of SAAJ .............................................................................................................................. 586 Contents 16 The Java EE 5Tutorial • September 2007 SAAJMessages ........................................................................................................................... 586 SAAJ Connections ..................................................................................................................... 589 SAAJ Tutorial .................................................................................................................................... 590 Creating and Sending a SimpleMessage ................................................................................. 591 Adding Content to theHeader ................................................................................................. 598 Adding Content to the SOAPPart Object ................................................................................. 599 Adding aDocument to the SOAP Body .................................................................................. 600 ManipulatingMessage Content Using SAAJ orDOMAPIs ................................................ 601 Adding Attachments .................................................................................................................. 601 Adding Attributes ...................................................................................................................... 603 Using SOAP Faults ..................................................................................................................... 608 Code Examples ................................................................................................................................. 613 Request Example ........................................................................................................................ 613 Header Example ......................................................................................................................... 614 DOMandDOMSource Examples ........................................................................................... 617 Attachments Example ............................................................................................................... 622 SOAP Fault Example ................................................................................................................. 624 Further Information about SAAJ .................................................................................................... 627 Part IV Enterprise Beans ...............................................................................................................................629 20 Enterprise Beans ...............................................................................................................................631 What Is an Enterprise Bean? ............................................................................................................ 631 Benefits of Enterprise Beans ..................................................................................................... 631 When to Use Enterprise Beans ................................................................................................. 632 Types of Enterprise Beans ......................................................................................................... 632 What Is a Session Bean? .................................................................................................................... 633 StateManagementModes ......................................................................................................... 633 When to Use Session Beans ...................................................................................................... 634 What Is aMessage-Driven Bean? .................................................................................................... 634 WhatMakesMessage-Driven Beans Different fr