XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application- P1

Chia sẻ: Thanh Cong | Ngày: | Loại File: PDF | Số trang:50

0
180
lượt xem
96
download

XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application- P1

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application- P1: Là một nhà phát triển Web, bạn biết những thách thức trong việc xây dựng các ứng dụng mạnh mẽ trên nhiều nền tảng. Tạo các ứng dụng di động trở nên thật sự có thể bằng cách sử dụng Java cho code và XML để tổ chức và quản lý dữ liệu. "XML, XSLT, Java, và JSP: Một trường hợp học" sẽ giúp bạn tối đa hóa khả năng của XML, XSLT, Java, và JSP trong các ứng dụng web của bạn....

Chủ đề:
Lưu

Nội dung Text: XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application- P1

  1. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
  2. Contents At a Glance 1 Introduction and Requirements XML, XSLT, 2 An Environment for Java Software Java, and JSP: Development 3 Java Servlets and JavaServer Pages: A Case Study in Jakarta Tomcat Developing a 4 XML and XSLT: Xerces and Xalan Web Application 5 bonForum Chat Application: Use and Design 6 bonForum Chat Application: Implementation 7 JavaServer Pages:The Browseable User Interface 8 Java Servlet and Java Bean: BonForumEngine and BonForumStore 9 Java Applet Plugged In: BonForumRobot 10 JSP Taglib:The bonForum Custom Tags 11 XML Data Storage Class: ForestHashtable 12 Online Information Sources A CDROM Contents B Some Copyrights and Licenses C Source Code for bonForum Web Application D Sun Microsystems, Inc. Binary Code License Agreement
  3. XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application Westy Rockwell www.newriders.com 201 West 103rd Street, Indianapolis, Indiana 46290 An Imprint of Pearson Education Boston • Indianapolis • London • Munich • New York • San Francisco
  4. XML, XSLT, Java, and JSP: A Case Publisher Study in Developing a Web Application David Dwyer Translation from the German language edition of: XML, XSLT, Java, and JSP by Westy Rockwell  2000 Galileo Press Associate Publisher GmbH Bonn, Germany Al Valvano FIRST EDITION: July 2001 Executive Editor All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or Stephanie Wall mechanical, including photocopying or recording, or by any Managing Editor information storage and retrieval system, without written Gina Brown permission from the publisher, except for the inclusion of brief quotations in a review. Product Marketing International Standard Book Number: 0-7357-1089-9 Manager Library of Congress Catalog Card Number: 00-110885 Stephanie Layton 05 04 03 02 01 7 6 5 4 3 2 1 Publicity Manager Interpretation of the printing code:The rightmost double- Susan Nixon digit number is the year of the book’s printing; the right- most single-digit number is the number of the book’s Software printing. For example, the printing code 01-1 shows that the Development first printing of the book occurred in 2001. Specialist Composed in Bembo and MCPdigital by New Riders Jay Payne Publishing Project Editor Printed in the United States of America Elise Walter Trademarks Copy Editor All terms mentioned in this book that are known to be Krista Hansing trademarks or service marks have been appropriately capital- ized. New Riders Publishing cannot attest to the accuracy of Indexer this information. Use of a term in this book should not be Larry Sweazy regarded as affecting the validity of any trademark or service mark. Java and JavaServer Pages (JSP) are registered trade- Manufacturing marks of Sun Microsystems, Inc. Coordinator Jim Conway Warning and Disclaimer Book Designer This book is designed to provide information about XML, Louisa Klucznik XSLT, Java, and JSP. Every effort has been made to make this book as complete and as accurate as possible, but no war- Cover Designer ranty or fitness is implied. Aren Howell The information is provided on an as-is basis.The authors Proofreader and New Riders Publishing shall have neither liability nor responsibility to any person or entity with respect to any loss Jeannie Smith or damages arising from the information contained in this Composition book or from the use of the discs or programs that may Gina Rexrode accompany it.
  5. ❖ THIS BOOK IS DEDICATED TO MEMORIES OF YOU, DON ROCKWELL, SR. YOU GAVE SO MUCH TO ME! DID I EVER SAY ENOUGH, SOMEHOW , FOR YOU TO KNOW HOW MUCH I LOVE YOU? WITHOUT YOUR LOVE AND KIND GENEROSITY, I WOULD NOT FEEL HALF SO FORTUNATE TO BE ALIVE TODAY. BESIDES THAT, YOU TAUGHT ME TO ENJOY THIS LIFE, SWIM IN THE BLUE OCEANS, AND DIVE DOWN SO DEEPLY. I WOULD MISS YOU FOREVER, IF I WERE NOT SO SURE THAT YOU ARE HERE. THANK YOU! W R. . ❖
  6. TABLE OF CONTENTS 1 Introduction and Requirements 1 1.1 The Goal of This Book 1 1.2 Why Use This Book? 2 1.3 How to Use This Book 4 1.4 Some Choices Facing Web Application Developers 6 1.5 Development Choices Made for This Book 8 1.6 A Note About Platform Independence 14 2 An Environment for Java Software Development 15 2.1 Java 2 Platform, Standard Edition 15 2.2 Compiling Java Programs 19 2.3 Running Java Programs 30 2.4 Debugging Java Programs 32 2.5 Other Features of ElixirIDE 33 3 Java Servlets and JavaServer Pages: Jakarta Tomcat 35 3.1 Apache Software Foundation 35 3.2 Jakarta Tomcat 36 3.3 Installing Tomcat 37 3.4 Running Tomcat 39
  7. Contents vii 3.5 Tomcat Examples of Servlets and JSPs 49 3.6 Adding Your Tomcat Web Application 49 3.7 Java Servlets and JSPs 53 3.8 The ServletConfig and ServletContext Classes 57 3.9 Web Application Scopes 58 4 XML and XSLT: Xerces and Xalan 61 4.1 Apache XML Project 61 4.2 Installing Xerces 62 4.3 Xerces Parses XML 64 4.4 SAX Sees XML as Events 67 4.5 Installing Xalan 67 4.6 Xalan Transforms XML Using XSLT 70 4.7 Using Beanshell with Xalan 72 4.8 Using Xalan from the Command Line 73 4.9 Zvon XSL Tutorial 73 4.10 Xerces and Xalan versus XT and XP 73 4.11 JSP and XML Synergy 74 5 bonForum Chat Application: Use and Design 77 5.1 Installing and Running bonForum 77 5.2 Changing the bonForum Web Application 83 5.3 Using XML to Design Web Applications 86 5.4 XML Data Flows in Web Applications 98
  8. viii Contents 6 bonForum Chat Application: Implementation 103 6.1 Building the bonForum Web Chat 103 6.2 Displaying and Selecting Chat Subjects 138 6.3 Displaying Chat Messages 140 6.4 Finding the Chat Element 146 6.5 Displaying and Selecting Chats 148 6.6 Displaying Guests in Chat 150 6.7 Outputting the bonForum Data as XML 150 6.8 Future of bonForum Project 151 7 JavaServer Pages: The Browseable User Interface 155 7.1 JSP-Based Web Applications 155 7.2 Viewing bonForum from Its JSP Documents 163 7.3 Further Discussion About the JSP in bonForum 187 8 Java Servlet and Java Bean: BonForumEngine and BonForumStore 189 8.1 The BonForumEngine Servlet 189 8.2 The BonForumStore Class 262 9 Java Applet Plugged In: BonForumRobot 285 9.1 Hands-on with Java Applets 285 9.2 XSLTProcessor Applet 290 9.3 BonForumRobot 290
  9. Contents ix 10 JSP Taglib: The bonForum Custom Tags 303 10.1 Java Servlets, JSP, and Tag Libraries 303 10.2 The bonForum Tag Library 316 10.3 The OutputDebugInfoTag Class 324 10.4 The OutputPathNamesTag Class 331 10.5 The OutputChatMessagesTag Class 340 10.6 XSLT and the TransformTag Class 352 10.7 Displaying the Available Chats 371 10.8 Displaying the Available bonForums 376 10.9 Displaying the Guests in a Chat 379 11 XML Data Storage Class: ForestHashtable 385 11.1 Overview of bonForum Data Storage 385 11.2 The NodeKey Class 387 11.3 The BonNode Class 388 11.4 ForestHashtable Maps Data Trees 390 11.5 Caching Keys for Fast Node Access 398 11.6 Adding ForestHashtable Nodes 404 11.7 Deleting ForestHashtable Nodes 411 11.8 Editing ForestHashtable Nodes 414 11.9 Getting ForestHashtable as XML 416
  10. x Contents 11.10 More Public ForestHashtable Methods 424 11.11 Initializing the bonForumXML Database 427 11.12 Runtime bonForumXML Database 429 11.13 More ForestHashtable Considerations 432 12 Online Information Sources 437 12.1 Always Useful Sites 437 12.2 Apache Software Foundation 438 12.3 Big Corporations 438 12.4 CSS 439 12.5 DOM Information 439 12.6 HTML 439 12.7 HTTP 439 12.8 Java 440 12.9 JavaServer Pages 441 12.10 Java Servlets 443 12.11 Linux 445 12.12 Open Source 445 12.13 RDF 446 12.14 Web Applications 446 12.15 Web Browsers 446 12.16 Web Servers 446 12.17 XML 447 12.18 XSL 452 A CD-ROM Contents 455 \Sun 456 \Apache 456 \bonForum 456 \tools 458 E-Book 458
  11. Contents xi B Some Copyrights and Licenses 459 BonForum License 459 Apache Xerces License 460 Apache Xalan License 461 Jakarta Tomcat License 462 C Source Code for bonForum Web Application 465 D Sun Microsystems, Inc. Binary Code License Agreement 703 Index
  12. About the Author Westy Rockwell considers himself a world citizen. Currently he is a senior developer at tarent GmbH, a Web development company in Bonn, Germany. His greatest pleasure is enjoying the company of his wife, Zamina, and their two daughters, Joaquina and Jennifer. Somehow, they tolerate his intense involvement with computers. Westy has more than 15 years of experience as a professional soft- ware developer, but his involvement with computers dates back longer yet. In 1965, he programmed the Pythagorean theorem into an IBM 1620 with punched cards. His faculty adviser told him to stop spending so much time on programming, which had no career future. In 1970, while studying IBM 360 programming, he was con- sidered too radical for saying that computers would one day play chess. It was not until the early 1980s, with the arrival of micro- computers, that his career and his passion could merge. His real software education came from deeply hacking many microcomputers, including the ZX80, the Osborne, the Vic20, the C64, various Amigas, and, of course, IBM PCs. His career, mean- while, involved him with more respectable software and hardware, including UNIX, workstations, minicomputers, mainframes, and, of course, IBM PCs. Interest in hardware design, along with C and assembly languages, culminated in 1994 when he built the prototype for an extremely successful dual-processor alcohol analyser, including the PCB design, operating system, and application software. Soon afterward, while developing man-machine interfaces, the pre- release version of Borland Delphi turned Westy into a Windows developer. He went on to work on three-tier systems based on Windows NT, including corporate asset management, document imaging, and work management systems. For more than a year now he has refused to touch SQL or Visual tools, and he is enthusiastically pursuing Web browser- and server-based applications using Java, Tomcat, Xerces, and Xalan. xii
  13. About the Technical Reviewers These reviewers contributed their considerable hands-on expertise to the entire development process for XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application. As the book was being written, these dedicated professionals reviewed all the material for technical content, organization, and flow.Their feedback was critical to ensuring that XML, XSLT, Java, and JSP: A Case Study in Developing a Web Application fits our reader’s need for the highest-quality technical information. Brad Irby holds a bachelor of computer science degree from the University of North Carolina, and he has been a programmer and system designer since 1985. He has worked with many different languages and databases over the years, but he now specializes in application development using a Microsoft SQL Server back end. A pri- vate consultant for eight years, Brad has been following the progress of the W3C and the XML specification since its inception, and he has done extensive work using the XML extensions of SQL Server to transfer data over secure internet links. He can be reached at Brad@BradIrby.com. Perry Tew graduated from Georgia Institute of Technology with a degree in chemi- cal engineering, but he has since fallen in love with computer programming. Perry began his IT career as a MCSD and currently programs with Java. He works as an integration specialist for a major contact lens producer. He spends his free time with his wife, Paula, basking in they joy of parenthood brought by the arrival of their newborn, Joshua. Acknowledgments Most of all, I want to thank Jennifer, Joaquina and Zamina Rockwell, who are the real treasures in my life.Without their love, understanding, playfulness and patience this book could never have been written. There are so many others to thank, I know I will omit some here: those who toiled behind the scenes, those who taught me, worked with me, helped me come to this point in my professional career. If you are one of these, I would like to thank you as well. Please forgive the unintentional omission of your name. Thanks are especially due to Elmar Geese, CEO of tarent GmbH, for making this book possible. Also, Manfred Weltecke, for his masterful translation of the first book version into German, to which it largely owes its success. Much credit for that success also belongs to Harald Aberfeld, Michael Klink and Florian Hawlitzek, for their tech- nical editing of the German edition. xiii
  14. Thanks to all my colleagues at tarent GmbH, for their selfless support of the book project: Alex Steeg, Alexander Scharch, Boris Esser, Harald Aberfeld, Hendrik Helwich, Kerstin Weber, Markus Heiliger, Martina Hundhausen, Matthias Esken, May-Britt Baumann, Michael Klink, Robert Schuster,Thomas Mueller-Ackermann,Vanessa Haering, and Vera Schwingenheuer. My absence from their projects while working on this book created extra work for them; I appreciate that truly. Thanks to the staff of Galileo Press, especially my editors Judith Stevens and Corinna Stefani, for making the German edition happen. Others there whose work on the book is appreciated are: proofreaders Claudia Falk and Hoger Schmidt, cover designer Barbara Thoben, illustrator Leo Leowald, producer Petra Strauch, and com- puter typographer Joerg Gitzelmann.Thanks also to Petra Walther and Stefan Krumbiegel of Galileo Press for supporting the German edition online. Thanks to Lau Shih-Hor and Agnes Chin of Elixir Technologies, for adding value to the CDROM.Thanks to the developers of TextPad, so useful for a technical writer. Thanks to Jen Wilson for creating bonForum.links2go.com, in support of the book project. This book depends so much upon those who make the open source projects it and its example project depend upon.Thanks to all involved with the Apache Software Foundation, especially its Jakarta and Apache XML projects.Thanks also to the staff and providers of SourceForge for making it a superb place to develop and learn about open source software.Thanks to Sun for making its JDK available for learning Java. Many thanks to the staff of New Riders who made the English version of the book happen. Especially to Stephanie Wall (Executive Editor), who went way beyond the call of duty to keep the book alive until publication, and to Elise Walter (Project Editor), who always kept her good humor no matter how late my requests for changes came to her.The book was vastly improved by the “no-holds-barred” technical edi- tors, including Brad Irby, Erin Mulder, and Perry Tew.Thanks to Jay Payne (Media Developer), who produced the CD-ROM. After working with words for over a year myself, I know I owe so much of this book’s existence to Krista Hansing (Copy Editor), Larry Sweazy (Indexer), Gina Rexrode (Compositor).Thanks also to Susan Nixon (Public Relations). Thanks to Jeffrey E. Northridge, whose friendship and partnership-in-programming has been so valued by me.Thanks to Jaime del Palacio, a superb software developer (and nephew).Thanks to PhoenixFire, for giving me that first, all-important chance as a professional software developer (If you read this, please contact me!).Thanks to John Haefeli of ISI, who provided so many difficult real-world problems to solve with C. Thanks to Alvaro Pastor, Glenn Forrester, and all the gang who were at Intoximeters West, especially to Doug, Iza, Petcy who helped so much to develop me as a software developer.Thanks to Paul McEvoy for his mentoring and my appreciation of cafe latte.Thanks to Elliot Mehrbach for helping me learn SQL and Delphi. Finally, I would like to thank especially Daph, Cita and Marcos Rockwell, and all my other relatives, for their unconditional love.Thanks also to Nature and Life, for their unconditional and priceless support. xiv
  15. Tell Us What You Think As the reader of this book, you are the most important critic and commentator.We value your opinion and want to know what we’re doing right, what we could do bet- ter, what areas you’d like to see us publish in, and any other words of wisdom you’re willing to pass our way. As an Executive Editor at New Riders Publishing, I welcome your comments.You can fax, email, or write me directly to let me know what you did or didn’t like about this book—as well as what we can do to make our books stronger. Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message. When you write, please be sure to include this book’s title and author, as well as your name and phone or fax number. I will carefully review your comments and share them with the author and editors who worked on the book. Fax: 317-581-4663 Email: stephanie.wall@newriders.com Mail: Stephanie Wall Executive Editor New Riders Publishing 201 West 103rd Street Indianapolis, IN 46290 USA xv
  16. Introduction For more than 20 years, I have read books about software development. Many of these repeated information available to me elsewhere. Formerly, that information was often from magazines; recently its source is the Internet. A few books, refreshingly, were based instead upon the authors’ “hands on” experiences with the art and science of software development. You can now write a book about how to become a gourmet chef without ever having cooked a meal. Simply download a collection of recipes from the Web, organize and paraphrase them, and, presto! A book is born, ready to meet the market demand. Especially in the field of software development, many books seem to have been writ- ten in this way. When I was asked to write a book about Web application development with XML and Java, I replied that the book would have to be a practical “how-to” manual, based upon real development experiences. Its target audience would be software developers trying to understand and harness those technologies. I knew that to write that book, I would have to “cook the meal” myself. My fundamental task would be to develop a functional and timely Web application project, of at least plausible utility. Surfing the Web, I soon gathered very much information. I determined which of all the available tools and products this book would feature. Most of them were then in a state of flux, and all are still evolving. In fact, a worldwide effort is continually imple- menting products based on ever-evolving tools and standards related to XML and Java. Even for an experienced software developer, putting all this information and tech- nology to practical use was no simple task. Many of the well-documented tools were obsolete, and the more current tools were often not well documented. Extremely active mailing lists were frequented by early adopters building real Web applications; these pioneers often faced with incompatibilities between the tools and the standards. At first, my plan was to complete the earlier chapters, which present the tools and technologies, and then to develop the book project and write the later chapters. It soon became clear that this would put the cart before the horse. I decided to first cre- ate the Web application and only then, always in the context of that project, to discuss how XML and Java-based technologies could be applied by the reader. That is when the fun started. I designed and implemented a Web chat application called bonForum. It is based on XML and XSLT, Java servlets and applets, and JSP. It presented me with many of the most challenging tasks of Web application design. As a very popular and timely type of Web application, I trust that it will interest the reader. As an experiment and a tutorial, its design and implementation provide a framework for ongoing development by the readers of this book. It can and should morph into other types of Web applications besides a chat room. I welcome bug reports, fixes, suggestions, feedback, and communication! Please contact me at mail@bonforum.org. Look for errata, version updates, mailing lists, and related information at http://www.bonforum.org. xvi
  17. Conventions Used in This Book Monospaced font is used to indicate code, including commands, options, objects, and so on. It is also used for Internet addresses. Italics are used to introduce and define a new term. Code continuation characters are used in code listings that are too long to fit within the book’s margins.They should not be used in actual implementation. How This Book Is Organized This book is organized so that you can easily follow along with the case study and build the Web chat application along with the author and his team. Each chapter builds on the previous one. Chapter 1, “Introduction and Requirements,” explains the goal of writing this book. It also describes why certain tools were selected for the project. Chapter 2, “An Environment for Java Software Development,” teaches you how to set up an inexpensive Java development environment. It shows you how to compile, debug and run the Web application example project. Chapter 3, “Java Servlets and JavaServer Pages: Jakarta Tomcat,” introduces Tomcat, which is an HTTP server and a container for Java Servlets and JavaServer Pages. Chapter 4, “XML and XSLT: Xerces and Xalan,” introduces Xerces, a DOM and a SAX parser, and Xalan, an XSLT and XPATH processor. Chapter 5, “BonForum Chat Application: Use and Design,” introduces you to bonForum, the Web chat application that will be the major subject of the rest of the book. It was designed as a tool to explore each of the subjects of this book, XML, XSLT, Java Servlets, Java Applets and JavaServer Pages, while solving some real Web application problems. Chapter 6, “BonForum Chat Application: Implementation,” continues the overview of bonForum that began in Chapter 5. Some tougher implementation problems are also highlighted, and suggestions for future development of the Web chat are given. Chapter 7, “Java Servlet and Java Bean: BonForum Engine and bon Forum Store,” teaches the JSP technology that the Tomcat Server supports, as JavaServer Pages are used to create a BUI, a browseable user interface, for our Web application. Chapter 8, “Java Servlet in Charge: BonForumEngine,” describes the central class in the bonForum Web application. It also illustrates some themes common to using Java Servlets in Web applications. Chapter 9, “Java Applet Plugged In: BonForumRobot,” discusses the bonForumRobot applet, which is part of the bonForum Web chat application.This chapter teaches how to create and deploy a Java Applet to control a Web application user interface and use Sun Java Plug-in to support an Applet on the client. xvii
  18. Chapter 10, “JSP Taglib:The bonForum Custom Tags,” explains how to use a JSP Tag Library with the bonForum Web application. All the functions that are included in the multi-purpose ChoiceTag are discussed, which are used on many of the JSP docu- ments in the Web chat example.This chapter also shows you how the Apache Xalan XSLT processor is used from the custom tag. Chapter 11, “XML Data Storage Class: ForestHashtable,” shows how data storage for the XML data in the bonForum chat application is implemented.This chapter also teaches how to add a few tricks to a descendant of the Hashtable class to optimize XML element retrieval and simulate a database program design. Chapter 12, “Online Information Sources,” provides links to XML, XSLT, Java Servlet and JSP information. Appendices A and B provide the CD-ROM contents and copyright information. The project’s source code is listed in Appendix C. An added note: when the author uses the term “we” throughout the book, he is referring to the team that worked on the bonForum Web application. xviii
  19. 1 Introduction and Requirements I N THIS CHAPTER, YOU FIND OUT WHAT we want this book to provide.We also pre- sent the choices made to support the “practical” side of the book. Here we try to jus- tify the software tools and libraries that we selected to illustrate a large subject: developing Web applications powered by XML, XSLT, Java servlets, Java applets, and JavaServer Pages. 1.1 The Goal of This Book While writing this book, we have assumed that you, its reader, are a software developer with some Java experience and that you want to build Web applications based on XML, XSLT, Java servlets, Java applets, and JavaServer Pages.The goal of this book is to support you as you learn about using all of these increasingly important technologies together.This book will help you become familiar with a set of widely available and professional software tools that covers all these technologies. Furthermore, it will intro- duce you to many of the tasks that you will encounter in your own projects, by tack- ling these tasks within the context of a realistically large example project: a Web application named bonForum. The examples and the Web application project for this book were developed on a PC using Windows NT 4.0. If you prefer, you can use this book together with Windows 95, 98, or 2000 instead.With a bit more effort, an experienced developer could use much of the material in this book with a Linux or UNIX operating
Đồng bộ tài khoản