The Java™ EE 5 Tutorial

The Java™ EE 5 Tutorial

-

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

Description

The Java™ EE 5
Tutorial
For Sun Java System Application Server Platform Edition 9
Jennifer Ball
Debbie Carson
Ian Evans
Scott Fordin
Kim Haase
Eric Jendrock
June 16, 2006 Copyright © 2006 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, JavaBeans, JavaServer, JavaServer Pages, Enterprise Java-
Beans, Java Naming and Directory Interface, JavaMail, JDBC, EJB, JSP, J2EE, J2SE, “Write Once, Run
Anywhere”, 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 ...

Sujets

Informations

Publié par
Nombre de visites sur la page 126
Langue English
Signaler un problème
The Java™ EE 5 Tutorial For Sun Java System Application Server Platform Edition 9 Jennifer Ball Debbie Carson Ian Evans Scott Fordin Kim Haase Eric Jendrock June 16, 2006 Copyright © 2006 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, JavaBeans, JavaServer, JavaServer Pages, Enterprise Java- Beans, Java Naming and Directory Interface, JavaMail, JDBC, EJB, JSP, J2EE, J2SE, “Write Once, Run Anywhere”, 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 © 2006 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, JavaBeans, JavaServer, JavaServer Pages, Enterprise Java- Beans, Java Naming and Directory Interface, JavaMail, JDBC, EJB, JSP, J2EE, J2SE, “Write Once, Run Anywhere”, 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. . . . . . . . . . . . . . . . . . . . . . . . . xxvii Who Should Use This Tutorial xxvii Prerequisites xxvii How to Read This Tutorial xxviii About the Examples xxx Further Information xxxiv How to Print This Tutorial xxxiv Typographical Conventions xxxiv Feedback xxxv Chapter 1: Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Java EE Application Model 2 Distributed Multitiered Applications 3 Security 4 Java EE Components 4Clients 4 Web Components 7 Business Components 7 Enterprise Information System Tier 9 Java EE Containers 9 Container Services 9 Container Types 10 Web Services Support 11 XML 12 SOAP Transport Protocol 13 WSDL Standard Format 13 UDDI and ebXML Standard Formats 13 Java EE Application Assembly and Deployment 14 Packaging Applications 14 iii iv CONTENTS Development Roles 16 Java EE Product Provider 16 Tool Provider 16 Application Component Provider 17 Application Assembler 17 Application Deployer and Administrator 18 Java EE 5 APIs 19 Enterprise JavaBeans Technology 19 Java Servlet Technology 20 JavaServer Pages Technology 20 JavaServer Pages Standard Tag Library 20 JavaServer Faces 20 Java Message Service API 21 Java Transaction API 21 JavaMail API 21 JavaBeans Activation Framework 22 Java API for XML Processing 22 Java API for XML Web Services (JAX-WS) 22 Java Architecture for XML Binding (JAXB) 23 SOAP with Attachments API for Java 23 Java API for XML Registries 23 J2EE Connector Architecture 24 Java Database Connectivity API 24 Java Persistence API 24 Java Naming and Directory Interface 25 Java Authentication and Authorization Service 26 Simplified Systems Integration 26 Sun Java System Application Server Platform Edition 9 26 Tools 27 Starting and Stopping the Application Server 28 Starting the Admin Console 29 Starting and Stopping the Java DB Database Server 30 Debugging Java EE Applications 30 Part One: The Web Tier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .33 Chapter 2: Getting Started with Web Applications. . . . . . . . . .35 Web Application Life Cycle 38 Web Modules 40 Packaging Web Modules 42 Deploying a WAR File 43 CONTENTS v Testing Deployed Web Modules 44 Listing Deployed Web Modules 44 Updating Web Modules 45 Undeploying Web Modules 47 Configuring Web Applications 48 Mapping URLs to Web Components 48 Declaring Welcome Files 49 Setting Initialization Parameters 50 Mapping Errors to Error Screens 50 Declaring Resource References 51 Duke’s Bookstore Examples 54 Accessing Databases from Web Applications 54 Populating the Example Database 55 Creating a Data Source in the Application Server 55 Further Information 56 Chapter 3: Java Servlet Technology . . . . . . . . . . . . . . . . . . . . . 57 What Is a Servlet? 57 The Example Servlets 58 Troubleshooting 60 Servlet Life Cycle 61 Handling Servlet Life-Cycle Events 61 Handling Errors 64 Sharing Information 64 Using Scope Objects 64 Controlling Concurrent Access to Shared Resources 65 Accessing Databases 67 Initializing a Servlet 68 Writing Service Methods 69 Getting Information from Requests 70 Constructing Responses 72 Filtering Requests and Responses 75 Programming Filters 75 Programming Customized Requests and Responses 77 Specifying Filter Mappings 80 Invoking Other Web Resources 82 Including Other Resources in the Response 82 Transferring Control to Another Web Component 84 Accessing the Web Context 85 Maintaining Client State 86 vi CONTENTS Accessing a Session 86 Associating Objects with a Session 86 Session Management 87 Session Tracking 88 Finalizing a Servlet 89 Tracking Service Requests 89 Notifying Methods to Shut Down 90 Creating Polite Long-Running Methods 91 Further Information 92 Chapter 4: JavaServer Pages Technology . . . . . . . . . . . . . . . .93 What Is a JSP Page? 93 Example 94 The Example JSP Pages 97 The Life Cycle of a JSP Page 102 Translation and Compilation 102 Execution 103 Creating Static Content 105 Response and Page Encoding 106 Creating Dynamic Content 106 Using Objects within JSP Pages 106 Unified Expression Language 108 Immediate and Deferred Evaluation Syntax 110 Value and Method Expressions 112 Defining a Tag Attribute Type 119 Deactivating Expression Evaluation 120 Literal Expressions 121 Resolving Expressions 122 Implicit Objects 125 Operators 126 Reserved Words 127 Examples 128 Functions 129 JavaBeans Components 131 JavaBeans Component Design Conventions 131 Creating and Using a JavaBeans Component 133 Setting JavaBeans Component Properties 134 Retrieviponent Properties 136 Using Custom Tags 137 Declaring Tag Libraries 137 CONTENTS vii Including the Tag Library Implementation 139 Reusing Content in JSP Pages 140 Transferring Control to Another Web Component 141 jsp:param Element 141 Including an Applet 142 Setting Properties for Groups of JSP Pages 144 Further Information 147 Chapter 5: JavaServer Pages Documents . . . . . . . . . . . . . . . 149 The Example JSP Document 150 Creating a JSP Document 152 Declaring Tag Libraries 154 Including Directives in a JSP Document 156 Creating Static and Dynamic Content 158 Using the jsp:root Element 161 Using the jsp:output Element 162 Identifying the JSP Document to the Container 166 Chapter 6: JavaServer Pages Standard Tag Library . . . . . . . 167 The Example JSP Pages 168 Using JSTL 169 Tag Collaboration 170 Core Tag Library 172 Variable Support Tags 172 Flow Control Tags 174 URL Tags 177 Miscellaneous Tags 178 XML Tag Library 180 Core Tags 181 Flow Control Tags 182 Transformation Tags 183 Internationalization Tag Library 184 Setting the Locale 185 Messaging Tags 185 Formatting Tags 186 SQL Tag Library 187 query Tag Result Interface 189 Functions 191 Further Information 192 viii CONTENTS Chapter 7: Custom Tags in JSP Pages . . . . . . . . . . . . . . . . . . .195 What Is a Custom Tag? 196 The Example JSP Pages 196 Types of Tags 198 Tags with Attributes 199 Bodies 202 Tags That Define Variables 203 Communication between Tags 203 Encapsulating Reusable Content Using Tag Files 204 Tag File Location 205le Directives 206 Evaluating Fragments Passed to Tag Files 215 Examples 215 Tag Library Descriptors 220 Top-Level Tag Library Descriptor Elements 222 Declaring Tag Files 222Handlers 225 Declaring Tag Attributes for Tag Handlers 227 Declaring Tag Variables lers 229 Programming Simple Tag Handlers 231 Including Tag Handlers in Web Applications 231 How Is a Simple Tag Handler Invoked? 232 Tag Handlers for Basic Tags 232lers for Tags with Attributes 232 Tag Hands with Bodies 236lers for Tags That Define Variables 237 Cooperating Tags 240 Examples 242 Chapter 8: Scripting in JSP Pages . . . . . . . . . . . . . . . . . . . . . .251 The Example JSP Pages 252 Using Scripting 253 Disabling Scripting 253 Declarations 254 Initializing and Finalizing a JSP Page 254 Scriptlets 255 Expressions 256 Programming Tags That Accept Scripting Elements 257 TLD Elements 257 Tag Handlers 257 CONTENTS ix Tags with Bodies 260 Cooperating Tags 261 Tags That Define Variables 263 Chapter 9: JavaServer Faces Technology . . . . . . . . . . . . . . . 265 JavaServer Faces Technology Benefits 266 What is a JavaServer Faces Application? 268 A Simple JavaServer Faces Application 268 Steps in the Development Process 269 Mapping the FacesServlet Instance 270 Creating the Pages 271 Defining Page Navigation 278 Configuring Error Messages 279 Developing the Beans 279 Adding Managed Bean Declarations 280 User Interface Component Model 281 User Interface Component Classes 282 Component Rendering Model 284 Conversion Model 289 Event and Listener Model 290 Validation Model 291 Navigation Model 292 Backing Beans 295 The Life Cycle of a JavaServer Faces Page 300 Further Information 306 Chapter 10: Using JavaServer Faces Technology in JSP Pages . . 307 The Example JavaServer Faces Application 308 Setting Up a Page 310 Using the Core Tags 314 Adding UI Components to a Page Using the HTML Component Tags 316 UI Component Tag Attributes 317 Adding a Form Component 320 Using Text Components 321 Using Command Components for Performing Actions and Navigation 327 Using Data-Bound Table Components 329 x CONTENTS Adding Graphics and Images With the graphicImage Tag 333 Laying Out Components with the UIPanel Component 334 Rendering Components for Selecting One Value 337 Rendering Components for Selecting Multiple Values 339 The UISelectItem, UISelectItems, and UISelectItemGroup Components 340 Displaying Error Messages with the message and messages Tags 344 Using Localized Data 345 Loading a Resource Bundle 346 Referencing Localized Static Data 347 Referencing Error Messages 348 Using the Standard Converters 349 Converting a Component’s Value 351 Using DateTimeConverter 352 Using NumberConverter 354 Registering Listeners on Components 356 Registering a Value-Change Listener on a Component 356 Registering an Action Listener on a Component 357 Using the Standard Validators 359 Validating a Component’s Value 360 Using the LongRangeValidator 361 Binding Component Values and Instances to External Data Sources 361 Binding a Component Value to a Property 363 Binding a Component Value to an Implicit Object 365 Binding a Component Instance to a Bean Property 366 Binding Converters, Listeners, and Validators to Backing Bean Prop- erties 367 Referencing a Backing Bean Method 369 Referencing a Method That Performs Navigation 370ng a Method That Handles an Action Event 371 Referencing a Method That Performs Validation 372 Referencing a Method That Handles a Value-change Event 372 Using Custom Objects 373 Using a Custom Converter 374 Using a Custom Validator 375 Using a Custom Component 376 Chapter 11: Developing with JavaServer Faces Technology .379 Writing Bean Properties 380