Beginning Java EE 6 with GlassFish 3 2nd Edition - Pdf 11

this print for content only—size & color not accurate spine = x.xxx" xxx page count
Goncalves
Java

EE 6 Platform with GlassFish

3
Beginning
Java

EE 6 Platform
with GlassFish

3
From Novice to Professional
SECOND EDITION
  CYAN
  MAGENTA
  YELLOW
  BLACK
  PANTONE 123 C
Antonio Goncalves
Expert member of the JSR-316 (Java

EE 6),
JSR-317 (JPA 2.0), and JSR-318 (EJB

3.1) groups
Companion
eBook Available
Step by step and easy to follow, this book describes

and RESTful web services. And all the code examples are specifically written to
work with GlassFish™ 3, the very latest version of the Reference Implementation
for the Java EE platform. This 2nd edition adds new sections that were not in the 1st
edition and uses the very latest updates of the specifications.
Antonio Goncalves
Expert member of the JSR-316 (Java
TM
EE 6), JSR-317 (JPA 2.0), and
JSR-318 (EJB
TM
3.1) groups
Antonio Goncalves,
Author of
Beginning Silverlight 2
Beginning Silverlight 3
Pure ASP.NET: A Code-
Intensive Premium
Reference
Shelve in:
Java Programming
User level:
Beginning–Intermediate
Pro JPA 2:
Mastering the Java
Persistence API
Spring Recipes,
2nd Edition
Pro
JSF and Ajax
Beginning Java

 TEChNOLOGY
www.it-ebooks.info www.it-ebooks.info
Beginning Java™ EE 6
Platform
with GlassFish™ 3

Second Edition
Copy Editor: Ami Knox
Compositor: Kimberly Burton
Indexer: Potomac Indexing, LLC
Artist: April Milne
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
[email protected], or visit www.springeronline.com.
For information on translations, please e-mail [email protected], or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our
Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales.
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.
www.it-ebooks.infoTo Eloïse, who fills my heart with love. www.it-ebooks.infoi
v

Contents at a Glance
■Foreword xvii

■Chapter 1: Java EE 6 at a Glance 1
Understanding Java EE 1
A Bit of History 2
Standards 4
Architecture 4
Java EE 6 Specifications 10
What’s New in Java EE 6? 12
Lighter 13
Easier to Use 15
Richer 16
More Portable 18
The CD-BookStore Application 18
Setting Up Your Environment 20
JDK 1.6 20
Maven 2 21
JUnit 4 27
Derby 10.6 31
GlassFish v3.0.1 34
www.it-ebooks.info
■ CONTENTS vi
Summary 41
■Chapter 2: Java Persistence 43
JPA Specification Overview 44
A Brief History of the Specification 44
What’s New in JPA 2.0? 45
Reference Implementation 45
Understanding Entities 46

Access Type of an Embeddable Class 89
Relationship Mapping 91
Relationships in Relational Databases 92
Entity Relationships 93
Fetching Relationships 105
Ordering Relationships 107
Inheritance Mapping 110
Inheritance Strategies 110
Type of Classes in the Inheritance Hierarchy 118
Summary 121
■Chapter 4: Managing Persistent Objects 123
How to Query an Entity 123
Entity Manager 127
Obtaining an Entity Manager 128
Persistence Context 130
Manipulating Entities 132
Cache API 142
JPQL 145
Select 146
From 148
Where 148
Order By 150
Group By and Having 150
Bulk Delete 150
www.it-ebooks.info
■ CONTENTS viii
Bulk Update 151


i
x
EJB Lite 189
Reference Implementation 189
Putting It All Together 190
Writing the Book Entity 191
Writing the BookEJB Stateless Session Bean 191
Persistence Unit for the BookEJB 193
Writing the Main Class 194
Compiling and Packaging with Maven 194
Deploying on GlassFish 196
Running the Main Class with the Application Client Container 197
Writing the BookEJBTest Class 197
Summary 199
■Chapter 7: Session Beans and the Timer Service 201
Session Beans 201
Stateless Beans 202
Stateful Beans 205
Singletons 207
Session Bean Model 214
Asynchronous Calls 226
Embeddable Usage 228
The Timer Service 230
Calendar-Based Expression 231
Automatic Timer Creation 233
Programmatic Timer Creation 234
Summary 235
■Chapter 8: Callbacks and Interceptors 237
Session Beans Life Cycle 237

