Sebesta PWWW6e Page iii Tuesday, February 2, 2010 4:31 PMPR OGR AMM IN G T H EW ORLD W IDE W EBS I XTH EDI TI O NROBERT W. SEBESTAUniversity of Colorado at Colorado SpringsAddison-WesleyBoston Columbus Indianapolis New York San Francisco Upper Saddle RiverAmsterdam Cape Town Dubai London Madrid Milan Munich Paris Montreal TorontoDelhi Mexico City Sao Paulo Sydney Hong Kong Seoul Singapore Taipei Tokyo

Sebesta PWWW6e Page iv Tuesday, February 2, 2010 4:31 PMEditor in Chief: Michael HirschAcquisitions Editor: Matt GoldsteinEditorial Assistant: Chelsea BellManaging Editor: Jeffrey HolcombSenior Production Project Manager: Marilyn LloydMedia Producer: Katelyn BollerDirector of Marketing: Margaret WaplesMarketing Coordinator: Kathryn FerrantiSenior Manufacturing Buyer: Carol MelvilleText Designer: Gillian Hall/The Aardvark GroupCover Designer: Elena SidorovaCover Image: Andrew Parkinson/Getty ImagesProject Management: Dennis Free/Aptara , Inc.Full Service Vendor: Aptara, Inc.Text and Cover Printer: Courier StoughtonThe interior of this book was composed in FrameMaker 7.0, JansonText, Courier10PitchBTCopyright 2011, 2010, 2008, 2006, 2003, 2002 Pearson Education, Inc., publishing as Addison-Wesley. Allrights reserved. Manufactured in the United States of America. This publication is protected by Copyright, andpermission should be obtained from the publisher prior to any prohibited reproduction, storage in a retrievalsystem, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, orlikewise. To obtain permission(s) to use material from this work, please submit a written request to PearsonEducation, Inc., Permissions Department, 501 Boylston Street, Suite 900, Boston, Massachusetts 02116.Many of the designations by manufacturers and seller to distinguish their products are claimed as trademarks.Where those designations appear in this book, and the publisher was aware of a trademark claim, thedesignations have been printed in initial caps or all caps.Library of Congress Cataloging-in-Publication DataSebesta, Robert W.Programming the World Wide Web, 2010 / Robert W. Sebesta. -- 6th ed.p. cm.Previous eds. under title: Programming the World Wide Web.ISBN 978-0-13-213081-31. Internet programming.2. World Wide Web. I. Sebesta, Robert W. Programming the World Wide Web. II. Title.QA76.625.S42 2010b006.7'6--dc2220090538551098765432 1—CRS—1413121110ISBN-10:0-13-213081-5ISBN-13: 978-0-13-213081-3

Sebesta PWWW6e Page v Tuesday, February 2, 2010 4:31 PMTo Aidan

Sebesta PWWW6e Page vi Tuesday, February 2, 2010 4:31 PM

Sebesta PWWW6e Page vii Tuesday, February 2, 2010 4:05 PMBrief Contents12345678910111213Fundamentals1Introduction to XHTML35Cascading Style Sheets91The Basics of JavaScript129JavaScript and XHTML DocumentsDynamic Documents with JavaScriptIntroduction to XMLIntroduction to FlashIntroduction to PHPIntroduction to AjaxJava Web Software185227265311353401431Introduction to ASP.NET495Database Access through the Web559vii

Sebesta PWWW6e Page viii Tuesday, February 2, 2010 4:05 PMviiiBrief Contents1415Introduction to Ruby601Introduction to Rails645Appendix A Introduction to Java 687Appendix B Named Colors 703Appendix C Java Applets 707Index 735

Sebesta PWWW6e Page ix Tuesday, February 2, 2010 4:05 . Brief Introduction to the Internet 2The World Wide Web 6Web Browsers 7Web Servers 8Uniform Resource Locators 11Multipurpose Internet Mail Extensions 13The Hypertext Transfer Protocol 15Security 18The Web Programmer’s Toolbox 20Summary 28Review Questions 30Exercises 322Introduction to XHTML2. and Evolution of HTML and XHTML 36Basic Syntax 38Standard XHTML Document Structure 39Basic Text Markup 40Images 49Hypertext Links 54Lists 57Tables 63Forms 71Syntactic Differences between HTML and XHTML 84Summary 85Review Questions 86Exercises 88ix

