You are on page 1of 16

Developing Applications with Java EE 6 on WebLogic Server 12c

Student Guide - Volume I

D65269GC30 Edition 3.0 June 2012 D77932

Authors
Matt Heimer Tom McGinn

Copyright 2012, Oracle and/or its affiliates. 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. Governments 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 and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

Technical Contr ibutors and Reviewer s


Amitava Kundu Anjana Shenoy Ben Tegha Bob Kellogg Camilla Haase Diganta Choudhury Evan Troyka Joe Boulenouar Mark Judd Ravi Narasimhacharya

Editor s
Malavika Jinka Aju Kumar Rashmi Rajagopal

Gr aphic Designer
Seema Bopaiah

Publisher s
Pavithran Adka Syed Ali

Contents

Java Platform, Enterprise Edition Course Goals 1-2 Course Objectives 1-3 Audience 1-4 Topics Not Covered 1-5 How Prepared Are You? 1-7 Class Introductions 1-8 Course Environment 1-9 Java Technology Product Groups 1-10 Requirements of Enterprise Applications 1-11 Enterprise Application Infrastructure Technologies 1-12 Separation of Business Logic from Platform Services 1-13 Java EE Profiles 1-14 Java EE 6 Web Profile 1-15 Full Java EE 6 Requirements 1-17 Java EE Technology Specifications 1-18 Java EE Services and APIs 1-19 Java EE Component Containers 1-20 Java EE Service Infrastructure 1-21 Java EE Platform Tiers and Architecture 1-23 N-Tier Architectural Model 1-24 Java EETiered Architecture 1-25 Additional Resources 1-26 Quiz 1-27 Summary 1-30 Practice 1: Overview 1-32 Enterprise Development Tools and Application Servers Objectives 2-2 Java Application Servers 2-3 Java EE Implementations 2-4 Selecting an Application Server 2-5 Oracle WebLogic 12c 2-6 WebLogic 12c Zip Distribution 2-7 Development Tools 2-8

iii

Java EE Development Tools 2-9 Configuring NetBeans and WebLogic Server 2-10 Starting WebLogic Server with NetBeans 2-12 Steps for Developing a Java EE Application 2-13 Java EE Application Development Process 2-15 Java EE Containers 2-16 Java EE Component Characteristics 2-17 Component State and Properties 2-18 Encapsulated Components 2-19 Component Proxies 2-20 Configuring and Packaging Java EE Applications 2-21 Web Application Elements 2-23 Web Archive File Creation 2-24 Java Archive Files 2-25 Enterprise Archive Files 2-26 Deployment Descriptors 2-27 Vendor-Specific Deployment Descriptors 2-28 Quiz 2-29 Additional Resources 2-36 Summary 2-37 Practice 2: Overview 2-38 3 JavaBeans, Annotations, and Logging Objectives 3-2 Java SE Features Common in EE Applications 3-3 JavaBeans 3-4 What Are Suitable JavaBeans? 3-5 Features and Benefits of JavaBeans 3-6 Creating a JavaBean 3-7 Bean Properties 3-8 A Basic JavaBeans Component 3-9 Persistence 3-10 Tool Support 3-11 Quiz 3-12 Java Logging 3-13 Logging Frameworks 3-14 java.util.logging.Logger Class 3-15 Log Levels 3-16 Logging Method Categories 3-17 Logging Method Usage 3-18 Where Log Records Go 3-19

iv

Guarded Logging 3-20 Logging Handlers and Formatters 3-21 Logger Hierarchy 3-22 Logging Configuration 3-23 Application Servers and Logging 3-24 WebLogic Server Logging Bridge Handler 3-25 WebLogic Log Files 3-26 WebLogic Log File Format 3-27 WebLogic Logging Service Configuration 3-28 Quiz 3-29 Annotations 3-30 XML Based Deployment Data 3-31 Compiler Supported Annotations 3-32 Lifecycle Annotations 3-33 Custom Annotations 3-34 Annotation Elements 3-35 Annotation Placement 3-36 Annotation Retention 3-37 Annotations in Java EE 3-38 Quiz 3-39 Additional Resources 3-40 Summary 3-41 Practice 3-1 Overview: Adding a Logger to an application 3-42 Practice 3-2 Overview: Adding a JavaBeans Persistence Mechanism to the JavaFXMediaViewer Application 3-43 Practice 3-3 Overview: Logging to the WebLogic Server Log File 3-44 4 Web Component Model Objectives 4-2 Why Create Web Applications 4-3 Web Application Overview 4-4 Role of Web Components in a Java EE Application 4-5 MVC Architecture 4-6 MVC in a Java EE Web Application 4-7 HTTP Request-Response Model 4-8 GET and POST Requests 4-10 Content Type and the Response Header 4-11 Overview of Web Components 4-12 Comparison of Servlets and JSP Components 4-13 Servlet Example 4-14 JSP Component Example 4-16

