Sei sulla pagina 1di 16

D18378GC20

Edition 2.0

March 2007

D49830

D18378GC20 Edition 2.0 March 2007 D49830 ® OracleAS 10 g R3: Build Java EE Applications I

®

OracleAS 10g R3: Build Java EE Applications I

Volume I Student Guide

Authors

Jeff Gallus

Gary Williams

Technical Contributors and Reviewers

Ken Cooper

Douglas Clarke

Pam Gamer

Kate Heap

Taj Islam

Raghu Kodali

Lynn Munsinger

Frank Nimphius

Glenn Stokol

Volker Zell

Editors

Amitha Narayan

Arijit Ghosh

Graphic Designer

Satish Bettegowda

Publishers

Michael Sebastian Almeida Nita Brozowski Sujatha Nagendra

Copyright © 2007, Oracle. All rights reserved.

Disclaimer

This document contains proprietary information and is protected by copyright and other intellectual property laws. You may copy and print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way. Except where your use constitutes "fair use" under copyright law, you may not use, share, download, upload, copy, print, display, perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization of Oracle.

The information contained in this document is subject to change without notice. If you find any problems in the document, please report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not warranted to be error-free.

Restricted Rights Notice

If this documentation is delivered to the United States Government or anyone using the documentation on behalf of the United States Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTS The U.S. Government’s rights to use, modify, reproduce, release, perform, display, or disclose these training materials are restricted by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract.

Trademark Notice

Oracle, JD Edwards, PeopleSoft, and Siebel are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Contents

Preface

I

Introduction Course Objectives

Course Environment

Course Overview

About the Course Application

Service Request Schema

I-2

I-4

I-3

I-8

I-7

1 Java EE: Overview Objectives 1-2 Java Platform, Enterprise Edition

Java EE Platform

Distributed Multitiered Applications

Java EE Architecture

Benefits of the Java EE Platform

Java EE Components

1-4

1-6

1-10

1-3

1-5

1-8

Java EE 5.0 Components Business-Tier Components Enterprise JavaBeans (EJB)

Java EE 5.0 Components Business-Tier Components Enterprise JavaBeans (EJB)
Java EE 5.0 Components Business-Tier Components Enterprise JavaBeans (EJB)

1-11

1-12

1-13

Types of EJB

Java Persistence API

Web Services

Client-Tier Components

Java EE Web-Tier Components

What Is a Servlet?

What Is a JavaServer Page?

What Is JavaServer Faces?

Web-Tier Components: Summary

Java EE Communication APIs

Java EE Server

Java EE Applications

Packaging Java EE Applications

Packaging Java EE Application Components

1-15

1-17

1-16

1-18

1-19

1-21

1-22

1-23

1-25

1-24

1-26

1-28

1-29

1-30

Oracle Application Server Containers for J2EE (OC4J)

1-31

OC4J Architecture

Java EE Application Deployment to

Oracle Application Server 10 g

Security in Java EE Applications

Using Deployment Descriptors for Declarative Security

JDeveloper and Java EE

Oracle JDeveloper 10 g Environment Oracle JDeveloper 10 g

Visual Design Tools Summary 1-39

1-32

1-33

1-34

1-36

1-37

1-38

1-35

2 Designing Java EE Applications Objectives 2-2

Realizing Benefits of Java EE

Java EE Issues

2-4

2-3

Java EE Design Patterns

2-5

The MVC Design Pattern

2-6

The Model

2-7

The View

2-8

The Controller

MVC in Oracle Application Server 10g Containers for Java EE (OC4J)

Struts: Overview

Struts Components

Struts Page Flow Design

JSF: Overview

Oracle Application Development Framework (ADF)

Oracle ADF Implementation

Oracle WebCenter Suite

Summary 2-20 Practice Overview:

Identifying MVC components

2-9

2-11

2-15

2-12

2-13

2-16

2-17

2-18

2-21

3 Creating the Persistence Layer Objectives 3-2

What Is Persistence?

Persistence: Overview

3-3

3-4

Persistence Layer

3-5

What Are POJOs?

3-6

EJB 3.0 - Java Persistence API

JPA Entities

3-8

3-7

iv

2-10

Entity Annotations

3-9

How Do JPA Entities Work? Java Persistence API at Work

JPA O/R Mapping

Mapping Types

Specifying Object Relational (O/R) Mapping

Mapping Relationships Between Entities

