You are on page 1of 16

Oracle9i: Build J2EE Applications

Volume I Student Guide

D16501GC10 Edition 1.0 March 2003 D37834

Authors
Lynn Munsinger Sunitha Patel Glenn Stokol

Copyright 2003, Oracle. All rights reserved. This documentation contains proprietary information of Oracle Corporation. It is provided under a license agreement containing restrictions on use and disclosure and is also protected by copyright law. Reverse engineering of the software is prohibited. If this documentation is delivered to a U.S. Government Agency of the Department of Defense, then it is delivered with Restricted Rights and the following legend is applicable: Restricted Rights Legend Use, duplication or disclosure by the Government is subject to restrictions for commercial computer software and shall be deemed to be Restricted Rights software under Federal law, as set forth in subparagraph (c)(1)(ii) of DFARS 252.227-7013, Rights in Technical Data and Computer Software (October 1988). This material or any portion of it may not be copied in any form or by any means without the express prior written permission of Oracle Corporation. Any other copying is a violation of copyright law and may result in civil and/or criminal penalties. If this documentation is delivered to a U.S. Government Agency not within the Department of Defense, then it is delivered with Restricted Rights, as defined in FAR 52.227-14, Rights in Data-General, including Alternate III (June 1987). The information in this document is subject to change without notice. If you find any problems in the documentation, please report them in writing to Education Products, Oracle Corporation, 500 Oracle Parkway, Box SB-6, Redwood Shores, CA 94065. Oracle Corporation does not warrant that this document is error-free. Oracle and all references to Oracle Products are trademarks or registered trademarks of Oracle Corporation. All other products or company names are used for identification purposes only, and may be trademarks of their respective owners.

Technical Contributors and Reviewers


Anna Atkinson William Bates Scott Brewton Steve Button Olivier Caudron Sam Chou Rob Clark Ken Cooper Scott Davies Edward Dowgiallo Julie Fowler Rob Giardina Craig Hollister Taj-ul Islam Peter Laseau Wendy Liau Glenn Maslen Peter Moskovits Monica Motley-Mosser Steve Muench Rama Notowidigdo Debabrata Panda Ashesh Parekh Nagavalli Pataballa Ian Purvis Holger Dindler-Rasmussen Prasad Shiva Raghav Srinivasan Janet Stern Ulrich Vogel Prasad Yammanur Lelia Yin

Publisher
Nita K. S. Brozowski

Contents

Preface 1 Introduction Course Objectives 1-2 Course Environment 1-4 Course Overview 1-5 About the Course Applications 1-8 Order Entry Schema 1-9 Human Resources (HR) Schema 1-10 HR Application Flow Diagram 1-11 Summary 1-12

2 J2EE Overview Objectives 2-2 Java 2, Enterprise Edition Platform 2-3 The J2EE Platform 2-4 Benefits of the J2EE Platform 2-5 J2EE Components 2-7 J2EE 1.3 Components 2-9 J2EE Architecture 2-10 Client Tier Components 2-12 J2EE Web Tier Components 2-13 What Is a Servlet? 2-16 What Is a JavaServer Page (JSP)? 2-16 Web Tier Components: Summary 2-17 Business Tier Components 2-18 Enterprise JavaBeans (EJB) 2-19 J2EE Communication APIs 2-20 J2EE Server 2-21 Oracle9iAS Containers for J2EE 2-23 J2EE Applications 2-24 Packaging J2EE Application Components 2-25 JARs 2-26 WARs 2-27 EJB JARs 2-28 EARs 2-29 Deployment Path for a J2EE Application: Example 2-30 OC4J Architecture 2-31 OC4J Server Configuration Files 2-32 Relation of Configuration Files 2-33 Data Sources 2-34 Utilizing Data Sources 2-35 OC4J Deployment 2-36
iii

Oracle Enterprise Manager 2-37 JDeveloper and J2EE 2-38 Oracle9i JDeveloper Environment 2-39 Summary 2-40 Practice 2-1 Overview 2-41 3 Architecting J2EE Applications Objectives 3-2 Realizing J2EE Benefits 3-3 J2EE Issues 3-4 J2EE Design Patterns 3-5 Implementing Design Patterns by Using MVC 3-6 The Model 3-7 The View 3-8 The Controller 3-9 MVC in Oracle9iAS 3-10 Designing J2EE Applications 3-11 Flow Diagram: Example 3-12 Summary 3-13 Practice 3-1 Overview 3-14 4 Creating the Web Tier: Servlets 4-1 Objectives 4-2 Overview 4-3 About Java Servlets 4-4 Principal Features of Servlets 4-5 Life Cycle of Servlets 4-6 HTTP Servlets 4-7 Inside an HTTP Servlet 4-8 Servlet Example 4-9 The doGet() Method 4-10 The doPost() Method 4-11 The HttpServletRequest Object 4-12 The HttpServletResponse Object 4-13 Methods for Invoking Servlets 4-14 Your First Servlet 4-15 Handling Input: The Form 4-16 Handling Input: The Servlet 4-17 Initialization and Destruction 4-18 Error Handling 4-19 Debugging a Servlet 4-20