Servlet and JSP Component Collaboration 4-17 Runtime Behavior of Servlets and JSP Components 4-18 JSF Facelet Example 4-19 JSP Expression Language (EL) 4-20 Web Context Root and Alias Mapping 4-21 Data Scopes 4-22 Cascading Style Sheets 4-23 Web Development Tools 4-24 Additional Resources 4-25 Quiz 4-26 Summary 4-30 Practice 4: Overview 4-31 5 Developing with JavaServer Faces Technology Objectives 5-2 JavaServer Faces (JSF) 5-3 JavaServer Faces: Benefits 5-4 Inversion of Control 5-5 JavaServer Faces 2.0: New Features 5-6 JSF Application Elements 5-7 JSF Hello World 5-8 JSF Forms 5-9 JSF Page Tags 5-10 JSF Tag Libraries 5-11 Tag Library Documentation 5-12 Managed Beans 5-13 Managed Bean Configuration 5-14 Managed Beans and Forms 5-15 An Example JSF Application 5-16 JSF Page: index.xhtml 5-17 GuessBean Managed Bean 5-18 JSF Deployment Descriptor 5-20 JSF Application Life Cycle Phases 5-21 An Initial JSF Page Request 5-22 JSF Postback Requests 5-23 JSF Conversion and Validation 5-24 Text and HTML Within Facelet Pages 5-25 Creating HyperLinks 5-26 Managed Bean Life Cycle and Scope 5-27 Managed Bean Dependency Injection 5-29 Unified Expression Language 5-30

vi

Using the Unified EL to Reference Managed Beans 5-31 JSF Page Navigation 5-32 ui:repeat Tag 5-33 <ui:repeat>: Example 5-34 Additional Resources 5-35 Quiz 5-36 Summary 5-40 Practice 5-1: Overview Creating a JSF Application 5-41 Practice 5-2: Overview Adding a media View Page 5-42 Practice 5-3: Overview Adding a settings View Page 5-43 6 Using AJAX and Composite Components with JSF Objectives 6-2 JSF 2.0 and AJAX 6-3 Using the <f:ajax> Tag 6-4 JSF Life Cycle and AJAX 6-5 Simple AJAX Example 6-6 JSF AJAX Attributes 6-7 execute and render Special Keywords 6-9 Where Do You Put the <f:ajax> Tag? 6-10 AJAX Field Validation 6-11 Using the JavaScript API for JSF 2.0 6-12 Using the JavaScript API 6-13 Composite Components 6-14 Composite Component Usage Contract 6-15 Composite Component Implementation 6-16 Using the Composite Component 6-17 Composite Components Are Flexible 6-18 Essential Composite Tags 6-19 Additional Resources 6-20 Quiz 6-21 Summary 6-24 Practice 6-1 Overview: Creating a JSF Composite Component 6-25 Practice 6-2 Overview: Adding AJAX to the settings View Page 6-26 Apache Trinidad JSF Component Library and Mobile Development Objectives 7-2 User-Agent Detection 7-3 HTML5 Video Support 7-4 <video> Tag 7-5 JSF Component Libraries 7-6

vii

Apache Trinidad 7-7 Configuring Trinidad 7-8 Creating a Facelet with Trinidad 7-9 Creating Forms with Trinidad 7-10 Form Layout 7-11 Using <tr:panelTabbed> 7-12 Trinidad Skinning 7-13 Available Skins 7-14 Creating Custom Skins 7-15 Mobile Web Development 7-16 Trinidad Mobile Support 7-17 Mobile Web Design 7-18 Mobile Viewports 7-19 Quiz 7-20 Summary 7-21 Practice 7-1 Overview: (Optional) Implementing HTML5 Video 7-22 Practice 7-2 Overview: Using the Trinidad Component Library 7-23 Practice 7-3 Overview: Creating Mobile Web Applications 7-24 8 Dependency Injection With CDI Objectives 8-2 Before Dependency Injection 8-3 Dependency Injection in Java EE 5 8-4 Other Forms of Dependency Injection 8-5 Contexts and Dependency Injection (CDI) 8-6 Enabling CDI 8-7 Using CDI 8-8 Bean Scopes 8-9 Qualifiers 8-10 @Named Qualifier 8-13 Injecting Classes Without a no-arg Constructor 8-14 Producers 8-15 Alternatives 8-17 Enabling @Alternative Classes 8-18 @Alternative Producers 8-19 Additional CDI 8-20 Additional Resources 8-21 Quiz 8-22 Summary 8-23 Practice 8 Overview: Contexts and Dependency Injection 8-24

