The J2EE™ 1.4 Tutorial

The J2EE™ 1.4 Tutorial

Documents
902 pages
Lire
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

The J2EE™ 1.4 Tutorial
Eric Armstrong
Stephanie Bodoff
Debbie Carson
Ian Evans
Maydene Fisher
Dale Green
Kim Haase
Eric Jendrock
Monica Pawlan
Beth Stearns
May 30, 2003 Copyright © 2003 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A.
All rights reserved.U.S. Government Rights - Commercial software. Government users are subject to the
Sun Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its supple-
ments.
This distribution may include materials developed by third parties.
Sun, Sun Microsystems, the Sun logo, Java, J2EE, JavaServer Pages, Enterprise JavaBeans, Java
Naming and Directory Interface, EJB, JSP, J2EE, J2SE and the Java Coffee Cup logo are trademarks or
registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
Unless otherwise licensed, software code in all technical materials herein (including articles, FAQs, sam-
ples) is provided under this License.
Products covered by and information contained in this service manual are controlled by U.S. Export Con-
trol laws and may be subject to the export or import laws in other countries. Nuclear, missile, chemical
biological weapons or nuclear maritime end uses or end users, whether direct or indirect, are strictly pro-
hibited. Export or reexport to countries subject to U.S. embargo or to entities identified on U.S. export
exclusion lists, including, but not limited to, the denied persons and specially designated nationals ...

Sujets

Informations

