You are previewing Java Enterprise Best Practices.
O'Reilly logo
Java Enterprise Best Practices

Book Description

Java developers typically go through four "stages" in mastering Java. In the first stage, they learn the language itself. In the second stage, they study the APIs. In the third stage, they become proficient in the environment. It is in the fourth stage --"the expert stage"-- where things really get interesting, and Java Enterprise Best Practices is the tangible compendium of experience that developers need to breeze through this fourth and final stage of Enterprise Java mastery. Crammed with tips and tricks, Java Enterprise Best Practices distills years of solid experience from eleven experts in the J2EE environment into a practical, to-the-point guide to J2EE. Java Enterprise Best Practices gives developers the unvarnished, expert-tested advice that the man pages don't provide--what areas of the APIs should be used frequently (and which are better avoided); elegant solutions to problems you face that other developers have already discovered; what things you should always do, what things you should consider doing, and what things you should never do--even if the documentation says it's ok. Until Java Enterprise Best Practices, Java developers in the fourth stage of mastery relied on the advice of a loose-knit community of fellow developers, time-consuming online searches for examples or suggestions for the immediate problem they faced, and tedious trial-and-error. But Java has grown to include a huge number of APIs, classes, and methods. Now it is simply too large for even the most intrepid developer to know it all. The need for a written compendium of J2EE Best Practices has never been greater. Java Enterprise Best Practices focuses on the Java 2 Enterprise Edition (J2EE) APIs. The J2EE APIs include such alphabet soup acronyms as EJB, JDBC, RMI, XML, and JMX.