Managing Persistence of JPA Entities

Entity Manager

JPA Entity Manager Life Cycle of an Entity

3-10

3-11

3-12

3-13

3-14

3-15

3-16

3-17

3-19

3-20

Persistence Context and Scope

3-21

Manipulating Records with JPA

3-22

Writing Basic JPA QL SELECT Statements

Creating JPA Entities Summary 3-25 Practice Overview:

3-26

Developing EJBs

3-24

3-23

4 Implementing Business Logic Objectives 4-2

What Is a Session Bean?

Session Beans and JPA Entities

What Session Beans Do

Stateless and Stateful Session Beans Life Cycle of a Stateless Session Bean Life Cycle of a Stateful Session Bean

Stateless Session Bean: Facade

Implementing JPA Queries

Session Bean: Interface

4-12

Stateful Session Bean: Example

EJB Client

Interceptor Methods and Classes

Creating a Session Facade Bean in JDeveloper

Adding Methods to the Bean

Summary 4-17 Practice Overview: Developing Session Facade Beans

4-3

4-5

4-4

4-6

4-7

4-8

4-9

4-10

4-11

4-13

4-14

4-15

4-16

4-18

5 Implementing the Web Tier with Servlets Objectives 5-2 Overview 5-3

v

About Java Servlets

5-4

Principal Features of Servlets

Life Cycle of Servlets

HTTP Servlets

Inside an HTTP Servlet

The doGet() Method

The doPost() Method

The HttpServletRequest Object 5-11

The HttpServletResponse Object 5-12

HttpSession 5-13

Session Objects

Methods for Invoking Servlets

Handling Input: The Form

Handling Input: The Servlet Initialization and Destruction

Error Handling

Debugging a Servlet

JDeveloper Environment

Servlet Mapping

Servlet Mapping in JDeveloper

Invoking a Servlet

Specifying Java EE Web Module Settings Summary 5-26

Practice: Overview

5-5

5-6

5-7

5-8

5-9

5-10

5-14

5-15

5-16

5-17

5-18

5-19

5-20

5-22

5-24

5-27

5-21

5-23

5-25

6 Creating the Web Tier Using JavaServer Pages Objectives 6-2

JavaServer Pages

Comparing Servlets and JSPs

Invoking JSPs

6-3

6-5

Date JSP

Date Servlet

Automated JSP Features

JSP Life Cycle

Basic JSP Elements Declarations 6-11 Expressions 6-13 Scriptlets 6-14

6-6

6-7

6-9

6-10

6-8

6-4

Implicit Objects

6-15

Example 6-17

Directives 6-19 include : Example

page Directive

JSP and JavaBeans

Using JavaBeans with JSP

scope Attribute of <jsp:useBean> Tag

6-20

6-23

6-21

6-24

6-26

Accessing and Setting Bean Property

JSP XML Document

Traditional Syntax Versus XML Syntax

JSP Segments

JDeveloper and JSPs Summary 6-34 Practice Overview:

Developing JSPs

6-32

6-29

6-33

6-35

6-27

6-30

7 Creating the Web Tier Using JavaServer Faces Objectives 7-2

JSF: Overview

7-3

JSF: Benefits

7-4

Key Terms

JSF Architecture

JSF Components

JSF UI Components

JSF Component Architecture

7-5

7-6

7-8

7-9

7-10

Tag Handlers and Component Trees

Tag Libraries

Configuration Files

7-12

7-13

JSF Renderers

7-14

Managed Beans

7-15

7-11

Expression Language

Life Cycle of a JSF Page

JSF Life Cycle: Initial Request

JSF Life Cycle: Postback

Formal Phases: JSP

Using JSF Tag Libraries

JSF Applications

JSF and JDeveloper 10.1.3

JSF Navigation Diagram

Summary 7-27 Practice Overview: Developing JSF Pages

7-16

7-17

7-19

7-22

7-18

7-21

7-23

7-24

7-26

7-28

8

Binding Data in an Application Objectives 8-2

ADF Architecture

ADF Model (ADF Databinding)

Types of Data Controls

Model Layer Components Creating a Data Control

Generated Files in the Data Model Project

Data Control Palette

8-3

8-4

8-5

8-6

8-7

8-9

Creating Bindings

8-10

Types of Bindings

8-11

Generated Files in the View Project

8-12

8-8

Examining the DataBindings.cpx File

8-13

