preface


"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:


Chapter 1, Setup

This chapter describes the environment that is needed to run the Ajax examples in this book. The examples use the Tomcat container, but if you are experienced with another J2EE container, you should be able to use that container as well.


Chapter 2, JavaScript for Ajax

This chapter explains how to use JavaScript to access Ajax functionality and demonstrates how JavaScript is used to make asynchronous calls with the XMLHttpRequest object.


Chapter 3, A Simple Ajax Servlet

This chapter explains how to service an Ajax client using a servlet. This is where this book differs from other Ajax books: it uses Java on the backend rather than another technology such as PHP, Perl, or Rails.


Chapter 4, XML and JSON for Ajax

Although XML seems to be an integral part of Ajax, it is not required. This chapter discusses how to use XML to index the data coming back to the client and presents JSON as an attractive alternative to XML for performing the same function.


Chapter 5, Getting Useful Data

This chapter illustrates how to store the data for Ajax applications in a database, as well as how to retrieve that data.


Chapter 6, Ajax Libraries and Toolkits

A large number of frameworks and toolkits have appeared on the Ajax scene to help developers leverage some of the necessary functions that have to be written to support Ajax. This chapter explores several of those frameworks and toolkits: Dojo, Rico, Prototype, DWR, and Scriptaculous.


Chapter 7, Ajax Tags

JavaServer Pages (JSPs) have the ability to reuse code through tag libraries. This chapter explains how to create Ajax tags for JSPs.


Chapter 8, Ajax on Struts

Integrating Ajax into Struts applications is the subject of this chapter.


Chapter 9, JavaServer Faces and Ajax

This chapter provides an example of how to use Ajax with JavaServer Faces.


Chapter 10, Google Web Toolkit

The Google Web Toolkit, which allows for roundtrip debugging on Ajax code, offers a very exciting entry into using Ajax with Java. This chapter provides a tutorial for using this cutting-edge toolkit privided by Google for Ajax developers.

Conventions Used in This Book

The following typographical conventions are used in this book:


Plain text

Indicates menu titles, menu options, buttons, and keyboard accelerators (such as Alt and Ctrl).


Italic

Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix utilities.


Constant width

Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, the contents of files, and the output from commands.


Constant width bold

Shows commands or other text that should be typed literally by the user.

This icon signifies a tip, suggestion, or general note.


This icon indicates a warning or caution.


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:

O'Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/9780596101879

To comment or ask technical questions about this book, send email to:

bookquestions@oreilly.com

For more information about our books, conferences, Resource Centers, and the O'Reilly Network, see our web site at:

http://www.oreilly.com

Safari® Enabled

When you see a Safari® Enabled icon on the cover of your favorite technology book, that means the book is available online through the O'Reilly Network Safari Bookshelf.

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




Ajax on Java
Ajax on Java
ISBN: 0596101872
EAN: 2147483647
Year: 2007
Pages: 78

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net