viii

Using JSF and Bean Validation Objectives 9-2 JSF Process Validation Details 9-3 Input Conversion 9-4 Validation 9-5 Validation and Conversion 9-7 Validation and Conversion Errors 9-8 Error Messages 9-11 Updated GuessNumberExample 9-12 Bean Validation 9-13 Bean Validation: Example 9-14 Bean Validation Messages 9-15 Default Bean Validations Constraints 9-16 Custom Bean Validation 9-18 Constraint Annotation 9-19 Constraint Implementation 9-20 Applying the ValidLicenseKey Annotation 9-22 Additional Resources 9-23 Quiz 9-24 Summary 9-28 Practice 9-1 Overview: Apply Built-in JSF and Bean Validation 9-29 Practice 9-2 Overview: Create a Custom Bean Validation Constraint 9-30

10 Developing Servlets Objectives 10-2 Basics of the Servlet API 10-3 Basic Servlet 10-4 Basic Servlet Result 10-6 Servlet Configuration 10-7 Deployment Descriptors 10-8 Deployment Descriptor: Example 10-9 Setting Response Headers 10-10 Creating the Response Body 10-11 Uploading Files 10-12 Server-side File Processing 10-13 The RequestDispatcher Interface 10-16 The RequestDispatcher Target and the Context Root 10-18 The forward and include Methods 10-19 Using the Session Management API 10-20 Session Binding 10-21

ix

Retrieving a Session Object 10-22 Additional Resources 10-23 Quiz 10-24 Summary 10-28 Practice 10-1: Overview Create a servlet to download media items 10-29 Practice 10-2: Overview Create a servlet to upload media items 10-30 11 Developing With JavaServer Pages Technology Objectives 11-2 JSP Technology as a Presentation Mechanism 11-3 Presentation Using JSP Pages Compared to Servlets 11-5 Worker Beans, JSTL, and Custom Tags 11-7 JSP Page Translation Procedure 11-8 Java Code Embedded in JSP Pages 11-9 Syntactic Forms of JSP Tags 11-10 JSP Technology Directives 11-11 JSP Directives 11-12 Declarations, Expressions, and Scriptlets 11-13 Declaration Scripting Elements 11-14 Expression Scripting Elements 11-15 Scriptlet Scripting Elements 11-16 JavaServer Pages Scopes 11-18 Using EL to Access Scoped Beans 11-19 Request-Scope Beans and Collecting Data from Servlets 11-20 The taglib Directive 11-21 The Expression Language (EL) 11-22 The JSTL Core Tag Library 11-23 JSTL Examples 11-24 Additional Resources 11-25 Quiz 11-26 Summary 11-29 Practice 11-1: Developing JSPs 11-30 Practice 11-2: Implementing JSP Footers and Dynamic Styling 11-31 12 EJB Component Model Objectives 12-2 EJB Component Types 12-3 Enterprise Application Tiers 12-4 Benefits of EJBs 12-5 Analysis of the Session EJB Component Model 12-6 EJB Lite 12-7

Embedded EJB Container 12-8 EJB Objects and Proxies 12-9 Local and Distributed Client Views 12-10 Using Annotations to Obtain an EJB Reference in a Managed Component 12-11 Comparison of Stateless and Stateful Behavior 12-12 Stateless Session Bean Cardinality 12-13 Stateful Session Bean Cardinality 12-14 Singleton Session Bean Characteristics 12-15 Creating Session Beans 12-16 Declaring a Local Session Bean 12-17 Declaring a Remote Session Bean 12-18 Requirements for a Session Bean Class 12-19 Life Cycle of a Stateless Session Bean 12-20 Life Cycle of a Stateful Session Bean 12-21 The SessionContext Object 12-22 Standard JNDI Names 12-23 Stand-alone Java SE Clients 12-24 Additional Resources 12-25 Quiz 12-26 Summary 12-29 Practice 12-1 Overview: Creating a Singleton Bean 12-30 13 The Java Persistence API Objectives 13-2 The Java Persistence API 13-3 Object-Relational Mapping 13-4 Normalized Data Mapping 13-5 Use of an Entity Component Across a Set of Database Tables 13-6 Entity Class Requirements 13-7 Creating an Entity 13-8 Entity Mapping 13-9 Primary Keys 13-10 Entity Component Primary Key Association 13-11 Changing Default Mapping 13-12 Persistent Fields Versus Persistent Properties 13-13 Persistence Data Types 13-15 Applying Bean Validation to Entities 13-16 Persistence Units 13-17 The persistence.xml file 13-18 The Persistence Context 13-19 The Entity Manager 13-20