Publié par
Nombre de visites sur la page 108
Langue English
Signaler un problème
The J2EE™ 1.4 Tutorial Eric Armstrong Stephanie Bodoff Debbie Carson Ian Evans Maydene Fisher Dale Green Kim Haase Eric Jendrock Monica Pawlan Beth Stearns May 30, 2003 Copyright © 2003 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. All rights reserved.U.S. Government Rights - Commercial software. Government users are subject to the Sun Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its supple- ments. This distribution may include materials developed by third parties. Sun, Sun Microsystems, the Sun logo, Java, J2EE, JavaServer Pages, Enterprise JavaBeans, Java Naming and Directory Interface, EJB, JSP, J2EE, J2SE and the Java Coffee Cup logo are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. Unless otherwise licensed, software code in all technical materials herein (including articles, FAQs, sam- ples) is provided under this License. Products covered by and information contained in this service manual are controlled by U.S. Export Con- trol laws and may be subject to the export or import laws in other countries. Nuclear, missile, chemical biological weapons or nuclear maritime end uses or end users, whether direct or indirect, are strictly pro- hibited. Export or reexport to countries subject to U.S. embargo or to entities identified on U.S. export exclusion lists, including, but not limited to, the denied persons and specially designated nationals lists is strictly prohibited. DOCUMENTATION IS PROVIDED "AS IS" AND ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MER- CHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID. Copyright © 2003 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, États- Unis. Tous droits réservés. Droits du gouvernement américain, utlisateurs gouvernmentaux - logiciel commercial. Les utilisateurs gouvernmentaux sont soumis au contrat de licence standard de Sun Microsystems, Inc., ainsi qu aux dis- positions en vigueur de la FAR [ (Federal Acquisition Regulations) et des suppléments à celles-ci. Cette distribution peut comprendre des composants développés pardes tierces parties. Sun, Sun Microsystems, le logo Sun, Java, JavaServer Pages, Enterprise JavaBeans, Java Naming and Directory Interface, EJB, JSP, J2EE, J2SE et le logo Java Coffee Cup sont des marques de fabrique ou des marques déposées de Sun Microsystems, Inc. aux États-Unis et dans d’autres pays. A moins qu’autrement autorisé, le code de logiciel en tous les matériaux techniques dans le présent (arti- cles y compris, FAQs, échantillons) est fourni sous ce permis. Les produits qui font l’objet de ce manuel d’entretien et les informations qu’il contient sont régis par la législation américaine en matière de contrôle des exportations et peuvent être soumis au droit d’autres pays dans le domaine des exportations et importations. Les utilisations finales, ou utilisateurs finaux, pour des armes nucléaires, des missiles, des armes biologiques et chimiques ou du nucléaire maritime, directe- ment ou indirectement, sont strictement interdites. Les exportations ou réexportations vers des pays sous embargo des États-Unis, ou vers des entités figurant sur les listes d’exclusion d’exportation américaines, y compris, mais de manière non exclusive, la liste de personnes qui font objet d’un ordre de ne pas partic- iper, d’une façon directe ou indirecte, aux exportations des produits ou des services qui sont régi par la législation américaine en matière de contrôle des exportations ("U .S. Commerce Department’s Table of Denial Orders "et la liste de ressortissants spécifiquement désignés ("U.S. Treasury Department of Spe- cially Designated Nationals and Blocked Persons "),, sont rigoureusement interdites. LA DOCUMENTATION EST FOURNIE "EN L’ÉTAT" ET TOUTES AUTRES CONDITIONS, DEC- LARATIONS ET GARANTIES EXPRESSES OU TACITES SONT FORMELLEMENT EXCLUES, DANS LA MESURE AUTORISEE PAR LA LOI APPLICABLE, Y COMPRIS NOTAMMENT TOUTE GARANTIE IMPLICITE RELATIVE A LA QUALITE MARCHANDE, A L’APTITUDE A UNE UTILISATION PARTICULIERE OU A L’ABSENCE DE CONTREFAÇON. Contents About This Tutorial. . . . . . . . . . . . . . . . . . . . . . . . . . .xix Who Should Use This Tutorial xix How to Read This Tutorial xix About the Examples xxi How to Print This Tutorial xxiii Typographical Conventions xxiii Chapter 1: Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Web Services Support 2 Extensible Markup Language 2 HTTP-SOAP Transport Protocol 3 WSDL Standard Format 3 UDDI and ebXML Standard Formats 4 Distributed Multitiered Applications 4 J2EE Components 5 J2EE Clients 6 Web Components 8 Business Components 8 Enterprise Information System Tier 10 J2EE Containers 10 Container Services 10 Container Types 11 Packaging 12 Development Roles 13 J2EE Product Provider 14 Tool Provider 14 Application Component Provider 14 Application Assembler 15 Application Deployer and Administrator 16 iii iv CONTENTS J2EE APIs 16 Enterprise JavaBeans Technology 16 JDBC API 17 Java Servlet Technology 17 JavaServer Pages Technology 17 Java Message Service 18 Java Naming and Directory Interface 18 Java Transaction API 18 JavaMail API 19 JavaBeans Activation Framework 19 Java API for XML Processing 19 Java API for XML Registries 19 Java API for XML-Based RPC 20 SOAP with Attachments API for Java (SAAJ) 20 J2EE Connector Architecture 21 Java Authentication and Authorization Service 21 Simplified Systems Integration 21 Chapter 2: Understanding XML. . . . . . . . . . . . . . . . . . . . . . . . . .23 Introduction to XML 23 What Is XML? 23 Why Is XML Important? 28 How Can You Use XML? 31 XML and Related Specs: Digesting the Alphabet Soup 33 Basic Standards 34 Schema Standards 38 Linking and Presentation Standards 40 Knowledge Standards 42 Standards That Build on XML 43 Summary 45 Generating XML Data 45 Writing a Simple XML File 45 Defining the Root Element 46 Writing Processing Instructions 50 Introducing an Error 52 Substituting and Inserting Text 53 Creating a Document Type Definition (DTD) 56 Documents and Data 62 Defining Attributes and Entities in the DTD 62 Referencing Binary Entities 69 CONTENTS v Defining Parameter Entities and Conditional Sections 71 Resolving A Naming Conflict 74 Using Namespaces 76 Designing an XML Data Structure 79 Saving Yourself Some Work 79 Attributes and Elements 79 Normalizing Data 82 Normalizing DTDs 83 Summary 84 Chapter 3: Getting Started with Web Applications . . . . . . . . . 85 Web Application Life Cycle 86 J2EE 1.4 Application Server 88 Components 88 Setting Up To Build and Deploy Tutorial Examples 89 Starting and Stopping the J2EE Application Server 90 Starting the deploytool Utility 91 Web Modules 91 Creating a Web Module 92 Configuring Web Modules 93 Mapping URLs to Web Components 94 Declaring Welcome Files 95 Setting Initialization Parameters 96 Specifying Error Mappings 96 Declaring References to Environment Entries, Resource Environment Entries, or Resources 97 Deploying Web Modules 98 Listing Deployed Web Modules 98 Running Web Applications 98 Updating Web Modules 99 Undeploying W 101 Duke’s Bookstore Examples 101 Accessing Databases from Web Applications 102 Starting the PointBase Database Server 103 Populating the Example Database 104 Defining a Data Source in the J2EE Server 105 Configuring the Web Application to Reference a Data Source with JNDI 106 Mapping the Web Application JNDI Name to a Data Source 107 Further Information 107 vi CONTENTS Chapter 4: Java API for XML Processing . . . . . . . . . . . . . . . . .109 The JAXP APIs 109 An Overview of the Packages 110 The Simple API for XML (SAX) APIs 111 The SAX Packages 114 The Document Object Model (DOM) APIs 114 The DOM Packages 116 The XML Stylesheet Language for Transformation (XSLT) APIs 117 The XSLT Packages 118 Compiling and Running the Programs 118 Where Do You Go from Here? 118 Chapter 5: Simple API for XML . . . . . . . . . . . . . . . . . . . . . . . . .121 When to Use SAX 122 Echoing an XML File with the SAX Parser 123 Creating the Skeleton 123 Importing Classes 124 Setting up for I/O 124 Implementing the ContentHandler Interface 125 Setting up the Parser 126 Writing the Output 127 Spacing the Output 128 Handling Content Events 128 Compiling and Running the Program 133 Checking the Output 134 Identifying the Events 135 Compressing the Output 137 Inspecting the Output 140 Documents and Data 141 Adding Additional Event Handlers 141 Identifying the Document’s Location 142 Handling Processing Instructions 144 Summary 145 Handling Errors with the Nonvalidating Parser 145 Displaying Special Characters and CDATA 153 Handling Special Characters 153 Handling Text with XML-Style Syntax 154 Handling CDATA and Other Characters 155 Parsing with a DTD 156 DTD’s Effect on the Nonvalidating Parser 156 CONTENTS vii Tracking Ignorable Whitespace 158 Cleanup 159 Empty Elements, Revisited 160 Echoing Entity References 160 Echoing the External Entity 160 Summarizing Entities 161 Choosing your Parser Implementation 162 Using the Validating Parser 162 Configuring the Factory 162 Validating with XML Schema 163 Experimenting with Validation Errors 166 Error Handling in the Validating Parser 168 Parsing a Parameterized DTD 169 DTD Warnings 170 Handling Lexical Events 171 How the LexicalHandler Works 172 Working with a LexicalHandler 172 Using the DTDHandler and EntityResolver 178 The DTDHandler API 178 The EntityResolver API 179 Further Information 180 Chapter 6: Document Object Model . . . . . . . . . . . . . . . . . . . 181 When to Use DOM 182 Documents vs. Data 182 Mixed Content Model 183 A Simpler Model 184 Increasing the Complexity 185 Choosing Your Model 187 Reading XML Data into a DOM 188 Creating the Program 188 Additional Information 193 Looking Ahead 195 Displaying a DOM Hierarchy 195 Echoing Tree Nodes 195 Convert DomEcho to a GUI App 195 Create Adapters to Display the DOM in a JTree 201 Finishing Up 211 Examining the Structure of a DOM 211 Displaying A Simple Tree 212 viii CONTENTS Displaying a More Complex Tree 214 Finishing Up 221 Constructing a User-Friendly JTree from a DOM 222 Compressing the Tree View 222 Acting on Tree Selections 228 Handling Modifications 238 Finishing Up 238 Creating and Manipulating a DOM 238 Obtaining a DOM from the Factory 239 Normalizing the DOM 242 Other Operations 244 Finishing Up 247 Validating with XML Schema 247 Overview of the Validation Process 248 Configuring the DocumentBuilder Factory 248 Validating with Multiple Namespaces 250 Further Information 253 Chapter 7: XML Stylesheet Language for Transformations. . .255 Introducing XSLT and XPath 255 The JAXP Transformation Packages 256 How XPath Works 257 XPATH Expressions 257 The XSLT/XPath Data Model 258 Templates and Contexts 259 Basic XPath Addressing 259 Basic XPath Expressions 260 Combining Index Addresses 261 Wildcards 261 Extended-Path Addressing 262 XPath Data Types and Operators 263 String-Value of an Element 263 XPath Functions 264 Summary 267 Writing Out a DOM as an XML File 268 Reading the XML 268 Creating a Transformer 270 Writing the XML 272 Writing Out a Subtree of the DOM 273 Summary 274 CONTENTS ix Generating XML from an Arbitrary Data Structure 275 Creating a Simple File 275 Creating a Simple Parser 277 Modifying the Parser to Generate SAX Events 279 Using the Parser as a SAXSource 286 Doing the Conversion 288 Transforming XML Data with XSLT 289 Defining a Simple
Document Type 289 Creating a Test Document 291 Writing an XSLT Transform 292 Processing the Basic Structure Elements 293 Writing the Basic Program 297 Trimming the Whitespace 299 Processing the Remaining Structure Elements 302 Process Inline (Content) Elements 306 Printing the HTML 311 What Else Can XSLT Do? 311 Transforming from the Command Line with Xalan 313 Concatenating Transformations with a Filter Chain 314 Writing the Program 314 Understanding How the Filter Chain Works 317 Testing the Program 318 Conclusion 321 Further Information 321 Chapter 8: Building Web Services With JAX-RPC . . . . . . . . . . 323 Types Supported By JAX-RPC 324 J2SE SDK Classes 324 Primitives 325 Arrays 325 Value Types 326 JavaBeans Components 326 Creating a Web Service with JAX-RPC 326 Building the Service 328 Packaging the Service 330 Specifying the Endpoint Address 331 Deploying the Service 332 Creating Web Service Clients with JAX-RPC 332 Static Stub Client Example 332 Dynamic Proxy Client Example 335 x CONTENTS Dynamic Invocation Interface (DII) Client Example 338 J2EE Application Client Example 342 Other JAX-RPC Client Examples 346 Further Information 346 Chapter 9: SOAP with Attachments API for Java . . . . . . . . . .347 Overview of SAAJ 348 Messages 348 Connections 352 Tutorial 353 Creating and Sending a Simple Message 354 Adding Content to the Header 362 Adding Content to the SOAP Body 363 Adding Content to the SOAPPart Object 364 Adding a Document to the SOAP Body 366 Manipulating Message Content Using SAAJ or DOM APIs 366 Adding Attachments 367 Adding Attributes 369 Using SOAP Faults 375 Code Examples 380 Request.java 380 MyUddiPing.java 381 HeaderExample.java 389 SOAPFaultTest.java 390 DOMExample.java 391 Conclusion 393 Further Information 394 Chapter 10: Java API for XML Registries . . . . . . . . . . . . . . . . . .395 Overview of JAXR 395 What Is a Registry? 395 What Is JAXR? 396 JAXR Architecture 397 Implementing a JAXR Client 399 Establishing a Connection 400 Querying a Registry 405 Managing Registry Data 410 Using Taxonomies in JAXR Clients 416 Running the Client Examples 421