You are previewing Beginning Java™ EE 6 Platform with GlassFish™ 3: From Novice to Professional.
O'Reilly logo
Beginning Java™ EE 6 Platform with GlassFish™ 3: From Novice to Professional

Book Description

Sun's enterprise Java™ platform, Java EE (Enterprise Edition), is getting a facelift! Beginning Java EE 6 Platform with GlassFish 3 is one of the first tutorial books on the forthcoming Java EE 6 Platform.

Step-by-step and easy to follow, this book describes many of the Java EE 6 specifications and reference implementations and shows them in action using practical examples.

This book uses the new version of GlassFish™ 3 to deploy and administer the code examples.

Written by an expert member of the Java EE 6 specification request and review board in the Java Community Process (JCP), this book contains the best information possible, from an expert's perspective in enterprise Java technologies and platform.

What you'll learn

  • Get started with the new Java EE 6 Platform from Sun.

  • Explore and use the new EJB 3.1 and JPA 2.0 APIs from entities to session beans to message-driven beans, and more.

  • Discover the new web-tier development APIs including JSPs, JSTL, and Expression Language, and especially the new JSF 2.0 and Facelets.

  • Uncover the new web services, RESTful services, and more available in Java EE 6.

  • Create dynamic user interfaces for your enterprise and transactional Java applications.

  • And more...

Who is this book for?

This book is suitable for advanced Java programmers as well as Java EE 6 beginners. Architects will also find information about how to layer their Java EE applications.