Pages and Components. 279
Renderer. 280 www.it-ebooks.info
■ CONTENTS xi
Converters and Validators 281
Managed Beans and Navigation 281
Ajax Support 282
Web Interface Specifications Overview 282
A Brief History of Web Interfaces 283
JSP 2.2, EL 2.2, and JSTL 1.2 283
JSF 2.0 283
What’s New in JSF 2.0 284
Reference Implementation 284
Putting It All Together 284
Writing the Book Entity 286
Writing the BookEJB 286

XML Actions 319
Functions 321
Facelets 322
JavaServer Faces 323
Life Cycle 324
Standard HTML Components 326
Resource Management 336
Composite Components 337
Implicit Objects 342
Summary 343
■Chapter 12: Processing and Navigation 345
The MVC Pattern 345
FacesServlet 346
FacesContext 349
Faces Config 350
Managed Beans 351
How to Write a Managed Bean 351
Managed Bean Model 352
Navigation 356
Message Handling 361
www.it-ebooks.info
■ CONTENTS xiii
Conversion and Validation 363
Converters 363
Custom Converters 365
Validators 366
Custom Validators 367

MDB Model 401
MDB as a Consumer 405
MDB as a Producer 406
Transaction 407
Handling Exceptions 408
Putting It All Together 409
Writing the OrderDTO 409
Writing the OrderSender 409
Writing the OrderMDB 410
Compiling and Packaging with Maven 411
Creating the Administered Objects 413
Deploying the MDB on GlassFish 413
Running the Example 414
Summary 414
■Chapter 14: SOAP Web Services 415
Understanding SOAP Web Services 415
XML 416
WSDL 417
SOAP 417
UDDI 417
Transport Protocol 418
SOAP Web Services Specification Overview 418
A Brief History of SOAP Web Services 418
Java EE Specifications 418
Reference Implementation 420
How to Invoke a SOAP Web Service 420
Java Architecture for XML Binding 422
Binding 423
www.it-ebooks.info
■ CONTENTS

Reference Implementation 460
The REST Approach 460
www.it-ebooks.info
■ CONTENTS xvi
From the Web to Web Services 460
A Web-Browsing Experience 460
Uniform Interface 461
Addressability 461
Connectedness 462
Statelessness 463
Java API for RESTful Web Services 463
The JAX-RS Model 464
How to Write a REST Service 464
URI Definition 465
Extracting Parameters 467
Consuming and Producing Content Types 468
Entity Provider 471
Methods or the Uniform Interface 473
Contextual Information 474
Exception Handling 475
Life Cycle 476
Putting It All Together 477
Writing the Book Entity 477
Writing the BookResource 478
Configuring Jersey 481
Compiling and Packaging with Maven 481
Deploying on GlassFish 482

sum of its features but rather the community that created it, as well as the very nature of it being a
standard that lets you choose or change your implementation as you wish. Freedom is not simply about
open source but also about open standards.

Alexis Moussine-Pouchkine
GlassFish Team, Oracle
www.it-ebooks.infoxviii
About the Author