iv

SingleThreadModel 4-21 JDeveloper Environment 4-22 Servlet Mapping 4-23 Servlet Mapping in Jdeveloper 4-24 Invoking a Servlet 4-25 Specifying J2EE Web Module Settings 4-26 Creating a Connection to Oracle9iAS 4-27 Deploying to OC4J 4-28 Summary 4-29 Practice 4-1, 4-2, and 4-3 Overview 4-30 5 Accessing the Database with Servlets Objectives 5-2 Review of JDBC 5-3 Querying in JDBC 5-4 JDBC and Servlets 5-5 Synchronizing Shared Resources 5-6 Improving Database Performance 5-7 Connection Pooling 5-9 Data Sources 5-10 Data Source Definition 5-11 Example: data-sources.xml 5-12 Using Data Sources 5-13 Summary 5-14 Practice 5-1 Overview 5-15 6 Using Advanced Techniques in Servlets Objectives 6-2 Overview 6-3 HTTP Headers 6-4 Request Headers 6-5 Sending a Response 6-6 Response Headers 6-7 Setting Status Codes 6-8 Example 6-9 Sending Multimedia Content 6-10 Cookies 6-12 Setting Cookies 6-13 Retrieving Cookies 6-14 About State Preservation 6-15 State Preservation: Example 6-16

ServletContext 6-17 RequestDispatcher 6-18 RequestDispatcher Example 6-19 Servlet Filters 6-20 Using Filters 6-21 doFilter () Method 6-22 Configuring Filters 6-24 Application Lifecycle Events 6-25 ServletContext Events 6-26 HttpSession Events 6-27 Example of an Event Listener 6-28 Error Handling 6-29 Summary 6-30 Practice 6-1 and 6-2 Overview 6-31 7 Maintaining State in J2EE Applications Objectives 7-2 Overview 7-3 Session Basics 7-4 Threading 7-6 URL Rewriting 7-7 HttpSession 7-8 Session Objects 7-9 Session-Based Page Counter 7-10 Session Life Cycle 7-11 Session Tracking in OC4J 7-12 Sessions and Events 7-13 Creating Distributable Applications 7-17 Summary 7-18 Practice 7-1 Overview 7-19 8 Creating the Web Tier: JavaServer Pages Objectives 8-2 JavaServer Pages 8-3 Comparing Servlets and JSPs 8-4 Invoking JavaServer Pages 8-5 The Date JSP 8-6 The Date Servlet 8-7 Automated JSP Features 8-8 JSP Lifecycle 8-9 Basic JSP Elements 8-10

vi

Declarations 8-11 Expressions 8-12 Scriptlets 8-13 Implicit Objects 8-14 Example 8-16 Directives 8-18 include: Example 8-19 The page Directive 8-20 JSP and JavaBeans 8-22 Using JavaBeans with JSP 8-23 scope Attribute of <jsp:useBean> Tag 8-25 Accessing and Setting Bean Property 8-26 JSP XML Document 8-28 Traditional Syntax Versus XML Syntax 8-29 JDeveloper and JavaServer Pages 8-31 JSP Tag Insight 8-32 Summary 8-33 Practice 8-1, 8-2, and 8-3 Overview 8-34 9 Modularizing JavaServer Pages Development with Tags Objectives 9-2 Custom Tags 9-3 Creating Custom Tags 9-4 Tag Handlers 9-5 Tag Handler Example 9-6 Tag Handler Lifecycle 9-7 Tag Library Descriptors 9-8 Using a Custom Tag 9-9 Tags with Attributes 9-10 TagExtraInfo Class 9-11 Tag Libraries 9-12 Creating a Custom Tag in JDeveloper 9-13 Creating a Tag Library in JDeveloper 9-14 Creating a Tag Handler in JDeveloper 9-15 Adding Attributes 9-16 Adding Scripting Variables 9-17 Packaging Tag Libraries 9-18 Tag Libraries in JDeveloper 9-19 Registering Tag Libraries 9-20 Using Tag Insight 9-23 Edge Side Include (ESI) Tags 9-24

