CYAN YELLOW MAGENTA BLACK PANTONE 123 C EMPOWERING PRODUCTIVITY FOR THE JAVA™ DEVELOPER THE EXPERT’s VOICE® IN JAVA™ TECHNOLOGY Companion Author of eBook Available Practical DWR 2 Projects Practical Ajax Projects with Java™ Technology Practical Practical JavaScript™, Dear Reader, DOM Scripting, and Ajax Practical Projects Ajax represents a brave, newish world of web development where coding on D the client is just as important as on the server side. Hundreds of libraries exist that purport to make it easier for you, and there’s always the “Do It Yourself” approach. Which route should you take? If you work with Java™ technologies, one choice that stands out is DWR, W DWR 2 or Direct Web Remoting. With DWR, JavaScript™-based client code that calls server-side objects works as if it were all running in the same process space. The simplicity and power DWR blends together has few rivals today. In this, the first DWR book to be published, you’ll be introduced to DWR R and all it has to offer, including reverse Ajax, XML and annotation-based con- figuration, container-managed security, simple POJO-based development, and greatly simplified client-side coding. You’ll learn by doing as you explore six fully functional applications including the following: • A webmail client for remotely accessing your e-mail accounts 2 • A wiki for collaborative efforts • A file manager for remotely managing your server’s file system Projects • A portal for enterprise reporting needs • A project management/time-tracking system P • Even a fun little game! r In addition to DWR, you’ll also see how other popular libraries help realize o the RIA/Web 2.0 vision, including Spring, Hibernate, dHTMLx, DataVision, Freemarker, and Ext JS. j e If you’re doing RIA development in Java, DWR is for you, as too is this book! c Frank W. Zammetti SCJP, MCSD, MCP, CNA, CIW Associate, author, husband, father, Vorlon t s Explore the design and construction of six complete, rich Web 2.0 applications utilizing DWR, Companion eBook one of the hottest libraries in the Ajax realm today. THE APRESS JAVA™ ROADMAP Pro Ajax and Java™ Frameworks See last page for details Foundations of Ajax Practical on $10 eBook version DWR 2 Projects Practical Ajax Projects with Java™ Technology SOURCE CODE ONLINE Z a www.apress.com ISBN-13: 978-1-59059-941-9 m Frank W. Zammetti java.apress.com ISBN-10: 1-59059-941-1 m 54699 e t Foreword by Joe Walker, Creator of DWR t US $46.99 i Shelve in Java Programming User level: 9 781590 599419 Intermediate–Advanced this print for content only—size & color not accurate spine = 1.072" 568 page count 9411FM.qxd 1/2/08 5:20 PM Page i Practical DWR 2 Projects Frank W. Zammetti 9411FM.qxd 1/2/08 5:20 PM Page ii Practical DWR 2 Projects Copyright © 2008 by Frank W.Zammetti All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-59059-941-9 ISBN-10 (pbk): 1-59059-941-1 ISBN-13 (electronic): 978-1-4302-0556-2 ISBN-10 (electronic): 1-4302-0556-3 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Java™and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in theUS and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement from Sun Microsystems, Inc. Lead Editor: Steve Anglin Technical Reviewer: Herman van Rosmalen Editorial Board: Steve Anglin, Ewan Buckingham, Tony Campbell, Gary Cornell, Jonathan Gennick, KevinGoff, Jonathan Hassell, Matthew Moodie, Joseph Ottinger, Jeffrey Pepper, BenRenow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Project Manager: Beth Christmas Copy Editor: Ami Knox Associate Production Director: Kari Brooks-Copony Production Editor: Kelly Winquist Compositor: Dina Quan Proofreader: April Eddy Indexer: Julie Grady Artists: Anthony Volpe, April Milne Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or visit http://www.springeronline.com. For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600, Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com. The information in this book is distributed on an “as is” basis, without warranty. Although every precau- tion has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at http://www.apress.com. 9411FM.qxd 1/2/08 5:20 PM Page iii Let’s see ...this is my third book now ...the first I dedicated to my wife,kids,mom,dad, and John Sheridan for keeping the Shadows off our backs.The second I dedicated to all theanimals I’ve eaten,a batch of childhood friends who helped shape my early life, DennyCrane,and my wife and kids once more.So,I’ve covered everyone that counts at least once.So,who to dedicate this one to? Who’s worthy of my adulation and respect? Oh,oh! I know ...ME! I dedicate this book to ME! OK,fine,I guess I can’t do that. So,I instead dedicate this book to my wife and kids.AGAIN. I dedicate this book to my sister because I just realized I didn’tcover everyone that counts,but now I have,sis! I dedicate this book to the folks at Harmonix and Bungie because Guitar Hero,Rock Band, and Halo just flat-out rule.I need some Dream Theater,Queensryche,Shadow Gallery, Fates Warning,and Enchant to make my life complete though,so get on that,OK guys? And lastly,I dedicate this book to all the alien species we have yet to meet.I just hope those dudes need books on web programming,because my kids eat like you wouldn’t believe! I’lleven take Quatloos! 9411FM.qxd 1/2/08 5:20 PM Page iv 9411FM.qxd 1/2/08 5:20 PM Page v Contents at a Glance Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Illustrator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii PART 1 n n nSetting the Table nCHAPTER 1 An Introduction to Ajax,RPC,and Modern RIAs . . . . . . . . . . . . . . . . . . 3 nCHAPTER 2 Getting to Know DWR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 nCHAPTER 3 Advanced DWR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 PART 2 n n nThe Projects nCHAPTER 4 InstaMail:An Ajax-Based Webmail Client. . . . . . . . . . . . . . . . . . . . . . 129 nCHAPTER 5 Share Your Knowledge:DWiki,the DWR-Based Wiki. . . . . . . . . . . 189 nCHAPTER 6 Remotely Managing Your Files:DWR File Manager. . . . . . . . . . . . . 259 nCHAPTER 7 Enter the Enterprise:A DWR-Based Report Portal. . . . . . . . . . . . . . 329 nCHAPTER 8 DWR for Fun and Profit (a DWR Game!). . . . . . . . . . . . . . . . . . . . . . . . 419 nCHAPTER 9 Timekeeper:DWR Even Makes Project Management Fun!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457 nINDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523 v 9411FM.qxd 1/2/08 5:20 PM Page vi 9411FM.qxd 1/2/08 5:20 PM Page vii Contents Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii About the Author. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii About the Illustrator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii PART 1 n n nSetting the Table nCHAPTER 1 An Introduction to Ajax, RPC, and Modern RIAs. . . . . . . . . . . 3 A Brief History of Web Development:The “Classic”Model. . . . . . . . . . . . . 3 Dawn of a Whole New World:The PC Era. . . . . . . . . . . . . . . . . . . . . . . 5 Yet Another Revolution:Enter the Web . . . . . . . . . . . . . . . . . . . . . . . . . 7 What’s So Wrong with the Classic Web?. . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Enter Ajax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Why Is Ajax a Paradigm Shift? On the Road to RIAs. . . . . . . . . . . . . . . . . . 18 The Flip Side of the Coin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Let’s Get to the Good Stuff:Our First Ajax Code,the Manual Way. . . . . . 25 A Quick Postmortem. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Hey,I Thought This Was Ajax?!?. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Cutting IN the Middle Man:Ajax Libraries to Ease Our Pain. . . . . . . . . . . . 33 Alternatives to Ajax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Hmm,Are We Forgetting Something? What Could It Be? Oh Yeah,DWR!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 nCHAPTER 2 Getting to Know DWR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 First Things First:Why DWR at All? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 DWR:RPC on Steroids for the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 DWR Architectural Overview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Getting Ready for the Fun:Your DWR Development Environment. . . . . . . 49 vii 9411FM.qxd 1/2/08 5:20 PM Page viii viii nCONTENTS A Simple Webapp to Get Us Started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Getting the Lay of the Land:Directory Structure . . . . . . . . . . . . . . . . 52 From Code to Executable:Ant Build Script . . . . . . . . . . . . . . . . . . . . . 53 Application Configuration:web.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 The Markup:index.jsp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 On the Server Side:MathServlet.java. . . . . . . . . . . . . . . . . . . . . . . . . . 56 The Workhorse:MathDelegate.java. . . . . . . . . . . . . . . . . . . . . . . . . . . 58 It’s Alive:Seeing It in Action. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Adding DWR to the Mix. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 The DWR Test/Debug Page. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Configuring DWR Part 1:web.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Configuring DWR Part 2:dwr.xml. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Built-in Creators and Converters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 The <init> Section. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 The <allow> Section. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 The <signatures> Section. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Interacting with DWR on the Client. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Basic Call Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Call Metadata Object Approach. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 A Word on Some Funky Syntax. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Setting Beans on a Remote Object. . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Extended Data Passing to Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . 87 Interacting with DWR on the Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 DWR Configuration and Other Concepts:The engine.js File . . . . . . . . . . . 90 Call Batching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 A Quick Look at util.js,the DWR Utility Package . . . . . . . . . . . . . . . . . . . . . 92 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 nCHAPTER 3 Advanced DWR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Locking the Doors:Security in DWR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Deny by Default. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 J2EE Security and DWR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 When Perfection Is Elusive:Error Handling in DWR Applications . . . . . . 101 Handling Warnings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Handling Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Edge Cases:Improper Responses . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 The Mechanics of Handling Exceptional Situations. . . . . . . . . . . . . 105 Another Word on Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Help from Elsewhere:Accessing Other URLs. . . . . . . . . . . . . . . . . . . . . . . 107
Description: