Table Of ContentThe J2EE™ 1.4 Tutorial
For Sun Java System Application Server Platform Edition
8.2
Eric Armstrong
Jennifer Ball
Stephanie Bodoff
Debbie Bode Carson
Ian Evans
Dale Green
Kim Haase
Eric Jendrock
December 7, 2005
Copyright©2006SunMicrosystems,Inc.,4150NetworkCircle,SantaClara,California95054,U.S.A.
Allrightsreserved.U.S.GovernmentRights-Commercialsoftware.Governmentusersaresubjecttothe
SunMicrosystems,Inc.standardlicenseagreementandapplicableprovisionsoftheFARanditssupple-
ments.
This distribution may include materials developed by third parties.
Sun, Sun Microsystems, the Sun logo, Java, JavaBeans, JavaServer, JavaServer Pages, Enterprise
JavaBeans,JavaNamingandDirectoryInterface,JavaMail,JDBC,EJB,JSP,J2EE,J2SE,“WriteOnce,
RunAnywhere”,andtheJavaCoffeeCuplogoaretrademarksorregisteredtrademarksofSunMicrosys-
tems, Inc. in the U.S. and other countries.
Unlessotherwiselicensed,softwarecodeinalltechnicalmaterialsherein(includingarticles,FAQs,sam-
ples) is provided under this License.
ProductscoveredbyandinformationcontainedinthisservicemanualarecontrolledbyU.S.ExportCon-
trollawsandmaybesubjecttotheexportorimportlawsinothercountries.Nuclear,missile,chemical
biologicalweaponsornuclearmaritimeendusesorendusers,whetherdirectorindirect,arestrictlypro-
hibited.ExportorreexporttocountriessubjecttoU.S.embargoortoentitiesidentifiedonU.S.export
exclusionlists,including,butnotlimitedto,thedeniedpersonsandspeciallydesignatednationalslistsis
strictly prohibited.
DOCUMENTATION IS PROVIDED "AS IS" AND ALL EXPRESS OR IMPLIED CONDITIONS,
REPRESENTATIONSANDWARRANTIES,INCLUDINGANYIMPLIEDWARRANTYOFMER-
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©2006SunMicrosystems,Inc.,4150NetworkCircle,SantaClara,California95054,États-
Unis. Tous droits réservés.
Droitsdugouvernementaméricain,utlisateursgouvernmentaux-logicielcommercial.Lesutilisateurs
gouvernmentauxsontsoumisaucontratdelicencestandarddeSunMicrosystems,Inc.,ainsiquauxdis-
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, JavaBeans, JavaServer, JavaServer Pages, Enterprise
JavaBeans,JavaNamingandDirectoryInterface,JavaMail,JDBC,EJB,JSP,J2EE,J2SE,“WriteOnce,
RunAnywhere”,etlelogoJavaCoffeeCupsontdesmarquesdefabriqueoudesmarquesdéposéesde
Sun Microsystems, Inc. aux États-Unis et dans d’autres pays.
Amoinsqu’autrementautorisé,lecodedelogicielentouslesmatériauxtechniquesdansleprésent(arti-
cles y compris, FAQs, échantillons) est fourni sous ce permis.
Lesproduitsquifontl’objetdecemanueld’entretienetlesinformationsqu’ilcontientsontrégisparla
législationaméricaineenmatièredecontrôledesexportationsetpeuventêtresoumisaudroitd’autres
paysdansledomainedesexportationsetimportations.Lesutilisationsfinales,ouutilisateursfinaux,pour
desarmesnucléaires,desmissiles,desarmesbiologiquesetchimiquesoudunucléairemaritime,directe-
mentouindirectement,sontstrictementinterdites.Lesexportationsouréexportationsversdespayssous
embargodesÉtats-Unis,ouversdesentitésfigurantsurleslistesd’exclusiond’exportationaméricaines,
ycompris,maisdemanièrenonexclusive,lalistedepersonnesquifontobjetd’unordredenepaspartic-
iper,d’unefaçondirecteouindirecte,auxexportationsdesproduitsoudesservicesquisont régiparla
législationaméricaineenmatièredecontrôledesexportations("U.S.CommerceDepartment’sTableof
DenialOrders"etlalistederessortissantsspécifiquementdésignés("U.S.TreasuryDepartmentofSpe-
cially Designated Nationals and Blocked Persons "),, sont rigoureusement interdites.
LADOCUMENTATIONESTFOURNIE"ENL’ÉTAT"ETTOUTESAUTRESCONDITIONS,DEC-
LARATIONS ET GARANTIES EXPRESSES OU TACITES SONT FORMELLEMENT EXCLUES,
DANSLAMESUREAUTORISEEPARLALOIAPPLICABLE,YCOMPRISNOTAMMENTTOUTE
GARANTIE IMPLICITE RELATIVE A LA QUALITE MARCHANDE, A L’APTITUDE A UNE
UTILISATION PARTICULIERE OU A L’ABSENCE DE CONTREFAÇON.
Contents
Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxxi
About This Tutorial. . . . . . . . . . . . . . . . . . . . . . . . .xxxiii
Who Should Use This Tutorial xxxiii
Prerequisites xxxiii
How to Read This Tutorial xxxiv
About the Examples xxxvi
Further Information xxxix
How to Buy This Tutorial xl
How to Print This Tutorial xl
Typographical Conventions xli
Acknowledgments xli
Feedback xlii
Chapter 1: Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Distributed Multitiered Applications 2
J2EE Components 3
J2EE Clients 4
Web Components 6
Business Components 6
Enterprise Information System Tier 8
J2EE Containers 8
Container Services 8
Container Types 9
Web Services Support 10
XML 11
SOAP Transport Protocol 12
WSDL Standard Format 12
UDDI and ebXML Standard Formats 12
iii
iv
Packaging Applications 13
Development Roles 15
J2EE Product Provider 15
Tool Provider 15
Application Component Provider 16
Application Assembler 16
Application Deployer and Administrator 17
J2EE 1.4 APIs 18
Enterprise JavaBeans Technology 18
Java Servlet Technology 19
JavaServer Pages Technology 19
Java Message Service API 19
Java Transaction API 19
JavaMail API 20
JavaBeans Activation Framework 20
Java API for XML Processing 20
Java API for XML-Based RPC 20
SOAP with Attachments API for Java 21
Java API for XML Registries 21
J2EE Connector Architecture 22
JDBC API 22
Java Naming and Directory Interface 22
Java Authentication and Authorization Service 23
Simplified Systems Integration 24
Sun Java System Application Server Platform Edition 8 24
Technologies 25
Tools 26
Starting and Stopping the Application Server 27
Starting the Admin Console 28
Starting the deploytool Utility 29
Starting and Stopping the Derby Database Server 29
Debugging J2EE Applications 30
Chapter 2: Understanding XML. . . . . . . . . . . . . . . . . . . . . . . . . .33
Introduction to XML 33
What Is XML? 33
Why Is XML Important? 38
How Can You Use XML? 40
Generating XML Data 43
Writing a Simple XML File 43
v
Defining the Root Element 44
Writing Processing Instructions 48
Introducing an Error 49
Substituting and Inserting Text 50
Creating a Document Type Definition 54
Documents and Data 59
Defining Attributes and Entities in the DTD 59
Referencing Binary Entities 66
Defining Parameter Entities and Conditional Sections 68
Resolving a Naming Conflict 72
Using Namespaces 73
Designing an XML Data Structure 76
Saving Yourself Some Work 77
Attributes and Elements 77
Normalizing Data 79
Normalizing DTDs 81
Summary 81
Chapter 3: Getting Started with Web Applications . . . . . . . . . 83
Web Application Life Cycle 86
Web Modules 88
Packaging Web Modules 90
Deploying Web Modules 92
Listing Deployed Web Modules 95
Updating Web Modules 96
Undeploying Web Modules 98
Configuring Web Applications 99
Mapping URLs to Web Components 99
Declaring Welcome Files 101
Setting Initialization Parameters 102
Mapping Errors to Error Screens 102
Declaring Resource References 103
Duke’s Bookstore Examples 103
Accessing Databases from Web Applications 104
Populating the Example Database 105
Creating a Data Source in the Application Server 106
Specifying a Web Application’s Resource Reference 106
Mapping the Resource Reference to a Data Source 107
Further Information 108
vi
Chapter 4: Java API for XML Processing . . . . . . . . . . . . . . . . .109
The JAXP APIs 109
An Overview of the Packages 110
The Simple API for XML APIs 111
The SAX Packages 114
The Document Object Model APIs 114
The DOM Packages 116
The Extensible Stylesheet Language Transformations APIs 117
The XSLT Packages 118
Using the JAXP Libraries 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 124
Importing Classes 124
Setting Up for I/O 125
Implementing the ContentHandler Interface 125
Setting up the Parser 127
Writing the Output 128
Spacing the Output 128
Handling Content Events 129
Compiling and Running the Program 134
Checking the Output 135
Identifying the Events 136
Compressing the Output 138
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
vii
Tracking Ignorable Whitespace 157
Cleanup 159
Empty Elements, Revisited 159
Echoing Entity References 160
Echoing the External Entity 160
Summarizing Entities 161
Choosing Your Parser Implementation 161
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 168
DTD Warnings 170
Handling Lexical Events 170
How the LexicalHandler Works 171
Working with a LexicalHandler 172
Using the DTDHandler and EntityResolver 177
The DTDHandler API 178
The EntityResolver API 179
Further Information 179
Chapter 6: Document Object Model . . . . . . . . . . . . . . . . . . . 181
When to Use DOM 182
Documents Versus 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 192
Looking Ahead 194
Displaying a DOM Hierarchy 195
Convert DomEcho to a GUI Application 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 211
Displaying a More Complex Tree 214
viii
Finishing Up 220
Constructing a User-Friendly JTree from a DOM 221
Compressing the Tree View 221
Acting on Tree Selections 227
Handling Modifications 237
Finishing Up 237
Creating and Manipulating a DOM 237
Obtaining a DOM from the Factory 237
Normalizing the DOM 241
Other Operations 243
Finishing Up 246
Validating with XML Schema 246
Overview of the Validation Process 247
Configuring the DocumentBuilder Factory 247
Validating with Multiple Namespaces 249
Further Information 252
Chapter 7: Extensible Stylesheet Language Transformations253
Introducing XSL, XSLT, and XPath 254
The JAXP Transformation Packages 254
How XPath Works 255
XPath Expressions 255
The XSLT/XPath Data Model 256
Templates and Contexts 257
Basic XPath Addressing 257
Basic XPath Expressions 258
Combining Index Addresses 259
Wildcards 259
Extended-Path Addressing 260
XPath Data Types and Operators 261
String-Value of an Element 261
XPath Functions 262
Summary 265
Writing Out a DOM as an XML File 265
Reading the XML 266
Creating a Transformer 267
Writing the XML 270
Writing Out a Subtree of the DOM 271
Summary 272
Generating XML from an Arbitrary Data Structure 272
ix
Creating a Simple File 273
Creating a Simple Parser 275
Modifying the Parser to Generate SAX Events 277
Using the Parser as a SAXSource 284
Doing the Conversion 286
Transforming XML Data with XSLT 287
Defining a Simple <article> Document Type 287
Creating a Test Document 289
Writing an XSLT Transform 290
Processing the Basic Structure Elements 291
Writing the Basic Program 295
Trimming the Whitespace 297
Processing the Remaining Structure Elements 300
Process Inline (Content) Elements 304
Printing the HTML 309
What Else Can XSLT Do? 309
Transforming from the Command Line with Xalan 311
Concatenating Transformations with a Filter Chain 311
Writing the Program 311
Understanding How the Filter Chain Works 315
Testing the Program 316
Further Information 318
Chapter 8: Building Web Services with JAX-RPC . . . . . . . . . . 319
Setting the Port 320
Creating a Simple Web Service and Client with JAX-RPC 320
Coding the Service Endpoint Interface and Implementation Class 322
Building the Service 323
Packaging and Deploying the Service 324
Static Stub Client 327
Types Supported by JAX-RPC 330
J2SE SDK Classes 331
Primitives 331
Arrays 332
Value Types 332
JavaBeans Components 332
Web Service Clients 333
Dynamic Proxy Client 333
Dynamic Invocation Interface Client 336
Application Client 340
x
More JAX-RPC Clients 343
Web Services Interoperability and JAX-RPC 344
Further Information 344
Chapter 9: SOAP with Attachments API for Java . . . . . . . . . .345
Overview of SAAJ 346
Messages 346
Connections 350
Tutorial 352
Creating and Sending a Simple Message 353
Adding Content to the Header 362
Adding Content to the SOAPPart Object 363
Adding a Document to the SOAP Body 364
Manipulating Message Content Using SAAJ or DOM APIs 364
Adding Attachments 365
Adding Attributes 368
Using SOAP Faults 373
Code Examples 378
Request.java 378
MyUddiPing.java 380
HeaderExample.java 387
DOMExample.java and DOMSrcExample.java 388
Attachments.java 392
SOAPFaultTest.java 394
Further Information 395
Chapter 10: Java API for XML Registries . . . . . . . . . . . . . . . . . .397
Overview of JAXR 397
What Is a Registry? 397
What Is JAXR? 398
JAXR Architecture 399
Implementing a JAXR Client 400
Establishing a Connection 401
Querying a Registry 408
Managing Registry Data 413
Using Taxonomies in JAXR Clients 421
Running the Client Examples 426
Before You Compile the Examples 427
Compiling the Examples 429
Description:The J2EE™ 1.4 Tutorial. For Sun Java System Application Server Platform Edition. 8.2. Eric Armstrong. Jennifer Ball. Stephanie Bodoff. Debbie Bode