vii

JESI Tags 9-25 Available JESI Tags 9-26 JESI Example 9-27 Summary 9-29 Practice 9-1 Overview 9-30 10 Communicating in J2EE Objectives 10-2 Overview of RMI 10-3 Role of RMI in J2EE 10-4 Communication in a J2EE Environment 10-5 How Clients Locate a Distributed Object 10-7 Java Naming and Directory Interface (JNDI) 10-8 J2EE Container and JNDI Interface 10-9 Naming Service 10-10 JNDI Terminology 10-11 Main JNDI Class and Interface 10-12 Accessing an Object in JNDI Namespace 10-13 Getting the JNDI InitialContext 10-14 Initial Context Factories 10-16 The lookup () Method 10-17 Obtaining a Reference to a Local Resource 10-18 Obtaining a Reference to a Remote Resource 10-19 Setting JNDI Environment Properties 10-20 Using RMI over HTTP Tunneling 10-23 Using Environment References with JNDI 10-24 Configuring Environment Variables 10-25 Specifying an EJB Reference 10-27 Configuring EJB References 10-28 Configuring Data Source References 10-30 Summary 10-32 Practice 10-1 Overview 10-33 11 Creating the Business Tier: Enterprise JavaBeans Objectives 11-2 Enterprise JavaBeans 11-3 When to Use Enterprise JavaBeans 11-4 Types of Enterprise Beans 11-5 Types of EJBs: Session Beans 11-7 Types of EJBs: Entity Beans 11-9 Types of EJBs: Message-Driven Beans 11-10

viii

EJB Architecture 11-11 EJB Server 11-12 EJB Container 11-13 Services Provided by the EJB Container 11-14 EJB Client 11-16 EJB Interfaces and Classes 11-17 Remote Interface and Remote Object 11-18 Home Interface and Home Object 11-19 Local Interface and Local Home Interface 11-20 EJB Bean Class 11-21 The EJB Deployment Process 11-22 The ejb-jar.xml File 11-23 The orion-ejb-jar.xml File 11-24 Creating an EJB in JDeveloper 11-25 Using the EJB Wizard 11-26 Adding Methods to the Bean 11-28 Deploying to Oracle9iAS from JDeveloper 11-29 Summary 11-30 Practice 11-1 Overview 11-31 12 Implementing Business Tasks with Session EJBs Objectives 12-2 Revisiting EJB Terminology 12-3 How a Remote Client Accesses the Bean Methods 12-5 Session Beans 12-6 javax.ejb.SessionBean Interface 12-8 Types of Session Beans 12-10 When to Use Session Beans 12-12 Lifecycle of a Stateless Session Bean 12-14 Home Interface for Stateless Session Beans 12-15 Remote Interface for Stateless Session Beans 12-17 The Session Bean Class 12-18 The Session Bean Class: Business Methods 12-20 Bean Class for the Stateless Session Bean 12-21 Deployment Descriptor 12-22 Client Application 12-23 Client Application for Stateless Session Beans 12-24 Lifecycle of a Stateful Session Bean 12-28 Home Interface for Stateful Session Bean 12-29 Client Application for Stateful Session Bean 12-30 Summary 12-31 Practice 12-1 and 12-2 Overview 12-32
ix

13 Managing Persistent Data in the Business Tier Objectives 13-2 Entity Beans 13-3 Representing Data in Entity Beans 13-5 When to Use Entity Beans 13-6 Callback Methods to Load and Store Data 13-7 Session Beans Versus Entity Beans 13-9 Types of Entity Beans 13-11 BMP Versus CMP Beans 13-12 Components of an Entity Bean 13-14 Creating, Removing, Finding, and Selecting Entity Beans 13-15 Home Interface of Entity Bean 13-17 Creating a Bean Instance 13-18 Finding an Entity Bean Instance 13-20 Removing an Entity Bean 13-22 Home Methods of Entity Beans 13-23 Component Interfaces of an Entity Bean 13-24 Primary Key Class of an Entity Bean 13-25 Bean Class of an Entity Bean 13-26 javax.ejb.EntityBean Interface 13-28 Lifecycle of an Entity Bean 13-30 Deployment Descriptor 13-32 Summary 13-35 Practice 13-1 Overview 13-36 14 Achieving Flexible State Management in the Business Tier Objectives 14-2 BMP Entity Beans 14-3 Developing a BMP Entity Bean 14-4 BMP Bean: Example 14-5 Remote Interface: JobsBMP 14-6 Home Interface: JobsBMPHome 14-7 Primary Key Class: JobsBMPPK 14-9 User-Defined Exception: JobSalException 14-10 Bean Class: JobsBMPBean 14-11 create () and ejbCreate() 14-13 Bean Class: JobsBMPBean 14-14 Deployment Descriptor 14-23 Bean-Managed Transaction 14-25 Container-Managed Transaction 14-26 Creating Data Source data-sources.xml 14-27

