Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Java EE 7 Recipes
A Problem-Solution Approach
Josh Juneau
i
Apress-
Contents
About the Author xxxvii
About the Technical Reviewers xxxix
Acknowledgments xli
Introduction xliii
Chapter 1: Introduction to Servlets 1
1 -1. Setting Up a Java Enterprise Environment 2
Problem 2
Solution 2
How It Works 2
1-2. Developing a Servlet 3
Problem 3
Solution 3
How It Works 5
1-3. Packaging, Compiling, and Deploying a Servlet 7
Problem 7
Solution 7
How It Works 8
1-4. Registering Servlets Without WEB-XML 9
Problem 9
Solution 9
How It Works 11
vii
CONTENTS
1-5. Displaying Dynamic Content with a Servlet 12
Problem 12
Solution 12
How It Works 14
1-6. Handling Requests and Responses 15
Problem 15
Solution 15
How It Works 17
1-7. Listening for Servlet Container Events 18
Problem 18
Solution 18
How It Works 20
1-8. Setting Initialization Parameters 21
Problem 21
Solution #1 21
Solution #2 22
How It Works 22
1-9. Filtering Web Requests 23
Problem 23
Solution 23
How It Works 24
1-10. Listening for Attribute Changes 25
Problem 25
Solution 25
How It Works 26
1-11. Applying a Listener to a Session 27
Problem 27
Solution 27
How It Works 28
viii
CONTENTS
1-12. Managing Session Attributes 29
Problem 29
Solution 29
How It Works 31
1-13. Downloading a File 31
Problem 31
Solution 31
How It Works 34
1-14. Dispatching Requests 35
Problem 35
Solution 35
How It Works 39
1-15. Redirecting to a Different Site 40
Problem 40
Solution 40
How It Works 40
1-16. Securely Maintaining State Within the Browser 41
Problem 41
Solution 41
How It Works 43
1-17. Finalizing Serviet Tasks 45
Problem 45
Solution 45
How It Works 45
1-18. Reading and Writing with Nonblocking I/O 46
Problem 46
Solution 46
How It Works 50
ix
CONTENTS
H Chapter 2: JavaServer Pages 53
2-1. Creating a Simple JSP Page 53
Problem 53
Solution 54
How It Works 55
2-2. Embedding Java into a JSP Page 56
Problem 56
Solution 56
How It Works 57
2-3. Separating Business Logic from View Code 58
Problem 58
Solution 58
How It Works 59
2-4. Yielding or Setting Values 60
Problem 60
Solution 60
How It Works 62
2-5. Invoking a Function in a Conditional Expression 63
Problem 63
Solution 63
How It Works 66
2-6. Creating a JSP Document 67
Problem 67
Solution 67
How It Works 68
2-7. Embedding Expressions in EL 69
Problem 69
Solution 69
How It Works 71
x
CONTENTS
2-8. Accessing Parameters in Multiple Pages 74
Problem 74
Solution 75
How It Works 76
2-9. Creating a Custom JSP Tag 77
Problem 77
Solution 77
How It Works 79
2-10. Including Other JSPs into a Page 81
Problem 81
Solution 81
How It Works 82
2-11. Creating an Input Form for a Database Record 83
Problem 83
Solution 83
How It Works 86
2-12. Looping Through Database Records Within a Page 87
Problem 87
Solution 87
How It Works 91
2-13. Handling JSP Errors 91
Problem 91
Solution 91
How It Works 93
2-14. Disabling Scriptlets in Pages 94
Problem 94
Solution 94
How It Works 94
xi
CONTENTS
2-15. Ignoring EL in Pages 95
Problem 95
Solution #1 95
Solution #2 95
Solution #3 95
How It Works 96
11Chapter 3: The Basics of JavaServer Faces 97
3- 1. Writing a Simple JSF Application 97
Problem 97
Solution #1 98
Solution #2 100
How It Works 102
3-2. Writing a Managed Bean 103
Problem 103
Solution 104
How It Works 108
3-3. Building Sophisticated JSF Views with Components 110
Problem 110
Solution 110
How It Works 116
3-4. Displaying Messages in JSF Pages 118
Problem 118
Solution 118
How It Works 121
3-5. Navigation Based Upon Conditions 122
Problem 122
Solution 123
How It Works 128
xii
CONTENTS
3-6. Updating Messages Without Recompiling 129
Problem 129
Solution 129
How It Works 131
3-7. Validating User Input 132
Problem 132
Solution 132
How It Works 138
3-8. Evaluation of Page Expressions Immediately 139
Problem 139
Solution 139
How It Works 141
3-9. Passing Page Parameters to Methods 141
Problem 141
Solution 141
How It Works 145
3-10. Arithmetic and Reserved Words in Expressions 146
Problem 146
Solution 146
How It Works 148
3-11. Creating Bookmarkable URLs 150
Problem 150
Solution 150
How It Works 152
3-12. Displaying Lists of Objects 153
Problem 153
Solution 153
How It Works 155
xiii
CONTENTS
3-13. Invoking Managed Bean Actions on Life-Cycle Phase Events 157
Problem157
Solution157
How It Works157
SiChapter 4: Facelets 159
4- 1. Creating a Page Template 159
Problem 159
Solution 159
How It Works 161
4-2. Applying a Template to Your Views 164
Problem 164
Solution 164
How It Works 171
4-3. Ensuring Resource Availability from All Views 174
Problem 174
Solution 174
How It Works 177
4-4. Creating Reusable Templates That Act As Components 177
Problem 177
Solution 177
How It Works 185
4-5. Handling Variable-Length Data on a Page 187
Problem 187
Solution 188
How It Works 190
4-6. Debugging View Content 195
Problem 195
Solution 195
How It Works -|g7
xiv
CONTENTS
4-7. Writing a Custom Resolver for Locating Facelets Templates and Resources 197
Problem 197
Solution 197
How It Works 198
IfChapter 5: JavaServer Faces Standard Components 199
Component and Tag Primer 199
Common Component Tag Attributes 201
Common JavaScript Component Tags 202
Binding Components to Properties 203
5-1. Creating an Input Form 203
Problem 203
Solution 203
How It Works 207
5-2. Invoking Actions from Within a Page 209
Problem 209
Solution 209
How It Works 212
5-3. Displaying Output 214
Problem 214
Solution 214
How It Works 217
5-4. Adding Form Validation 220
Problem 220
Solution #1 220
Solution #2 221
Solution #3 222
How It Works 224
5-5. Adding Select Lists to Pages 226
Problem 226
Solution 226
How It Works 229
xv
CONTENTS
5-6. Adding Graphics to Your Pages230
Problem230
Solution230
How It Works231
5-7. Adding Check Boxes to a View 231
Problem231
Solution 231
How It Works 234
5-8. Adding Radio Buttons to a View 236
Problem 236
Solution 236
How It Works 237
5-9. Structuring View Layout 238
Problem 238
Solution 238
How It Works 242
5-10. Displaying a Collection of Data 245
Problem 245
Solution 245
How It Works 249
5-11. Utilizing Custom JSF Component Libraries 252
Problem 252
Solution 252
How It Works 252
5-12. Implementing File Uploading 253
Problem 253
Solution 253
How It Works 253
xv i
CONTENTS
^Chapter 6: Advanced JavaServer Faces and Ajax 255
6-1. Validating Input with Ajax 255
Problem 255
Solution 256
How It Works 260
6-2. Submitting Pages Without Page Reloads 262
Problem 262
Solution 262
How It Works 263
6-3. Making Partial-Page Updates 263
Problem 263
Solution 264
How It Works 264
6-4. Applying Ajax Functionality to a Group of Components 265
Problem 265
Solution 265
How It Works 269
6-5. Custom Processing of Ajax Functionality 269
Problem 269
Solution 269
How It Works 271
6-6. Custom Conversion of Input Values 271
Problem 271
Solution 272
How It Works 273
6-7. Maintaining Managed Bean Scopes for a Session 274
Problem 274
Solution 274
How It Works 282
xvii
CONTENTS
6-8. Listening for System-Level Events 283
Problem 283
Solution 283
How It Works 284
6-9. Listening for Component Events 285
Problem 285
Solution 285
How It Works 286
6-10. Invoking a Managed Bean Action on Render 286
Problem 286
Solution 286
How It Works 288
6-11. Asynchronously Updating Components 289
Problem 289
Solution 289
How It Works 291
6-12. Developing JSF Components Containing HTML5 292
Problem 292
Solution 292
How It Works 294
6-13. Listening to JSF Phases 295
Problem 295
Solution 295
How It Works 296
6-14. Adding Autocompletion to Text Fields 297
Problem 297
Solution 297
How It Works 299
xviii
CONTENTS
6-15. Developing Custom Constraint Annotations 300
Problem 300
Solution 300
How It Works 302
6-16. Customizing Data Tables 304
Problem 304
Solution 304
How It Works 306
6-17. Developing a Page Flow 308
Problem 308
Solution 308
How It Works 311
6-18. Constructing a JSF View in Pure HTML5 314
Problem 314
Solution 314
How It Works 315
HChapter 7: JDBC 317
7-1. Obtaining Database Drivers and Adding Them to the CLASSPATH 317
Problem 317
Solution 318
How It Works 318
7-2. Connecting to a Database 319
Problem 319
Solution #1 319
Solution #2 319
How It Works 322
7-3. Handling Database Connection Exceptions 324
Problem 324
Solution 324
How It Works 325
xix
CONTENTS
7-4. Simplifying Connection Management 325
Problem325
Solution325
How It Works 329
7-5. Querying a Database 329
Problem 329
Solution 330
How It Works 330
7-6. Performing CRUD Operations 332
Problem 332
Solution 332
How It Works 334
7-7. Preventing SQL Injection 335
Problem 335
Solution 335
How It Works 339
7-8. Utilizing Java Objects for Database Access 341
Problem 341
Solution 341
How It Works 347
7-9. Displaying Database Results in JSF Views 348
Problem 348
Solution 348
How It Works 351
7-10. Navigating Data with Scrollable ResultSets 351
Problem 351
Solution 351
How It Works 352
xx
CONTENTS
7-11. Calling PL/SQL Stored Procedures 353
Problem 353
Solution 353
How It Works 354
7-12. Querying and Storing Large Objects 355
Problem 355
Solution 355
How It Works 357
7-13. Caching Data for Use When Disconnected 358
Problem 358
Solution 358
How It Works 361
7-14. Joining RowSet Objects When Not Connected to the Data Source 363
Problem 363
Solution 364
How It Works 367
^Chapter 8: Object-Relational Mapping 369
8- 1. Creating an Entity 370
Problem 370
Solution 370
How It Works 372
8-2. Mapping Data Types 374
Problem 374
Solution 374
How It Works 376
8-3. Creating a Persistence Unit 377
Problem 377
Solution 377
How It Works 378
xxi
CONTENTS
8-4. Using Database Sequences to Create Primary Key Values 380
Problem 380
Solution 380
How It Works 382
8-5. Generating Primary Keys with More Than One Attribute 383
Problem 383
Solution #1 383
Solution #2 386
How It Works 389
8-6. Defining a One-to-One Relationship 391
Problem 391
Solution 391
How It Works 393
8-7. Defining One-to-Many and Many-to-One Relationships 394
Problem 394
Solution 394
How It Works 396
8-8. Defining a Many-to-Many Relationship 397
Problem 397
Solution 398
How It Works 399
8-9. Querying with Named Queries... 401
Problem 401
Solution 401
How It Works 402
8-10. Performing Validation on Entity Fields 402
Problem 402
Solution 403
How It Works404
xxii
CONTENTS
8- 11. Generating Database Schema Objects Automatically 404
Problem 404
Solution 405
How It Works 405
USChapter 9: Enterprise JavaBeans 409
9- 1. Obtaining an Entity Manager 409
Problem 409
Solution #1 410
Solution #2 410
How It Works 410
9-2. Developing a Stateless Session Bean 411
Problem 411
Solution #1 411
Solution #2 412
How It Works 415
9-3. Developing a Stateful Session Bean 417
Problem 417
Solution 417
How It Works 421
9-4. Utilizing Session Beans with JSF 422
Problem 422
Solution #1 422
Solution #2 425
How It Works 427
9-5. Persisting an Object 429
Problem 429
Solution 429
How It Works 429
xxiii
CONTENTS
9-6. Updating an Object429
Problem429
Solution429
How It Works430
9-7. Returning a Table Model430
Problem430
Solution #1430
Solution #2432
How It Works433
9-8. Creating a Singleton Bean 434
Problem434
Solution 434
How It Works 436
9-9. Scheduling a Timer Service 437
Problem 437
Solution #1 437
Solution #2 438
How It Works 438
9-10. Performing Optional Transaction Life-Cycle Callbacks 441
Problem 441
Solution 441
How It Works 442
9-11. Ensuring a Stateful Session Bean Is Not Passivated 442
Problem 442
Solution 442
How It Works 443
9-12. Denoting Local and Remote Interfaces 443
Problem 443
Solution 443
How It Works 443
xxiv
CONTENTS
9-13. Processing Messages Asynchronously from Enterprise Beans 444
Problem 444
Solution 445
How It Works 445
^Chapter 10: The Query API and JPQL 447
10-1. Querying All Instances of an Entity 448
Problem 448
Solution #1 448
Solution #2 448
How It Works 448
10-2. Setting Parameters to Filter Query Results 450
Problem 450
Solution 450
How It Works 450
10-3. Returning a Single Object 451
Problem 451
Solution 451
How It Works 452
10-4. Creating Native Queries 452
Problem 452
Solution #1 452
Solution #2 452
How It Works 453
10-5. Querying More Than One Entity 454
Problem 454
Solution #1 454
Solution #2 455
How It Works 456
XXV
CONTENTS
10-6. Calling JPQL Aggregate Functions 458
Problem 458
Solution 458
How It Works 458
10-7. Invoking Database Stored Procedures Natively 459
Problem 459
Solution 459
How It Works 460
10-8. Joining to Retrieve Instances Matching All Cases 460
Problem 460
Solution 460
How It Works 461
10-9. Joining to Retrieve All Rows Regardless of Match 461
Problem 461
Solution 461
How It Works 462
10-10. Applying JPQL Functional Expressions 462
Problem 462
Solution 462
How It Works 463
10-11. Forcing Query Execution Rather Than Cache Use 464
Problem 464
Solution 464
How It Works 465
10-12. Performing Bulk Updates and Deletes 465
Problem 455
Solution 455
How It Works 455
xxvi
CONTENTS
10-13. Retrieving Entity Subclasses 467
Problem 467
Solution 467
How It Works 468
10-14. Joining with ON Conditions 468
Problem 468
Solution 468
How It Works 469
HChapter 11: Oracle's GlassFish 471
11-1. Installing the GlassFish Application Server 471
Problem 471
Solution #1 471
Solution #2 471
How It Works 475
11-2. Logging into the Administrative Console 476
Problem 476
Solution 476
How It Works 478
11 -3. Changing the Administrator User Password 480
Problem 480
Solution #1 481
Solution #2 481
How It Works 481
11-4. Deploying a WAR File 482
Problem 482
Solution #1 482
Solution #2 483
How It Works 483
xxvii
CONTENTS
11 -5. Adding a Database Resource 484
Problem 484
Solution 484
How It Works 487
11-6. Adding Forms-Based Authentication 488
Problem 488
Solution 488
How It Works 493
11-7. Configuring and Using JavaMail 494
Problem 494
Solution 494
How It Works 496
MChapter 12: Contexts and Dependency Injection 497
12-1. Injecting a Bean or Other Object 497
Problem 497
Solution 497
How It Works 498
12-2. Binding a Bean to JSF Views 499
Problem 499
Solution #1 499
Solution #2 501
How It Works 502
12-3. Allocating a Specific Bean for Injection 503
Problem 503
Solution 503
How It Works 505
12-4. Determining Scope of a Bean 506
Problem 506
Solution 506
How It Works 507
xxviii
CONTENTS
12-5. injecting Non-bean Objects 508
Problem 508
Solution #1 508
How It Works 511
12-6. Ignoring Classes 512
Problem 512
Solution #1 512
Solution #2 512
How It Works 512
12-7. Disposing of Producer Fields 513
Problem 513
Solution 513
How It Works 513
12-8. Specifying an Alternative Implementation at Deployment Time 513
Problem 513
Solution 514
How It Works 514
12-9. Injecting Bean Metadata 514
Problem 514
Solution 514
How It Works 515
IChapter 13: Java Message Service 517
13- 1. Creating JMS Resources 517
Problem 517
Solution #1 518
Solution #2 520
How It Works 521
13-2. Creating a Session 522
Problem 522
Solution 523
How It Works 523
xxix
CONTENTS
13-3. Creating and Sending a Message524
Problem524
Solution #1524
Solution #2525
How It Works 525
13-4. Receiving Messages 527
Problem 527
Solution #1 527
Solution #2 528
How It Works 528
13-5. Filtering Messages 529
Problem 529
Solution 529
How It Works 531
13-6. Inspecting Message Queues 531
Problem 531
Solution 531
How It Works 532
13-7. Creating Durable Message Subscribers 532
Problem 532
Solution 532
How It Works 535
13-8. Delaying Message Delivery 536
Problem 536
Solution 536
How it Works 536
£1Chapter 14: Authentication and Security 537
14- 1. Setting Up Application Users and Groups in GlassFish 537
Problem 537
Solution 537
How It Works 540
xxx
CONTENTS
14-2. Performing Basic Web Application Authentication 540
Problem 540
Solution 540
Solution #2 541
How It Works 542
14-3. Developing a Programmatic Login Form 544
Problem 544
Solution 544
How It Works 556
14-4. Managing Page Access Within a JSF Application 557
Problem 557
Solution 557
How It Works 558
14-5. Configuring LDAP Authentication Within GlassFish 559
Problem 559
Solution 559
How It Works 560
14-6. Configuring Custom Security Certificates Within GlassFish 561
Problem 561
Solution 561
How It Works 562
HChapter 15: Java Web Services 563
15-1. Creating a JAX-WS Web Service Endpoint 564
Problem 564
Solution #1 564
Solution #2 566
How It Works 569
xxxi
CONTENTS
15-2. Deploying a JAX-WS Web Service 572
Problem 572
Solution #1 572
Solution #2 572
Solution #3 572
How It Works 573
15-3. Consuming a JAX-WS Web Service via WSDL 573
Problem 573
Solution 573
How It Works 574
15-4. Consuming a JAX-WS Web Service via a Stand-Alone Application Client 574
Problem 574
Solution 574
How It Works 575
15-5. Integrating Web Services into a Java EE Project 577
Problem 577
Solution 577
How It Works 579
15-6. Developing a RESTful Web Service 579
Problem 579
Solution #1 579
Solution #2 580
How It Works 583
15-7. Consuming and Producing with REST 585
Problem 585
Solution585
How It Works586
15-8. Writing a JAX-RS Client 586
Problem586
Solution586
How It Works, 587
xxxii
CONTENTS
15-9. Filtering Requests and Responses 591
Problem 591
Solution 591
How It Works 592
15-10. Processing Long-Running Operations Asynchronously 594
Problem 594
Solution 594
How It Works 595
39Chapter 16: Enterprise Solutions Using Alternative Programming Languages 597
16-1. Developing Servlets with Groovy 598
Problem 598
Solution 598
How It Works 599
16-2. Working with Groovy Servlet Parameters 600
Problem 600
Solution 600
How It Works 601
16-3. Developing Servlets with Jython 602
Problem 602
Solution 602
How It Works 603
^Chapter 17: WebSockets and JSON-P 605
17-1. Creating a WebSocket Endpoint 605
Problem 605
Solution 605
How It Works 606
17-2. Sending Messages to a WebSocket Endpoint 606
Problem 606
Solution 606
How It Works 608
xxxiii
CONTENTS
17-3. Building a JSON Object 610
Problem 610
Solution 610
How It Works 611
17-4. Writing a JSON Object to Disk 612
Problem 612
Solution 612
How It Works 612
17-5. Reading JSON from an Input Source 612
Problem 612
Solution 613
How It Works 613
HChapter 18: JavaFX in the Enterprise 615
18- 1. Developing a Basic JavaFX Application 616
Problem 616
Solution 616
How It Works 619
18-2. Incorporating Databases into a JavaFX Application 621
Problem 621
Solution 621
How It Works 628
18-3. Constructing a Sophisticated Ul Containing EJB Data 629
Problem 629
Solution 629
How It Works 633
18-4. Developing an Enterprise Application Using MVC 635
Problem 635
Solution 635
How It Works 638
xxxiv
CONTENTS
18-5. Incorporating REST Services into JavaFX Applications 640
Problem 640
Solution 640
How It Works 644
if Chapter 19: Concurrency and Batch Applications 647
19-1. Creating Resources for Processing Tasks Asynchronously in an Application Server 647
Problem 647
Solution #1 647
Solution #2 649
How It Works 650
19-2. Configuring and Creating a Reporter Task 651
Problem 651
Solution 651
How It Works 654
19-3. Running More Than One Task Concurrently 655
Problem 655
Solution 655
How It Works 657
19-4. Utilizing Transactions Within a Task 658
Problem 658
Solution 658
How It Works 659
19-5. Running Concurrent Tasks at Scheduled Times 659
Problem 659
Solution 659
How It Works 661
19-6. Creating Thread Instances 662
Problem 662
Solution 662
How It Works 664
XXXV
CONTENTS
19-7. Creating an Item-Oriented Batch Process 664
Problem 664
Solution 664
How It Works 668
BAppendix A: Java EE Development with NetBeans IDE 671
A-1. Configuring Application Servers Within NetBeans 671
Developing Java Enterprise Applications 673
A-2. Creating a NetBeans Java Web Project 673
A-3. Creating JSF Application Files 675
A-4. Developing EJB Entity Classes 678
A-5. JPQL 679
A-6. HTML5 679
Index 683
xxxvi