xi

Entity Instance Management 13-21 Entity Instance Life Cycle and Entity Manager Methods 13-23 Entity Bean States 13-24 Entity Manager Methods 13-25 Managed Entities 13-26 JPA Queries 13-27 A Native Query: Example 13-28 Additional Resources 13-29 Quiz 13-30 Summary 13-33 Practice 13-1: Overview Implementing a JPA Application 13-34 Practice 13-2: Overview Creating a dynamic JPQL query 13-35 14 Implementing a Transaction Policy Objectives 14-2 Transaction Semantics 14-3 Atomicity 14-4 Locking and Isolation 14-6 Transaction Models 14-7 Comparison of Programmatic and Declarative Transactions 14-9 Programmatic Scoping as Opposed to Reusability 14-10 Using JTA to Scope Transactions Programmatically 14-11 Getting a Reference to the UserTransaction Interface 14-12 Using the begin,commit, and rollback Methods 14-13 Implementing a Container-Managed Transaction Policy 14-14 Container Interactions with the Transaction Management Infrastructure 14-15 How to Control the Containers Behavior Using Transaction Attributes 14 -17 Predicting the Effect of Transaction Scope on Application Performance 14-18 Transaction Scope and Entity Synchronization 14-19 Optimistic Locking and Versioning 14-20 Implementing Versioning 14-21 Pessimistic Locking 14-22 Effect of Exceptions on Transaction State 14-23 Using the EJBContext Object to Check and Control Transaction State 14-24 Additional Resources 14-25 Quiz 14-26 Summary 14-30 Practice 14-1: Overview Implement Optimistic Locking 14-31

xii

15 Web Service and Integration Technology Survey Objectives 15-2 N-Tier Architectural Model 15-3 Business and Resource Layers 15-4 The Integration Layer 15-5 The DAO Pattern 15-6 The Java Persistence API 15-7 The Java Messaging Service (JMS) API & MDBs 15-8 The JavaMail API 15-9 JNDI 15-10 Java EE Connector Architecture 15-11 Web Services 15-12 Web Services APIs 15-13 Transactions and Resources 15-15 Additional Resources 15-16 Summary 15-17 16 Implementing a Security Policy Objectives 16-2 Exploiting Container-Managed Security 16-3 Security Concepts 16-4 End-to-End Security Model 16-5 Container-Managed Security 16-6 Container-Managed Authentication 16-7 Authentication in the Web Tier 16-8 Web-Tier Authentication Challenge Methods 16-9 Basic Authentication Dialog Box 16-10 Authentication from the Web Tier to the EJB Tier 16-11 Authentication of Non-Web Clients 16-12 Interaction With the Security Infrastructure 16-13 User Roles and Responsibilities 16-14 Role-Based Java EE Security Model 16-15 Creating a Role-Based Security Policy 16-16 Role Mapping 16-17 Role-Based Authorization in the Web Tier 16-19 web.xml Security Constraints 16-20 Role-Based Authorization in the EJB Tier 16-21 EJB Tier Security Annotations 16-22 Using the Security API 16-23 Web-Tier Security API 16-24 EJB-Tier Security API 16-25

xiii

CDI Security API 16-26 Configuring Authentication in the Web Tier Selecting Basic Authentication 16-28 HTTP Error Pages 16-29 Selecting Form Authentication 16-30

16-27

Creating an HTML Login Page for FormBased Authentication Programmatic Logins 16-32 Security Exceptions 16-33 Additional Resources 16-34 Quiz 16-35 Summary 16-41 Practice 16-1 Overview: Using Declarative Security 16-42

16-31

A Developing Java EE Applications Using Messaging Objectives A-2 Messaging System Participants A-3 Administered Objects A-4 Messaging Clients A-5 Messages A-6 Point-to-Point Messaging Architecture A-7 Publish/Subscribe Messaging Architecture A-8 Creating a Queue Message Producer A-9 Example Code for Message Producer A-12 Queue Message Browser A-15 Example Code for Queue Message Browser A-16 Evaluating the Capabilities and Limitations of EJB Components as Messaging Clients A-19 Additional Resources A-20 Summary A-21 Quiz A-22 Practice A: Overview A-25 B Developing Message-Driven Beans Objectives B-2 Introducing Message-Driven Beans B-3 Java EE Technology Client View of Message Driven Beans Life Cycle of a Message-Driven Bean B-5 Types of Message-Driven Beans B-6 Creating a JMS Message-Driven Bean B-7 Message-Driven Bean: Example B-9 B-4

xiv

Callback Methods in a Bean Class Additional Resources B-12 Summary B-13 Quiz B-14 Practice B: Overview B-16

B-10

xv