Ajax on Java
"This is cool, look!" I told a group of coworkers. "What is it?" one of them asked. "It's Google Maps, and it uses Ajax," I said. "What's Ajax?" "It stands for Asynchronous JavaScript and XML. It allows a request from a web page to go to the server, get data, and display it without the user hitting submit and waiting for a page refresh." "Wow, that could give my application the responsiveness of a desktop application!" Until now, the choice for web developers has been between thin-client web applications and rich applications that require installs. With Ajax, you can build web applications that have the responsiveness of rich applications, without the overhead of keeping the end user up-to-date with the latest software. This is truly a great opportunity for web developers to write more responsive applications. Ajax: Some History
In the beginning there was HTML, and the world saw that it was good. Soon after came web applications, and the world was overjoyed with the ability to interact with data. There were search engines, online bill paying services, stock trading sites, interactive games, online shopping facilities, and much, much more. So what's missing from the world of web applications? The answer is responsiveness. Back in 1984, I experienced my first real intuitive interaction with a computer. I was in college, and in the dorm was a study lab that had just been equipped with Apple Computer, Inc.'s new product: the Macintosh. These computers had a definite wow effect on the students. There was only one program, MacWrite, but that was enough for me. I was immediately sold on the friendly, easy-to-use experience that the MacWrite application gave me, as were many other students. Until recently, browser-based web applications haven't been able to deliver the kind of experience users expect from desktop applications. Sure, some web applications do it with a rich client. But rich clients require overhead not present in browser-based applications. For ease of deployment and of keeping users current with the latest version, nothing beats a browser-based application. What would be ideal would be a browser-based application with a rich-client feel. Meet Ajax. Some of you probably know that Ajax technology has been around for a while, and that it wasn't always called Ajax. The term Ajax (Asynchronous JavaScript and XML) was coined by Jesse James Garrett of Adaptive Path in his article "Ajax: A New Approach to Web Applications" (http://www.adaptivepath.com/publications/essays/archives/000385.php). After that article appeared, there were plenty of comments about the fact that the approach wasn't really "new"; many developers were creating asynchronous applications before XMLHttpRequest became available. Java applets, despite their shortcomings, could deliver web applications that felt more like desktop applications. So could Flash applications. So what's changed? What's the big deal? Well, now we at least have a name for the practice. That may not seem like much, but it gives us common ground to discuss it. Just as design patterns give us names to use when discussing programming techniques, the name Ajax instantly tells us which web programming technique is being used. Since Garrett's article was published, there has been much discussion of how to use Ajax and of its capabilities and shortcomings. The appearance of articles, tools, and information relating to Ajax has lead to an explosion of interest. As information about Ajax becomes more and more widely available, Ajax techniques and usage will become more mainstream and will come to be expected by the users of the web applications we write. That is the power of a name. Ajax narrows the gap between a rich client application and a thin, browser-based client application. This book will introduce you to Ajax by illustrating how to create Ajax applications in a server-side Java environment: how to add Ajax features to servlet-based applications, JSPs, JSF applications, and so on. So, join me in this exciting endeavor: let's strive to make our web applications more interactive, less boring, and more efficient by avoiding redundant data entry and long wait times between page loadsin short, to create a user experience closer to that of a real desktop application. These are some of the promises of Ajax technology. Audience
This book was written for progressive Java developers of all levels, especially those developing web applications. I say "progressive" because with the information provided in this book, you will be able to take your web programming to the next level. That next level is a higher level of usability for your customers, where clunky web applications are replaced with more-responsive, Ajax-enhanced applications. Assumptions This Book Makes
Java developers with web application experience should have no trouble understanding this book. I assume some experience with Java servlets, HTML, and JavaScript. Some experience with XML parsing is helpful, but not necessary. Contents of This Book
This book is divided into 10 chapters:
Conventions Used in This Book
The following typographical conventions are used in this book:
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product's documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Ajax on Java by Steven Douglas Olson. Copyright 2007 O'Reilly Media, Inc., 978-0-596-10187-9." If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com. How to Contact Us
Please address comments and questions concerning this book to the publisher:
We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:
To comment or ask technical questions about this book, send email to:
For more information about our books, conferences, Resource Centers, and the O'Reilly Network, see our web site at:
Safari® Enabled
Safari offers a solution that's better than e-books. It's a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com. Acknowledgments
I am very grateful for the help that I received while writing this book. In January 2004, when I read Jesse James Garrett's now famous article describing and coining the term Ajax, I felt that it was the start of a revolution in web development. Although some very innovative developers had already begun using Ajaxian techniques for a richer web experience, the movement really moved from a smoldering potential into a raging fire after early 2004. I am grateful to the army of developers who have crafted such frameworks as DWR (Joe Walker), Dojo, Rico (Richard Cowen, Bill Scott, Darren James), and Scriptaculous (Thomas Fuchs). Also, thanks to the Google Web Toolkit team and Ed Burns, Greg Murray, and Tor Norbye for their work on JavaServer Faces and Ajax. Many evangelists have helped the movement as well. One site that has been a great source of information is Ajaxian.com, run by Ben Galbraith and Dion Almaer. Thanks to this site, much information and help for developers is available. My editor, Mike Loukides, has been a huge help in this effort. Mike helped make many difficult subjects easy and turned some of my cryptic sentences into readable, understandable prose. He has been an invaluable resource. The reviewers for this book have also been very helpful. Michael Davis examined much of the code and helped identify problems. David Lakis helped with the flow and helped make sure that the content was readable. Vimal Kansal reviewed many of the technical details. Finally, I'm grateful to my family for putting up with me throughout this project. I'd like to thank my kids: Jordan, Erik, Stefani, Matthew, and Kyra. I couldn't have done this without the help and support of my wife, Erin; my thanks and love go especially to her. Steven Douglas Olson November 2006 |
Категории