ADF Binding

8-14

Objects and Metadata Files

8-15

 

Opening a Page Definition File

8-16

Editing a Page Definition File

8-17

Editing Bindings

8-18

Expression Language (EL) and Bindings

8-19

Accessing Bindings Programmatically

Accessing Data Controls Programmatically

Understanding the Bindings On a Page

8-20

8-22

8-21

Search Page Bindings Drag-and-Drop Usability ADF Binding: Summary

Search Page Bindings Drag-and-Drop Usability ADF Binding: Summary
Search Page Bindings Drag-and-Drop Usability ADF Binding: Summary

8-23

8-26

8-27

Summary 8-28 Practice Overview: Using Databinding

8-29

9 Enhancing the Web Tier with ADF Faces Components Objectives 9-2

ADF Faces: Overview

What ADF Faces Brings to JSF

Key Terms

Components 9-6 Rendering and Behavior

ADF Faces Components: Examples

ADF Faces Components

9-3

9-4

9-5

9-7

9-9

9-8

Data Components Input Components Layout Components

Data Components Input Components Layout Components
Data Components Input Components Layout Components

9-10

9-11

9-12

viii

Navigational Components

9-14

Output Components Facets 9-16

Custom Configuration Files and Libraries

ADF Faces Client-Side Validation

Look and Feel Customization

ADF Faces Skins

Debugging 9-22 ADF Faces: Consistent Programming Model

Multiclient Development Conditional Rendering Partial Page Rendering

Summary 9-27 Practice Overview:

Using ADF Faces Components

9-15

9-18

9-19

9-20

9-21

9-23

9-24

9-25

9-26

9-28

10 Planning Navigation and Page Flow Objectives 10-2

Traditional Navigation

10-3

What Is JSF Navigation?

10-4

JSF Navigation: Example

10-5

JSF Navigation Rules

faces-config Console 10-8 faces-config.xml 10-9

10-6

JSF Navigation Modeler

10-10

JSF Navigation Diagram

10-11

Navigation Elements

10-12

Global Rules

10-14

Pattern-Based Rules

10-15

JSF: Example

JSF Navigation: Example

Using the JSF Configuration Editor

Managed Beans

10-16

10-18

10-22

10-21

Creating Managed Beans

10-24

Managed Bean: Example

10-25

Setting Managed Bean Scope

Relationships Between Managed Beans

Managed Properties

Managed Properties: Examples

Using the Managed Bean in the JSF Page

Summary 10-32 Practice Overview: Developing JSF Applications

10-26

10-29

10-27

10-31

10-28

ix

10-33

11 Enhancing the Application’s Usability Objectives 11-2 Internationalization 11-3

Resource Bundles

Internationalizing an Application

11-4

11-6

Using Resource Bundles Handling Error Messages

Using Resource Bundles Handling Error Messages

11-7

11-8

Control Hints and Validation

Using Control Hints

Accessing Control Hints

Control Hint Properties

ADF Declarative Validation

Adding Validation

Internationalizing Control Hints

Internationalizing the Message Bundle (Control Hints)

Internationalizing the Message Bundle (Control Hints): Example

Internationalizing the Message Bundle (Control Hints)

Internationalized Error Messages

Summary 11-22 Practice Overview: Internationalizing an Application

11-10

11-11

11-12

11-13

11-15

11-16

11-17

11-18

11-20

11-21

11-23

12 Building a Query Page Objectives 12-2

12-3

The Search Page

Using a Parameterized Named Query

Defining Parameters for Named Queries

The Query Expression

Moving the Named Query Forward

Adding the Named Query to the Session Facade

Re-Creating the Data Control Building a Parameterized Form

Defining Display Properties of Parameters

Elements of an ADF Parameter Form

Using the Expression Language Editor

Editing Bindings

Binding a Button or Link to a Method

Working with the Binding

12-4

12-5

12-6

12-7

12-9

12-10

12-11

12-12

12-13

12-15

12-14

12-16

12-8

11-19

Using the Return to Display the Results

12-17

Display the Results

12-18

Using EL to Customize Search Results

12-19

x

Setting a Refresh Condition

12-20

Summary 12-22 Practice Overview: Creating a Search Form

12-23

13 Creating Updatable Pages Objectives 13-2 Using Components That Facilitate Data Entry

13-3

Using Methods on Pages Executing Methods in ADF