Table of Contents

  1. Copyright
  2. Foreword
  3. About the Author
  4. About the Technical Reviewer
  5. Acknowledgments
  6. Introduction
    1. How Is This Book Structured?
    2. Downloading and Running the Code
    3. Contacting the Author
  7. 1. Java EE 6 at a Glance
    1. 1.1. Understanding Java EE
      1. 1.1.1. A Bit of History
      2. 1.1.2. Standards
      3. 1.1.3. Architecture
        1. 1.1.3.1. Components
        2. 1.1.3.2. Containers
        3. 1.1.3.3. Services
        4. 1.1.3.4. Network Protocols
        5. 1.1.3.5. Packaging
        6. 1.1.3.6. Java Standard Edition
      4. 1.1.4. Java EE 6 Specifications
    2. 1.2. What's New in Java EE 6?
      1. 1.2.1. Lighter
        1. 1.2.1.1. Pruning
        2. 1.2.1.2. Profiles
      2. 1.2.2. Easier to Use
      3. 1.2.3. Richer
      4. 1.2.4. More Portable
    3. 1.3. The CD-BookStore Application
    4. 1.4. Setting Up Your Environment
      1. 1.4.1. JDK 1.6
      2. 1.4.2. Maven 2
        1. 1.4.2.1. A Bit of History
        2. 1.4.2.2. Project Descriptor
        3. 1.4.2.3. Managing Artifacts
        4. 1.4.2.4. Project Modularity
        5. 1.4.2.5. Plug-ins and Life Cycle
        6. 1.4.2.6. Installation
        7. 1.4.2.7. Usage
      3. 1.4.3. JUnit 4
        1. 1.4.3.1. A Bit of History
        2. 1.4.3.2. How Does It Work?
        3. 1.4.3.3. Test Methods
        4. 1.4.3.4. Assert Methods
        5. 1.4.3.5. Fixtures
        6. 1.4.3.6. Launching JUnit
        7. 1.4.3.7. JUnit Integration
      4. 1.4.4. Derby 10.4
        1. 1.4.4.1. Installation
        2. 1.4.4.2. Usage
      5. 1.4.5. GlassFish v3
        1. 1.4.5.1. A Bit of History
        2. 1.4.5.2. GlassFish v3 Architecture
        3. 1.4.5.3. Update Center
        4. 1.4.5.4. GlassFish Subprojects
        5. 1.4.5.5. Administration
        6. 1.4.5.6. Admin Console
        7. 1.4.5.7. The asadmin CLI
        8. 1.4.5.8. Installing GlassFish
    5. 1.5. Summary
  8. 2. Java Persistence
    1. 2.1. JPA Specification Overview
      1. 2.1.1. A Brief History of the Specification
      2. 2.1.2. What's New in JPA 2.0?
      3. 2.1.3. Reference Implementation
    2. 2.2. Understanding Entities
      1. 2.2.1. Object-Relational Mapping
      2. 2.2.2. Querying Entities
      3. 2.2.3. Callbacks and Listeners
    3. 2.3. Putting It All Together
      1. 2.3.1. Writing the Book Entity
      2. 2.3.2. Writing the Main Class
      3. 2.3.3. Persistence Unit for the Main Class
      4. 2.3.4. Compiling with Maven
      5. 2.3.5. Running the Main Class with Derby
      6. 2.3.6. Writing the BookTest Class
      7. 2.3.7. Persistence Unit for the BookTest Class
      8. 2.3.8. Running the BookTest Class with Embedded Derby
    4. 2.4. Summary
  9. 3. Object-Relational Mapping
    1. 3.1. How to Map an Entity
      1. 3.1.1. Configuration by Exception
      2. 3.1.2. Elementary Mapping
      3. 3.1.3. Tables
        1. 3.1.3.1. @Table
        2. 3.1.3.2. @SecondaryTable
      4. 3.1.4. Primary Keys
        1. 3.1.4.1. @Id and @GeneratedValue
        2. 3.1.4.2. Composite Primary Keys
        3. 3.1.4.3. @EmbeddedId
        4. 3.1.4.4. @IdClass
      5. 3.1.5. Attributes
        1. 3.1.5.1. @Basic
        2. 3.1.5.2. @Column
        3. 3.1.5.3. @Temporal
        4. 3.1.5.4. @Transient
        5. 3.1.5.5. @Enumerated
      6. 3.1.6. Access Type
      7. 3.1.7. Collection of Basic Types
      8. 3.1.8. Map of Basic Types
    2. 3.2. Mapping with XML
    3. 3.3. Embeddables
      1. 3.3.1. Access Type of an Embeddable Class
    4. 3.4. Relationship Mapping
      1. 3.4.1. Relationships in Relational Databases
      2. 3.4.2. Entity Relationships
        1. 3.4.2.1. Unidirectional and Bidirectional
        2. 3.4.2.2. @OneToOne Unidirectional
        3. 3.4.2.3. @OneToMany Unidirectional
        4. 3.4.2.4. @ManyToMany Bidirectional
      3. 3.4.3. Fetching Relationships
      4. 3.4.4. Ordering Relationships
        1. 3.4.4.1. @OrderBy
        2. 3.4.4.2. @OrderColumn
    5. 3.5. Inheritance Mapping
      1. 3.5.1. Inheritance Strategies
        1. 3.5.1.1. Single-Table Strategy
        2. 3.5.1.2. Joined Strategy
        3. 3.5.1.3. Table-per-Class Strategy
        4. 3.5.1.4. Overriding Attributes
      2. 3.5.2. Type of Classes in the Inheritance Hierarchy
        1. 3.5.2.1. Abstract Entity
        2. 3.5.2.2. Nonentity
        3. 3.5.2.3. Mapped Superclass
    6. 3.6. Summary
  10. 4. Managing Persistent Objects
    1. 4.1. How to Query an Entity
    2. 4.2. Entity Manager
      1. 4.2.1. Obtaining an Entity Manager
      2. 4.2.2. Persistence Context
      3. 4.2.3. Manipulating Entities
        1. 4.2.3.1. Persisting an Entity
        2. 4.2.3.2. Finding by ID
        3. 4.2.3.3. Removing an Entity
        4. 4.2.3.4. Orphan Removal
        5. 4.2.3.5. Synchronizing with the Database
        6. 4.2.3.6. Flushing Data
        7. 4.2.3.7. Refreshing an Entity
        8. 4.2.3.8. Content of the Persistence Context
        9. 4.2.3.9. Contains
        10. 4.2.3.10. Clear and Detach
        11. 4.2.3.11. Merging an Entity
        12. 4.2.3.12. Updating an Entity
        13. 4.2.3.13. Cascading Events
      4. 4.2.4. Cache API
    3. 4.3. JPQL
      1. 4.3.1. Select
      2. 4.3.2. From
      3. 4.3.3. Where
        1. 4.3.3.1. Binding Parameters
        2. 4.3.3.2. Subqueries
      4. 4.3.4. Order By
      5. 4.3.5. Group By and Having
      6. 4.3.6. Bulk Delete
      7. 4.3.7. Bulk Update
    4. 4.4. Queries
      1. 4.4.1. Dynamic Queries
      2. 4.4.2. Named Queries
      3. 4.4.3. Native Queries
    5. 4.5. Concurrency
      1. 4.5.1. Versioning
      2. 4.5.2. Optimistic Locking
      3. 4.5.3. Pessimistic Locking
    6. 4.6. Summary
  11. 5. Callbacks and Listeners
    1. 5.1. Entity Life Cycle
    2. 5.2. Callbacks
    3. 5.3. Listeners
    4. 5.4. Summary
  12. 6. Enterprise Java Beans
    1. 6.1. Understanding EJBs
      1. 6.1.1. Types of EJBs
      2. 6.1.2. Anatomy of an EJB
      3. 6.1.3. EJB Container
      4. 6.1.4. Embedded Container
      5. 6.1.5. Dependency Injection and JNDI
      6. 6.1.6. Callback Methods and Interceptors
      7. 6.1.7. Packaging
    2. 6.2. EJB Specification Overview
      1. 6.2.1. History of the Specification
      2. 6.2.2. What's New in EJB 3.1
      3. 6.2.3. EJB Lite
      4. 6.2.4. Reference Implementation
    3. 6.3. Putting It All Together
      1. 6.3.1. Writing the Book Entity
      2. 6.3.2. Writing the BookEJB Stateless Session Bean
      3. 6.3.3. Persistence Unit for the BookEJB
      4. 6.3.4. Writing the Main Class
      5. 6.3.5. Compiling and Packaging with Maven
      6. 6.3.6. Deploying on GlassFish
      7. 6.3.7. Running the Main Class with Derby
      8. 6.3.8. Writing the BookEJBTest Class
    4. 6.4. Summary
  13. 7. Session Beans and the Timer Service
    1. 7.1. Session Beans
      1. 7.1.1. Stateless Beans
      2. 7.1.2. Stateful Beans
      3. 7.1.3. Singletons
        1. 7.1.3.1. Initialization
        2. 7.1.3.2. Chaining Singletons
        3. 7.1.3.3. Concurrency
        4. 7.1.3.4. Container-Managed Concurrency
        5. 7.1.3.5. Concurrency Not Allowed
      4. 7.1.4. Session Bean Model
        1. 7.1.4.1. Interfaces and Bean Class
        2. 7.1.4.2. Remote, Local, and No-Interface Views
        3. 7.1.4.3. Bean Class
        4. 7.1.4.4. Client View
        5. 7.1.4.5. @EJB
        6. 7.1.4.6. Global JNDI Access
        7. 7.1.4.7. Session Context
        8. 7.1.4.8. Deployment Descriptor
        9. 7.1.4.9. Dependency Injection
        10. 7.1.4.10. Environment Naming Context
      5. 7.1.5. Asynchronous Calls
      6. 7.1.6. Embeddable Usage
    2. 7.2. The Timer Service
      1. 7.2.1. Calendar-Based Expression
      2. 7.2.2. Automatic Timer Creation
      3. 7.2.3. Programmatic Timer Creation
    3. 7.3. Summary
  14. 8. Callbacks and Interceptors
    1. 8.1. Session Beans Life Cycle
      1. 8.1.1. Stateless and Singleton
      2. 8.1.2. Stateful
      3. 8.1.3. Callbacks
    2. 8.2. Interceptors
      1. 8.2.1. Around-Invoke Interceptors
      2. 8.2.2. Method Interceptors
      3. 8.2.3. Life-Cycle Interceptor
      4. 8.2.4. Chaining and Excluding Interceptors
    3. 8.3. Summary
  15. 9. Transactions and Security
    1. 9.1. Transactions
      1. 9.1.1. ACID
      2. 9.1.2. Local Transactions
      3. 9.1.3. XA and Distributed Transactions
    2. 9.2. Transaction Support in EJB
      1. 9.2.1. Container-Managed Transactions
        1. 9.2.1.1. Marking a CMT for Rollback
        2. 9.2.1.2. Exception Handling
      2. 9.2.2. Bean-Managed Transactions
    3. 9.3. Security
      1. 9.3.1. Principals and Roles
      2. 9.3.2. Authentication and Authorization
    4. 9.4. Security Support in EJB
      1. 9.4.1. Declarative Security
      2. 9.4.2. Programmatic Security
    5. 9.5. Summary
  16. 10. JavaServer Faces
    1. 10.1. Understanding JSF
      1. 10.1.1. FacesServlet and faces-config.xml
      2. 10.1.2. Pages and Components
      3. 10.1.3. Renderer
      4. 10.1.4. Converters and Validators
      5. 10.1.5. Managed Beans and Navigation
      6. 10.1.6. Ajax Support
    2. 10.2. Web Interface Specifications Overview
      1. 10.2.1. A Brief History of Web Interfaces
      2. 10.2.2. JSP 2.2, EL 2.2, and JSTL 1.2
      3. 10.2.3. JSF 2.0
      4. 10.2.4. What's New in JSF 2.0
      5. 10.2.5. Reference Implementation
    3. 10.3. Putting It All Together
      1. 10.3.1. Writing the Book Entity
      2. 10.3.2. Writing the BookEJB
      3. 10.3.3. Writing the BookController Managed Bean
      4. 10.3.4. Writing the newBook.xhtml Page
      5. 10.3.5. Writing the listBooks.xhtml Page
      6. 10.3.6. Configuration with web.xml
      7. 10.3.7. Compiling and Packaging with Maven
      8. 10.3.8. Deploying on GlassFish
      9. 10.3.9. Running the Example
    4. 10.4. Summary
  17. 11. Pages and Components
    1. 11.1. Web Pages
      1. 11.1.1. HTML
      2. 11.1.2. XHTML
      3. 11.1.3. CSS
      4. 11.1.4. DOM
      5. 11.1.5. JavaScript
    2. 11.2. Java Server Pages
      1. 11.2.1. Directive Elements
      2. 11.2.2. Scripting Elements
      3. 11.2.3. Action Elements
      4. 11.2.4. Putting It All Together
    3. 11.3. Expression Language
    4. 11.4. JSP Standard Tag Library
      1. 11.4.1. Core Actions
      2. 11.4.2. Formatting Actions
      3. 11.4.3. SQL Actions
      4. 11.4.4. XML Actions
      5. 11.4.5. Functions
    5. 11.5. Facelets
    6. 11.6. JavaServer Faces
      1. 11.6.1. Life Cycle
      2. 11.6.2. Standard HTML Components
        1. 11.6.2.1. Commands
        2. 11.6.2.2. Inputs
        3. 11.6.2.3. Outputs
        4. 11.6.2.4. Selections
        5. 11.6.2.5. Graphics
        6. 11.6.2.6. Grid and Tables
        7. 11.6.2.7. Error Messages
        8. 11.6.2.8. Miscellaneous
        9. 11.6.2.9. Templating
      3. 11.6.3. Resource Management
      4. 11.6.4. Composite Components
      5. 11.6.5. Implicit Objects
    7. 11.7. Summary
  18. 12. Processing and Navigation
    1. 12.1. The MVC Pattern
      1. 12.1.1. FacesServlet
      2. 12.1.2. FacesContext
      3. 12.1.3. Faces Config
    2. 12.2. Managed Beans
      1. 12.2.1. How to Write a Managed Bean
      2. 12.2.2. Managed Bean Model
        1. 12.2.2.1. @ManagedBean
        2. 12.2.2.2. Scopes
        3. 12.2.2.3. @ManagedProperty
        4. 12.2.2.4. Life Cycle and Callback Annotations
      3. 12.2.3. Navigation
      4. 12.2.4. Message Handling
    3. 12.3. Conversion and Validation
      1. 12.3.1. Converters
      2. 12.3.2. Custom Converters
      3. 12.3.3. Validators
      4. 12.3.4. Custom Validators
    4. 12.4. Ajax
      1. 12.4.1. General Concepts
      2. 12.4.2. Support in JSF
      3. 12.4.3. Putting It All Together
    5. 12.5. Summary
  19. 13. Sending Messages
    1. 13.1. Understanding Messages
      1. 13.1.1. JMS
      2. 13.1.2. MDB
    2. 13.2. Messaging Specification Overview
      1. 13.2.1. A Brief History of Messaging
      2. 13.2.2. JMS 1.1
      3. 13.2.3. EJB 3.1
      4. 13.2.4. Reference Implementation
    3. 13.3. How to Send and Receive a Message
    4. 13.4. Java Messaging Service
      1. 13.4.1. Point-to-Point
      2. 13.4.2. Publish-Subscribe
      3. 13.4.3. JMS API
        1. 13.4.3.1. Administered Objects
        2. 13.4.3.2. ConnectionFactory
        3. 13.4.3.3. Destination
        4. 13.4.3.4. Injection
        5. 13.4.3.5. Connection
        6. 13.4.3.6. Session
        7. 13.4.3.7. Messages
        8. 13.4.3.8. Header
        9. 13.4.3.9. Properties
        10. 13.4.3.10. Body
        11. 13.4.3.11. MessageProducer
        12. 13.4.3.12. MessageConsumer
        13. 13.4.3.13. Synchronous Delivery
        14. 13.4.3.14. Asynchronous Delivery
      4. 13.4.4. Selectors
      5. 13.4.5. Reliability Mechanisms
        1. 13.4.5.1. Setting Message Time-to-Live
        2. 13.4.5.2. Specifying Message Persistence
        3. 13.4.5.3. Controlling Acknowledgment
        4. 13.4.5.4. Creating Durable Subscribers
        5. 13.4.5.5. Setting Priorities
    5. 13.5. Message-Driven Beans
      1. 13.5.1. How to Write an MDB
      2. 13.5.2. MDB Model
        1. 13.5.2.1. @MessageDriven
        2. 13.5.2.2. @ActivationConfigProperty
        3. 13.5.2.3. Dependencies Injection
        4. 13.5.2.4. MDB Context
        5. 13.5.2.5. Life Cycle and Callback Annotations
      3. 13.5.3. MDB As a Consumer
      4. 13.5.4. MDB As a Producer
      5. 13.5.5. Transaction
      6. 13.5.6. Handling Exceptions
    6. 13.6. Putting It All Together
      1. 13.6.1. Writing the OrderDTO
      2. 13.6.2. Writing the OrderSender
      3. 13.6.3. Writing the OrderMDB
      4. 13.6.4. Compiling and Packaging with Maven
      5. 13.6.5. Creating the Administered Objects
      6. 13.6.6. Deploying the MDB on GlassFish
      7. 13.6.7. Running the Example
    7. 13.7. Summary
  20. 14. SOAP Web Services
    1. 14.1. Understanding Web Services
      1. 14.1.1. UDDI
      2. 14.1.2. WSDL
      3. 14.1.3. SOAP
      4. 14.1.4. Transport Protocol
      5. 14.1.5. XML
    2. 14.2. Web Services Specification Overview
      1. 14.2.1. A Brief History of Web Services
      2. 14.2.2. Java EE Specifications
        1. 14.2.2.1. JAX-WS 2.2
        2. 14.2.2.2. Web Services 1.2
        3. 14.2.2.3. JAXB 2.2
        4. 14.2.2.4. WS-Metadata 2.0
        5. 14.2.2.5. JAXR 1.0
      3. 14.2.3. Reference Implementation
      4. 14.2.4. How to Invoke a Web Service
    3. 14.3. Java Architecture for XML Binding
      1. 14.3.1. Binding
      2. 14.3.2. Annotations
    4. 14.4. The Invisible Part of the Iceberg
      1. 14.4.1. WSDL
      2. 14.4.2. SOAP
    5. 14.5. Java API for XML-Based Web Services
      1. 14.5.1. JAX-WS Model
        1. 14.5.1.1. Web Service Endpoints
        2. 14.5.1.2. Annotations
        3. 14.5.1.3. @WebService
        4. 14.5.1.4. @WebMethod
        5. 14.5.1.5. @WebResult
        6. 14.5.1.6. @WebParam
        7. 14.5.1.7. @OneWay
        8. 14.5.1.8. All Together
        9. 14.5.1.9. Life Cycle and Callback
        10. 14.5.1.10. Web Service Context
      2. 14.5.2. Invoking a Web Service
    6. 14.6. Putting It All Together
      1. 14.6.1. Writing the CreditCard Class
      2. 14.6.2. Writing the CardValidator Web Service
      3. 14.6.3. Compiling and Packaging with Maven
      4. 14.6.4. Deploying on GlassFish
      5. 14.6.5. Writing the Web Service Consumer
      6. 14.6.6. Generating Consumer's Artifacts and Packaging with Maven
      7. 14.6.7. Running the Main Class
    7. 14.7. Summary
  21. 15. RESTful Web Services
    1. 15.1. Understanding RESTful Web Services
      1. 15.1.1. Resources
      2. 15.1.2. URI
      3. 15.1.3. Representations
      4. 15.1.4. WADL
      5. 15.1.5. HTTP
        1. 15.1.5.1. Request and Response
        2. 15.1.5.2. HTTP Methods
        3. 15.1.5.3. GET
        4. 15.1.5.4. POST
        5. 15.1.5.5. PUT
        6. 15.1.5.6. DELETE
        7. 15.1.5.7. Others
        8. 15.1.5.8. Content Negotiation
        9. 15.1.5.9. Content Types
        10. 15.1.5.10. Status Codes
        11. 15.1.5.11. Caching and Conditional Requests
    2. 15.2. RESTful Web Services Specification
      1. 15.2.1. A Brief History of REST
      2. 15.2.2. JAX-RS 1.1
      3. 15.2.3. What's New in JAX-RS 1.1?
      4. 15.2.4. Reference Implementation
    3. 15.3. The REST Approach
      1. 15.3.1. From the Web to Web Services
      2. 15.3.2. A Web-Browsing Experience
      3. 15.3.3. Uniform Interface
      4. 15.3.4. Addressability
      5. 15.3.5. Connectedness
      6. 15.3.6. Statelessness
    4. 15.4. Java API for RESTful Web Services
      1. 15.4.1. The JAX-RS Model
      2. 15.4.2. How to Write a REST Service
      3. 15.4.3. URI Definition
      4. 15.4.4. Extracting Parameters
      5. 15.4.5. Consuming and Producing Content Types
      6. 15.4.6. Entity Provider
      7. 15.4.7. Methods or the Uniform Interface
      8. 15.4.8. Contextual Information
        1. 15.4.8.1. Headers
        2. 15.4.8.2. Building URIs
      9. 15.4.9. Exception Handling
      10. 15.4.10. Life Cycle
    5. 15.5. Putting It All Together
      1. 15.5.1. Writing the Book Entity
      2. 15.5.2. Writing the BookResource
        1. 15.5.2.1. Header
        2. 15.5.2.2. Creating a New Book
        3. 15.5.2.3. Getting a Book by ID
        4. 15.5.2.4. Deleting a Book
      3. 15.5.3. Configuration with web.xml
      4. 15.5.4. Compiling and Packaging with Maven
      5. 15.5.5. Deploying on GlassFish
      6. 15.5.6. Running the Example
    6. 15.6. Summary