Developing Java™ Web Applications
Total Page:16
File Type:pdf, Size:1020Kb
ECLIPSE WEB TOOLS PLATFORM the eclipse series SERIES EDITORS Erich Gamma ■ Lee Nackman ■ John Wiegand Eclipse is a universal tool platform, an open extensible integrated development envi- ronment (IDE) for anything and nothing in particular. Eclipse represents one of the most exciting initiatives hatched from the world of application development in a long time, and it has the considerable support of the leading companies and organ- izations in the technology sector. Eclipse is gaining widespread acceptance in both the commercial and academic arenas. The Eclipse Series from Addison-Wesley is the definitive series of books dedicated to the Eclipse platform. Books in the series promise to bring you the key technical information you need to analyze Eclipse, high-quality insight into this powerful technology, and the practical advice you need to build tools to support this evolu- tionary Open Source platform. Leading experts Erich Gamma, Lee Nackman, and John Wiegand are the series editors. Titles in the Eclipse Series John Arthorne and Chris Laffra Official Eclipse 3.0 FAQs 0-321-26838-5 Frank Budinsky, David Steinberg, Ed Merks, Ray Ellersick, and Timothy J. Grose Eclipse Modeling Framework 0-131-42542-0 David Carlson Eclipse Distilled 0-321-28815-7 Eric Clayberg and Dan Rubel Eclipse: Building Commercial-Quality Plug-Ins, Second Edition 0-321-42672-X Adrian Colyer,Andy Clement, George Harley, and Matthew Webster Eclipse AspectJ:Aspect-Oriented Programming with AspectJ and the Eclipse AspectJ Development Tools 0-321-24587-3 Erich Gamma and Kent Beck Contributing to Eclipse: Principles, Patterns, and Plug-Ins 0-321-20575-8 Jeff McAffer and Jean-Michel Lemieux Eclipse Rich Client Platform: Designing, Coding, and Packaging Java™ Applications 0-321-33461-2 Steve Northover and Mike Wilson SWT:The Standard Widget Toolkit,Volume 1 0-321-25663-8 Diana Peh,Alethea Hannemann, Paul Reeves, and Nola Hague BIRT:A Field Guide to Reporting 0-321-44259-8 Jason Weathersby, Don French,Tom Bondur, Jane Tatchell, and Iana Chatalbasheva Integrating and Extending BIRT 0-321-44385-3 For more information on books in this series visit www.awprofessional.com/series/eclipse ECLIPSE WEB TOOLS PLATFORM DEVELOPING JAVA™ WEB APPLICATIONS Naci Dai Lawrence Mandel Arthur Ryman Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 [email protected] For sales outside the United States please contact: International Sales [email protected] Visit us on the Web: www.awprofessional.com Library of Congress Cataloging-in-Publication Data Dai, Naci. Eclipse Web tools platform : developing Java Web applications / Naci Dai, Lawrence Mandel, Arthur Ryman. p. cm. Includes bibliographical references and index. ISBN 978-0-321-39685-3 (pbk. : alk. paper) 1. Web site development 2. Java (Computer program language) 3. Internet programming. I. Mandel, Lawrence. II. Ryman, Arthur. III. Title. TK5105.888.D32 2007 006.7'6—dc22 2007010167 Copyright © 2007 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, write to: Pearson Education, Inc. Rights and Contracts Department 75 Arlington Street, Suite 300 Boston, MA 02116 Fax: (617) 848-7047 ISBN 13: 978-0-321-39685-3 ISBN 10: 0-321-39685-5 Text printed in the United States on recycled paper at Courier in Stoughton, Massachusetts. First printing, May 2007 To my wife and best friend, Karen, who encourages and helps me in all my endeavors, and to my daughters, Maya and Ela, for letting me use their weekends and playtime for writing this book. I love you all. —N.D. To my wife, Elana, who inspires, encourages, and challenges me to try new things, like writing this book, and to my dad, Fred (), who bought me my first computer and who I know would have thought this stuff was so cool. —L.M. To my late father, Sydney Ryman, who taught me to love books and who died peacefully at the age of 85 while I was writing this one. Dad, thank you for all those weekend trips to the public library. —A.R. This page intentionally left blank Contents Foreword xvii Preface xix Acknowledgments xxiii About the Authors xxv Part I ❍ Getting Started 1 Chapter 1 Introduction 3 Java Web Application Development and Eclipse 3 What This Book Contains 4 How This Book Is Organized 5 Source Code Examples 8 Introducing League Planet 9 Summary 10 Chapter 2 About the Eclipse Web Tools Platform Project 13 WTP Is Born 13 WTP Economics 15 Reducing Development Expense 16 Generating Revenue 19 The Structure of WTP 22 The Scope of WTP 23 WTP Subprojects 24 The Architecture of WTP 27 The WST Subproject 29 The JST Subproject 34 vii viii Contents Contributing to WTP 37 Become a User 37 Monitor the Newsgroup 37 Report a Problem 38 Suggest an Improvement 38 Fix a Bug 39 Write an Article or Tutorial 39 Become a Committer 40 Grow the Community 40 Summary 40 Chapter 3 Quick Tour 41 Overview 41 Iteration 1: J2EE Web Applications 44 Add a Server Runtime Environment 46 Create a Dynamic Web Project 51 Create and Edit a JSP 56 Run the JSP on the Server 57 Summary of Iteration 1 60 Iteration 2: Servlets and Scriptlets 60 Add a Java Scriptlet to a JSP 60 Debug a JSP 61 Create a Servlet 64 Debug a Servlet 69 Summary of Iteration 2 70 Iteration 3: Database Access 71 Connect to a Database 72 Execute SQL Statements 75 Add Database Access to a Web Application 78 Summary of Iteration 3 82 Iteration 4: Web Services 82 Deploy a Web service 83 Use a Test Client 86 Monitor SOAP Messages 87 Summary of Iteration 4 88 Summary 88 Chapter 4 Setting Up Your Workspace 91 Installing and Updating WTP 91 The Installable Components of WTP 91 WTP Build Types 92 Contents ix Installation via Update Manager 95 Installation via Zip Files 98 Installing Third-Party Content 100 JDK Setup 103 Verifying the Installation 103 Updating WTP 104 Configuring WTP 105 Data Preferences 107 Internet Preferences 107 Server Preferences 107 Validation Preferences 107 Web and XML Preferences 108 Web Services Preferences 108 XDoclet Preferences 109 Sharing Settings 110 Summary 110 Part II ❍ Java Web Application Development 111 Chapter 5 Web Application Architecture and Design 113 The Web Landscape 113 Web Applications 115 Java Web Applications 116 Layered Web Application Design 120 Model View Controller (MVC) for the Web 123 Java Application Frameworks 128 Service-Oriented Architecture (SOA) 130 Providing Services: The Service Layer 130 Case Study: League Planet 133 Summary 135 Chapter 6 Organizing Your Development Project 137 Web Project Types and J2EE Applications 138 Web Projects 139 J2EE Modules 140 Creating Applications 140 Creating EJB Applications 148 Creating Enterprise Applications 154 Advanced Web Projects 160 Modeling the Developer View 162 x Contents Example Projects 165 Basic Enterprise Application 165 Dividing a Web Module into Multiple Projects 171 Using Maven for Web Application Development 180 Summary 196 Chapter 7 The Presentation Tier 199 Introduction 199 Interaction Design 200 Graphic Design 203 The Structure of the Presentation Tier 204 Iteration 1: Static Web Projects, HTML, and the Structured Source Editors 208 Static Web Projects 208 HTML 211 Structured Source Editors 218 Templates 221 Snippets 224 Summary of Iteration 1 230 Iteration 2: CSS 230 Summary of Iteration 2 234 Iteration 3: JavaScript 234 E-Mail Address Obfuscation 234 Data Entry Form Validation 236 Summary of Iteration 3 247 Iteration 4: XML and XSLT 248 XML 248 XSLT 252 Summary of Iteration 4 257 Iteration 5: DTD 257 Summary of Iteration 5 261 Iteration 6: Servers, Dynamic Web Projects, and Servlets 261 Servers 261 Dynamic Web Projects 267 Servlets 270 Summary of Iteration 6 279 Iteration 7: JSP 279 Summary of Iteration 7 289 Iteration 8: Monitoring HTTP Sessions 289 HTTP Sessions 289 Contents xi The TCP/IP Monitor 290 Viewing HTTP Sessions with the TCP/IP Monitor 291 Modifying and Resending a Message 293 Summary of Iteration 8 295 Summary 295 Chapter 8 The Business Logic Tier 297 A Common Business Tier Design 300 Iteration 1: The Domain Model 301 J2EE Utility Projects 301 The Object Model 304 The Service Layer 310 The Data Access Layer 315 Testing 320 Summary of Iteration 1 324 Iteration