Executing Bound Methods on JSF Page Load Constructors 13-7

Transactional Methods mergeEntity() 13-9

persistEntity() 13-10

Using Transactional Methods

13-4

13-5

13-8

13-11

13-6

Building an Insert Form Displaying the Results Using Backing Beans Backing Bean: Example Creating Backing Beans

Building an Insert Form Displaying the Results Using Backing Beans Backing Bean: Example Creating Backing Beans
Building an Insert Form Displaying the Results Using Backing Beans Backing Bean: Example Creating Backing Beans
Building an Insert Form Displaying the Results Using Backing Beans Backing Bean: Example Creating Backing Beans
Building an Insert Form Displaying the Results Using Backing Beans Backing Bean: Example Creating Backing Beans

13-12

13-15

13-16

13-17

13-18

Creating Managed Beans by Binding a Command Button

ADF Binding

Configuring ADF Binding Access for Managed Beans Summary 13-23

Practice Overview: Completing the Edit Page

13-20

13-21

13-22

13-24

14 Handling Application Events Objectives 14-2

JSF Event Model Types of Events

Action Events

Creating Action Events Value Change Events

14-3

14-4

14-5

14-6

14-7

Creating Value Change Events

14-8

Event Listener Classes

14-9

Handling Action Events

14-10

Handling Value Change Events

14-11

Event and Listener Execution Order

Validation in the JSF Life Cycle

Entity Validation in the Commit Cycle

Handling Validation Exceptions

14-12

14-15

14-13

14-16

xi

Creating Custom Exception Handlers

14-17

Changing Life Cycle Exception Reporting

Customizing Page Exception Handling

JavaServer Faces Validators

ADF Binding Validation

Additional ADF Faces Validators

Creating Backing Bean Validation in JDeveloper

Backing Bean Validator: Code Example

Input Validation

Summary 14-27 Practice Overview: Wiring the Application Together

14-19

14-20

14-21

14-22

14-23

14-24

14-25

14-26

14-28

15 Deploying Java EE Applications Objectives 15-2

Java EE Server

Java EE Application Assembly and Deployment

Planning the Deployment Process

Deployment Containers

Packaging Java EE Application Components

JARs 15-10

WARs 15-11

EJB JARs

The EJB Deployment Process

EARs 15-14

EAR File Structure for a Java EE Application: Example

Working with Deployment Descriptors

JSR-88 Deployment Plans

Creating a JSR-88 Deployment Plan

Working with JSR-88 Deployment Plans

Oracle Application Server Containers for Java EE (OC4J)

OC4J Architecture

OC4J Server Configuration Files

Relation of Configuration Files

Application Logging

Data Sources

15-26

data-sources.xml : Example 15-27

Deploying an Application: Web Tier

Data Source Definition

15-3

15-5

15-6

15-8

15-9

15-12

15-13

15-15

15-16

15-18

15-17

15-19

15-20

15-21

15-24

15-22

15-23

15-25

15-28

Example: Creating a Web Deployment Profile

15-29

Deploying an Application: EJB Tier

15-30

Example: Creating an EJB Deployment Profile

15-31

xii

Deploying an Application: EAR File

15-32

Example: Creating the Application Deployment Profile

Example: Creating the Application Deployment Descriptor

Java EE Application Deployment to Oracle Application Server 10 g

Deploying with JSR-88 Ant Tasks

Oracle Enterprise Manager

Deploying with Application Server Control

Summary 15-39 Practice Overview: Deploying Java EE Applications

15-33

15-34

15-36

15-37

15-38

15-40

15-35

16 Communicating Between Application Components Objectives 16-2

Communications: Overview

Messaging Systems: Overview Synchronous and Asynchronous

16-3

16-4

16-5

Messaging in Action Messaging Systems Two Domains for JMS Java Message Service

Messaging in Action Messaging Systems Two Domains for JMS Java Message Service
Messaging in Action Messaging Systems Two Domains for JMS Java Message Service
Messaging in Action Messaging Systems Two Domains for JMS Java Message Service

16-6

16-7

16-8

16-9

Message-Oriented Middleware and Queuing

Architecture of a JMS Application

Point-To-Point Model

Publish-And-Subscribe Model

JMS Connector Architecture in OC4J 10 g

Life Cycle of a Message-Driven Bean

Using JMS Interfaces

Setting Up the JMS Messaging Service

16-10

16-11

16-12

16-13

