Java Web Development
Total Page:16
File Type:pdf, Size:1020Kb
ARTOF JAVA WEB DEVELOPMENT STRUTS TAPESTRY COMMONS VELOCITY Neal Ford JUNIT AXIS COCOON INTERNETBEANS WEBWORK MANNING Art of Java Web Development Art of Java Web Development STRUTS, TAPESTRY, COMMONS, VELOCITY, JUNIT, AXIS, COCOON, INTERNETBEANS, WEBWORK NEAL FORD MANNING Greenwich (74° w. long.) For online information and ordering of this and other Manning books, go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Fax: (203) 661-9018 Greenwich, CT 06830 email: [email protected] ©2004 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books they publish printed on acid-free paper, and we exert our best efforts to that end. Manning Publications Co. Copyeditor: Liz Welch 209 Bruce Park Avenue Typesetter: Dottie Marsico Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN: 1-932394-06-0 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – VHG – 08 07 06 05 04 03 To Chuck, who still teaches me stuff daily brief contents PART ITHE EVOLUTION OF WEB ARCHITECTURE AND DESIGN....................................................... 1 1 I State-of-the-art web design 3 2 I Building web applications 27 3 I Creating custom JSP tags 61 4 I The Model 2 design pattern 91 PART II WEB FRAMEWORKS ........................................ 131 5 I Using Struts 133 6 I Tapestry 159 7 I WebWork 199 8 I InternetBeans Express 227 9 I Velocity 261 10 I Cocoon 283 11 I Evaluating frameworks 311 vii viii BRIEF CONTENTS PART III BEST PRACTICES............................................ 327 12 I Separating concerns 329 13 I Handling flow 371 14 I Performance 409 15 I Resource management 445 16 I Debugging 475 17 I Unit testing 521 18 I Web services and Axis 543 19 I What won’t fit in this book 563 contents preface xvii acknowledgments xix about the book xxi about the cover illustration xxx PART ITHE EVOLUTION OF WEB ARCHITECTURE AND DESIGN....................................................... 1 State-of-the-art web design 3 1 1.1 A brief history of Java web development 4 1.2 The importance of design patterns 6 The Model-View-Controller design pattern 7 I The emergence of Model 2 9 I Evolution 10 1.3 Using frameworks 11 A flavor of the Struts framework 12 I A flavor of the Turbine framework 14 I Objectively choosing a framework 20 1.4 Best practices 20 Business rules 20 I Where should the rules reside? 22 Leveraging best practices 24 1.5 Summary 25 ix x CONTENTS Building web applications 27 2 2.1 Building web applications with servlets 29 The eMotherEarth servlet application 29 Evaluating the servlet approach 50 2.2 Building web applications with JSP 50 The JSP eMotherEarth application 51 Evaluating the JSP approach 59 2.3 Summary 60 Creating custom JSP tags 61 3 3.1 The case for custom tags 62 3.2 The tag interfaces 63 The Tag interface 63 I The IterationTag interface 64 The BodyTag interface 65 3.3 Building simple tags 66 The HtmlSqlResult tag 66 I Registering the tag 71 3.4 Validating tag attributes 75 Adding DbPool to the application tag 75 3.5 Using prebuilt tags 80 Using JSTL 81 I Using other taglibs 84 3.6 Custom tag considerations 86 Resource usage 87 I Building a framework 88 3.7 Now that we’re here, where are we? 88 3.8 Summary 89 The Model 2 design pattern 91 4 4.1 Using Model 2 as your framework 92 The Model 2 schedule application 93 Options in Model 2 116 4.2 Parameterizing commands with controller servlets 117 An example of parameterizing commands 118 Advantages and disadvantages 127 4.3 Summary 128 CONTENTS xi PART II WEB FRAMEWORKS ........................................ 131 Using Struts 133 5 5.1 Building Model 2 Web applications with Struts 134 The Struts schedule application 134 I Value objects as form beans 136 I Objectifying commands with Struts’ actions 137 Configuring Struts applications 139 I Using Struts’ custom tags to simplify JSP 142 I Internationalization with Struts 145 Struts’ support for data entry 147 I Declarative validations 151 5.2 Evaluating Struts 156 5.3 Summary 157 Tapestry 159 6 6.1 Overview 160 6.2 The architecture 160 6.3 A simple Tapestry application 162 Tapestry Hello, World 162 6.4 The Tapestry framework 167 Framework classes and interfaces 167 I Components 170 6.5 Scheduling in Tapestry 173 Bootstrapping the application 173 I The Home page 176 The custom table component 180 I The Add page 185 6.6 Evaluating Tapestry 192 Documentation and samples 192 I Debugging support 195 Using Tapestry 196 6.7 Summary 197 WebWork 199 7 7.1 Overview 200 The architecture 201 I The configuration 202 7.2 Key concepts 203 Actions 204 I Key interfaces 204 I The value stack 205 Expression language 206 I BeanInfo classes 207 Templates 207 xii CONTENTS 7.3 Scheduling in WebWork 208 The configuration 208 I The View page 209 The Add page 214 I Validations 220 7.4 Evaluating WebWork 224 7.5 Summary 226 InternetBeans Express 227 8 8.1 Overview 228 8.2 The architecture 230 DataExpress 230 I InternetBeans Express 233 8.3 InternetBeans Express components 234 ixPageProducer 234 I ixComponents 236 8.4 Scheduling with InternetBeans 237 Data connectivity 238 I The View page 242 I The Add page 245 I Validations 249 8.5 JSP custom tags 255 8.6 Evaluating InternetBeans Express 257 Documentation and samples 257 Using InternetBeans Express 258 8.7 Summary 259 Velocity 261 9 9.1 Overview 262 9.2 The architecture 263 9.3 Key concepts 265 Setting up Velocity 265 I The Velocity Template Language 268 I Context 269 9.4 Scheduling with Velocity 269 The View page 271 I The Add page 274 Validations 278 CONTENTS xiii 9.5 Evaluating Velocity 281 Documentation and samples 281 I Using Velocity 282 9.6 Summary 282 Cocoon 283 10 10.1 Overview 284 10.2 The architecture 285 The publishing framework 285 I The web framework 288 10.3 Key concepts 289 The publishing framework 289 I The sitemap 295 The web framework 299 10.4 Scheduling in Cocoon 302 The sitemap 303 I The action 304 I The view 305 10.5 Evaluating Cocoon 307 Documentation and samples 307 I Source code 308 Debugging 308 10.6 Summary 309 Evaluating frameworks 311 11 11.1 Evaluation criteria 312 Suitability to the application 312 I Documentation 315 Source code 316 I Tool support 317 I External criteria 318 11.2 Design considerations 319 Adherence to good design principles 319 I The user interface 320 I Innovative features 321 Insularity 322 I “Feel” 322 11.3 What I like 323 Transparent infrastructure 323 I Innovative ideas 323 Ultra-high cohesion and low coupling 324 Evaluating frameworks as a hobby 324 11.4 Summary 324 xiv CONTENTS PART III BEST PRACTICES............................................ 327 Separating concerns 329 12 12.1 Using interfaces to hide implementation 330 JDBC interfaces 331 I Interfaces in frameworks 331 Decoupled classes 332 12.2 Using JavaBeans 333 Model beans 334 12.3 Using Enterprise JavaBeans 337 The EJB architecture 338 I Porting from JavaBeans to Enterprise JavaBeans 340 I Using EJBs in web frameworks 360 Managing JNDI context 361 12.4 Performing validations with model beans 362 Client-side validations 362 Building client-side validations from the server 365 12.5 Summary 368 Handling flow 371 13 13.1 Application usability options 372 Building the base: eMotherEarth.com 372 I Page-at-a-time scrolling 378 I Sortable columns 384 User interface techniques in frameworks 389 13.2 Building undo operations 390 Leveraging transaction processing 391 I Using the Memento design pattern 394 I Undo in frameworks 401 13.3 Using exception handling 401 The difference between technical and domain exceptions 401 Creating custom exception classes 402 I Where to catch and handle exceptions 403 I Exceptions in frameworks 406 13.4 Summary 407 Performance 409 14 14.1 Profiling 410 Measuring memory 410 I Performance profiling 412 Load testing 419 I Performance of profiling frameworks 421 CONTENTS xv 14.2 Common performance pitfalls 421 Object creation 422 I Extraneous object references 424 String usage 426 14.3 Pooling 427 Simple object pools 427 I Soft and weak references 428 Commons pools 433 I Pooling in frameworks 440 14.4 Designing for scalability 440 When to scale up to EJB 441 Molding your architecture for the future 441 14.5 When to optimize 442 14.6 Summary 443 Resource management 445 15 15.1 Caching strategies 446 Caching with the Flyweight design pattern 447 I Caching with the Façade design pattern 453 I Resource management in frameworks 469 15.2 Other resources you need to manage 470 Effectively using JNDI 470 I Using lazy instantiation 472 Working with web collections 472 15.3 Summary 473 Debugging 475 16 16.1 Debugging web applications 476 16.2 Debugging with the SDK 483 Starting the debugger 483 I Running the debugger 486 Breakpoints and steps 489 I Accessing variables 490 Effectively using jdb 492 16.3 Debugging with IDEs 493 Debugging with NetBeans 493 I Debugging with JBuilder 498 Differences between debuggers 502 16.4 Evaluating