Client Class: JobsBMPClient 14-29 Summary 14-31 Practice14-1 Overview 14-32 15 Achieving Portable State Management in the Business Tier Objectives 15-2 Features of CMP Entity Beans 15-3 Implementing Methods in CMP Beans and BMP Beans 15-5 Developing a CMP Entity Bean 15-7 CMP Bean: Example 15-8 Bean Class of a CMP EJB: CMP Fields 15-9 Remote Interface: Departments 15-10 Home Interface: DepartmentsHome 15-11 Bean Class: DepartmentsBean 15-12 Deployment Descriptor ejb-jar.xml 15-15 Mapping CMP Fields to Database 15-19 Default Mapping of CMP Fields to Database 15-20 Explicit Mapping of CMP Fields to Database 15-21 Client for Departments Bean 15-22 TopLink Value Addition to EJB 15-25 Summary 15-26 Practice 15-1 Overview 15-27 16 Developing Message-Driven Beans Objectives 16-2 Overview of Messaging Systems 16-3 Types of Message Consumption 16-4 Java Message Service (JMS) 16-6 JMS Application Architecture 16-7 Point-to-Point Model 16-8 Publish-and-Subscribe Model 16-9 Using JMS Interfaces 16-10 JMS Message Structure 16-12 Sending a Message to a Queue 16-13 Receiving Messages 16-15 Asynchronous Message Delivery 16-16 Message-Driven Beans 16-17 Message-Driven Bean Architecture 16-18 State Diagram of a Message-Driven Bean 16-19 Developing Message-Driven Beans 16-21 Installing and Configuring the Oracle JMS Resource Provider 16-22

xi

Oracle JMS Resource Provider: Configuration Files 16-23 Interfaces to Be Implemented for MDBs 16-24 Implementing a Message-Driven Bean Class 16-25 Receiving Messages in a Message-Driven Bean Class 16-26 Creating the Deployment Descriptor 16-27 ejb-jar.xml: Example 16-29 Mapping in OC4J-Specific Deployment Descriptor 16-30 orion-ejb-jar.xml: Example 16-31 Creating an MDB with JDeveloper 16-32 Testing the Message-Driven Bean 16-36 Summary 16-37 Practice 16-1 Overview 16-38 17 Integrating J2EE Components Objectives 17-2 Overview 17-3 Creating Remote Clients for EJBs 17-4 Importing the EJB Home Interface 17-5 Create a Reference to the EJB 17-6 Passing Arguments to the EJB Method 17-7 Creating an EJB Reference 17-8 Creating Local Clients for EJBs 17-9 The ejb-local-ref Element 17-10 EJB Tags 17-11 The useHome Tag 17-12 The useBean Tag 17-13 The createBean Tag 17-14 The iterate Tag 17-15 Using the EJB Tags 17-16 Deploying an Application: Web Tier 17-17 Deploying an Application: EJB Tier 17-18 Deploying an Application: EAR File 17-19 Deploying from OEM 17-20 Summary 17-21 Practice 17-1 and 17-2 Overview 17-22 18 Distributing Modular Applications: Introduction to Web Services Objectives 18-2 What Is a Web Service? 18-3 Web Service 18-4 Service-Oriented Architecture 18-6 Web Services Constituents 18-7
xii

Benefits of Web Services 18-9 Web Services Model 18-11 RPC-Style Web Services 18-12 Document-Style Web Services 18-13 Oracle Support for Web Services 18-14 SOAP: XML Messaging for Web Services 18-15 Communication with SOAP 18-16 SOAP Messages 18-17 WSDL: Web Services Description Language 18-19 UDDI Registry 18-22 How UDDI Is Used 18-24 Searching for a Web Service by Using UDDI 18-25 UDDI Specification 18-26 UDDI Data Structure 18-27 UDDI Registration Example 18-28 tModel 18-29 UDDI Support in Oracle9i JDeveloper 18-33 UDDI Browsing with Oracle9i Jdeveloper 18-34 UDDI Publishing and Browsing with Oracle Enterprise Manager 18-35 Summary 18-36 Practice 18-1 Overview 18-37 19 Distributing Modular Applications: Developing Web Services Objectives 19-2 Oracle9iAS Web Services 19-3 Developing a Web Service with a Stateless Java Class 19-4 Defining an Interface 19-5 Defining a Stateless Java Class 19-6 Modifying the config.xml File 19-8 Creating the .ear File 19-10 Deploying the Hello Web Service 19-11 Oracle9iAS Web Service Home Page 19-12 Testing the Deployed Web Service 19-13 Home Page for Hello Web Service 19-14 Testing the sayHello Operation 19-15 Invoking the Hello Web Service with a Client Application 19-17 The Client Application 19-19 Compiling and Running the Client Application 19-20 Serializing and Encoding Parameters and Results 19-21 Web Service with Oracle9i JDeveloper 19-22