Table of Contents

  1. Java Enterprise Best Practices
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Preface
      1. Audience
      2. Organization
      3. Conventions Used in This Book
      4. How to Contact Us
      5. Acknowledgments
    4. 1. Introduction to Java Enterprise Best Practices
      1. How Does a Best Practice Come About?
      2. Can Best Practices Be Arguable?
      3. What’s in This Book?
      4. About the Practices Themselves
      5. Enterprise Java Programming Resources Online
    5. 2. EJB Best Practices
      1. Design
        1. Know When to Use EJBs
        2. Use Standard Design Architecture
        3. Use CMP Entity Beans
        4. Use Design Patterns
          1. Session façade
          2. Value objects
      2. Implementation
        1. Use Local Interfaces for Entity Beans
        2. Use Business Interfaces
        3. Handle Exceptions in EJB Code Correctly
        4. Know When to Use Compound Primary Keys
        5. Know How to Handle Large Queries
        6. Use Dirty Flags in ejbStore
        7. Use Lazy Loading
        8. Cache JNDI Lookup Objects
        9. Use Business Delegates for Clients
        10. Write Dual CMP/BMP Entity Beans
        11. Create Domain Object Factories
      3. Deployment and Packaging
        1. Create a Build Environment
        2. Separate Components and Create Shared Libraries
        3. Write Testing and Diagnostics Code
    6. 3. Servlet Best Practices
      1. Working Effectively with Servlets
        1. Choose the Right Servlet Framework
          1. Tips for selecting a framework
          2. High-profile frameworks
        2. Use Pre-Encoded Characters
        3. Load Configuration Files from the Classpath
        4. Think of Sessions as a Local Cache
          1. Architecture of a shopping cart
          2. When to use sessions
        5. Don’t Use SingleThreadModel
      2. Caching with Servlets
        1. Pregenerate Content Offline and Cache Like Mad
          1. Pregeneration tools
          2. Cache on the client
          3. Cache at the proxy
          4. Cache on the server
          5. . . . Or don’t cache at all
      3. Other Servlet Tips
        1. Use Content-Disposition to Send a File
        2. Hire a UI Designer
    7. 4. JDBC Best Practices
      1. Configuration
        1. Avoid Type 1 and Type 3 Drivers
          1. When to use a Type 1 or Type 3 driver
          2. Choosing between Type 2 and Type 4 drivers
        2. Use DataSource Whenever Possible
        3. Leverage Proprietary Connection Properties
        4. Pool Database Connections
      2. Design
        1. Separate Application, Persistence, and JDBC Logic
        2. Avoid Long-Lived Transactions
        3. Do Not Rely on Built-in Key Generation
        4. Don’t Be Afraid to Use Optimistic Concurrency
      3. Code
        1. Use PreparedStatement
        2. Leverage PreparedStatement Pooling
        3. Question Using Nonstandard SQL
        4. Delay Access to Metadata
        5. Reference Columns by Number
        6. Navigate a ResultSet Forward Only
        7. Close Statements and Result Sets
        8. Thread JDBC Code in Swing Applications
        9. Minimize Database Calls and Perform Lazy-Loading
        10. Cache Lookup Tables
        11. Use wasNull( ) Checks
      4. General Database
        1. Always Optimize SQL Before Other Optimizations
        2. Do Not Store Binary Data in Database
        3. Normalize First, Denormalize Later
        4. Do Not Use Primary Keys with Real-World Meaning
    8. 5. XML Best Practices
      1. XML Authoring
        1. Use Entity References
        2. Use Parameter Entities
        3. Use Elements Sparingly, Attributes Excessively
      2. SAX
        1. Use the InputSource Class Correctly
        2. Understand How SAX Handles Entity Resolution
        3. Consider Using Partial Validation
      3. DOM
        1. Bootstrap DOM Correctly
          1. DOM Levels 1 and 2
          2. DOM Level 3
        2. Don’t Be Afraid to Use Helper Classes
        3. Avoid Class Comparisons
      4. JAXP
        1. Don’t Be Afraid to Use Format Transformations
    9. 6. RMI Best Practices
      1. Marshalling and Unmarshalling Objects
        1. Use Value Objects to Separate Marshalling Code from Your Application Logic
        2. Use Flat Hierarchies When Designing Value Objects
        3. Be Aware of How Externalization and Serialization Differ with Respect to Superclasses
        4. Don’t Marshal Inner Classes
        5. Always Explicitly Initialize Transient Fields Inside Marshalling Methods
        6. Always Set the serialVersionUID
        7. Set Version Numbers Independently of serialVersionUID
        8. Never Use Default Serialization
        9. Always Unit-Test Marshalling Code
        10. Profile Before Customizing
        11. Consider Using Byte Arrays to Store Marshalling Results
      2. Making Applications More Robust
        1. Include Logic for Retrying Remote Calls
        2. Associate Unique Identifiers with Requests
        3. Distinguish Between Network Lag Time and Server Load
        4. Wrap RMI Calls in Command Objects
        5. Consider Using a Naming Service
        6. Don’t Throw RemoteException in Your Server Code
        7. Distinguish Between Different Types of Remote Exceptions
          1. AccessException
          2. AlreadyBoundException
          3. ConnectException
          4. MarshalException
          5. NoSuchObjectException
          6. NotBoundException
          7. StubNotFoundException
          8. UnknownHostException
          9. UnmarshalException
        8. Use the Unreferenced Interface to Clean Up Allocated Server State
        9. Always Configure the Distributed Garbage Collector
      3. Improving Application Performance
        1. Cache Stubs to Remote Servers
        2. Consider Caching Return Values from Distributed Calls
        3. Use Batch Methods to Group Related Method Calls
        4. Use a Server-Side Proxy to Manage Transactions Across Servers
        5. Use Asynchronous Messaging Wherever Possible
      4. Further Reading
    10. 7. Java Management Extensions
      1. Naming
        1. Name MBean Domains Just Like Packages
        2. Use the Key Property List to Help Avoid Collisions
      2. Instrumentation
        1. Avoid Overinstrumentation
          1. Monitoring (attributes)
          2. Controlling (operations)
          3. Events (notifications)
        2. Implement MBeanRegistration
        3. Use Dynamic MBeans
          1. Each MBean feature can have a description
          2. You, as the MBean developer, have control over the creation of the metadata . . .
          3. The management interface can change dynamically
          4. There are fewer source files to maintain
        4. Use a Dynamic MBean Façade Pattern
          1. Attributes
          2. Operations
          3. Notifications
          4. DynamicMBean implementation
        5. Externalize the Description of an MBean’s Management Interface
        6. Use createMBean( ) to Create MBeans
    11. 8. Enterprise Internationalization
      1. Internationalization and Localization
        1. The Model-View-Controller Paradigm
      2. Presentation Layer
        1. Do Not Rely on Browser Settings for Storing Localization Preferences
        2. Use JSP Custom Tags or the JSTL for Locale-Specific Data Presentation
        3. Take a Portal-Based Approach to JSP Development
        4. Customize Localization and Data Manipulation Using XML and XSTL
      3. Business Object Layer
        1. Design Internationalization-Ready Business Objects
        2. Create a Simple Interface for Supporting Localization in Business Objects
        3. Take the Locale into Consideration in the Application Controller
      4. Data Access Layer
        1. Configure Your Database for Internationalization
        2. Use Standard Java Classes (Instead of Database Constructs) for Collation and for Sorting Database Information
        3. Design Your Database Schema to Be Internationalization-Ready
    12. 9. JSP Best Practices
      1. Appropriate Usage of JSP in an Enterprise Application
        1. Favor Using JSP as the View Only
        2. Consider Using Separate JSP Pages for Each Role
        3. Avoid Using Scripting Elements
      2. Page Design
        1. Put Shared Page Elements in Separate Files
        2. Use a Layout Page to Merge Shared Parts and Main Content
        3. Choose an Appropriate Include Mechanism
        4. Consider Using a Shared Configuration File
        5. Consider Using Filters to Simplify URLs
        6. Consider Internationalization from the Start
        7. Enable URL Rewriting
      3. Caching
        1. Cache Data Used in Pages
        2. Consider Enabling Client-Side Caching for Semistatic Pages
        3. Prevent Client-Side Caching for Dynamic Pages
      4. Error Handling
        1. Use Common Error Pages for the Whole Application
        2. Capture Enough Information to Fine-Tune the Application
        3. Consider Using <c:catch> to Handle Recoverable Errors
      5. Custom Component Development
        1. Choosing Between Beans and Custom Actions
        2. Consider Using Immutable Objects
        3. Consider Using a Reset Property When Capturing Input
        4. Always Include the <uri> Element in the TLD
        5. Design Tag Handler Classes for Instance Pooling
        6. Consider Adopting the JSTL Conventions
        7. Leverage the JSTL Classes and Interfaces
        8. Consider Developing a Tag Library Validator
      6. Deployment
        1. Consider Precompilation
    13. 10. JavaMail Best Practices
      1. Understanding Enterprise Email
      2. Sending Email
        1. Use Dynamic Content Strategies
        2. Use XML for Content
        3. Use Templates for Repeated Content
        4. Accommodate Various Client Capabilities
        5. Use Multipart/Related for Rich Messages
        6. Use Related Alternatives
      3. Email for System Integration
        1. Consider Email as an Enterprise Bridge
        2. Retrieve Incoming Mail Efficiently
        3. Use Inbox Listeners Carefully
        4. Choose an Effective Delivery Mechanism
        5. Beware Email in Transactions
        6. Cut and Run
        7. Use an Email Management Framework
        8. Incorporate Security
        9. Use Secure Protocols
        10. Use JAF for Bridging Content
      4. Performance Optimization
        1. Use Fetch Profiles
        2. Manage Attachments in Memory
        3. Use JavaMail to Search
    14. 11. Enterprise Performance Tuning Best Practices
      1. Performance Planning
        1. Perform Initial Performance Planning
        2. Set Performance Targets
      2. The Performance Environment
        1. Use a Client Simulation Tool
        2. Don’t Use JVM Profiling Tools
        3. Use Monitoring Tools
        4. Use Test Systems
      3. Proactive Performance Management in Production
        1. Plan for Performance Factors
      4. Efficient Distributed Computing Architecture
        1. Know Distributed Computing Restrictions
      5. Tuning Procedure
      6. User Perceptions
      7. Tuning Techniques
        1. Choose the Right Data Structure
        2. Transactions
      8. Miscellaneous Best Practices
        1. Design
        2. Monitoring
        3. Parallelism
        4. Distributed Computing
        5. JMS
    15. 12. List of Contributors
    16. Index
    17. Colophon
    18. SPECIAL OFFER: Upgrade this ebook with O’Reilly