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 OverviewObjectives Objectives 2-2 Java 2, Enterprise Edition Platform 2-3 J2EE Platform 2-4 Benefits of the J2EE Platform 2-5 J2EE Components 2-7 J2EE 1.3 Components 2-8 J2EE Architecture 2-9 Client-Tier Components 2-10 J2EE Web-Tier Components 2-11 What Is a Servlet? 2-13 What Is a JavaServer Page (JSP)? 2-14 Web-Tier Components: Summary 2-15 Business-Tier Components 2-16 Enterprise JavaBeans (EJB) 2-17 J2EE Communication APIs 2-18 J2EE Server 2-19 Oracle Application Server 10g Containers for J2EE (OC4J) 2-21 J2EE Applications 2-22 Packaging J2EE Application Components 2-23 JARs 2-24 WARs 2-25 EJB JARs 2-26 EARs 2-27 EAR File Structure for a J2EE Application: Example 2-28 OC4J Architecture 2-29 OC4J Server Configuration Files 2-30 Relation of Configuration Files 2-31 Data Sources 2-32 Application Logging 2-33 J2EE Application Deployment to Oracle Application Server 10g 2-34 iii Oracle Enterprise Manager 2-35 JDeveloper and J2EE 2-36 Oracle JDeveloper 10g Environment 2-37 Oracle JDeveloper 10g Visual Design Tools 2-38 Summary 2-39 Practice 2-1: Overview 2-40 3 Designing 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 Oracle Application Server 10g Containers for J2EE 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 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-23 Servlet Mapping 4-24 Servlet Mapping in JDeveloper 4-25 Invoking a Servlet 4-26 Specifying J2EE Web Module Settings 4-27 Creating a Connection to Oracle Application Server 10g 4-28 Deploying to OC4J 4-29 Summary 4-30 Practices 4-1, 4-2, and 4-3: Overview 4-31 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 Transaction Handling 5-7 Connection Pooling 5-9 Data Sources 5-10 Data Source Definition 5-11 data-sources.xml: Example 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 v ServletContext 6-17 RequestDispatcher 6-18 RequestDispatcher: Example 6-19 Servlet Filters 6-20 Using Filters 6-21 doFilter() Method 6-22 Using Filters 6-23 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 Practices 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 vi 8 Creating the Web Tier: JavaServer Page Objectives 8-2 JavaServer Pages 8-3 Comparing Servlets and JSPs 8-4 Invoking JSPs 8-5 The Date JSP 8-6 The Date Servlet 8-7 Automated JSP Features 8-8 JSP Life Cycle 8-9 Basic JSP Elements 8-10 Declarations 8-11 Expressions 8-12 Scriptlets 8-13 Implicit Objects 8-14 Example 8-16 Directives 8-18 include: Example 8-19 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 JSPs 8-31 Creating JSPs Visually 8-32 JSP Tag Insight 8-33 Summary 8-34 Practices 8-1, 8-2, and 8-3: Overview 8-35 9 Modularizing JavaServer Pages Development with Tags Objectives 9-2 Custom Tags 9-3 Custom Tag Library Components 9-4 Tag Handler: Example 9-5 Tag Library Descriptors 9-6 Using a Custom Tag 9-7 Tags with Attributes 9-8 Creating a Custom Tag in JDeveloper 9-9 Tag Libraries in JDeveloper 9-10 Registering Tag Libraries 9-11 Using Tag Insight 9-14 vii JSP Standard Tag Library (JSTL) 9-15 Core Tag Library 9-16 Utilizing Core Tags 9-18 Expression Language 9-19 Using Iteration Tags 9-20 Using the URL Tags 9-21 XML Tag Library 9-23 SQL Tag Library 9-24 Accessing a Database with SQL Tags 9-25 Querying Using SQL Tags 9-26 Inserting, Updating, and Deleting Data 9-27 Formatting Tags 9-28 Internationalization Concepts 9-29 Internationalizing Strings 9-30 Formatting Numbers and Dates 9-31 Transforming XML Documents 9-34 JSTL in JDeveloper 9-35 Summary 9-36 Practice 9-1: Overview 9-37 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 Component 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 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-24 Using Environment References with JNDI 10-25 Configuring Environment Variables 10-26 Specifying an EJB Reference 10-28 viii Configuring EJB References 10-29 Configuring Data Source References 10-31 Summary 10-33 Practice 10-1: Overview 10-34 11 Creating the Business Tier: Enterprise JavaBeans Objectives 11-2 Enterprise JavaBeans (EJB) 11-3 When to Use EJBs 11-4 Types of EJBs 11-5 Session Beans 11-7 Entity Beans 11-9 Message-Driven Beans 11-10 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 ejb-jar.xml File 11-23 orion-ejb-jar.xml File 11-24 Creating an EJB in JDeveloper 11-25 Using the EJB Wizard 11-26 Using the EJB Wizard 11-27 Adding Methods to the Bean 11-28 Deploying to Oracle Application Server 10g from JDeveloper 11-29 Summary 11-30 Practice 11-1: Overview 11-31 ix 12 Implementing Business Tasks with Session EJBs Objectives 12-2 Session Beans 12-3 javax.ejb.SessionBean Interface 12-5 Types of Session Beans 12-7 When to Use Session Beans 12-9 Life Cycle of a Stateless Session Bean 12-11 Home Interface for Stateless Session Beans 12-12 Remote Interface for Stateless Session Beans 12-14 The Session Bean Class 12-15 The Session Bean Class: Business Methods 12-17 Bean Class for the Stateless Session Bean 12-18 Deployment Descriptor 12-19 Client Application 12-20 Client Application for Stateless Session Beans 12-21 Life Cycle of a Stateful Session Bean 12-25 Home Interface for Stateful Session Bean 12-26 Client Application for Stateful Session Bean 12-27 Summary 12-28 Practices 12-1 and 12-2: Overview 12-29 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 Beans Versus CMP Beans 13-12 Components of an Entity Bean 13-14 Creating, Removing, Finding, and Selecting Entity Beans 13-15 Home Interface of an 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 Life Cycle of an Entity Bean 13-30 x Deployment Descriptor 13-32 Summary 13-35 Practice 13-1: Overview 13-36 14 Achieving State Management in the Business Tier Objectives 14-2 Features of BMP Entity Beans 14-3 Developing a BMP Entity Bean 14-4 Features of CMP Entity Beans 14-5 Implementing Methods in CMP Beans and BMP Beans 14-7 Developing a CMP Entity Bean 14-9 CMP Bean: Example 14-10 Bean Class of a CMP EJB: CMP Fields 14-11 Remote Interface: Departments 14-12 Home Interface: DepartmentsHome 14-13 Bean Class: DepartmentsBean 14-14 Deployment Descriptor ejb-jar.xml 14-17 Mapping CMP Fields to Database Table Columns 14-21 Default Mapping of CMP Fields to Database Table Columns 14-22 Explicit Mapping of CMP Fields to Database Table Columns 14-23 Client for Departments Bean 14-25 Summary 14-28 Practice 14-1: Overview 14-29 15 Container-Managed Relationships (CMRs) Objectives 15-2 Relationships 15-3 Implementing Relationships 15-4 Cardinality and Direction of Relationships 15-5 One-to-One Relationships 15-7 One-to-Many Relationships 15-8 Many-to-Many Relationships 15-9 Oracle TopLink 15-10 TopLink: Integration of J2EE Applications with Data Sources at Run Time 15-11 TopLink: Integrated with Oracle JDeveloper 10g 15-12 Implementing Relationships 15-13 Defining Abstract Accessor Methods 15-14 Accessor Methods in 1:1 Relationships 15-15 Accessor Methods in 1:M Relationships 15-16 Checking Relationship Mappings in JDeveloper 15-17 Accessor Methods in M:N Relationships 15-18 xi Implementing a Relationship in the Deployment Descriptor 15-19 Implementing 1:1 Relationships 15-21 Implementing 1:M Relationships 15-23 Implementing M:N Relationships 15-25 Mapping Relationship Fields to Database 15-27 Default Mapping of Relationship Fields 15-28 Explicit Mapping of Relationship Fields 15-30 Using JDeveloper to Create CMR Beans 15-31 Summary 15-32 Practice 15: Overview 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 MDB Architecture 16-18 Associating JMS Resources with an MDB 16-19 State Diagram of an MDB 16-20 Developing MDBs 16-22 Interfaces to Be Implemented for MDBs 16-23 Implementing an MDB Class 16-24 Receiving Messages in an MDB Class 16-25 Creating the Deployment Descriptor 16-26 ejb-jar.xml: Example 16-28 Mapping in OC4J-Specific Deployment Descriptor 16-29 orion-ejb-jar.xml: Example 16-30 Creating an MDB with JDeveloper 16-31 Testing the MDB 16-34 Summary 16-35 xii 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 ejb-local-ref Element 17-10 EJB Tags 17-11 useHome Tag 17-12 useBean Tag 17-13 createBean Tag 17-14 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 Oracle Enterprise Manager 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 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 Web Services Description Language (WSDL) 18-19 WSDL 18-20 UDDI Registry 18-22 How UDDI Is Used 18-24 Searching for a Web Service by Using UDDI 18-25 xiii UDDI Specification 18-26 tModel 18-27 UDDI Support in Oracle JDeveloper 10g 18-31 UDDI Browsing with Oracle JDeveloper 10g 18-32 UDDI Publishing and Browsing with Oracle Enterprise Manager 18-33 Summary 18-34 Practice 18-1: Overview 19 Distributing Modular Applications: Developing Web Services Objectives 19-2 Oracle Application Server 10g 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-7 Creating the Web Service 19-10 Creating the Client Application 19-11 Deploying the Web Service 19-13 Testing the Web Service 19-14 Web Service Home Page 19-15 Testing the Deployed Web Service with Home Page 19-16 Testing the sayHello Operation 19-17 Serializing and Encoding Parameters and Results 19-19 Developing a Stored Procedure Web Service 19-20 Generating Wrapper Classes Using JPublisher 19-21 Exposing a Function as a Web Service by Using Oracle JDeveloper 10g 19-22 Publishing the Package as a Web Service 19-23 JMS Web Services 19-24 Summary 19-26 Practice 19-1: Overview 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 Service (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 xiv 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 Caller’s 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 Mapping JAZN Identities to a Logical Role 20-32 Mapping Results in orion-ejb-jar.xml 20-34 Accessing the EJB with New Permissions 20-35 Summary 20-36 Practice 20-1: Overview 20-37 21 Oracle Application Server 10g Transaction Support Objectives 21-2 What Is a Transaction? 21-3 Enterprise JavaBeans (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 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: The setRollbackOnly() Method 21-16 JDeveloper: Setting Transaction Attributes 21-17 Java Transaction API (JTA) 21-19 JTA: The UserTransaction Interface 21-20 Bean-Managed Transactions 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 xv Local and Global Transactions 21-26 Single-Phase Commit 21-27 Data Source Revisited 21-28 Default data-sources.xml 21-29 Emulated Versus Nonemulated 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 Summary 21-38 A Practice Solutions B Schema Descriptions C Oracle JDeveloper 10g Quick Reference D BMP Entity EJBs J2EE Connector Architecture xvi