1. Trang chủ
  2. » Công Nghệ Thông Tin

The j2eetm tutorial - phần 1 docx

35 178 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 35
Dung lượng 1,1 MB

Nội dung

The J2EE TM Tutorial Stephanie Bodoff Dale Green Eric Jendrock Monica Pawlan Beth Stearns Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Copyright © 2001 by Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California 94303 U.S.A. All rights reserved. RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the United States Government is subject to the restrictions set forth in DFARS 252.227-7013(c)(1)(iii) and FAR 52.227-19. The release described in this book may be protected by one or more U.S. patents, foreign patents, or pending applications. Sun, Sun Microsystems, Sun Microsystems Computer Corporation, the Sun logo, the Sun Microsystems Computer Corporation logo, Java, JavaSoft, Java Software, JavaScript, JDBC, JDBC Compliant, JavaOS, JavaBeans, Enterprise JavaBeans, JavaServer Pages, J2EE, J2SE, JavaMail, Java Naming and Directory Interface, EJB, and JSP are trademarks or registered trademarks of Sun Microsystems, Inc. UNIX ® is a registered trademark in the United States and other countries, exclusively licensed through X/Open Com- pany, Ltd. All other product names mentioned herein are the trademarks of their respective owners. THIS PUBLICATION IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THIS PUBLICATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW EDITIONS OF THE PUBLICATION. SUN MICROSYSTEMS, INC. MAY MAKE IMPROVEMENTS AND/OR CHANGES IN THE PROD- UCT(S) AND/OR THE PROGRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com iii Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Who Should Use This Tutorial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xviii Prerequisites for the Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xviii Where to Find the Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xviii How to Build and Run the Examples. . . . . . . . . . . . . . . . . . . . . . . . .xviii Related Information. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx How to Print This Tutorial. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Typographical Conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii Chapter 1: Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Distributed Multitiered Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 J2EE Application Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Client Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Application Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Web Browsers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Applets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 JavaBeans™ Component Architecture . . . . . . . . . . . . . . . . . . . . . 27 J2EE Server Communications. . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Thin Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Business Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Enterprise Information System Tier. . . . . . . . . . . . . . . . . . . . . . . . . . . 30 J2EE Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Containers and Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Container Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Packaging. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com iv Development Roles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34 J2EE Product Provider. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34 Tool Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .34 Application Component Provider . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 Enterprise Bean Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 Web Component Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 J2EE Application Client Creation. . . . . . . . . . . . . . . . . . . . . . . . . .35 Application Assembler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .35 Application Deployer and Administrator. . . . . . . . . . . . . . . . . . . . . . . .36 Reference Implementation Software. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36 Web Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Database Access. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 J2EE APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37 Enterprise JavaBeans Technology 2.0 . . . . . . . . . . . . . . . . . . . . . .37 JDBC™ 2.0 API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 Java Servlet Technology 2.3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 JavaServer Pages (JSP) Technology 1.2. . . . . . . . . . . . . . . . . . . . .38 Java Message Service (JMS) 1.0 . . . . . . . . . . . . . . . . . . . . . . . . . .39 Java Transaction API (JTA) 1.0 . . . . . . . . . . . . . . . . . . . . . . . . . . .39 JavaMail™ Technology 1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39 JavaBeans Activation Framework 1.0 . . . . . . . . . . . . . . . . . . . . . .39 Java API for XML (JAXP) 1.1. . . . . . . . . . . . . . . . . . . . . . . . . . . .39 J2EE Connector API 1.0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40 Java Authentication and Authorization Service (JAAS) 1.0 . . . . .40 Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .40 Application Deployment Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . .40 Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .42 Chapter 2: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .43 Setting Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44 Getting the Example Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44 Building the Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44 Checking the Environment Variables . . . . . . . . . . . . . . . . . . . . . . . . . .45 Starting the J2EE™ Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45 Starting the deploytool. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45 Creating the J2EE™ Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46 Creating the Enterprise Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46 Coding the Enterprise Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46 Coding the Remote Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46 Coding the Home Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .47 Coding the Enterprise Bean Class . . . . . . . . . . . . . . . . . . . . . . . . .47 Compiling the Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48 Packaging the Enterprise Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com v Creating the J2EE™ Application Client . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Coding the J2EE Application Client . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Locating the Home Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Creating an Enterprise Bean Instance . . . . . . . . . . . . . . . . . . . . . . 51 Invoking a Business Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 ConverterClient Source Code . . . . . . . . . . . . . . . . . . . . . . . . 51 Compiling the Application Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Packaging the J2EE Application Client . . . . . . . . . . . . . . . . . . . . . . . . 52 Specifying the Application Client’s Enterprise Bean Reference . . . . . 53 Creating the Web Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Coding the Web Client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Compiling the Web Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Packaging the Web Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Specifying the Web Client’s Enterprise Bean Reference. . . . . . . . . . . 56 Specifying the JNDI Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 Deploying the J2EE™ Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Running the J2EE™ Application Client . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Running the Web Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Modifying the J2EE™ Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Modifying a Class File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Adding a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Modifying the Web Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Modifying a Deployment Setting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Common Problems and Their Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Cannot Start the J2EE Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Naming and Directory Service Port Conflict . . . . . . . . . . . . . . . . 61 Web Service Port Conflict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Compilation Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Ant Cannot Locate the Build File . . . . . . . . . . . . . . . . . . . . . . . . . 61 The Compiler Cannot Resolve Symbols. . . . . . . . . . . . . . . . . . . . 62 J2EE Application Client Runtime Errors. . . . . . . . . . . . . . . . . . . . . . . 62 The Client Cannot Find ConverterApp.ear. . . . . . . . . . . . . . . . . . 62 The Client Cannot Find the ConverterClient Component. . . . . . . 62 The Login Failed. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 The J2EE Application Has Not Been Deployed . . . . . . . . . . . . . . 63 The JNDI Name is Incorrect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Web Client Runtime Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 The Web Context in the URL is Incorrect . . . . . . . . . . . . . . . . . . 63 The J2EE Application Has Not Been Deployed . . . . . . . . . . . . . . 63 The JNDI Name is Incorrect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Deployment Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com vi Chapter 3: Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65 A Session Bean Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 Session Bean Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 The SessionBean Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . .68 The ejbCreate Methods. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68 Business Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69 Home Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 Remote Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71 Helper Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72 State Management Modes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72 Stateful Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72 Stateless Session Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73 Choosing Between Stateful and Stateless Session Beans . . . . . . . . . . .73 The Life Cycle of a Session Bean. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74 The Stateful Session Bean Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . .74 The Stateless Session Bean Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . .75 Other Enterprise Bean Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76 Accessing Environment Entries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .76 Comparing Enterprise Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .77 Passing an Enterprise Bean’s Object Reference . . . . . . . . . . . . . . . . . .78 Chapter 4: Entity Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81 Characteristics of Entity Beans. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82 Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82 Bean-Managed Persistence. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .82 Container-Managed Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . .82 Shared Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83 Primary Key. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com vii A Bean-Managed Persistence Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Entity Bean Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 The EntityBean Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 The ejbCreate Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 The ejbPostCreate Method . . . . . . . . . . . . . . . . . . . . . . . . . . 86 The ejbRemove Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 The ejbLoad and ejbStore Methods . . . . . . . . . . . . . . . . . . . 87 The Finder Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 The Business Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 Database Calls. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Home Interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Remote Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Tips for Running the AccountEJB Example . . . . . . . . . . . . . . 94 Setting Up the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 Running the New Enterprise Bean Wizard. . . . . . . . . . . . . . . . . . 95 Deploying the J2EE Application. . . . . . . . . . . . . . . . . . . . . . . . . . 95 Running the J2EE Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Mapping Table Relationships For Bean-Managed Persistence. . . . . . . . . . 96 One-to-One Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Tips for Running the StorageBinEJB Example . . . . . . . . . . . . 99 One-to-Many Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 A Helper Class for the Child Table. . . . . . . . . . . . . . . . . . . . . . . 101 Tips for Running the OrderEJB Example: . . . . . . . . . . . . . . . 104 An Entity Bean for the Child Table . . . . . . . . . . . . . . . . . . . . . . 104 Tips for Running the SalesRepEJB Example: . . . . . . . . . . . . 107 Many-to-Many Relationships. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Tips for running the EnrollerEJB example: . . . . . . . . . . . . . . . 110 About Container-Managed Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 A Container-Managed Persistence Example. . . . . . . . . . . . . . . . . . . . . . . 111 Primary Key Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Creating a Primary Key Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Class Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Bean-Managed Persistence and the Primary Key Class. . . . . . . . . . . 113 Container-Managed Persistence and the Primary Key Class. . . . . . . 114 Getting the Primary Key. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 The Life Cycle of an Entity Bean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 Chapter 5: Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Web Component Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com viii Packaging Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122 Creating a WAR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122 Adding a WAR to a J2EE Application . . . . . . . . . . . . . . . . . . . . . . . .123 Adding a Web Component to a WAR . . . . . . . . . . . . . . . . . . . . . . . . .123 Configuring Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .124 Application-Level Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . .125 Context Root. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125 WAR-Level Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125 Context Parameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125 References to Environment Entries, Enterprise Beans, Resource En- vironment Entries, or Resources. . . . . . . . . . . . . . . . . . . . . . . . . .126 Event Listeners . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 Error Mapping. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126 Filter Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .127 Component-Level Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . .128 Initialization Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128 Specifying an Alias Path. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128 Deploying Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 Executing Web Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 Updating Web Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129 Chapter 6: Java Servlet Technology . . . . . . . . . . . . . . . . . . . . . .133 What is a Servlet? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .134 The Example Servlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .135 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 Servlet Life Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139 Monitoring Servlet Life Cycle Events . . . . . . . . . . . . . . . . . . . . . . . . .140 Defining The Listener Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . .140 Specifying Event Listener Classes . . . . . . . . . . . . . . . . . . . . . . . .141 Handling Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 Sharing Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 Scope Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .142 Controlling Concurrent Access to Shared Resources . . . . . . . . . . . . .143 Initializing a Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145 Writing Service Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .145 Getting Information From Requests . . . . . . . . . . . . . . . . . . . . . . . . . .146 Constructing Responses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148 Filtering Requests and Responses. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151 Defining the Filter Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152 Specifying Filter Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com ix Invoking Other Web Resources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Including the Content of Another Resource in the Response . . . . . . 157 Transferring a Control to Another Web Component . . . . . . . . . . . . . 158 Accessing the Web Context. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 Maintaining Client State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Accessing a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Associating Attributes with a Session . . . . . . . . . . . . . . . . . . . . . . . . 161 Notifying Objects That Are Added To a Session . . . . . . . . . . . . 161 Session Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Session Tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Finalizing a Servlet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Tracking Service Requests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 Providing a Clean Shutdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 Creating Polite Long-Running Methods . . . . . . . . . . . . . . . . . . . . . . 166 Chapter 7: JavaServer Pages™ Technology. . . . . . . . . . . . . . . 167 What is a JSP Page?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 The Example JSP Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 The Life Cycle of a JSP Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 Translation and Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Buffering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Handling Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 Initializing and Finalizing a JSP Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 Creating Static Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 Creating Dynamic Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Using Objects Within JSP Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Implicit Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Application-Specific Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 Shared Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 JSP Scripting Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Declarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Scriptlets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 Including Content in a JSP Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Transferring Control to Another Web Component . . . . . . . . . . . . . . . . . . 186 Param Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Including an Applet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Extending the JSP Language. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com x Chapter 8: JavaBeans™ Components in JSP™ Pages . . . . . . .191 JavaBeans Component Design Conventions. . . . . . . . . . . . . . . . . . . . . . . .192 Why Use a JavaBeans Component? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .193 Creating and Using a JavaBeans Component . . . . . . . . . . . . . . . . . . . . . . .194 Setting JavaBeans Component Properties. . . . . . . . . . . . . . . . . . . . . . . . . .194 Retrieving JavaBeans Component Properties. . . . . . . . . . . . . . . . . . . . . . .197 Chapter 9: Custom Tags in JSP™ Pages . . . . . . . . . . . . . . . . . . .199 What is a Custom Tag? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200 The Example Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200 Using Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204 Declaring Tag Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204 Types of Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Simple Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Tags With Attributes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .205 Tags With Bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .207 Choosing Between Passing Information as Attributes or Body . .207 Tags That Define Scripting Variables . . . . . . . . . . . . . . . . . . . . .207 Cooperating Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .208 Defining Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .208 Tag Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .209 Tag Library Descriptors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210 Listener Element. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211 Tag Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211 Simple Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212 Tag Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212 Body-content Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213 Tags With Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .213 Defining Attributes in a Tag Handler . . . . . . . . . . . . . . . . . . . . . .213 Attribute Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214 Attribute Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215 Tags With Bodies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216 Tag Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216 Body-content Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 Tags That Define Scripting Variables . . . . . . . . . . . . . . . . . . . . . . . . .218 Tag Handlers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .218 Providing Information About the Scripting Variable . . . . . . . . . .219 Cooperating Tags. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... 315 315 315 315 316 316 316 316 316 317 317 317 317 317 318 318 319 319 319 320 320 320 3 21 Glossary 323 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com xvi Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Preface T HE Java Tutorial has been an indispensable resource for many programmers learning the Java programming... Version - http://www.simpopdf.com xix and the J2SE 1. 3 from: http://java.sun.com/j2se /1. 3/ The examples are distributed with a configuration file for ant version 1. 2 A portable make tool, ant is hosted by the Jakarta project at the Apache Software Foundation You can download ant from: http://jakarta.apache.org/builds/jakarta-ant/release/v1.2/bin To build the examples: 1 Download and install the J2SE SDK 1. 3,... J2EE SDK 1. 3, and ant 2 The installation instructions for the J2SE SDK, J2EE SDK, and ant explain how to set the required environment variables Verify that the environment variables have been set to the values noted in the following table Environment Variable Value JAVA_HOME The location of the J2SE SDK installation J2EE_HOME The location of the J2EE SDK installation ANT_HOME The location of the ant... Should include the bin directory of the J2EE SDK and ant installation 3 Go to the examples/src directory 4 Open the file build.xml in a text editor Edit the line: and set the value of the j2ee-home property to be the J2EE SDK installation directory 5 Execute ant target For example, to build all the examples, execute ant all or to build the web layer... This tutorial hopes to serve the same role for developers encountering the Java™ 2 Platform, Enterprise Edition (J2EE™) for the first time It follows an example-oriented focus similar to the Java Tutorial Who Should Use This Tutorial xvii About the Examples xviii Related Information xx How to Print This Tutorial xxi Typographical Conventions xxi Acknowledgments xxii Who Should Use This Tutorial This tutorial. .. Version - http://www.simpopdf.com xviii About the Examples This tutorial includes many complete, working examples Prerequisites for the Examples To understand the examples you will need a good knowledge of the Java programming language, SQL, and relational database concepts The following topics in the Java Tutorial are particularly relevant: Topic Java Tutorial JDBC™ http://java.sun.com/docs/books /tutorial/ jdbc... http://java.sun.com/docs/books /tutorial/ essential/threads JavaBeans™ http://java.sun.com/docs/books /tutorial/ javabeans Security http://java.sun.com/docs/books /tutorial/ security1.2 Where to Find the Examples If you are viewing this online, and you want to build and run the examples, you need to download the tutorial bundle from: http://java.sun.com/j2ee/download.html #tutorial Once you have installed the bundle, the example... is in the examples/src directory, with subdirectories ejb for enterprise bean technology examples, web for web technology examples, and connector for connector technology examples How to Build and Run the Examples This tutorial documents the J2EE SDK version 1. 3 To build, deploy, and run the examples you need a copy of the J2EE SDK 1. 3 and the J2SE™ SDK 1. 3, also known as a JDK You can download the J2EE... deploying J2EE applications It covers the main component technologies comprising the J2EE platform and describes how to develop J2EE components and deploy them on the J2EE SDK This tutorial is not intended for J2EE server or tools vendors It does not explain how to implement the J2EE architecture, nor does it explain the internals of the J2EE SDK The J2EE specifications describe the J2EE architecture and can... Unregistered Version - http://www.simpopdf.com xxii Acknowledgments The J2EE tutorial team would like to thank the J2EE SDK team for their technical advice and enthusiasm We would also like to thank our manager Jim Inscore for his support and steadying influence The chapters on web components use an example and some material that first appeared in the servlet trail of the Java Tutorial The chapter on custom . . . . . . . . . 11 3 Bean-Managed Persistence and the Primary Key Class. . . . . . . . . . . 11 3 Container-Managed Persistence and the Primary Key Class. . . . . . . 11 4 Getting the Primary Key exam- ples. How to Build and Run the Examples This tutorial documents the J2EE SDK version 1. 3. To build, deploy, and run the examples you need a copy of the J2EE SDK 1. 3 and the J2SE™ SDK 1. 3,. from: http://jakarta.apache.org/builds/jakarta-ant/release/v1.2/bin To build the examples: 1. Download and install the J2SE SDK 1. 3, J2EE SDK 1. 3, and ant. 2. The installation instructions for the J2SE SDK, J2EE SDK,

Ngày đăng: 13/08/2014, 08:21