xiii

Hello Web Service with Oracle9i JDeveloper 19-23 Adding Hello.java and HelloImpl.java to the Project 19-24 Creating a Web Service with HelloImpl.java 19-25 Generating the Stub File 19-32 Generating the Sample Java Client 19-33 Run the Client Application 19-35 Developing a Stored Procedure Web Service 19-36 Generating Wrapper Classes Using JPublisher 19-37 config.xml File for Stored Procedure Web Service 19-38 Exposing a Function as a Web Service by Using Oracle9i Jdeveloper 19-40 Publishing the Package as a Web Service 19-41 Summary 19-42 Practice 19-1 Overview 19-43 20 Implementing Security in J2EE Applications Objectives 20-2 Goals of J2EE Security Architecture 20-3 Overview of J2EE Security Architecture 20-4 Java Authentication and Authorization Services (JAAS) 20-5 Authorization of a Client 20-8 JAAS Provider Types in OC4J 20-9 Configuring Security 20-10 Defining the Users, Groups, and Roles 20-11 Managing Users and Groups with the JAZN Admintool 20-13 Defining the Logical Roles 20-15 Defining and Using Logical Roles in Web Applications (web.xml) 20-16 Defining and Using Logical Roles in EJBs (ejb-jar.xml) 20-18 Mapping Logical Roles to Users and Groups 20-19 Programmatic Access to a Callers Security Context 20-20 Client Authentication 20-21 EJB Client Authentication with the jndi.properties file 20-22 EJB Client Authentication with a Hashtable 20-23 Setting Access Control with JDeveloper 20-24 Creating Web Application Security Roles 20-25 Web Application Login Authentication 20-26 Web Application Authorization 20-27 Creating EJB Security Roles 20-28 Setting Method Permissions 20-29 Method Access in EJB Deployment Descriptors 20-30 Creating a Mapping for the Logical Roles 20-31

xiv

Mapping JAZN Identities to a Logical Role 20-32 Mapping Results in orion-ejb-jar.xml 20-33 Accessing the EJB with New Permissions 20-34 Summary 20-35 Practice 20-1 Overview 20-36 21 Oracle9iAS: Advanced Topics for J2EE Developers Objectives 21-2 What Is a Transaction? 21-3 EJB Support for Transactions 21-4 EJB Transaction Model 21-5 Demarcating Transactions 21-6 Container-Managed Transactions 21-7 CMT: Transaction Attributes 21-8 CMT: Transaction Attributes 21-9 Transaction Attribute: NotSupported 21-10 Transaction Attribute: Required 21-11 Transaction Attribute: Supports 21-12 Transaction Attribute: RequiresNew 21-13 Transaction Attribute: Mandatory 21-14 Transaction Attribute: Never 21-15 CMT: setRollbackOnly () Method 21-16 JDeveloper: Setting Transaction Attributes 21-17 Java Transaction API (JTA) 21-19 JTA: UserTransaction Interface 21-20 Bean-Managed Transaction Demarcation 21-21 BMT Demarcation: Process 21-22 Using UserTransaction Support in EJBs 21-23 Client Demarcated Transactions Using UserTransaction 21-24 BMT Demarcation: Restrictions 21-25 Local and Global Transactions 21-26 Single-Phase Commit 21-27 Data Sources Revisited 21-28 Default data-sources.xml 21-29 Emulated Versus Non-Emulated Data Sources 21-30 Retrieve Connection to Data Source 21-31 Global Transaction Resource Request Flow 21-33 Resource Request Flow 21-34 Enlisting Database Resources 21-36 Overview of J2EE Connector Architecture 21-38 OC4J J2EE Connector Architecture 21-39

xv

What Is a Resource Adapter? 21-40 Resource Adapter Deployment Descriptors 21-41 Deploying Stand-Alone Resource Adapters 21-42 Deploying Embedded Resource Adapters 21-43 Common Client Interface (CCI) API 21-44 Summary 21-45 Practice 21-1 Overview 21-46

xvi

You might also like