■ Antonio Goncalves is a senior software architect living in Paris. Initially
focused on Java development since the late 1990s, his career has taken him to
different countries and companies where he works now as a Java EE
consultant in software architecture. As a former BEA consultant, he has great
expertise in application servers such as WebLogic, JBoss, and, of course,
GlassFish. He is particularly fond of open source and is a member of the
OSSGTP (Open Source Solution Get Together Paris). He is also the cocreator
and coleader of the Paris Java User Group.
Antonio wrote his first book on Java EE 5, in French, back in 2007. Since
then he has joined the JCP and is an Expert Member of various JSRs (Java
EE 6, JPA 2.0, and EJB 3.1). For the last few years, Antonio has given talks at
international conferences mainly about Java EE, including JavaOne, The
Server Side Symposium, Devoxx, Jazoon and many Java User Groups. He has
also written numerous technical papers and articles for IT web sites (DevX)
and IT magazines (Programmez, Linux Magazine). Since 2009, he has been
part of the French Java podcast called Les Cast Codeurs (influenced by the Java Posse). For all his work
for the Java Community, Antonio has been elected Java Champion.
Antonio is a graduate of the Conservatoire National des Arts et Métiers in Paris (with an engineering

www.it-ebooks.info
x
x
Acknowledgments
Writing a book about a new specification such as Java EE 6 is an enormous task that requires the talent of
different people. First of all, I really want to thank Steve Anglin from Apress for giving me the
opportunity to contribute to the Apress Beginning series, which I greatly appreciated as a reader.
Throughout the writing process, I was constantly in contact with Mary Tobin and Tom Welsh who
reviewed the book as well as reassured me when I had doubt about finishing on time. Thanks to Jim
Farley who did an excellent job of giving me good technical advice to improve the book.
I also need to thank Alexis Midon and Sebastien Auvray, who coauthored the RESTful web services
chapter (Chapter 15). Alexis is a passionate software engineer and a REST enthusiast, and Sebastien is a
talented developer and a pragmatic adopter of REST. Thanks, guys, for your precious help.
A special thanks to Alexis Moussine-Pouchkine, who kindly agreed to write the foreword of this book
as well as the section on GlassFish. He was also a big help in contacting the right person to give me a
hand on a particular topic. I’m thinking of Ryan Lubke for JSF 2.0, Paul Sandoz for JAX-RS 1.1, and
François Orsini for Derby.
Thanks to Damien Gouyette for his help on JSF 2.0. Damien has great experience in web
development as a whole and JSF in particular. Thanks to Arnaud Heritier, who wrote the section on
Maven, as well as Nicolas de Loof, who did a last proofread on the topic.
Sebastien Moreno helped me on JUnit as well as reviewing the entire manuscript with David
Dewalle and Pascal Graffion. They had to put up with a tight schedule. Thank you very much for the hard
work.
Thanks to the proofreader Stefano Costa, who tried to add a Shakespearean touch to the book.
The diagrams in this book were made using the Visual Paradigm plug-in for IntelliJ IDEA. I would
like to thank both Visual Paradigm and JetBrains for providing me with a free license for their excellent
products.
I could not have written this book without the help and support of the Java community: people who
gave a bit of their time to help me through e-mails, mailing lists, or forums. Of course, the mailing lists of
the JCP expert groups are the first that come to mind; thanks to the expert members and the spec leads

software platform for enterprise development. Challenged at each new version, badly understood or
misused, overengineered, and competing with open source frameworks, J2EE was seen as a heavyweight
technology. Java EE benefited from these criticisms to improve and is today focused on simplicity.
If you are part of the group of people who still think that “EJBs are bad, EJBs are evil,” read this book,
and you’ll change your mind. EJBs (Enterprise Java Beans) are great, as is the entire Java EE 6 technology
stack. If, on the contrary, you are a Java EE adopter, you will see in this book how the platform has found
equilibrium, through its ease of development in all the stacks, new specifications, lighter EJB component
model, profiles, and pruning. If you are a beginner in Java EE, this is also the right book: it covers the
most important specifications in a very understandable manner and is illustrated with a lot of code and
diagrams to make it easier to follow.
Open standards are collectively one of the main strengths of Java EE. More than ever, an application
written with JPA, EJB, JSF, JMS, SOAP web services, or RESTful web services is portable across
application servers. Open source is another of Java EE’s strengths. As you’ll see in this book, most of the
Java EE 6 Reference Implementations use open source licensing (GlassFish, EclipseLink, Mojarra,
OpenMQ, Metro, and Jersey).
This book explores the innovations of this new version, and examines the various specifications and
how to assemble them to develop applications. Java EE 6 consists of nearly 30 specifications and is an
important milestone for the enterprise layer (EJB 3.1, JPA 2.0), for the web tier (Servlet 3.0, JSF 2.0), and
for interoperability (SOAP web services and RESTful services). This book covers a broad part of the Java
EE 6 specifications and uses the JDK 1.6 and some well-known design patterns, as well as the GlassFish
application server, the Derby database, JUnit, and Maven. It is illustrated abundantly with UML
diagrams, Java code, and screenshots.
How Is This Book Structured?
This book is not meant to be an exhaustive reference on Java EE 6. It concentrates on the most important
specifications and highlights the new features of this release. The structure of the book follows the
architecture layering of an application:
www.it-ebooks.info
■ PREFACE
CONTENTS

xxiii
www.apress.com. You can also download the code straight from the public Subversion at
http://beginningee6.kenai.com.
Contacting the Author
If you have any questions about the content of this book, the code, or any other topic, please contact me
at [email protected]. You can also visit my web site at www.antoniogoncalves.org and follow
me on Twitter at @agoncal. www.it-ebooks.info


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status