Guide to j2 ee

14
John Hunt and Chris Loftus Guide to J2EE: Enterprise Java Springer

description

a

Transcript of Guide to j2 ee

  • 1. John Hunt and Chris LoftusGuide to J2EE: Enterprise JavaSpringer

2. ContentsPart 1 Background 1WhyJ2EE? 1.1 Introduction 1.2 The Challenges Facing IT Organizations 1.3 Requirements on Enterprise Application Development 1.4 Technological Choices 1.5 Why Choose Java and J2EE? 1.6 A Note of Caution 1.7 Knowing the Technology Is not Enough 1.8 References3 3 4 5 6 7 8 9 102Introduction to Distributed Systems 2.1 What Is a Distributed Application or System? 2.2 Why Build Distributed Programs? 2.3 How Can Java Help With Distribution? 2.4 Distributed Object Systems 2.5 How Can J2EE Help? 2.6 Online Reference11 11 11 14 15 17 183The J2EE Tour 3.1 Introduction 3.2 The J2EE Platform 3.3 J2EE Technology Tour 3.3.1 Communication Services 3.3.2 Horizontal Services 3.3.3 Component Technologies 3.4 References19 19 19 22 23 26 28 30XIII 3. xiv Java and Remote Method Invocation 4.1 Introduction 4.2 Remote Method Invocation 4.2.1 The Remote Interface 4.2.2 Subclassing a Server Class 4.2.3 Running the rmic Compiler 4.2.4 Starting the Registry 4.3 The RMIClient 4.4 Performance 4.5 Passing Parameters 4.6 Online References31 31 31 32 32 36 36 37 38 43 44Activate Yourself! 5.1 Introduction 5.2 Extending RMI 5.3 Implementing an Activatable Server 5.3.1 " Remote Interface 5.3.2 The C1 i ent Class 5.3.3 The Activatable Server 5.3.4 The Server Setup 5.4 Running the Activatable Client-Server 5.5 Summary 5.6 Online Reference45 45 45 46 47 47 48 49 51 53 53JNDI 6.1 Introduction 6.2 What You Need to Get Started 6.3 LDAP 6.3.1 LDAP Data 6.4 What LDAP Can Do 6.5 Using LDAP 6.6 Using JNDI 6.7 Placing Data Into LDAP 6.7.1 The LDAPWrite Application 6.8 JNDI, RMI and LDAP 6.9 Summary55 55 57 57 58 59 60 61 64 64 67 69Java Message Service (JMS) 7.1 Introduction 7.2 Message Servers and JMS 7.2.1 What Is a Message Service? 7.2.2 Why Use a Message Service? 7.2.3 What Is JMS? 7.2.4 JMS API Concepts 7.3 Point to Point Communication 7.4 Publish and Subscribe Communication71 71 71 71 72 72 73 73 74 4. 7.57.67.77.87.9 7.10The JMS API 7.5.1 Connection Factories 7.5.2 Connections 7.5.3 Sessions 7.5.4 Messages 7.5.5 Destinations 7.5.6 Message Producers 7.5.7 Message Consumers Point to Point Application Development Steps 7.6.1 Publish Destinations 7.6.2 Define a Client 7.6.3 Define Clients That Receive Messages 7.6.4 Start Message Server 7.6.5 Compile and Start the Clients Publish and Subscribe Application Development Steps 7.7.1 Publish the Topic 7.7.2 '' Define the Publisher Client 7.7.3 Define the Subscriber Client 7.7.4 Start Message Server 7.7.5 Compile and Start the Clients Additional JMS Features 7.8.1 Specifying Message Persistence 7.8.2 Setting the Message Priority 7.8.3 Defining How Long a Message Lasts 7.8.4 Durable Subscriptions 7.8.5 Topic Message Selectors 7.8.6 Client Authentication 7.8.7 Transactions Summary Online ReferencesJava, IDL and Object Request Brokers 8.1 Introduction 8.2 CORBA 8.3 Java IDL 8.3.1 Java ORB 8.3.2 Java Name Server 8.3.3 Converting IDL to Java 8.3.4 Implementing the Server 8.3.5 Implementing the Client 8.3.6 Compiling the Server and Client 8.3.7 Running the Application 8.3.8 Java IDL and RMI 8.4 Online References75 75 76 76 76 80 80 81 81 83 83 88 89 89 92 92 92 93 94 95 96 96 96 96 97 97 97 97 98 98 99 99 99 100 101 101 102 103 106 107 108 108 108 5. xvtc 9Java Database Connectivity 9.1 Introduction 9.2 What Is JDBC? 9.3 What the Driver Provides 9.4 Registering Drivers 9.5 Opening a Connection 9.6 Obtaining Data From a Database 9.7 Creating a Table 9.8 Applets and Databases 9.9 Batch updates 9.10 Scrollable and Updateable ResultSets 9.10.1 Scrollable ResultSets 9.11 Updateable ResultSets 9.12 JDBC Data Sources 9.13 Connection Pooling 9.14 RowSet Objects 9.15 JDBC Metadata 9.15.1 DatabaseMetaData 9.15.2 ResultSetMetaData 9.16 Online References 9.17 References109 109 110 Ill Ill 112 114 116 117 117 118 119 121 123 125 127 130 130 131 132 13310 XML and Java 10.1 Introduction 10.2 XML Introduced 10.2.1 What is XML? 10.2.2 What Do XML Documents Look Like? 10.2.3 XML Vocabularies 10.2.4 Working With a DTD 10.3 XSL Transformations 10.4 Processing XML 10.5 The JAXP API 10.6 The SAX API 10.7 The DOM API 10.8 Loading an XML Document 10.9 Creating an XML Document in Java 10.10 Performing XSLT in JAX135 135 135 135 136 139 140 143 145 145 146 153 156 161 16411 JavaMail API: the Mail Is in 11.1 Introduction 11.2 The JavaMail API 11.3 Setting up JavaMail 11.4 Sending Email 11.5 Receiving Messages 11.6 Replying to Messages 11.7 Multipart MIME Messages169 169 169 170 170 174 178 181 6. 11.8 11.9 11.10 11.11 11.12 11.13Adding the Reply Text to a Reply Message Forwarding Sending Attachments Sending HTML Summary Online References181 183 183 187 188 188Part 2 EJB Architecture 12 The EJB Architecture 12.1 Introduction 12.2 EJB Server Elements 12.3 EJB Component Elements 12.3.1 Local and Remote Interfaces 12.3.2 The Process of Developing and Deploying EJB Components jn a Nutshell 12.3.3 The EJB Component Classes and Interfaces 12.4 Accessing EJBs From a Java Application Client 12.5 Reference191 191 192 194 195 196 197 200 20213 Stateless Session EJBs 13.1 Introduction 13.2 Stateless Session EJB Life Cycle 13.3 The Process of Developing a Stateless Session EJB 13.4 The Business Logic Interface 13.5 The Life Cycle Interface 13.6 The Component Class 13.6.1 The Session Context Object 13.6.2 Why Doesn't the Component Class "Implement" the Business or Life Cycle Interfaces? 13.7 The Deployment Descriptor Files 13.7.1 T h e e j b - j a r . x m l File 13.7.2 The JBossjboss.xml File 13.8 Deploying the EJB Component 13.9 Accessing the EJB From a Java Application Client203 203 204 207 208 209 210 211 212 213 214 216 216 21714 Entity EJBs: How to Implement a Container-Managed Entity EJB 14.1 Introduction 14.2 Entity EJB Life Cycle 14.3 The Process of Developing an Entity EJB 14.4 The Business Logic Interface 14.5 The Life Cycle Interface 14.5.1 Creator Methods 14.5.2 find Methods 14.5.3 Home Methods 14.5.4 Select Methods221 221 222 227 228 229 231 231 232 232 7. 14.6 14.7Primary Keys and the Primary Key Class The Component Class 14.7.1 The EntityContext Object 14.8 The Deployment Descriptor Files 14.8.1 The ejb-jar.xml File 14.8.2 The JBossjboss.xml File 14.8.3 The JBoss jbosscmp-jdbc.xml File 14.9 The EJB Query Language 14.9.1 Query Language Statements 14.9.2 The Deployment Descriptor 14.10 Accessing the EJB From a Java Application Client 14.11 Container-Managed Relationships 14.11.1 Declaring Container-Managed Relationships in a Component Class 14.11.2 The Relationship Deployment Descriptors 14.11.3 The JBoss jbosscmp-jdbc.xml File 14.12 Reference233 236 241 241 242 244 244 246 246 248 249 252 252 256 258 26015 Gluing EJBs Together 15.1 Introduction 15.2 The Bookstore EJB Interactions 15.3 The Environment Naming Context (ENC) 15.4 Some Design Issues to Consider When Gluing EJBs Together 15.4.1 Session EJBs as Facades 15.4.2 Using JNDI From an EJB 15.4.3 When not to Use Entity EJBs 15.4.4 Compile-Time Checking of the Implementation Class's Conformance to its Business Logic Interface 15.4.5 Improving Performance Through the Use of Bulk Accessor/Updator Methods 15.5 The Cart EJB Listings 15.6 The Timer Service261 261 261 269 273 273 275 27516 Message-Driven EJBs 16.1 Introduction 16.2 Message-Driven EJB Life Cycle 16.3 The Component Class 16.4 The Deployment Descriptor Files 16.4.1 The ejb-jar.xml File 16.4.2 The ejb-jar.xml file (for EJB 2.1) 16.4.3 The JBoss jboss.xml File 16.4.4 The JBoss jboss-destinations-service.xml file 16.4.5 DebugMonitor Connected to a JMS Topic 16.5 Accessing the EJB From Other EJBs291 291 291 293 297 297 299 301 302 302 307277 278 279 288 8. Part 3 Servlets and JSPs 17 Web Applications in Java 17.1 Introduction 17.2 What Are Servlets? 17.3 Web Applications 17.4 Structure of a Web Application 17.5 How Servlets Work 17.6 Why Use Servlets 17.7 The Structure of the Servlet API 17.8 Steps for Developing and Deploying a Web Application 17.9 Starting Tomcat 17.10 A Second Example Servlet 17.11 Should You Use doGet or doPost? 17.12 Tomcat 17.13 Summary 17.14 Online'References 17.15 References321 321 321 322 322 323 324 325 326 331 332 337 338 338 338 33818 Session Management and Life Cycle Monitoring 18.1 Introduction 18.2 Session Management 18.3 Session Tracking 18.3.1 URL Rewriting 18.3.2 Hidden Fields 18.3.3 Secure Sockets Layer Sessions 18.3.4 Cookies 18.3.5 Choosing a Session Tracking Approach 18.4 A Session Example 18.5 More Session Details 18.6 Session State 18.7 Session Life Cycle Monitoring 18.8 Servlet Context 18.9 ServletContext Example 18.10 Servlet Life Cycle Events 18.11 References341 341 341 344 344 345 345 345 346 346 347 349 354 356 358 359 36419 Java Server Pages 19.1 Introduction 19.2 What Is a JSP? 19.3 A Very Simple JSP 19.4 The Components of a JSP 19.4.1 Directives 19.4.2 Actions 19.4.3 Implicit Objects 19.4.4 JSP Scripting365 365 365 367 369 369 370 370 370 9. 19.5 19.6 19.7Making JSPs Interactive Why Use JSPs? Problems With JSPs371 374 37420 JSP Tags and Implicit Objects 20.1 Introduction 20.2 JSP Tags 20.2.1 JSP Directives 20.2.2 Scripting Elements 20.2.3 Actions 20.3 Implicit Objects 20.4 Scope377 377 377 378 380 383 386 38621 JSP Tag Libraries 21.1 Introduction 21.2 Why Use Tag Libraries? 21.3 Key Concepts 21.4 Building a Custom Tag 21.5 The Tag Interface 21.5.1 Other Tag Interfaces and Classes 21.6 Creating a Tag Library 21.6.1 Implement the Tag Handler Class 21.6.2 Define the Tag Library Descriptor 21.6.3 Map the Tag Library 21.6.4 Import the Tag Library 21.6.5 Run the Web Application 21.7 Adding Attributes to a Tag 21.8 Including Body Content 21.9 Guidelines for Developing Tag Libraries 21.10 Introducing Scripting Variables 21.11 Nested Tags 21.12 Tag Validation 21.13 Handling Tag Exceptions 21.14 JSTL 21.15 Summary 21.16 Online References389 389 389 390 391 392 393 394 394 395 396 397 398 398 400 406 407 408 409 409 410 412 41322 Request Dispatching 22.1 Introduction 22.2 Servlet Chaining 22.3 Request Dispatching 22.3.1 The RequestDi spatcher Interface 22.4 Obtaining a RequestDi spatcher 22.4.1 Forwarding Requests 22.4.2 An Example of Forwarding 22.4.3 Including Via Request Dispatching415 415 415 416 418 418 420 420 424 10. 23 Filtering 23.1 Introduction 23.2 Filters - the Very Concept! 23.3 What Can a Filter Do? 23.4 The Filter API 23.5 Implementing a Simple Filter 23.6 The Logging Filter Example 23.7 Wrapping Request and Response Objects 23.8 Filtering XML to Generate HTML431 431 431 432 434 435 439 443 44324 Securing Web Applications 24.1 Introduction 24.2 Traditional Approaches 24.2.1 Use the Web Server 24.2.2 Do-It-Yourself 24.3 Container-Managed Security 24.3.1 Defining Users 24.3.2 Configuring Access to Web Resources 24.3.3 Four Types of Authentication 24.4 Programmatic Security 24.5 JSP Configuration 24.5.1 Enabling and Disabling EL Evaluation 24.5.2 Enabling and Disabling Scripting 24.5.3 Declaring Page Encodings 24.5.4 Defining Implicit Includes 24.6 Conclusion 24.7 Online Reference453 453 453 453 454 455 457 458 460 463 466 467 467 467 468 468 46925 Deployment Configuration 25.1 Introduction 25.2 Context Initialization 25.3 Servlet Initialization 25.4 Servlet Loading 25.5 Session Configuration 25.6 Welcome Pages 25.7 Error Pages 25.8 MIME Mappings 25.9 Distributable Applications 25.10 Deployment Descriptor in J2EE 1.3 25.11 Deploying J2EE Applications in J2SE 1.4471 471 471 472 473 474 475 475 477 478 479 48026 Accessing EJBs from Servlets/JSPs 26.1 Introduction 26.2 Client Access to EJBs 26.3 Accessing EJBs From a Web Application 26.3.1 The Web Archive483 483 483 484 484 11. 26.4 26.5 26.626.3.2 The Enterprise Archive Caching EJB References An Example Summary486 488 489 493Part 4 Additional Technologies 27 Deployment Issues: Transactions 27.1 Introduction 27.2 Transaction Concepts 27.3 Types of Transaction Supported by EJB Servers 27.4 Container-Managed Transactions 27.4.1 The Required Attribute 27.4.2 The NotSupported Attribute 27.4.3 The Supports Attribute 27.4.4 The Requi resNew Attribute 27.4.5" The Mandatory Attribute 27.4.6 The Never Attribute 27.4.7 Transaction Deployment Descriptors 27.5 Bean-Managed Transactions 27.5.1 The UserTransaction Interface 27.5.2 Obtaining and using a UserTransacti on object 27.6 Transaction Isolation Levels 27.6.1 Lock Modes 27.6.2 Specifying Isolation Levels 27.7 Transactions and Exceptions 27.8 Reference497 497 497 501 501 502 502 503 503 503 505 505 507 507 509 510 513 514 515 51528 Deployment Issues: Security 28.1 Introduction 28.2 Security Concepts and Architecture 28.2.1 Authentication 28.2.2 Access Control (Authorization) 28.3 EJB Container-Managed Security 28.3.1 Declarative Security 28.3.2 Programmatic Security 28.3.3 Stakeholder Responsibilities 28.4 Example Use of the Java Authentication and Authorization Service (JAAS) 28.5 Reference517 517 518 520 520 521 521 525 526 526 53129 Bean-Managed Persistence 29.1 Introduction 29.2 The Entity EJB Life Cycle Revisited 29.3 BookltemEJB: the BMP version 29.4 The Deployment Descriptor Files 29.5 Accessing the Book Item BMP Entity From a Client533 533 533 535 546 548 12. 30 Stateful Session EJBs 30.1 Introduction 30.2 Stateful Session EJB Life Cycle 30.3 Rules on Allowable Instance Variables in the Implementation Class 30.4 The Process of Developing a Stateful Session EJB 30.4.1 The Purchase EJB Business Logic Interface 30.4.2 The Purchase EJB Life Cycle Interface 30.4.3 The Purchase EJB Component class 30.5 Transaction Synchronization Using the SessionSynchronization Interface 30.6 The Deployment Descriptor Files559 56031 J2EE 31.1 31.2 31.3 31.4 31.5 31.6 31.7 31.8563 563 564 566 567 568 570 575 576Connector Architecture Introduction Architectural Overview Connection Service Transaction Service Security Service Common Client Interface (CCI) Deploying Resource Adapters Reference549 549 550 552 552 552 553 55432 From Java to SVG 32.1 Introduction 32.2 What is SVG? 32.2.1 Advantages 32.2.2 Disadvantages 32.2.3 Obtaining an SVG Viewer 32.2.4 What Does SVG Look Like? 32.3 Creating SVG Using Java 32.3.1 Using the DOM API 32.3.2 Converting XML to SVG 32.4 Using Batik 32.4.1 SVG Viewer 32.4.2 SVG Rasterizer 32.4.3 SVG Generator: Generating SVG Content from Java Graphics 32.4.4 SwingDraw 32.5 Servlets and JSPs 32.6 Summary 32.7 Online References 32.8 Appendix: SVGCreator.Java577 577 577 577 578 578 579 579 580 581 586 586 586 586 588 589 590 590 59133 Web Services 33.1 Introduction593 593 13. 33.2What Are Web Services? 33.2.1 What Is SOAP? 33.2.2 SOAP With Attachments 33.2.3 WhatlsWSDL? 33.2.4 What Is UDDI? 33.3 What Is Axis? 33.4 An Axis-Based Web Services Client 33.5 Creating a Simple Web Service Driver 33.5.1 Setting up Tomcat for Web Services 33.5.2 Creating a Very Simple Web Service 33.5.3 Configuring a Web Service 33.5.4 Where Is WSDL? 33.6 Java Web Services Development Pack 33.7 SOAP with Attachments API for Java 33.8 Web Services and J2EE 33.9 Summary 33.10 Reference593 594 595 596 597 598 599 602 602 603 605 607 610 612 612 616 616Part 5 Design 34 J2EE Patterns 34.1 Introduction 34.2 The Motivation Behind Patterns 34.3 Design Patterns 34.3.1 What Are Design Patterns? 34.3.2 What They Are Not 34.3.3 Architectural Patterns 34.3.4 Documenting Patterns 34.3.5 When to Use Patterns 34.3.6 Strengths and Limitations of Design Patterns 34.4 What Are J2EE Design Patterns? 34.5 A Catalog of J2EE Patterns 34.6 The FrontController Pattern 34.6.1 Context 34.6.2 Problem 34.6.3 Forces 34.6.4 Solution 34.6.5 Strategies 34.6.6 Consequences 34.6.7 Related Patterns 34.7 The Request-Event-Dispatcher Pattern 34.7.1 Context 34.7.2 Problem 34.7.3 Forces 34.7.4 Solution 34.7.5 Strategies619 619 620 621 621 621 622 622 623 623 624 625 626 626 626 626 627 628 628 628 629 629 629 629 629 631 14. *--*n*fr-***! -r - -34.7.6 Consequences 34.7.7 Related Patterns 34.8 J2EE-based Model-View-Controller 34.8.1 Context 34.8.2 Problem 34.8.3 Forces 34.8.4 Solution 34.8.5 Strategies 34.8.6 Consequences 34.8.7 Related Patterns 34.9 Summary 34.10 Further Reading 34.11 ReferencesSWHfc 633 633 634 634 634 634 634 636 637 637 638 638 63835 The Fault Tracker J2EE Case Study 35.1 Introduction 35.2 The Fault Tracker Application 35.2.1 Requests for Change 35.2.2 Problem Reporting 35.3 Using the Fault Tracker 35.4 The Design of the Fault Tracker 35.4.1 What Is the Architecture? 35.5 Summary and Conclusions641 641 641 642 643 644 649 649 656Index659