16-14

16-15

16-17

16-16

Sending a Message to a Queue

16-18

Receiving Messages

16-20

Asynchronous Message Delivery

16-21

Message-Driven Beans

MDB Architecture

Anatomy of a Message Bean

Using Properties to Send Information

Creating a JMS/MDB Client Summary 16-27 Practice Overview:

Using a Message Bean

16-22

16-23

16-24

16-26

16-28

16-25

xiii

17 Developing the Business Tier with Web Services Objectives 17-2

What Is Service-Oriented Architecture (SOA)?

SOA: Development Life Cycle

What Is a Web Service? Benefits of Web Services Web Services and SOA

Web Services Model Anatomy of a Service

Defining Web Services

Basic Web Services

Two Development Use Cases

JAX-RPC 17-15 Simple Object Access Protocol (SOAP)

Web Services Definition Language (WSDL)

Anatomy of a WSDL Document Other Web Service Features

Using Http Analyzer

Why Web Services and Applications?

UDDI Registry

Options for Located Web Service

17-3

17-4

17-5

17-7

17-9

17-10

17-11

17-12

17-13

17-14

17-16

17-17

17-20

17-21

17-22

17-23

17-24

17-26

UDDI Publishing and Browsing with Oracle Enterprise Manager

17-27

Oracle Application Server 10 g Web Services

17-28

 

Developing a Web Service with a Stateless Java Class

17-29

Defining an Interface

17-30

 

Defining a Stateless Java Class

17-31

Creating the Web Service

17-33

Examining the Web Service Files

17-34

Deploying the Web Service

17-35

 

Web Service Home Page

17-36

Testing the Deployed Web Service with Home Page

17-37

 

Testing the sayHello Operation

17-38

Exposing and Publishing a PL/SQL Package as a Web Service Summary 17-40

17-39

Practice Overview: Creating a Web Service

17-41

18 Creating the Persistence Layer with TopLink Objectives 18-2

What Is TopLink? Benefits of TopLink

Where Does TopLink Fit?

18-4

18-3

18-5

xiv

TopLink Objects Extensions to JPA

TopLink Design Time

TopLink Mapping Editor

Mapping Types TopLink Caching

Transaction Features and Support

Performance and Tuning Options

TopLink JAXB

Creating a TopLink Persistence Layer

18-14

18-6

18-7

18-8

18-9

18-10

18-11

18-12

18-13

18-15

Creating TopLink Objects What Is a Named Query? Building Named Queries

Creating TopLink Objects What Is a Named Query? Building Named Queries
Creating TopLink Objects What Is a Named Query? Building Named Queries

18-16

18-18

18-19

Query Editor

Adding Queries Using the Mapping Editor

Creating a Java Console Test Client

The Test Client

Building Query Objects in Code Expressions 18-25

18-20

18-21

18-22

18-23

18-24

TopLink Support for Query Expressions

18-26

Query Object: Example

18-27

Using Sequences

18-28

TopLink Sessions

18-29

Role of the Session

18-30

 

POJO-Based Session Facade Methods

18-31

Unit of Work

18-32

Unit of Work: Life Cycle Unit of Work: Example

Unit of Work: Life Cycle Unit of Work: Example

18-34

18-35

ADF Model Databinding and TopLink

Data Control Palette

Creating a TopLink Data Control

Bindings 18-39 Summary 18-40 Practice Overview: Creating a TopLink Persistence Layer

18-36

18-37

18-38

19 Troubleshooting Objectives 19-2 Troubleshooting Basics Diagnosing the Problem

19 Troubleshooting Objectives 19-2 Troubleshooting Basics Diagnosing the Problem

19-3

19-4

Requesting Help

Creating Test Cases

19-5

19-6

xv

18-41

Logging and Diagnostics

Java Logging

19-9

Sending Logger Output to a File

ADF Logger

Using ADF Diagnostics

19-7

19-8

Configuring Java Logging

19-11

19-12

19-10

Turning on Diagnostics in JDeveloper

19-13

Turning on Diagnostics in OC4J

19-14

Sample Java Clients

19-15

Sample Client for Web Service

19-16

Tools and Utilities

19-17

Identifying Search Paths with FileMon

19-18

Testing Java Code with JUnit

19-19

Analyzing HTTP Requests

19-20

Debugging with JDeveloper Summary 19-23

19-21

Appendix A: Practice and Solutions