Sebesta PWWW6e Page x Tuesday, February 2, 2010 4:05 PMxContents3Cascading Style 1Introduction 92Levels of Style Sheets 93Style Specification Formats 94Selector Forms 96Property Value Forms 99Font Properties 100List Properties 106Color 110Alignment of Text 112The Box Model 114Background Images 119The span and div Tags 121Conflict Resolution 122Summary 124Review Questions 126Exercises 1274The Basics of .134.14129Overview of JavaScript 130Object Orientation and JavaScript 133General Syntactic Characteristics 134Primitives, Operations, and Expressions 137Screen Output and Keyboard Input 146Control Statements 150Object Creation and Modification 158Arrays 159Functions 164An Example 169Constructors 170Pattern Matching Using Regular Expressions 171Another Example 176Errors in Scripts 177Summary 179Review Questions 181Exercises 183

Sebesta PWWW6e Page xi Tuesday, February 2, 2010 4:05 PMContents5JavaScript and XHTML Documents5. JavaScript Execution Environment 186The Document Object Model 187Element Access in JavaScript 191Events and Event Handling 193Handling Events from Body Elements 197Handling Events from Button Elements 199Handling Events from Text Box and Password Elements 204The DOM 2 Event Model 214The navigator Object 219DOM Tree Traversal and Modification 221Summary 222Review Questions 223Exercises 2246Dynamic Documents with duction 228Positioning Elements 228Moving Elements 234Element Visibility 237Changing Colors and Fonts 238Dynamic Content 242Stacking Elements 245Locating the Mouse Cursor 249Reacting to a Mouse Click 251Slow Movement of Elements 253Dragging and Dropping Elements 256Summary 261Review Questions 262Exercises 2627Introduction to XML7. 266The Syntax of XML 268XML Document Structure 271Document Type Definitions 272227xi

Sebesta PWWW6e Page xii Tuesday, February 2, 2010 4:05 PMxiiContents7. 279XML Schemas 281Displaying Raw XML Documents 289Displaying XML Documents with CSS 291XSLT Style Sheets 293XML Processors 302Web Services 304Summary 305Review Questions 307Exercises 3098Introduction to Flash8. and Uses of Flash 312A First Look at the Flash Authoring Environment 312Drawing Tools 318Static Graphics 327Animation and Sound 332User Interactions 343Summary 348Review Questions 350Exercises 3519Introduction to rigins and Uses of PHP 354Overview of PHP 354General Syntactic Characteristics 355Primitives, Operations, and Expressions 356Output 362Control Statements 364Arrays 367Functions 376Pattern Matching 379Form Handling 382Files 388Cookies 391Session Tracking 394Summary 394Review Questions 396Exercises 398

Sebesta PWWW6e Page xiii Tuesday, February 2, 2010 4:05 PMContents10Introduction to Ajax10.110.210.310.410.5401Overview of Ajax 402The Basics of Ajax 405Return Document Forms 415Ajax Toolkits 419Security and Ajax 427Summary 428Review Questions 429Exercises 43011Java Web ction to Servlets 432The NetBeans Integrated Development Environment 437A Survey Example 447Storing Information on Clients 454JavaServer Pages 463JavaBeans 475Model-View-Controller Application Architecture 479JavaServer Faces 480Summary 489Review Questions 490Exercises 49212Introduction to ASP.NET12.112.212.312.412.512.6495Overview of the .NET Framework 496A Bit of C# 499Introduction to ASP.NET 503ASP.NET Controls 510ASP.NET AJAX 540Web Services 546Summary 555Review Questions 556Exercises 55813Database Access through the Web13.113.213.313.4559Relational Databases 560An Introduction to the Structured Query Language 562Architectures for Database Access 567The MySQL Database System 569xiii

Sebesta PWWW6e Page xiv Tuesday, February 2, 2010 4:05 PMxivContents13.5 Database Access with PHP and MySQL 57213.6 Database Access with JDBC and MySQL 58113.7 Database Access with ASP.NET and MySQL 589Summary 596Review Questions 598Exercises 60014Introduction to igins and Uses of Ruby 602Scalar Types and Their Operations 602Simple Input and Output 610Control Statements 613Fundamentals of Arrays 618Hashes 623Methods 625Classes 630Blocks and Iterators 635Pattern Matching 638Summary 641Review Questions 642Exercises 64215Introduction to Rails15.115.215.315.4645Overview of Rails 646Document Requests 648Rails Applications with Databases 655Rails with Ajax 675Summary 683Review Questions 684Exercises 685Appendix A Introduction to Java 687Appendix B Named Colors 703Appendix C Java Applets 707Index 735

