Table Of ContentMukhar_470-3Front.fm Page i Tuesday, October 4, 2005 6:20 AM
Beginning Java EE 5
From Novice to Professional
■ ■ ■
Kevin Mukhar and Chris Zelenak
with James L. Weaver and Jim Crume
Mukhar_470-3Front.fm Page ii Tuesday, October 4, 2005 6:20 AM
Beginning Java EE 5: From Novice to Professional
Copyright © 2006 by Kevin Mukhar and Chris Zelenak, with James L. Weaver and Jim Crume
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN (pbk): 1-59059-470-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Steve Anglin
Technical Reviewer: Dilip Thomas
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Tony Davis, Jason Gilmore,
Jonathan Hassell, Chris Mills, Dominic Shakeshaft, Jim Sumser
Project Manager: Sofia Marchant
Copy Edit Manager: Nicole LeClerc
Copy Editors: Marilyn Smith, Ami Knox, Nicole LeClerc
Assistant Production Director: Kari Brooks-Copony
Production Editor: Laura Cheu
Compositor: Susan Glinert Stevens
Proofreader: Elizabeth Berry
Indexer: Broccoli Information Management
Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or
visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA
94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com in the Source Code section.
Mukhar_470-3Front.fm Page iii Tuesday, October 4, 2005 6:20 AM
Contents at a Glance
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi
■ CHAPTER 1 Java EE Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
■ CHAPTER 2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
■ CHAPTER 3 JavaServer Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
■ CHAPTER 4 Advanced JSP Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
■ CHAPTER 5 JavaServer Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
■ CHAPTER 6 Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
■ CHAPTER 7 Working with Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
■ CHAPTER 8 Advanced Topics in JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
■ CHAPTER 9 EJB Fundamentals and Session Beans . . . . . . . . . . . . . . . . . . . . . . . 405
■ CHAPTER 10 EJB Entity Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
■ CHAPTER 11 EJB Relationships, EJB QL, and JDBC . . . . . . . . . . . . . . . . . . . . . . . . 473
■ CHAPTER 12 Design Patterns and EJB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
■ CHAPTER 13 Message-Driven Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 543
■ CHAPTER 14 Web Services and JAX-WS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
■ APPENDIX A Tomcat: Who Needs Java EE 5? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581
■ APPENDIX B SQL and EJB QL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585
■ APPENDIX C Java EE Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 607
■ INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 615
iii
Mukhar_470-3Front.fm Page v Tuesday, October 4, 2005 6:20 AM
CCoonntteennttss
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi
■ CHAPTER 1 Java EE Essentials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What Is Java EE? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
How Java EE Relates to J2SE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Why Java EE? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Multitier Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Single-Tier Systems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Client/Server (Two-Tier) Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . 5
N-Tier Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Vendor Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Scalability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Features and Concepts in Java EE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Java EE Clients and Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Containers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Java Servlets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
JavaServer Pages (JSPs). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
JavaServer Faces (JSF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
EJBs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
XML Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Web Services. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Transaction Support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Sample Java EE Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Application Client with EJB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
JSP Client with EJB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Applet Client with JSP and Database. . . . . . . . . . . . . . . . . . . . . . . . . 25
Web Services for Application Integration. . . . . . . . . . . . . . . . . . . . . . 25
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
v
Mukhar_470-3Front.fm Page vi Tuesday, October 4, 2005 6:20 AM
vi ■ CONTENTS
■ CHAPTER 2 Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Installing JBoss . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
JBoss Installation Problems and Solutions . . . . . . . . . . . . . . . . . . . . 32
Testing the JBoss Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Starting the Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
JBoss Server Installation Problem and Solution . . . . . . . . . . . . . . . . 34
Compiling and Deploying a JSP Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Creating the Example Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Learning to Say “Hello” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Application Creation Problems and Solutions . . . . . . . . . . . . . . . . . . 41
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
■ CHAPTER 3 JavaServer Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Introduction to JSP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
JSP Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Basic JSP Lifecycle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
JSP Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Directive Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Scripting Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Action Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Comments and Template Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Creating and Deploying a JSP Web Application . . . . . . . . . . . . . . . . . . . . 55
Writing the JSP Web Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Deploying the Web Application in Java EE. . . . . . . . . . . . . . . . . . . . . 59
Deploying the Web Application in Tomcat. . . . . . . . . . . . . . . . . . . . . 64
Handling Translation or Compilation Problems. . . . . . . . . . . . . . . . . 68
Handling JSP Initialization and End of Life. . . . . . . . . . . . . . . . . . . . . 71
JSP Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Using Implicit Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
The request Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
The response Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
The out Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
The session Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
The config Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
The exception Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
The application Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Using Standard Actions and Implicit Objects in JSP Pages. . . . . . . 76
Translation and Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Mukhar_470-3Front.fm Page vii Tuesday, October 4, 2005 6:20 AM
■ CONTENTS vii
Handling Errors and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Dealing with Exceptions through the page Directive . . . . . . . . . . . . 88
Dealing with Exceptions in the Deployment Descriptor . . . . . . . . . . 89
Adding Exception Handling in JSP Pages. . . . . . . . . . . . . . . . . . . . . . 89
Including and Forwarding from JSP Pages . . . . . . . . . . . . . . . . . . . . . . . . 98
include Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
forward Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Adding include and forward Actions to JSP Pages. . . . . . . . . . . . . 100
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
■ CHAPTER 4 Advanced JSP Topics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Expression Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Scriptless JSPs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Syntax of EL Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Errors and Default Values in EL Statements . . . . . . . . . . . . . . . . . . 116
JSP Pages That Use EL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Custom Actions and Tag Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
How Custom Actions Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Simple Tag Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Classic Tag Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
A Multitude of Custom Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
JSP Standard Tag Library (JSTL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Getting a JSTL Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Actions in the JSTL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Using the JSTL in a JSP. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
■ CHAPTER 5 JavaServer Faces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Introduction to JSF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
The Relationship Between JSF and Other
Java EE Technologies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Request Processing Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Installing JSF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Using JSF with JSP Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Creating a Simple JSF Application . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Reviewing the JSF Lifecycle for the Sample Application. . . . . . . . 184
Mukhar_470-3Front.fm Page viii Tuesday, October 4, 2005 6:20 AM
viii ■ CONTENTS
Using Managed Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
Configuring Managed Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Using Value Binding Expressions in JSP Pages. . . . . . . . . . . . . . . . 189
Using Method Binding Expressions in JSP Pages. . . . . . . . . . . . . . 191
Expanding the JSF Sample Application . . . . . . . . . . . . . . . . . . . . . . 192
Controlling Page Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Static and Dynamic Navigation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Navigation Rules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Adding Dynamic Navigation to the Sample JSF Application . . . . . 204
Accessing Context Data in Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Converting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Using Standard Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Using Custom Converters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Validating Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Using Standard Validators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Using Custom Validators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Bypassing Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Using Message Bundles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
■ CHAPTER 6 Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
HTTP and Server Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Request Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
How a Server Responds to Requests . . . . . . . . . . . . . . . . . . . . . . . . 234
The Servlet Model and HttpServlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Basic Servlet Design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
A Servlet That Responds to POST Requests . . . . . . . . . . . . . . . . . . 238
The request Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
The response Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Deployment Descriptors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250
Servlet Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Event Logging in Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Multithreading in Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Problems with Exception Handling. . . . . . . . . . . . . . . . . . . . . . . . . . 266
Error Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Session Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Creating and Using Sessions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Using Cookies in Place of Sessions . . . . . . . . . . . . . . . . . . . . . . . . . 279
Mukhar_470-3Front.fm Page ix Tuesday, October 4, 2005 6:20 AM
■ CONTENTS ix
Filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Implementing the Filter Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Modifying the Deployment Descriptor to Use a Filter. . . . . . . . . . . 282
The MVC Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
Model 1 vs. MVC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291
The Components of MVC. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Servlet Chaining . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Creating an MVC Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
■ CHAPTER 7 Working with Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
Connecting to Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Using Data Sources for Connections . . . . . . . . . . . . . . . . . . . . . . . . 311
Configuring a DataSource and Connection with Java EE. . . . . . . . 311
Configuring a DataSource and Connection with Tomcat . . . . . . . . 321
Closing Connections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Setting the Login Timeout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330
Logging with a DataSource . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Creating and Using Statement Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Executing Single Statements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Performing Batch Updates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Releasing Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Using Statements to Insert Data into a Database. . . . . . . . . . . . . . 336
Using the ResultSet Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Moving Through the ResultSet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Reading Data from Resultsets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Working with Null Values. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Using Updatable Resultsets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Keeping the ResultSet Open: ResultSet Holdability . . . . . . . . . . . . 353
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
■ CHAPTER 8 Advanced Topics in JDBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Prepared Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Reasons for Using Prepared Statements . . . . . . . . . . . . . . . . . . . . . 358
Creating a PreparedStatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Using a Prepared Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Mukhar_470-3Front.fm Page x Tuesday, October 4, 2005 6:20 AM
x ■ CONTENTS
Callable Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Reasons for Using Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . 371
Creating a CallableStatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Calling a Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Ending Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Managing Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Using Transactions with Stored Procedures . . . . . . . . . . . . . . . . . . 385
Using Distributed Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386
Locking and Isolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Setting Isolation Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Using Pessimistic and Optimistic Locking. . . . . . . . . . . . . . . . . . . . 392
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
■ CHAPTER 9 EJB Fundamentals and Session Beans . . . . . . . . . . . . . . . . . . 405
Understanding EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Why Use EJBs?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
The EJB Specification. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
The Three Kinds of EJBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Which Type of EJB Should You Use? . . . . . . . . . . . . . . . . . . . . . . . . 410
The Anatomy of a Session Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Developing Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Using a Stateless Session Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Choosing Between Stateful and Stateless Session Beans. . . . . . . 418
Using a Stateful Session Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
■ CHAPTER 10 EJB Entity Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
How Entity Beans Work with Session Beans . . . . . . . . . . . . . . . . . . . . . . 425
The Anatomy of an Entity Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
The Entity Bean Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Container-Managed Persistence and
the EntityManager Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Primary Keys. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Bean-Managed Persistence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Mukhar_470-3Front.fm Page xi Tuesday, October 4, 2005 6:20 AM
■ CONTENTS xi
Developing CMP Entity Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Building the CMP Entity Bean Application . . . . . . . . . . . . . . . . . . . . 431
Compiling the CMP Entity Bean Application. . . . . . . . . . . . . . . . . . . 438
Deploying the CMP Entity Bean Application. . . . . . . . . . . . . . . . . . . 439
Running the CMP Entity Bean Application . . . . . . . . . . . . . . . . . . . . 439
Reviewing the CMP Entity Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Reviewing the Session Bean. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Developing BMP Entity Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Building the BMP Entity Bean Application . . . . . . . . . . . . . . . . . . . . 442
Deploying the BMP Entity Bean Application. . . . . . . . . . . . . . . . . . . 458
Running the BMP Entity Bean Application . . . . . . . . . . . . . . . . . . . . 459
Reviewing the BMP Entity Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
The EJB Query Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 464
EJB QL Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Building and Deploying the EJB QL Queries Application . . . . . . . . 466
Running the EJB QL Queries Application. . . . . . . . . . . . . . . . . . . . . 469
Reviewing the Session Bean Find Methods. . . . . . . . . . . . . . . . . . . 470
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
■ CHAPTER 11 EJB Relationships, EJB QL, and JDBC . . . . . . . . . . . . . . . . . . . 473
Entity Bean Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
One-to-Many and Many-to-One Relationships . . . . . . . . . . . . . . . . 474
Many-to-Many Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
An EJB QL Query to Acquire a Subset of Data. . . . . . . . . . . . . . . . . 477
Container-Managed Relationships and EJB QL . . . . . . . . . . . . . . . . . . . . 478
Building the Application with CMR . . . . . . . . . . . . . . . . . . . . . . . . . . 480
Compiling the CMR Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491
Deploying the CMR Application. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Loading the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Running the CMR Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Reviewing the CMR Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493
JDBC with EJB Entity Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496
Implementing JDBC with EJB Applications . . . . . . . . . . . . . . . . . . . 497
Using JDBC with the StockList Bean . . . . . . . . . . . . . . . . . . . . . . . . 499
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503