More Java Pitfalls 50 New Time-Saving Solutions and Workarounds docx

476 258 0
More Java Pitfalls 50 New Time-Saving Solutions and Workarounds docx

Đ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

Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com More JavaPitfalls 50 New Time-Saving Solutions and Workarounds Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Michael C. Daconta Kevin T. Smith Donald Avondolio W. Clay Richardson More JavaPitfalls 50 New Time-Saving Solutions and Workarounds Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Publisher: Joe Wikert Executive Editor: Robert M. Elliott Assistant Developmental Editor: Emilie Herman Managing Editor: Micheline Frederick New Media Editor: Angela Denny Text Design & Composition: Wiley Composition Services This book is printed on acid-free paper. ∞ Copyright  2003 by Michael C. Daconta, Kevin T. Smith, Donald Avondolio, and W. Clay Richardson. All rights reserved. Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada 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, scanning, or otherwise, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rose- wood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8700. Requests to the Pub- lisher for permission should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4447, E-mail: permcoordinator@wiley.com. Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Neither the publisher nor author shall be liable for any loss of profit or any other commercial damages, including but not limited to special, inci- dental, consequential, or other damages. Wiley, the Wiley Publishing logo and related trade dress are trademarks or registered trade- marks of Wiley Publishing, Inc., in the United States and other countries, and may not be used without written permission. Java is a trademark or registered trademark of Sun Microsystems, Inc All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. For general information on our other products and services please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Cataloging-in-Publication Data: ISBN: 0-471-23751-5 Printed in the United States of America 10 9 8 7 6 5 4 3 2 1 Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com This book is dedicated to the memory of Edsger W. Dijkstra who said, “I mean, if 10 years from now, when you are doing something quick and dirty, you suddenly visualize that I am looking over your shoulders and say to yourself, ‘Dijkstra would not have liked this’, well that would be enough immortality for me.” We humbly disagree: 10 years of Dijkstra is just not long enough; may he happily haunt our consciousness for 10 10 years. Such an increase is more befitting his stature. Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Introduction xi Acknowledgments xvii Part One The Client Tier 1 Item 1: When Runtime.exec() Won’t 4 Item 2: NIO Performance and Pitfalls 17 Canonical File Copy 20 Little-Endian Byte Operations 21 Non-Blocking Server IO 26 Item 3: I Prefer Not to Use Properties 34 Item 4: When Information Hiding Hides Too Much 39 Item 5: Avoiding Granularity Pitfalls In java.util.logging 44 Item 6: When Implementations of Standard APIs Collide 53 Item 7: My Assertions are Not Gratuitous! 59 How to Use Assertions 59 Item 8: The Wrong Way to Search a DOM 66 Item 9: The Saving-a-DOM Dilemma 73 Item 10: Mouse Button Portability 80 Item 11: Apache Ant and Lifecycle Management 88 Item 12: JUnit: Unit Testing Made Simple 100 Contents vii Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Item 13: The Failure to Execute 108 Deploying Java Applications 109 The Java Extension Mechanism 110 Sealed Packages 111 Security 112 Item 14: What Do You Collect? 112 Item 15: Avoiding Singleton Pitfalls 117 When Multiple Singletons in Your VM Happen 119 When Singletons are Used as Global Variables, or Become Non-Singletons 120 Item 16: When setSize() Won’t Work 122 Item 17: When Posting to a URL Won’t 126 Connecting via HTTP with the java.net Classes 126 An Alternative Open Source HTTP Client 137 Item 18: Effective String Tokenizing 140 Item 19: JLayered Pane Pitfalls 146 Item 20: When File.renameTo() Won’t 151 Item 21: Use Iteration over Enumeration 157 Item 22: J2ME Performance and Pitfalls 162 Part Two The Web Tier 199 Item 23: Cache, It’s Money 200 Item 24: JSP Design Errors 208 Request/Response Paradigm 208 Maintaining State 209 JSP the Old Way 210 JSP Development with Beans (Model 1 Architecture) 214 JSP Development in the Model 2 Architecture 220 Item 25: When Servlet HttpSessions Collide 220 Item 26: When Applets Go Bad 227 Item 27: Transactional LDAP—Don’t Make that Commitment 235 Item 28: Problems with Filters 244 Item 29: Some Direction about JSP Reuse and Content Delivery 255 Item 30: Form Validation Using Regular Expressions 261 viii Contents Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com Item 31: Instance Variables in Servlets 269 Item 32: Design Flaws with Creating Database Connections within Servlets 279 Item 33: Attempting to Use Both Output Mechanisms in Servlets 291 Item 34: The Mysterious File Protocol 297 Item 35: Reading Files from Servlets 302 Web Application Deployment Descriptors 308 Item 36: Too Many Submits 312 Preventing Multiple Submits 314 Handling Multiple Submits 316 Part Three The Enterprise Tier 327 Item 37: J2EE Architecture Considerations 329 Item 38: Design Strategies for Eliminating Network Bottleneck Pitfalls 335 A Scenario 336 General Design Considerations 336 EJB Design Considerations 340 Item 39: I’ll Take the Local 341 Item 40: Image Obsession 348 Item 41: The Problem with Multiple Concurrent Result Sets 353 Item 42: Generating Primary Keys for EJB 359 A Simple Scenario 359 A “Client Control” Approach 360 The Singleton Approach 362 The Networked Singleton Approach 363 An Application Server-Specific Approach 363 Database Autogeneration Approaches 363 Other Approaches 364 Item 43: The Stateful Stateless Session Bean 365 Message-Driven Beans 366 Entity Bean 366 Stateful Session Bean 368 Stateless Session Bean 368 Item 44: The Unprepared PreparedStatement 372 Contents ix Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com [...]... programming interfaces (APIs), and better alternatives Here are some highlights of pitfalls in each area Performance has long been the bane of client-side Java The first book, Java Pitfalls: Time-Saving Solutions and Workarounds to Improve Programs, had numerous pitfalls on performance, and many other books and Web sites have come out on Java performance tuning This part has two pitfalls on performance:... 18: t.printStackTrace(); 19: } 20: } 21: } Listing 1.1 BadExecJavac .java A run of BadExecJavac produces the following: E:\classes\org\javapitfalls\item1 >java org.javapitfalls.item1.BadExecJavac java. lang.IllegalThreadStateException: process has not exited at java. lang.Win32Process.exitValue(Native Method) at BadExecJavac.main(BadExecJavac .java: 13) Æ The program failed to work because the exitValue()... command produces: E:\classes\org\javapitfalls\item1 >java org.javapitfalls.item1.GoodWindowsExec “dir * .java and Split cmd.exe /C dir * .java - http://www.simpopdf.com Unregistered Version Execing OUTPUT> Volume in drive E has no label OUTPUT> Volume Serial Number is 5C5F-0CC9 OUTPUT> OUTPUT> Directory of E:\classes\com\javaworld\jpitfalls\article2 OUTPUT> OUTPUT>10/23/00 09:01p 805 BadExecBrowser .java. .. exitVal); } catch (Throwable t) { t.printStackTrace(); } } } Listing 1.3 MediocreExecJavac .java 7 8 Simpo PDF Item 1 A run of MediocreExecJavac produces the following: E:\classes\org\javapitfalls\item1 >java Æ org.javapitfalls.item1.MediocreExecJavac Merge and Split Unregistered Version - http://www.simpopdf.com Usage: javac where includes -g Generate all debugging... Client Tier This part covers both J2ME and J2SE and explores pitfalls in developing both networked and standalone clients Topics covered include preferences, application deployment, logging, IO performance, and many more This part has 22 pitfalls Part Two: The Web Tier This part examines pitfalls in components that run inside the Web container, like servlets and JavaServer Pages (JSPs) These applications... expectations and the capability of an incomplete API Avoiding Granularity Pitfalls in java. util.logging (Item 5) The new java. util.logging API has some hidden complexities and relationships that affect the level of reporting granularity You must understand the relationship between loggers and handlers to effectively use this API The Wrong Way to Search a DOM (Item 8) With JDK 1.4, the Java platform... BadExecWinDir, which when run produces the following: E:\classes\org\javapitfalls\item1 >java org.javapitfalls.item1.BadExecWinDir java. io.IOException: CreateProcess: dir error=2 at java. lang.Win32Process.create(Native Method) at java. lang.Win32Process.(Unknown Source) at java. lang.Runtime.execInternal(Native Method) at BadExecWinDir.main(BadExecWinDir .java: 12) Æ As stated earlier, the error value of 2 means... directory command is part of the window command interpreter and not a separate executable To run the window command interpreter, you execute either command.com or cmd.exe depending on the windows operating system you are using Listing 1.4 runs a copy of the Windows Command Interpreter and then executes the user-supplied command (like dir) Simpo PDF Merge When Runtime.exec() Won’t 01: package org.javapitfalls.item1;... Performance and Pitfalls (Item 2) This pitfall examines the IO performance improvements of the New IO package (NIO) The pitfall examines file channels, ByteBuffers, and non-blocking server IO Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com J2ME Performance and Pitfalls (Item 22) This pitfall ports a Swing application to the J2ME platform and uncovers both API pitfalls and over... Differs from Java Pitfalls This book borrows all the good features from the first book and improves upon it in three ways: Broader Coverage The first book focused on the lang, util, io, and GUI packages, whereas this book covers the J2ME, J2SE, and J2EE platforms New Features This book covers the majority of new features like regular expressions, NIO, assertions, JAXR, JAXM, JAX-RPC, and many more Better . Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com More Java ™ Pitfalls 50 New Time-Saving Solutions and Workarounds Simpo PDF Merge and Split Unregistered Version. Merge and Split Unregistered Version - http://www.simpopdf.com Michael C. Daconta Kevin T. Smith Donald Avondolio W. Clay Richardson More Java ™ Pitfalls 50 New Time-Saving Solutions and Workarounds Simpo. J2ME and J2SE and explores pit- falls in developing both networked and standalone clients. Topics covered include preferences, application deployment, logging, IO performance, and many more.

Ngày đăng: 27/06/2014, 08:20

Từ khóa liên quan

Mục lục

  • @Team LiB

  • Cover

  • Contents

  • Introduction

  • Acknowledgments

  • Item 1: When Runtime.exec() Won’t

  • Item 2: NIO Performance and Pitfalls

    • Canonical File Copy

    • Little-Endian Byte Operations

    • Non-Blocking Server IO

    • Item 3: I Prefer Not to Use Properties

    • Item 4: When Information Hiding Hides Too Much

    • Item 7: My Assertions Are Not Gratuitous!

      • How to Use Assertions

      • Item 8: The Wrong Way to Search a DOM

      • Item 9: The Saving-a-DOM Dilemma

      • Item 10: Mouse Button Portability

      • Item 11: Apache Ant and Lifecycle Management

      • Item 12: JUnit: Unit Testing Made Simple

      • Item 13: The Failure to Execute

        • Deploying Java Applications

        • The Java Extension Mechanism

        • Sealed Packages

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan