3. Contents
Preface ...................................................................................................................................................29
Part I Introduction .........................................................................................................................................33
1 Overview ...............................................................................................................................................35
Java EE 6 Platform Highlights ............................................................................................................ 36
Java EE Application Model ................................................................................................................. 37
Distributed Multitiered Applications ............................................................................................... 37
Security .......................................................................................................................................... 39
Java EE Components ................................................................................................................... 40
Java EE Clients .............................................................................................................................. 40
Web Components ........................................................................................................................ 42
Business Components ................................................................................................................. 43
Enterprise Information System Tier .......................................................................................... 44
Java EE Containers .............................................................................................................................. 45
Container Services ....................................................................................................................... 45
Container Types ........................................................................................................................... 46
Web Services Support ......................................................................................................................... 47
XML ............................................................................................................................................... 47
SOAP Transport Protocol ........................................................................................................... 48
WSDL Standard Format .............................................................................................................. 48
Java EE Application Assembly and Deployment ............................................................................. 48
Packaging Applications ...................................................................................................................... 49
Development Roles ............................................................................................................................. 50
Java EE Product Provider ............................................................................................................ 51
Tool Provider ................................................................................................................................ 51
Application Component Provider ............................................................................................. 51
3
4. Contents
Application Assembler ................................................................................................................ 52
Application Deployer and Administrator ................................................................................. 52
Java EE 6 APIs ...................................................................................................................................... 53
Enterprise JavaBeans Technology .............................................................................................. 56
Java Servlet Technology .............................................................................................................. 57
JavaServer Faces Technology ...................................................................................................... 57
JavaServer Pages Technology ..................................................................................................... 58
JavaServer Pages Standard Tag Library ..................................................................................... 58
Java Persistence API ..................................................................................................................... 58
Java Transaction API ................................................................................................................... 59
Java API for RESTful Web Services ........................................................................................... 59
Managed Beans ............................................................................................................................ 59
Contexts and Dependency Injection for the Java EE Platform (JSR 299) ............................. 59
Dependency Injection for Java (JSR 330) .................................................................................. 60
Bean Validation ............................................................................................................................ 60
Java Message Service API ............................................................................................................ 60
Java EE Connector Architecture ................................................................................................ 60
JavaMail API ................................................................................................................................. 61
Java Authorization Contract for Containers ............................................................................ 61
Java Authentication Service Provider Interface for Containers ............................................. 61
Java EE 6 APIs in the Java Platform, Standard Edition 6.0 .............................................................. 62
Java Database Connectivity API ................................................................................................. 62
Java Naming and Directory Interface API ................................................................................ 62
JavaBeans Activation Framework .............................................................................................. 63
Java API for XML Processing ..................................................................................................... 63
Java Architecture for XML Binding ........................................................................................... 63
SOAP with Attachments API for Java ........................................................................................ 63
Java API for XML Web Services ................................................................................................. 64
Java Authentication and Authorization Service ....................................................................... 64
GlassFish Server Tools ........................................................................................................................ 64
2 Using the Tutorial Examples .............................................................................................................. 67
Required Software ............................................................................................................................... 67
Java Platform, Standard Edition ................................................................................................. 67
Java EE 6 Software Development Kit ......................................................................................... 68
4 The Java EE 6 Tutorial • March 2011
5. Contents
Java EE 6 Tutorial Component ................................................................................................... 68
NetBeans IDE ............................................................................................................................... 69
Apache Ant ................................................................................................................................... 70
Starting and Stopping the GlassFish Server ...................................................................................... 71
Starting the Administration Console ................................................................................................ 72
▼ To Start the Administration Console in NetBeans IDE .......................................................... 72
Starting and Stopping the Java DB Server ......................................................................................... 72
▼ To Start the Database Server Using NetBeans IDE .................................................................. 72
Building the Examples ........................................................................................................................ 73
Tutorial Example Directory Structure .............................................................................................. 73
Getting the Latest Updates to the Tutorial ....................................................................................... 74
▼ To Update the Tutorial Through the Update Center .............................................................. 74
Debugging Java EE Applications ....................................................................................................... 74
Using the Server Log .................................................................................................................... 74
Using a Debugger ......................................................................................................................... 75
Part II The Web Tier ......................................................................................................................................... 77
3 Getting Started with Web Applications ........................................................................................... 79
Web Applications ................................................................................................................................ 79
Web Application Lifecycle ................................................................................................................. 81
Web Modules: The hello1 Example ................................................................................................. 83
Examining the hello1 Web Module ......................................................................................... 84
Packaging a Web Module ............................................................................................................ 87
Deploying a Web Module ........................................................................................................... 88
Running a Deployed Web Module ............................................................................................ 89
Listing Deployed Web Modules ................................................................................................. 89
Updating a Web Module ............................................................................................................. 90
Dynamic Reloading ..................................................................................................................... 90
Undeploying Web Modules ........................................................................................................ 91
Configuring Web Applications: The hello2 Example ................................................................... 92
Mapping URLs to Web Components ........................................................................................ 92
Examining the hello2 Web Module ......................................................................................... 92
Building, Packaging, Deploying, and Running the hello2 Example .................................... 94
Declaring Welcome Files ............................................................................................................ 95
5
6. Contents
Setting Context and Initialization Parameters ......................................................................... 96
Mapping Errors to Error Screens ............................................................................................... 98
Declaring Resource References .................................................................................................. 99
Further Information about Web Applications ............................................................................... 101
4 JavaServer Faces Technology ..........................................................................................................103
What Is a JavaServer Faces Application? ......................................................................................... 104
JavaServer Faces Technology Benefits ............................................................................................ 105
Creating a Simple JavaServer Faces Application ............................................................................ 106
Developing the Backing Bean ................................................................................................... 106
Creating the Web Page .............................................................................................................. 107
Mapping the FacesServlet Instance ...................................................................................... 108
The Lifecycle of the hello Application ................................................................................... 108
▼ To Build, Package, Deploy, and Run the Application in NetBeans IDE .............................. 109
Further Information about JavaServer Faces Technology ............................................................ 110
5 Introduction to Facelets ...................................................................................................................111
What Is Facelets? ................................................................................................................................ 111
Developing a Simple Facelets Application ..................................................................................... 113
Creating a Facelets Application ................................................................................................ 113
Configuring the Application ..................................................................................................... 116
Building, Packaging, Deploying, and Running the guessnumber Facelets Example ......... 117
Templating ......................................................................................................................................... 119
Composite Components .................................................................................................................. 121
Resources ............................................................................................................................................ 124
6 Expression Language ........................................................................................................................125
Overview of the EL ............................................................................................................................ 125
Immediate and Deferred Evaluation Syntax .................................................................................. 126
Immediate Evaluation ............................................................................................................... 127
Deferred Evaluation ................................................................................................................... 127
Value and Method Expressions ....................................................................................................... 128
Value Expressions ...................................................................................................................... 128
Method Expressions .................................................................................................................. 132
6 The Java EE 6 Tutorial • March 2011
7. Contents
Defining a Tag Attribute Type ......................................................................................................... 134
Literal Expressions ............................................................................................................................ 135
Operators ............................................................................................................................................ 136
Reserved Words ................................................................................................................................. 136
Examples of EL Expressions ............................................................................................................. 137
7 Using JavaServer Faces Technology in Web Pages ...................................................................... 139
Setting Up a Page ............................................................................................................................... 139
Adding Components to a Page Using HTML Tags ....................................................................... 140
Common Component Tag Attributes ..................................................................................... 143
Adding HTML Head and Body Tags ....................................................................................... 145
Adding a Form Component ..................................................................................................... 145
Using Text Components ........................................................................................................... 146
Using Command Component Tags for Performing Actions and Navigation ................... 151
Adding Graphics and Images with the h:graphicImage Tag ............................................... 153
Laying Out Components with the h:panelGrid and h:panelGroup Tags ......................... 153
Displaying Components for Selecting One Value ................................................................. 155
Displaying Components for Selecting Multiple Values ........................................................ 157
Using the f:selectItem and f:selectItems Tags .............................................................. 159
Using Data-Bound Table Components .................................................................................. 160
Displaying Error Messages with the h:message and h:messages Tags .............................. 163
Creating Bookmarkable URLs with the h:button and h:link Tags ................................... 164
Using View Parameters to Configure Bookmarkable URLs ................................................. 165
Resource Relocation Using h:output Tags ............................................................................ 166
Using Core Tags ................................................................................................................................ 168
8 Using Converters, Listeners, and Validators ................................................................................. 171
Using the Standard Converters ........................................................................................................ 171
Converting a Component’s Value ............................................................................................ 172
Using DateTimeConverter ....................................................................................................... 173
Using NumberConverter ........................................................................................................... 175
Registering Listeners on Components ............................................................................................ 176
Registering a Value-Change Listener on a Component ........................................................ 177
Registering an Action Listener on a Component ................................................................... 178
Using the Standard Validators ......................................................................................................... 178
7
8. Contents
Validating a Component’s Value ............................................................................................. 179
Using LongRangeValidator ..................................................................................................... 180
Referencing a Backing Bean Method .............................................................................................. 180
Referencing a Method That Performs Navigation ................................................................. 181
Referencing a Method That Handles an Action Event .......................................................... 181
Referencing a Method That Performs Validation .................................................................. 181
Referencing a Method That Handles a Value-Change Event ............................................... 182
9 Developing with JavaServer Faces Technology ........................................................................... 183
Backing Beans .................................................................................................................................... 183
Creating a Backing Bean ........................................................................................................... 184
Using the EL to Reference Backing Beans ............................................................................... 185
Writing Bean Properties ................................................................................................................... 186
Writing Properties Bound to Component Values ................................................................. 187
Writing Properties Bound to Component Instances ............................................................. 192
Writing Properties Bound to Converters, Listeners, or Validators ..................................... 193
Writing Backing Bean Methods ...................................................................................................... 194
Writing a Method to Handle Navigation ................................................................................ 194
Writing a Method to Handle an Action Event ........................................................................ 196
Writing a Method to Perform Validation ............................................................................... 196
Writing a Method to Handle a Value-Change Event ............................................................. 197
Using Bean Validation ...................................................................................................................... 198
Validating Null and Empty Strings .......................................................................................... 201
10 JavaServer Faces Technology Advanced Concepts ...................................................................... 203
Overview of the JavaServer Faces Lifecycle .................................................................................... 204
The Lifecycle of a JavaServer Faces Application ............................................................................ 204
Restore View Phase .................................................................................................................... 207
Apply Request Values Phase ..................................................................................................... 207
Process Validations Phase ......................................................................................................... 208
Update Model Values Phase ..................................................................................................... 208
Invoke Application Phase ......................................................................................................... 209
Render Response Phase ............................................................................................................. 209
Partial Processing and Partial Rendering ....................................................................................... 210
The Lifecycle of a Facelets Application ........................................................................................... 210
8 The Java EE 6 Tutorial • March 2011
9. Contents
User Interface Component Model .................................................................................................. 211
User Interface Component Classes .......................................................................................... 211
Component Rendering Model ................................................................................................. 213
Conversion Model ..................................................................................................................... 214
Event and Listener Model ......................................................................................................... 215
Validation Model ....................................................................................................................... 216
Navigation Model ...................................................................................................................... 217
11 Configuring JavaServer Faces Applications ..................................................................................221
Using Annotations to Configure Managed Beans ......................................................................... 222
Using Managed Bean Scopes .................................................................................................... 222
Application Configuration Resource File ....................................................................................... 223
Ordering of Application Configuration Resource Files ........................................................ 224
Configuring Beans ............................................................................................................................. 226
Using the managed-bean Element ............................................................................................ 227
Initializing Properties Using the managed-property Element ............................................ 229
Initializing Maps and Lists ........................................................................................................ 234
Registering Custom Error Messages ............................................................................................... 234
Using FacesMessage to Create a Message .............................................................................. 235
Referencing Error Messages ..................................................................................................... 235
Registering Custom Localized Static Text ...................................................................................... 237
Using Default Validators .................................................................................................................. 238
Configuring Navigation Rules ......................................................................................................... 238
Implicit Navigation Rules ......................................................................................................... 241
Basic Requirements of a JavaServer Faces Application ................................................................. 241
Configuring an Application With a Web Deployment Descriptor ...................................... 242
Configuring Project Stage ......................................................................................................... 245
Including the Classes, Pages, and Other Resources ............................................................... 246
12 Using Ajax with JavaServer Faces Technology ............................................................................. 247
Overview ............................................................................................................................................. 248
About Ajax .................................................................................................................................. 248
Using Ajax Functionality with JavaServer Faces Technology ...................................................... 248
Using Ajax with Facelets ................................................................................................................... 249
Using the f:ajax Tag ................................................................................................................ 249
9
10. Contents
Sending an Ajax Request ................................................................................................................... 251
Using the event Attribute ......................................................................................................... 251
Using the execute Attribute ..................................................................................................... 252
Using the immediate Attribute ................................................................................................. 252
Using the listener Attribute ................................................................................................... 252
Monitoring Events on the Client ..................................................................................................... 253
Handling Errors ................................................................................................................................. 253
Receiving an Ajax Response ............................................................................................................. 254
Ajax Request Lifecycle ...................................................................................................................... 255
Grouping of Components ................................................................................................................ 256
Loading JavaScript as a Resource ..................................................................................................... 256
Using JavaScript API in a Facelets Application ...................................................................... 257
Using the @ResourceDependency Annotation in a Bean Class ............................................ 258
The ajaxguessnumber Example Application ................................................................................ 258
The ajaxguessnumber Source Files ......................................................................................... 258
Building, Packaging, Deploying, and Running the ajaxguessnumber Example ............... 260
Further Information about Ajax in JavaServer Faces .................................................................... 262
13 Advanced Composite Components ................................................................................................263
Attributes of a Composite Component .......................................................................................... 263
Invoking a Managed Bean ................................................................................................................ 264
Validating Composite Component Values .................................................................................... 265
The compositecomponentlogin Example Application ................................................................ 265
The Composite Component File .............................................................................................. 265
The Using Page ........................................................................................................................... 266
The Managed Bean .................................................................................................................... 266
▼ To Build, Package, Deploy, and Run the compositecomponentlogin Example Application
Using NetBeans IDE .................................................................................................................. 267
14 Creating Custom UI Components ...................................................................................................269
Determining Whether You Need a Custom Component or Renderer ....................................... 270
When to Use a Custom Component ........................................................................................ 270
When to Use a Custom Renderer ............................................................................................. 271
Component, Renderer, and Tag Combinations ..................................................................... 272
Steps for Creating a Custom Component ....................................................................................... 273
10 The Java EE 6 Tutorial • March 2011
11. Contents
Creating Custom Component Classes ............................................................................................ 273
Specifying the Component Family .......................................................................................... 275
Performing Encoding ................................................................................................................ 275
Performing Decoding ................................................................................................................ 276
Enabling Component Properties to Accept Expressions ...................................................... 277
Saving and Restoring State ........................................................................................................ 278
Delegating Rendering to a Renderer ............................................................................................... 279
Creating the Renderer Class ..................................................................................................... 279
Identifying the Renderer Type ................................................................................................. 280
Handling Events for Custom Components .................................................................................... 280
Creating the Component Tag Handler ........................................................................................... 280
Retrieving the Component Type .............................................................................................. 281
Setting Component Property Values ....................................................................................... 281
Providing the Renderer Type ................................................................................................... 282
Releasing Resources ................................................................................................................... 283
Defining the Custom Component Tag in a Tag Library Descriptor ........................................... 283
Creating a Custom Converter .......................................................................................................... 284
Implementing an Event Listener ..................................................................................................... 286
Implementing Value-Change Listeners .................................................................................. 287
Implementing Action Listeners ............................................................................................... 287
Creating a Custom Validator ........................................................................................................... 288
Implementing the Validator Interface ..................................................................................... 289
Creating a Custom Tag .............................................................................................................. 292
Using Custom Objects ...................................................................................................................... 294
Using a Custom Converter ....................................................................................................... 295
Using a Custom Validator ......................................................................................................... 296
Using a Custom Component .................................................................................................... 297
Binding Component Values and Instances to External Data Sources ........................................ 298
Binding a Component Value to a Property ............................................................................. 299
Binding a Component Value to an Implicit Object ............................................................... 300
Binding a Component Instance to a Bean Property .............................................................. 301
Binding Converters, Listeners, and Validators to Backing Bean Properties .............................. 302
15 Java Servlet Technology ...................................................................................................................305
What Is a Servlet? ............................................................................................................................... 306
11
12. Contents
Servlet Lifecycle ................................................................................................................................. 306
Handling Servlet Lifecycle Events ............................................................................................ 306
Handling Servlet Errors ............................................................................................................. 308
Sharing Information ......................................................................................................................... 308
Using Scope Objects .................................................................................................................. 308
Controlling Concurrent Access to Shared Resources ........................................................... 309
Creating and Initializing a Servlet ................................................................................................... 309
Writing Service Methods .................................................................................................................. 310
Getting Information from Requests ........................................................................................ 310
Constructing Responses ............................................................................................................ 311
Filtering Requests and Responses .................................................................................................... 312
Programming Filters .................................................................................................................. 312
Programming Customized Requests and Responses ............................................................ 314
Specifying Filter Mappings ....................................................................................................... 314
Invoking Other Web Resources ....................................................................................................... 316
Including Other Resources in the Response ........................................................................... 317
Transferring Control to Another Web Component .............................................................. 317
Accessing the Web Context .............................................................................................................. 317
Maintaining Client State ................................................................................................................... 318
Accessing a Session .................................................................................................................... 318
Associating Objects with a Session .......................................................................................... 318
Session Management ................................................................................................................. 319
Session Tracking ........................................................................................................................ 319
Finalizing a Servlet ............................................................................................................................. 320
Tracking Service Requests ........................................................................................................ 320
Notifying Methods to Shut Down ............................................................................................ 321
Creating Polite Long-Running Methods ................................................................................. 321
The mood Example Application ........................................................................................................ 322
Components of the mood Example Application ..................................................................... 322
Building, Packaging, Deploying, and Running the mood Example ...................................... 323
Further Information about Java Servlet Technology .................................................................... 324
16 Internationalizing and Localizing Web Applications .................................................................. 325
Java Platform Localization Classes .................................................................................................. 325
Providing Localized Messages and Labels ...................................................................................... 326
12 The Java EE 6 Tutorial • March 2011
13. Contents
Establishing the Locale .............................................................................................................. 326
Setting the Resource Bundle ..................................................................................................... 327
Retrieving Localized Messages ................................................................................................. 328
Date and Number Formatting ......................................................................................................... 329
Character Sets and Encodings .......................................................................................................... 329
Character Sets ............................................................................................................................. 329
Character Encoding ................................................................................................................... 330
Part III Web Services ......................................................................................................................................333
17 Introduction to Web Services ..........................................................................................................335
What Are Web Services? ................................................................................................................... 335
Types of Web Services ....................................................................................................................... 335
“Big” Web Services ..................................................................................................................... 336
RESTful Web Services ............................................................................................................... 336
Deciding Which Type of Web Service to Use ................................................................................ 338
18 Building Web Services with JAX-WS ............................................................................................... 339
Creating a Simple Web Service and Clients with JAX-WS ........................................................... 340
Requirements of a JAX-WS Endpoint ..................................................................................... 341
Coding the Service Endpoint Implementation Class ............................................................ 342
Building, Packaging, and Deploying the Service .................................................................... 342
Testing the Methods of a Web Service Endpoint ................................................................... 343
A Simple JAX-WS Application Client ..................................................................................... 344
A Simple JAX-WS Web Client ................................................................................................. 346
Types Supported by JAX-WS ........................................................................................................... 349
Schema-to-Java Mapping .......................................................................................................... 349
Java-to-Schema Mapping .......................................................................................................... 350
Web Services Interoperability and JAX-WS .................................................................................. 351
Further Information about JAX-WS ............................................................................................... 351
19 Building RESTful Web Services with JAX-RS ................................................................................. 353
What Are RESTful Web Services? ................................................................................................... 353
Creating a RESTful Root Resource Class ........................................................................................ 354
13
14. Contents
Developing RESTful Web Services with JAX-RS ................................................................... 354
Overview of a JAX-RS Application .......................................................................................... 356
The @Path Annotation and URI Path Templates ................................................................... 357
Responding to HTTP Resources .............................................................................................. 359
Using @Consumes and @Produces to Customize Requests and Responses .......................... 362
Extracting Request Parameters ................................................................................................ 364
Example Applications for JAX-RS ................................................................................................... 368
A RESTful Web Service ............................................................................................................. 368
The rsvp Example Application ................................................................................................ 370
Real-World Examples ............................................................................................................... 372
Further Information about JAX-RS ................................................................................................ 373
20 Advanced JAX-RS Features ...............................................................................................................375
Annotations for Field and Bean Properties of Resource Classes ................................................. 375
Extracting Path Parameters ...................................................................................................... 376
Extracting Query Parameters ................................................................................................... 377
Extracting Form Data ................................................................................................................ 377
Extracting the Java Type of a Request or Response ................................................................ 378
Subresources and Runtime Resource Resolution .......................................................................... 378
Subresource Methods ................................................................................................................ 379
Subresource Locators ................................................................................................................ 379
Integrating JAX-RS with EJB Technology and CDI ...................................................................... 380
Conditional HTTP Requests ............................................................................................................ 381
Runtime Content Negotiation ......................................................................................................... 382
Using JAX-RS With JAXB ................................................................................................................ 384
21 Running the Advanced JAX-RS Example Application ................................................................. 385
The customer Example Application ............................................................................................... 385
The customer Application Source Files .................................................................................. 385
The CustomerService Class ..................................................................................................... 385
The XSD Schema for the customer Application .................................................................... 387
The CustomerClient Class ....................................................................................................... 387
Building, Packaging, Deploying, and Running the customer Example .............................. 389
14 The Java EE 6 Tutorial • March 2011
15. Contents
Part IV Enterprise Beans ................................................................................................................................393
22 Enterprise Beans ................................................................................................................................395
What Is an Enterprise Bean? ............................................................................................................ 395
Benefits of Enterprise Beans ..................................................................................................... 396
When to Use Enterprise Beans ................................................................................................. 396
Types of Enterprise Beans ......................................................................................................... 396
What Is a Session Bean? .................................................................................................................... 397
Types of Session Beans .............................................................................................................. 397
When to Use Session Beans ...................................................................................................... 398
What Is a Message-Driven Bean? .................................................................................................... 399
What Makes Message-Driven Beans Different from Session Beans? .................................. 399
When to Use Message-Driven Beans ....................................................................................... 400
Accessing Enterprise Beans .............................................................................................................. 401
Using Enterprise Beans in Clients ............................................................................................ 401
Deciding on Remote or Local Access ....................................................................................... 402
Local Clients ............................................................................................................................... 403
Remote Clients ........................................................................................................................... 405
Web Service Clients ................................................................................................................... 406
Method Parameters and Access ................................................................................................ 407
The Contents of an Enterprise Bean ............................................................................................... 407
Packaging Enterprise Beans in EJB JAR Modules .................................................................. 407
Packaging Enterprise Beans in WAR Modules ...................................................................... 408
Naming Conventions for Enterprise Beans ................................................................................... 409
The Lifecycles of Enterprise Beans .................................................................................................. 410
The Lifecycle of a Stateful Session Bean .................................................................................. 410
The Lifecycle of a Stateless Session Bean ................................................................................. 411
The Lifecycle of a Singleton Session Bean ............................................................................... 411
The Lifecycle of a Message-Driven Bean ................................................................................. 412
Further Information about Enterprise Beans ................................................................................ 413
23 Getting Started with Enterprise Beans .......................................................................................... 415
Creating the Enterprise Bean ........................................................................................................... 415
Coding the Enterprise Bean Class ............................................................................................ 416
Creating the converter Web Client ........................................................................................ 416
15
16. Contents
Building, Packaging, Deploying, and Running the converter Example ............................ 417
Modifying the Java EE Application ................................................................................................. 419
▼ To Modify a Class File ................................................................................................................ 419
24 Running the Enterprise Bean Examples ........................................................................................ 421
The cart Example ............................................................................................................................. 421
The Business Interface ............................................................................................................... 422
Session Bean Class ..................................................................................................................... 422
The @Remove Method ................................................................................................................. 425
Helper Classes ............................................................................................................................. 426
Building, Packaging, Deploying, and Running the cart Example ...................................... 426
A Singleton Session Bean Example: counter ................................................................................. 428
Creating a Singleton Session Bean ........................................................................................... 428
The Architecture of the counter Example .............................................................................. 432
Building, Packaging, Deploying, and Running the counter Example ................................ 434
A Web Service Example: helloservice ......................................................................................... 435
The Web Service Endpoint Implementation Class ................................................................ 436
Stateless Session Bean Implementation Class ........................................................................ 436
Building, Packaging, Deploying, and Testing the helloservice Example ........................ 437
Using the Timer Service .................................................................................................................... 438
Creating Calendar-Based Timer Expressions ........................................................................ 439
Programmatic Timers ............................................................................................................... 441
Automatic Timers ...................................................................................................................... 443
Canceling and Saving Timers ................................................................................................... 444
Getting Timer Information ...................................................................................................... 444
Transactions and Timers .......................................................................................................... 445
The timersession Example ..................................................................................................... 445
Building, Packaging, Deploying, and Running the timersession Example ...................... 447
Handling Exceptions ......................................................................................................................... 449
25 A Message-Driven Bean Example ...................................................................................................451
simplemessage Example Application Overview ........................................................................... 451
The simplemessage Application Client ......................................................................................... 452
The Message-Driven Bean Class ..................................................................................................... 453
The onMessage Method ............................................................................................................. 454
16 The Java EE 6 Tutorial • March 2011
17. Contents
Packaging, Deploying, and Running the simplemessage Example ............................................ 455
Creating the Administered Objects for the simplemessage Example ................................. 455
▼ To Build, Deploy, and Run the simplemessage Application Using NetBeans IDE ........... 456
▼ To Build, Deploy, and Run the simplemessage Application Using Ant ............................. 457
Removing the Administered Objects for the simplemessage Example .............................. 457
26 Using the Embedded Enterprise Bean Container ........................................................................ 459
Overview of the Embedded Enterprise Bean Container ............................................................... 459
Developing Embeddable Enterprise Bean Applications ............................................................... 459
Running Embedded Applications ............................................................................................ 460
Creating the Enterprise Bean Container ................................................................................. 460
Looking Up Session Bean References ...................................................................................... 462
Shutting Down the Enterprise Bean Container ..................................................................... 462
The standalone Example Application ........................................................................................... 462
▼ Running the standalone Example Application ..................................................................... 463
27 Using Asynchronous Method Invocation in Session Beans ....................................................... 465
Asynchronous Method Invocation ................................................................................................. 465
Creating an Asynchronous Business Method ......................................................................... 466
Calling Asynchronous Methods from Enterprise Bean Clients ........................................... 467
The async Example Application ...................................................................................................... 468
Architecture of the async Example Application .................................................................... 468
▼ Configuring the Keystore and Truststore in GlassFish Server ............................................. 469
▼ Running the async Example Application in NetBeans IDE ................................................. 470
▼ Running the async Example Application Using Ant ............................................................ 471
Part V Contexts and Dependency Injection for the Java EE Platform ...................................................473
28 Introduction to Contexts and Dependency Injection for the Java EE Platform .......................475
Overview of CDI ................................................................................................................................ 476
About Beans ....................................................................................................................................... 477
About Managed Beans ...................................................................................................................... 477
Beans as Injectable Objects ............................................................................................................... 478
Using Qualifiers ................................................................................................................................. 479
17
18. Contents
Injecting Beans ................................................................................................................................... 480
Using Scopes ...................................................................................................................................... 480
Giving Beans EL Names .................................................................................................................... 482
Adding Setter and Getter Methods .................................................................................................. 482
Using a Managed Bean in a Facelets Page ....................................................................................... 483
Injecting Objects by Using Producer Methods .............................................................................. 483
Configuring a CDI Application ....................................................................................................... 484
Further Information about CDI ...................................................................................................... 484
29 Running the Basic Contexts and Dependency Injection Examples .......................................... 485
The simplegreeting CDI Example ................................................................................................ 485
The simplegreeting Source Files ........................................................................................... 486
The Facelets Template and Page ............................................................................................... 486
Configuration Files .................................................................................................................... 487
Building, Packaging, Deploying, and Running the simplegreeting CDI Example ......... 488
The guessnumber CDI Example ...................................................................................................... 490
The guessnumber Source Files ................................................................................................. 490
The Facelets Page ....................................................................................................................... 494
Building, Packaging, Deploying, and Running the guessnumber CDI Example ............... 496
30 Contexts and Dependency Injection for the Java EE Platform: Advanced Topics ...................499
Using Alternatives ............................................................................................................................. 499
Using Specialization .................................................................................................................. 500
Using Producer Methods and Fields ............................................................................................... 501
Using Events ....................................................................................................................................... 503
Defining Events .......................................................................................................................... 503
Using Observer Methods to Handle Events ............................................................................ 504
Firing Events ............................................................................................................................... 505
Using Interceptors ............................................................................................................................. 506
Using Decorators ............................................................................................................................... 508
Using Stereotypes .............................................................................................................................. 509
31 Running the Advanced Contexts and Dependency Injection Examples ................................. 511
The encoder Example: Using Alternatives .................................................................................... 512
18 The Java EE 6 Tutorial • March 2011
19. Contents
The Coder Interface and Implementations ............................................................................. 512
The encoder Facelets Page and Managed Bean ...................................................................... 513
Building, Packaging, Deploying, and Running the encoder Example ................................ 514
The producermethods Example: Using a Producer Method To Choose a Bean
Implementation ................................................................................................................................. 517
Components of the producermethods Example .................................................................... 517
Building, Packaging, Deploying, and Running the producermethods Example ............... 518
The producerfields Example: Using Producer Fields to Generate Resources ........................ 519
The Producer Field for the producerfields Example .......................................................... 520
The producerfields Entity and Session Bean ...................................................................... 521
The producerfields Facelets Pages and Managed Bean ..................................................... 522
Building, Packaging, Deploying, and Running the producerfields Example ................. 524
The billpayment Example: Using Events and Interceptors ........................................................ 525
The PaymentEvent Event Class ................................................................................................ 526
The PaymentHandler Event Listener ....................................................................................... 526
The billpayment Facelets Pages and Managed Bean ............................................................ 527
The LoggedInterceptor Interceptor Class ............................................................................ 529
Building, Packaging, Deploying, and Running the billpayment Example ........................ 530
The decorators Example: Decorating a Bean ............................................................................... 532
Components of the decorators Example .............................................................................. 532
Building, Packaging, Deploying, and Running the decorators Example .......................... 533
Part VI Persistence ..........................................................................................................................................535
32 Introduction to the Java Persistence API ...................................................................................... 537
Entities ................................................................................................................................................ 537
Requirements for Entity Classes .............................................................................................. 538
Persistent Fields and Properties in Entity Classes .................................................................. 538
Primary Keys in Entities ............................................................................................................ 543
Multiplicity in Entity Relationships ......................................................................................... 545
Direction in Entity Relationships ............................................................................................. 545
Embeddable Classes in Entities ................................................................................................ 548
Entity Inheritance .............................................................................................................................. 549
Abstract Entities ......................................................................................................................... 549
Mapped Superclasses ................................................................................................................. 549
19
20. Contents
Non-Entity Superclasses ........................................................................................................... 550
Entity Inheritance Mapping Strategies .................................................................................... 550
Managing Entities .............................................................................................................................. 553
The EntityManager Interface .................................................................................................. 553
Persistence Units ........................................................................................................................ 557
Querying Entities ............................................................................................................................... 558
Further Information about Persistence .......................................................................................... 559
33 Running the Persistence Examples ................................................................................................561
The order Application ...................................................................................................................... 561
Entity Relationships in the order Application ....................................................................... 562
Primary Keys in the order Application ................................................................................... 564
Entity Mapped to More Than One Database Table ............................................................... 567
Cascade Operations in the order Application ....................................................................... 567
BLOB and CLOB Database Types in the order Application ................................................ 568
Temporal Types in the order Application .............................................................................. 569
Managing the order Application’s Entities ............................................................................. 569
Building, Packaging, Deploying, and Running the order Application ............................... 571
The roster Application .................................................................................................................... 572
Relationships in the roster Application ................................................................................. 573
Entity Inheritance in the roster Application ......................................................................... 574
Criteria Queries in the roster Application ............................................................................ 575
Automatic Table Generation in the roster Application ...................................................... 577
Building, Packaging, Deploying, and Running the roster Application ............................. 577
The address-book Application ....................................................................................................... 580
Bean Validation Constraints in address-book ...................................................................... 580
Specifying Error Messages for Constraints in address-book .............................................. 581
Validating Contact Input from a JavaServer Faces Application .......................................... 581
Building, Packaging, Deploying, and Running the address-book Application ................ 582
34 The Java Persistence Query Language .......................................................................................... 585
Query Language Terminology ......................................................................................................... 586
Creating Queries Using the Java Persistence Query Language .................................................... 586
Named Parameters in Queries .................................................................................................. 587
Positional Parameters in Queries ............................................................................................. 587
20 The Java EE 6 Tutorial • March 2011
21. Contents
Simplified Query Language Syntax ................................................................................................. 588
Select Statements ........................................................................................................................ 588
Update and Delete Statements ................................................................................................. 588
Example Queries ................................................................................................................................ 589
Simple Queries ........................................................................................................................... 589
Queries That Navigate to Related Entities .............................................................................. 590
Queries with Other Conditional Expressions ........................................................................ 591
Bulk Updates and Deletes ......................................................................................................... 593
Full Query Language Syntax ............................................................................................................ 593
BNF Symbols .............................................................................................................................. 593
BNF Grammar of the Java Persistence Query Language ....................................................... 594
FROM Clause ................................................................................................................................. 598
Path Expressions ........................................................................................................................ 601
WHERE Clause ............................................................................................................................... 603
SELECT Clause ............................................................................................................................. 613
ORDER BY Clause ......................................................................................................................... 615
GROUP BY and HAVING Clauses .................................................................................................. 615
35 Using the Criteria API to Create Queries ........................................................................................ 617
Overview of the Criteria and Metamodel APIs .............................................................................. 617
Using the Metamodel API to Model Entity Classes ....................................................................... 619
Using Metamodel Classes ......................................................................................................... 620
Using the Criteria API and Metamodel API to Create Basic Typesafe Queries ......................... 620
Creating a Criteria Query .......................................................................................................... 620
Query Roots ................................................................................................................................ 621
Querying Relationships Using Joins ........................................................................................ 622
Path Navigation in Criteria Queries ........................................................................................ 623
Restricting Criteria Query Results ........................................................................................... 623
Managing Criteria Query Results ............................................................................................ 626
Executing Queries ...................................................................................................................... 627
36 Creating and Using String-Based Criteria Queries ...................................................................... 629
Overview of String-Based Criteria API Queries ............................................................................ 629
Creating String-Based Queries ................................................................................................. 629
Executing String-Based Queries .............................................................................................. 630
21
22. Contents
37 Controlling Concurrent Access to Entity Data with Locking ...................................................... 631
Overview of Entity Locking and Concurrency .............................................................................. 631
Using Optimistic Locking ......................................................................................................... 632
Lock Modes ........................................................................................................................................ 633
Setting the Lock Mode ............................................................................................................... 634
Using Pessimistic Locking ........................................................................................................ 634
38 Improving the Performance of Java Persistence API Applications By Setting a Second-Level
Cache ................................................................................................................................................... 637
Overview of the Second-Level Cache .............................................................................................. 637
Controlling Whether Entities May Be Cached ....................................................................... 638
Specifying the Cache Mode Settings to Improve Performance .................................................... 639
Setting the Cache Retrieval and Store Modes ......................................................................... 639
Controlling the Second-Level Cache Programmatically ....................................................... 641
Part VII Security ...............................................................................................................................................643
39 Introduction to Security in the Java EE Platform ......................................................................... 645
Overview of Java EE Security ........................................................................................................... 646
A Simple Security Example ....................................................................................................... 646
Features of a Security Mechanism ............................................................................................ 649
Characteristics of Application Security ................................................................................... 650
Security Mechanisms ........................................................................................................................ 651
Java SE Security Mechanisms ................................................................................................... 651
Java EE Security Mechanisms ................................................................................................... 652
Securing Containers .......................................................................................................................... 654
Using Annotations to Specify Security Information ............................................................. 654
Using Deployment Descriptors for Declarative Security ...................................................... 655
Using Programmatic Security .................................................................................................. 655
Securing the GlassFish Server .......................................................................................................... 656
Working with Realms, Users, Groups, and Roles .......................................................................... 656
What Are Realms, Users, Groups, and Roles? ........................................................................ 657
Managing Users and Groups on the GlassFish Server ........................................................... 660
Setting Up Security Roles .......................................................................................................... 662
Mapping Roles to Users and Groups ....................................................................................... 663
22 The Java EE 6 Tutorial • March 2011