J2EE tutorial for beginners

j2ee tutorial for beginners and j2ee tutorial for beginners eclipse, j2ee session management tutorial, j2ee spring mvc tutorial, j2ee tutorial transactions, j2ee technologies tutorial pdf free download
Dr.DouglasPatton Profile Pic
Dr.DouglasPatton,United States,Teacher
Published Date:26-07-2017
Your Website URL(Optional)
Comment
TM THE J2EE TutorialTM THE J2EE Tutorial Stephanie Bodoff Dale Green Kim Haase Eric Jendrock Monica Pawlan Beth Stearns Boston • San Francisco • New York • Toronto • Montreal London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico CityCopyright © 2002 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, CA 94303 USA. All rights reserved. Duke logo™ designed by Joe Palrang. Sun, Sun Microsystems, Sun logo, Java, JDBC, 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 Company, Ltd. . 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 TYPO- GRAPHICAL ERRORS. CHANGES ARE PERIODICALLY ADDED TO THE INFORMA- TION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW EDITIONS OF THE PUBLICATION. SUN MICROSYSTEMS, INC., MAY MAKE IMPROVEMENTS AND/OR CHANGES IN ANY TECHNOLOGY, PRODUCT, OR PROGRAM DESCRIBED IN THIS PUBLICATION AT ANY TIME. Pearson Education Corporate Sales Division One Lake Street Upper Saddle River, NJ 07458 (800) 382-3419 corpsalespearsontechgroup.com Visit Addison-Wesley on the Web: www.aw.com/cseng/ Library of Congress Control Number: 2002102527 All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. Published simultaneously in Canada. ISBN 0-201-79168-4 Text printed on recycled paper 1 2 3 4 5 6 7 8 9 10—MA—0605040302 First printing, March 2002Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xxi Chapter 1: Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Distributed Multitiered Applications 2 J2EE Components 3 J2EE Clients 4 Web Components 6 Business Components 6 Enterprise Information System Tier 8 J2EE Containers 8 Container Services 8 Container Types 9 Packaging 10 Development Roles 11 J2EE Product Provider 12 Tool Provider 12 Application Component Provider 12 Application Assembler 13 Application Deployer and Administrator 14 Reference Implementation Software 14 Database Access 15 J2EE APIs 15 Simplified Systems Integration 18 Tools 19 vvi CONTENTS Chapter 2: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21 Setting Up 22 Getting the Example Code 22 Getting the Build Tool (ant) 22 Checking the Environment Variables 23 Starting the J2EE Server 23 Starting the deploytool 23 Creating the J2EE Application 24 Creating the Enterprise Bean 24 Coding the Enterprise Bean 24 Compiling the Source Files 26 Packaging the Enterprise Bean 26 Creating the J2EE Application Client 28 Coding the J2EE Application Client 28 Compiling the Application Client 31 Packaging the J2EE Application Client 31 Specifying the Application Client’s Enterprise Bean Reference 32 Creating the Web Client 32 Coding the Web Client 32 Compiling the Web Client 34 Packaging the Web Client 34 Specifying the Web Client’s Enterprise Bean Reference 35 Specifying the JNDI Names 35 Deploying the J2EE Application 37 Running the J2EE Application Client 37 Running the Web Client 38 Modifying the J2EE Application 39 Modifying a Class File 39 Adding a File 39 Modifying the Web Client 39 Modifying a Deployment Setting 40 Common Problems and Their Solutions 40 Cannot Start the J2EE Server 40 Compilation Errors 41 Deployment Errors 42 J2EE Application Client Runtime Errors 43 Web Client Runtime Errors 44 Detecting Problems With the Verifier Tool 45 Comparing Your EAR Files with Ours 45 When All Else Fails 45CONTENTS vii Chapter 3: Enterprise Beans . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 What Is an Enterprise Bean? 48 Benefits of Enterprise Beans 48 When to Use Enterprise Beans 49 Types of Enterprise Beans 49 What Is a Session Bean? 49 State Management Modes 50 When to Use Session Beans 51 What Is an Entity Bean? 51 What Makes Entity Beans Different from Session Beans? 52 Container-Managed Persistence 53 When to Use Entity Beans 56 What Is a Message-Driven Bean? 56 What Makes Message-Driven Beans Different from Session and Entity Beans? 57 When to Use Message-Driven Beans 57 Defining Client Access with Interfaces 58 Remote Access 58 Local Access 59 Local Interfaces and Container-Managed Relationships 59 Deciding on Remote or Local Access 60 Performance and Access 61 Method Parameters and Access 61 The Contents of an Enterprise Bean 62 Naming Conventions for Enterprise Beans 62 The Life Cycles of Enterprise Beans 63 The Life Cycle of a Stateful Session Bean 63 The Life Cycle of a Stateless Session Bean 64 The Life Cycle of an Entity Bean 65 The Life Cycle of a Message-Driven Bean 67 Chapter 4: A Session Bean Example . . . . . . . . . . . . . . . . . . . . . 69 The CartEJB Example 70 Session Bean Class 70 Home Interface 74 Remote Interface 76 Helper Classes 76 Running the CartEJB Example 76 Other Enterprise Bean Features 78 Accessing Environment Entries 78 Comparing Enterprise Beans 80 Passing an Enterprise Bean’s Object Reference 80viii CONTENTS Chapter 5: Bean-Managed Persistence Examples. . . . . . . . . .83 The SavingsAccountEJB Example 84 Entity Bean Class 84 Home Interface 94 Remote Interface 96 Running the SavingsAccountEJB Example 97 deploytool Tips for Entity Beans with Bean-Managed Persistence 99 Mapping Table Relationships for Bean-Managed Persistence 99 One-to-One Relationships 99 One-to-Many Relationships 103 Many-to-Many Relationships 110 Primary Keys for Bean-Managed Persistence 113 The Primary Key Class 113 Primary Keys in the Entity Bean Class 115 Getting the Primary Key 116 Handling Exceptions 116 Chapter 6: Container-Managed Persistence Examples . . . .119 Overview of the RosterApp Application 120 The PlayerEJB Code 121 Entity Bean Class 122 Local Home Interface 126 Local Interface 127 A Guided Tour of the RosterApp Settings 128 RosterApp 128 RosterClient 129 RosterJAR 130 TeamJAR 131 Method Invocations in RosterApp 136 Creating a Player 137 Adding a Player to a Team 138 Removing a Player 139 Dropping a Player from a Team 140 Getting the Players of a Team 141 Getting a Copy of a Team’s Players 142 Finding the Players by Position 144 Getting the Sports of a Player 145 Running the RosterApp Example 147 Setting Up 147 Deploying the Application 147 Running the Client 148CONTENTS ix deploytool Tips for Entity Beans with Container-Managed Persistence 148 Specifying the Bean’s Type 148 Selecting the Persistent Fields and Abstract Schema Name 149 Defining EJB QL Queries for Finder and Select Methods 149 Generating SQL and Specifying Table Creation 149 Specifying the Database JNDI Name, User Name, and Password 150 Defining Relationships 150 Primary Keys for Container-Managed Persistence 151 The Primary Key Class 151 Primary Keys in the Entity Bean Class 152 Generating Primary Key Values 153 Chapter 7: A Message-Driven Bean Example . . . . . . . . . . . . 155 Example Application Overview 156 The J2EE Application Client 157 The Message-Driven Bean Class 157 The onMessage Method 158 The ejbCreate and ejbRemove Methods 159 Running the SimpleMessageEJB Example 159 Starting the J2EE Server 159 Creating the Queue 159 Deploying the Application 159 Running the Client 160 deploytool Tips for Message-Driven Beans 160 Specifying the Bean’s Type and Transaction Management 161 Setting the Message-Driven Bean’s Characteristics 161 deploytool Tips for JMS Clients 162 Setting the Resource References 162 Setting the Resource Environment References 163 Specifying the JNDI Names 163 Chapter 8: Enterprise JavaBeans Query Language . . . . . . . 165 Terminology 166 Simplified Syntax 167x CONTENTS Example Queries 167 Simple Finder Queries 167 Finder Queries That Navigate to Related Beans 169 Finder Queries with Other Conditional Expressions 170 Select Queries 172 Full Syntax 173 BNF Symbols 173 BNF Grammar of EJB QL 173 FROM Clause 176 Path Expressions 179 WHERE Clause 182 SELECT Clause 190 EJB QL Restrictions 191 Chapter 9: Web Clients and Components. . . . . . . . . . . . . . . .193 Web Client Life Cycle 194 Web Application Archives 196 Creating a WAR File 197 Adding a WAR File to an EAR File 197 Adding a Web Component to a WAR File 198 Configuring Web Clients 199 Application-Level Configuration 199 WAR-Level Configuration 200 Component-Level Configuration 202 Deploying Web Clients 203 Running Web Clients 203 Updating Web Clients 204 Internationalizing Web Clients 206 Chapter 10: Java Servlet Technology . . . . . . . . . . . . . . . . . . . .209 What Is a Servlet? 210 The Example Servlets 211 Troubleshooting 215 Servlet Life Cycle 216 Handling Servlet Life-Cycle Events 216 Handling Errors 218 Sharing Information 218 Using Scope Objects 219 Controlling Concurrent Access to Shared Resources 220 Accessing Databases 221 Initializing a Servlet 222CONTENTS xi Writing Service Methods 222 Getting Information from Requests 223 Constructing Responses 225 Filtering Requests and Responses 227 Programming Filters 229 Programming Customized Requests and Responses 230 Specifying Filter Mappings 232 Invoking Other Web Resources 234 Including Other Resources in the Response 234 Transferring Control to Another Web Component 236 Accessing the Web Context 237 Maintaining Client State 238 Accessing a Session 238 Associating Attributes with a Session 238 Session Management 239 Session Tracking 240 Finalizing a Servlet 241 Tracking Service Requests 242 Notifying Methods to Shut Down 242 Creating Polite Long-Running Methods 243 Chapter 11: JavaServer Pages Technology . . . . . . . . . . . . . . . 245 What Is a JSP Page? 246 The Example JSP Pages 249 The Life Cycle of a JSP Page 253 Translation and Compilation 253 Execution 254 Initializing and Finalizing a JSP Page 256 Creating Static Content 257 Creating Dynamic Content 257 Using Objects within JSP Pages 257 JSP Scripting Elements 260 Including Content in a JSP Page 263 Transferring Control to Another Web Component 265 Param Element 265 Including an Applet 265 Extending the JSP Language 267 Chapter 12: JavaBeans Components in JSP Pages. . . . . . . . . 269 JavaBeans Component Design Conventions 270 Why Use a JavaBeans Component? 271xii CONTENTS Creating and Using a JavaBeans Component 272 Setting JavaBeans Component Properties 273 Retrieving JavaBeans Component Properties 275 Chapter 13: Custom Tags in JSP Pages . . . . . . . . . . . . . . . . . . .279 What Is a Custom Tag? 280 The Example JSP Pages 281 Using Tags 285 Declaring Tag Libraries 285 Types of Tags 286 Defining Tags 289 Tag Handlers 289 Tag Library Descriptors 290 Simple Tags 293 Tags with Attributes 294 Tags With Bodies 296 Tags That Define Scripting Variables 298 Cooperating Tags 302 Examples 304 An Iteration Tag 304 A Template Tag Library 308 How Is a Tag Handler Invoked? 313 Chapter 14: Transactions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .315 What Is a Transaction? 316 Container-Managed Transactions 316 Transaction Attributes 317 Rolling Back a Container-Managed Transaction 321 Synchronizing a Session Bean’s Instance Variables 322 Methods Not Allowed in Container-Managed Transactions 323 Bean-Managed Transactions 323 JDBC Transactions 324 JTA Transactions 325 Returning without Committing 326 Methods Not Allowed in Bean-Managed Transactions 327 Summary of Transaction Options for Enterprise Beans 327 Transaction Timeouts 328 Isolation Levels 328 Updating Multiple Databases 329 Transactions in Web Components 331CONTENTS xiii Chapter 15: Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Overview 334 Security Roles 335 Declaring and Linking Role References 335 Mapping Roles to J2EE Users and Groups 337 Web-Tier Security 337 Protecting Web Resources 337 Controlling Access to Web Resources 338 Authenticating Users of Web Resources 338 Using Programmatic Security in the Web Tier 340 Unprotected Web Resources 340 EJB-Tier Security 340 Declaring Method Permissions 341 Using Programmatic Security in the EJB Tier 341 Unprotected EJB-Tier Resources 342 Application Client-Tier Security 342 Specifying the Application Client’s Callback Handler 343 EIS-Tier Security 343 Configuring Sign-On 344 Container-Managed Sign-On 344 Component-Managed Sign-On 344 Configuring Resource Adapter Security 345 Propagating Security Identity 346 Configuring a Component’s Propagated Security Identity 346 Configuring Client Authentication 347 J2EE Users, Realms, and Groups 348 Managing J2EE Users and Groups 349 Setting Up a Server Certificate 350 Chapter 16: Resource Connections . . . . . . . . . . . . . . . . . . . . . 353 JNDI Names and Resource References 354 deploytool Tips for Resource References 354 Database Connections for Enterprise Beans 357 Coded Connections 357 Connection Pooling 359 Mail Session Connections 359 Running the ConfirmerEJB Example 361 URL Connections 362 Running the HTMLReaderEJB Example 363xiv CONTENTS Chapter 17: J2EE Connector Architecture. . . . . . . . . . . . . . . . .365 About Resource Adapters 366 Resource Adapter Contracts 366 Administering Resource Adapters 368 The Black Box Resource Adapters 369 Transaction Levels 369 Properties 370 Configuring JDBC Drivers 371 Resource Adapter Tutorial 372 Setting Up 372 Deploying the Resource Adapter 372 Testing the Resource Adapter 373 Common Client Interface 375 Overview of the CCI 375 Programming with the CCI 376 Writing a CCI Client 385 CCI Tutorial 386 Chapter 18: The Duke’s Bank Application. . . . . . . . . . . . . . . . .391 Enterprise Beans 393 Session Beans 394 Entity Beans 397 Helper Classes 397 Database Tables 398 Protecting the Enterprise Beans 400 Application Client 400 The Classes and Their Relationships 401 BankAdmin Class 403 EventHandle Class 404 DataModel Class 405 Web Client 408 Design Strategies 409 Web Client Life Cycle 410 Protecting the Web Resources 414 Internationalization 414CONTENTS xv Building, Packaging, Deploying, and Running the Application 416 Adding Groups and Users to the Realm 416 Starting the J2EE Server, deploytool, and Database 417 Compiling the Enterprise Beans 418 Packaging the Enterprise Beans 418 Compiling the Web Client 419 Packaging the Web Client 419 Compiling the J2EE Application Client 419 Packaging the J2EE Application Client 419 Packaging the Enterprise Archive File 420 Opening the Enterprise Archive File 420 Reviewing JNDI Names 420 Mapping the Security Roles to Groups 423 Deploying the Duke’s Bank Application 423 Creating the Bank Database 424 Running the J2EE Application Client 424 Running the Web Client 425 Appendix A: HTTP Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427 HTTP Requests 428 HTTP Responses 428 Appendix B: J2EE SDK Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429 J2EE Administration Tool 430 Cleanup Tool 431 Cloudscape Server 431 Starting Cloudscape 432 Stopping Cloudscape 432 Running the Interactive SQL Tool 432 Cloudscape Server Configuration 433 Deployment Tool 434 J2EE Server 435 Key Tool 435 Packager Tool 436 EJB JAR File 436 Web Application WAR File 437 Application Client JAR File 437 J2EE Application EAR File 438 Specifying the Runtime Deployment Descriptor 438 Resource Adapter RAR File 439xvi CONTENTS Realm Tool 440 Examples 440 runclient Script 441 Syntax 441 Example 442 Accessing a Remote Server 442 Preventing the User Name and Password Prompts 443 Verifier Tool 443 Command-Line Verifier 443 Stand-Alone GUI Verifier 444 Appendix C: Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .445 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .449 About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . .473 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .475Foreword I joined Sun—actually, a small Sun spin-off called FirstPerson—in August 1993. I knew about the company because a few of my favorite coworkers had left NeXT to work at FirstPerson. But my main reason for joining was that I loved the cartoony user interfaces FirstPerson was developing, interfaces that featured 1 a character nicknamed Duke. Figure F–1 Duke, the Unofficial Mascot of the Java™ Platform FirstPerson’s first demo, called Star 7, was a household remote control with a small touchscreen. By the time I arrived, they were working on a demo for video on demand. The wonderfully loony animation for the video-on-demand demo was created by a San Francisco studio called Colossal Pictures (where, incidentally, my husband had gotten his start in the animation industry). Both demos were written using a programming language that was then called Oak. My first task was to help the creator of the Oak language, James Gosling, write the language specification. What I really wanted to do, though, was to write task- oriented documentation aimed at ordinary programmers. 1 You can get more information about Duke in the article “It’s Duke’s Birthday, Too”: http://java.sun.com/features/1999/05/duke.html. xviixviii FOREWORD By July 1994, FirstPerson was in turmoil, having failed to convince cable com- panies that their video-on-demand solution was what customers needed. I stayed at the company only because I was about to go on maternity leave. Programming for the Internet When I returned to work in the fall of 1994, the company’s dynamic and vision had completely changed. They had decided that the Oak language—with its abil- ity to produce platform-independent, secure, easily transported code—was ideal for the Internet. And they were creating a Web browser called WebRunner that showcased the ability to deliver Oak code, packaged in a form they called applets, over the Internet. I set to work writing a guide to help people write and use applets. When the WebRunner browser was first released in early 1995, the guide was part of the small set of documentation included with the browser. That guide was the grand- daddy of The J2EE™ Tutorial. The guide was the first documentation to include applets. It looked somewhat similar to The Java™ Tutorial, and in fact The Java™ Tutorial probably still has some of the text originally published in the guide. Because we had no HTML tools, however, I had to generate the guide completely by hand. Let me tell you, hand coding navigation links for a document in progress is not fun, even for a small document. Much less painful was making name changes: The language name changed from Oak to Java™, and the name of the browser from WebRun- ner to HotJava. Mary Enters the Picture In early 1995, we hired a contract writer named Mary Campione. She and I knew of each other from her time in NeXT developer support. Mary’s job was to help programmers use platform features such as threads. We soon realized that our work was too similar for us to do it separately, and we started working together on a programmer’s guide for the Java platform. On May 18, 1995, Mary Campione and I released the first version of our guide, which we called The Java™ Programmer’s Guide. It was an incomplete first draft—nothing pretty—but it provided people with the information they needed to get started programming for the Java platform.FOREWORD xix The next week, Sun officially announced the Java platform at a show called Sun- World. The best part of the show for us was the announcement that Netscape had agreed (just hours before) to support applets in their Web browser. In the following months, Mary and I continued to add to and refine our program- 2 mer’s guide. We worked together closely, sharing the same office and even the same train commute from San Francisco to Palo Alto. By coincidence, we even got pregnant within days of each other. By late 1995, the first wave of books in The Java Series was being developed. The Java Series was a group of books published by Addison-Wesley and written mainly by employees of what used to be FirstPerson. By that time, FirstPerson had been absorbed back into Sun, in the form of a division called JavaSoft. The 3 Series Editor was JavaSoft technical publications manager Lisa Friendly. Our programmer’s guide was slated to be one of the books in The Java Series, but the publisher wanted it to have a less intimidating name. So we changed its name to The Java™ Tutorial. There we were, two increasingly large women working insanely long hours to finish the book before the babies arrived in mid- 1996. We managed—just barely—to get the book to our publisher in time. We couldn’t have done it without the help of yet another ex-NeXTer, Randy Nelson, who took care of all the final details of the book and Web site. The Tutorial Team Grows When Mary and I returned from maternity leave, we felt completely over- whelmed. Our book and Web site covered the 1.0 version of the Java platform (JDK 1.0), but JDK 1.1 was scheduled to be released soon and work had already started on JDK 1.2 (which would be renamed to the Java 2 Platform, Standard Edition, Version 1.2—J2SE™ v 1.2, for short). We would be able to update our existing documentation to 1.1, but for 1.2 we’d need help. Help arrived in the form of guest authors and Alison Huml. The guest authors were writers and engineers on the teams developing the new 1.2 features. Alison was a postgraduate student with experience in both software and publishing. She did whatever was necessary to make the Tutorial succeed, ranging from produc- ing camera-ready copy for books to writing text and examples. 2 By looking at http://java.sun.com/docs/books/tutorial/information/his- tory.html, you can see what was in each of our updates. 3 Lisa has some great anecdotes about the early days of FirstPerson. You can read some of them athttp://java.sun.com/features/1998/05/birthday.html.xx FOREWORD Between 1998 and 2000, the Tutorial team updated the Web site many times and produced two completely new books, as well as two major revisions of the origi- nal book. In mid-2000, Mary retired from paid work. Alison and I still work on The Java™ Tutorial, in both its Web and book forms. Although we rely on guest authors from time to time, the rate of change has become less frantic as the J2SE platform matures. The J2EE Tutorial Now there’s a new platform—and a new tutorial—in town. The success of the Java 2 Platform, Enterprise Edition (J2EE™) has been phenomenal. Developers are clamoring for information about how to write applications using this new Java platform for the server. And this book helps, continuing the tradition of The Java™ Tutorial, but this time for the J2EE platform. Like the original Tutorial, this is an example-filled, easy-to-use entry point and quick reference for pro- gramming with the J2EE platform. And I’m sure, like the original tutorial team, Stephanie, Dale, Eric, Kim, and Beth all have stories to tell about the time they’ve spent working on the J2EE platform and bringing you this book. Just a note—Because the J2EE platform sits on top of the J2SE platform, you need to be comfortable writing programs for the J2SE platform before you can make full use of this book. If you’re not comfortable with the J2SE platform, go 4 to The Java™ Tutorial and learn Then come back here, so you can find out all about developing and deploying applications for the J2EE platform. Kathy Walrath Sun Microsystems San Francisco, CA December 21, 2001 4 On the Web at http://java.sun.com/docs/books/tutorial/, or in book form as The Java™ Tutorial: A Short Course on the Basics.

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.