La lecture en ligne est gratuite
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
Télécharger Lire

The Java™ EE 5 Tutorial

De
1224 pages
The Java™ EE 5
Tutorial
For Sun Java System Application Server Platform Edition 9
Jennifer Ball
Debbie Bode Carson
Ian Evans
Kim Haase
Eric Jendrock
February 18, 2006 Copyright © 2004 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 ...
Voir plus Voir moins
The Java™ EE 5 Tutorial For Sun Java System Application Server Platform Edition 9 Jennifer Ball Debbie Bode Carson Ian Evans Kim Haase Eric Jendrock February 18, 2006 Copyright © 2004 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 © 2004 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. . . . . . . . . . . . . . . . . . . . . . . . . . xxv Who Should Use This Tutorial xxv Prerequisites xxv About the Examples xxvi Further Information xxix How to Print This Tutorial xxix Typographical Conventions xxx Feedback xxx Chapter 1: Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Distributed Multitiered Applications 2 Java EE Components 3Clients 4 Web Components 6 Business Components 6 Enterprise Information System Tier 7 Java EE 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 13 Packaging Applications 13 Development Roles 15 Java EE Product Provider 15 Tool Provider 15 Application Component Provider 16 iii iv CONTENTS Application Assembler 16 Application Deployer and Administrator 17 Java EE 5 APIs 18 Enterprise JavaBeans Technology 18 Java Servlet Technology 19 JavaServer Pages Technology 19 JavaServer Pages Standard Tag Library 19 JavaServer Faces 20 Java Message Service API 20 Java Transaction API 20 JavaMail API 21 JavaBeans Activation Framework 21 Java API for XML Processing 21 Java API for XML-Based RPC 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 23 JDBC API 24 Java Naming and Directory Interface 24 Java Authentication and Authorization Service 25 Simplified Systems Integration 25 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 Derby Database Server 29 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 Web Modules 43 Testing Deployed Web Modules 46 Listing Deployed Web Modules 46 Updating Web Modules 47 Undeploying Web Modules 49 CONTENTS v Configuring Web Applications 50 Mapping URLs to Web Components 50 Declaring Welcome Files 51 Setting Initialization Parameters 52 Mapping Errors to Error Screens 52 Declaring Resource References 53 Duke’s Bookstore Examples 56 Accessing Databases from Web Applications 57 Populating the Example Database 57 Creating a Data Source in the Application Server 58 Further Information 58 Chapter 3: Java Servlet Technology . . . . . . . . . . . . . . . . . . . . . 59 What Is a Servlet? 59 The Example Servlets 60 Troubleshooting 62 Servlet Life Cycle 63 Handling Servlet Life-Cycle Events 63 Handling Errors 66 Sharing Information 66 Using Scope Objects 66 Controlling Concurrent Access to Shared Resources 68 Accessing Databases 69 Initializing a Servlet 70 Writing Service Methods 71 Getting Information from Requests 72 Constructing Responses 74 Filtering Requests and Responses 77 Programming Filters 77 Programming Customized Requests and Responses 79 Specifying Filter Mappings 82 Invoking Other Web Resources 84 Including Other Resources in the Response 84 Transferring Control to Another Web Component 86 Accessing the Web Context 87 Maintaining Client State 88 Accessing a Session 88 Associating Objects with a Session 88 Session Management 89 Session Tracking 90 vi CONTENTS Finalizing a Servlet 91 Tracking Service Requests 91 Notifying Methods to Shut Down 92 Creating Polite Long-Running Methods 93 Further Information 94 Chapter 4: JavaServer Pages Technology . . . . . . . . . . . . . . . .95 What Is a JSP Page? 95 Example 96 The Example JSP Pages 99 The Life Cycle of a JSP Page 104 Translation and Compilation 104 Execution 105 Creating Static Content 107 Response and Page Encoding 107 Creating Dynamic Content 108 Using Objects within JSP Pages 108 Unified Expression Language 109 Immediate and Deferred Evaluation Syntax 111 Value and Method Expressions 113 Defining a Tag Attribute Type 118 Deactivating Expression Evaluation 119 Literal Expressions 121 Resolving Expressions 122 Implicit Objects 125 Operators 126 Reserved Words 127 Examples 127 Functions 129 JavaBeans Components 130 JavaBeans Component Design Conventions 131 Creating and Using a JavaBeans Component 132 Setting JavaBeans Component Properties 133 Retrieviponent Properties 136 Using Custom Tags 136 Declaring Tag Libraries 137 Including the Tag Library Implementation 139 Reusing Content in JSP Pages 139 Transferring Control to Another Web Component 140 jsp:param Element 141 CONTENTS vii Including an Applet 141 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 193 Chapter 7: Custom Tags in JSP Pages . . . . . . . . . . . . . . . . . . . 195 What Is a Custom Tag? 196 The Example JSP Pages 196 viii CONTENTS Types of Tags 199 Tags with Attributes 200 Bodies 203 Tags That Define Variables 204 Communication between Tags 204 Encapsulating Reusable Content Using Tag Files 205 Tag File Location 206le Directives 207 Evaluating Fragments Passed to Tag Files 216 Examples 216 Tag Library Descriptors 221 Top-Level Tag Library Descriptor Elements 222 Declaring Tag Files 223Handlers 226 Declaring Tag Attributes for Tag Handlers 228 Declaring Tag Variables lers 230 Programming Simple Tag Handlers 232 Including Tag Handlers in Web Applications 232 How Is a Simple Tag Handler Invoked? 233 Tag Handlers for Basic Tags 233lers for Tags with Attributes 233 Tag Hands with Bodies 237lers for Tags That Define Variables 238 Cooperating Tags 241 Examples 243 Chapter 8: Scripting in JSP Pages . . . . . . . . . . . . . . . . . . . . . .253 The Example JSP Pages 254 Using Scripting 254 Disabling Scripting 255 Declarations 256 Initializing and Finalizing a JSP Page 256 Scriptlets 257 Expressions 257 Programming Tags That Accept Scripting Elements 258 TLD Elements 259 Tag Handlers 259 Tags with Bodies 261 Cooperating Tags 263 Tags That Define Variables 265 CONTENTS ix Chapter 9: JavaServer Faces Technology . . . . . . . . . . . . . . . 267 JavaServer Faces Technology Benefits 268 What Is a JavaServer Faces Application? 269 Framework Roles 270 A Simple JavaServer Faces Application 271 Steps in the Development Process 272 Creating the Pages 273 Defining Page Navigation 277 Developing the Beans 278 Adding Managed Bean Declarations 278 User Interface Component Model 279 User Interface Component Classes 280 Component Rendering Model 282 Conversion Model 287 Event and Listener Model 288 Validation Model 289 Navigation Model 290 Backing Beans 293 The Life Cycle of a JavaServer Faces Page 298 Request Processing Life Cycle Scenarios 299 Standard Request Processing Life Cycle 300 Further Information 305 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 313 Using the HTML Component Tags 316 UI Component Tag Attributes 317 The UIForm Component 319 The UIColumn Component 320 The UICommand Component 321 The UIData Component 323 The UIGraphic Component 326 The UIInput and UIOutput Components 327 The UIPanel Component 332 The UISelectBoolean Component 335 The UISelectMany Component 335 x CONTENTS The UIMessage and UIMessages Components 337 The UISelectOne Component 338 The UISelectItem, UISelectItems, and UISelectItemGroup Components 339 Using Localized Data 343 Loading a Resource Bundle 343 Referencing Localized Static Data 344 Referencing Error Messages 345 Using the Standard Converters 347 Converting a Component’s Value 348 Using DateTimeConverter 349 Using NumberConverter 351 Registering Listeners on Components 353 Registering a Value-Change Listener on a Component 354 Registering an Action Listener on a Component 355 Using the Standard Validators 356 Requiring a Value 358 Using the LongRangeValidator 359 Binding Component Values and Instances to External Data Sources 359 Binding a Component Value to a Property 361 Binding a Component Value to an Implicit Object 363 Binding a Component Instance to a Bean Property 364 Binding Converters, Listeners, and Validators to Backing Bean Prop- erties 365 Referencing a Backing Bean Method 367 Referencing a Method That Performs Navigation 368ng a Method That Handles an Action Event 369 Referencing a Method That Performs Validation 370 Referencing a Method That Handles a Value-change Event 370 Using Custom Objects 371 Using a Custom Converter 372 Using a Custom Validator 373 Using a Custom Component 374 Chapter 11: Developing with JavaServer Faces Technology .377 Writing Bean Properties 378 Writing Properties Bound to Component Values 379 Writiertind to Component Instances 387 Writing Properties Bound to Converters, Listeners, or Validators 389