Sebesta PWWW6e Page xv Thursday, February 4, 2010 3:45 PMPrefaceIt is difficult to overestimate the effect the World Wide Web has had on theday-to-day lives of people, at least those in the developed countries. In just15 years, we have learned to use the Web for a myriad of disparate tasks, rangingfrom the mundane task of shopping for airline tickets to the crucial earlymorning gathering of business news for a high-stakes day trader.The speed at which millions of Web sites appeared in the last decade wouldseem to indicate that the technologies used to build them were sitting on theshelf, fully developed and ready to use, even before the Web was developed.Also, one might guess that the tens of thousands of people who built those siteswere sitting around unemployed, waiting for an opportunity and already possessing the knowledge and abilities required to carry out this mammoth construction task when it appeared. Neither of these was true. The need for newtechnologies was quickly filled by a large number of entrepreneurs, some atexisting companies and some who started new companies. A large part of theprogrammer need was filled, at least to the extent to which it was filled, by newprogrammers, some straight from high school. Many, however, were previouslyemployed by other sectors of the software development industry. All of themhad to learn to use new languages and technologies.A visit to a bookstore, either a bricks-and-mortar store or a Web site, willturn up a large supply of books on Web technologies aimed at the practicingprofessional. One difficulty encountered by those teaching courses in Web programming technologies in colleges is the lack of textbooks that are targeted totheir needs. Most of the books that discuss Web programming were written forprofessionals, rather than college students. Such books are written to fulfill theneeds of professionals, which are quite different from those of college students.One major difference between an academic book and a professional book lies inthe assumptions made by the author about the prior knowledge and experienceof the audience. On the one hand, the backgrounds of professionals vary widely,making it difficult to assume much of anything. On the other hand, a book written for junior computer science majors can make some definite assumptionsabout the background of the reader.xv

Sebesta PWWW6e Page xvi Thursday, February 4, 2010 3:45 PMxviPrefaceThis book is aimed at college students, not necessarily only computer science majors, but anyone who has taken at least two courses in programming.Although students are the primary target, the book is also useful for professionalprogrammers who wish to learn Web programming.The goal of the book is to provide the reader with a comprehensive introduction to the programming tools and skills required to build and maintainserver sites on the Web. A wide variety of technologies are used in the construction of a Web site. There are now many books available for professionals thatfocus on these technologies. For example, there are dozens of books that specifically address only XHTML. The same is true for a half-dozen other Web technologies. This book provides an overview of how the Web works, as well asdescriptions of many of the most widely used Web technologies.The first five editions of the book were used to teach a junior-level Webprogramming course at the University of Colorado at Colorado Springs. Thechallenge for students in the course is to learn to use several differentprogramming languages and technologies in one semester. A heavy load of programming exercises is essential to the success of the course. Students in thecourse build a basic, static Web site, using only XHTML as the first assignment.Throughout the remainder of the semester, they add features to their site as thenew technologies are discussed in the course. Our students’ prior course work inJava and data structures, as well as C and assembly language, is helpful, as is thefact that many of them have learned some XHTML on their own before takingthe course.The most important prerequisite to the material of this book is a solid background in programming in some language that supports object-oriented programming. It is helpful to have some knowledge of a second programminglanguage and a bit of UNIX, particularly if a UNIX-based Web server is usedfor the course. Familiarity with a second language makes learning the new languages easier.Table of ContentsThe book is organized into three parts: the introduction (Chapter 1), client-sidetechnologies (Chapters 2–8), and server-side technologies (Chapters 9–15).Chapter 1 lays the groundwork for the rest of the book. A few fundamentalsare introduced, including the history and nature of the Internet, the WorldWide Web, browsers, servers, URLs, MIME types, and HTTP. Also includedin Chapter 1 are brief overviews of the most important topics of the rest of thebook.Chapter 2 provides an introduction to XHTML, including images, links,lists, tables, and forms. Small examples are used to illustrate many of theXHTML elements that are discussed in this chapter.The