
|
Web development just got a whole lot cleaner!by Duncan Kenzie AJAX is the hottest new technology to hit Web development in a long
time. Well, it's not really new. The technology behind AJAX has been
available for years now, but it has languished in relative obscurity
until this year, when a company called Adaptive Path coined the term
and wrote a seminal
essay on the subject. So What's All the Fuss About?Simply put, AJAX lets you create Web applications that respond immediately to user input or events. Suppose, for example, you have a form that requires users to type name and address information. In a non-AJAX application, you'd have them fill in all the details and then click OK to submit the form. Any errors would then be returned, along with the whole page contents, and shown somewhere on the page. This is analogous to green-screen programming, where the server has no interaction with the user until the Enter key is pressed. With AJAX, you can intercept user key strokes or mouse events as they happen and communicate with server-side code immediately. For example, as the user types a ZIP code, you could send that information to the server to do a lookup on city and state, find the info in a table, return it to the page, and update those form fields immediately in the page. This all happens seamlessly--without the page being entirely reloaded and without any annoying or confusing page flickers or clicks. In essence, this is event-driven client/server programming, where the "client" is a browser. So AJAX gives you all the benefits of client/server programming without the attendant headaches of software distribution issues; the client code is always up-to-date with the server code, because the client's just a browser. How Google Has Popularized AJAXLet's consider Gmail. Most of us are familiar with Web-based email programs, such as Yahoo's mail or hotmail. In these programs, when you compose an email, you can look up an intended recipient's email address by clicking a button that brings up a separate window with a list of contacts. You probably have many email addresses in your contacts list, and looking for a particular contact involves typing in a search value and clicking a search button or clicking on a letter. You then have to scroll through the subsetted list to find the contact you want and select it. So this involves at least four steps:
Gmail has reduced this to two steps:
How does Google do this? At first, I suspected they were loading my entire address list in the page, behind the scenes, and using a client-side script to traverse it. But that seemed unwieldy; it would mean that Google was reading and downloading all 500 of my contacts every time I went to look at my email, even before composing one. In fact, what Fig 1.1 Gmail AJAX contact dropdown box Google does is to employ AJAX technology in a very simple but powerful way. As I start typing characters in the To box, an AJAX call is made to the server to return up to 15 hits based on what I've typed so far. So in the example image below, you can see I've typed the letter m, and Gmail returns hits with M in them.If I then type an i, the list is narrowed down further. Each time I type a letter, the browser runs a script to do an AJAX call to the server. The server immediately responds to the AJAX call and returns a new list of hits, which get placed in the page dynamically.
Fig 1.2 Gmail AJAX contact dropdown box Here's a summary of what happens:
This function updates a specific portion of the page contents. Other Uses for AJAXGoogle Maps is another example
of AJAX put to good use. In this case, as you zoom in or out of a map,
Google makes an AJAX call to retrieve a different map image and display
it in your browser. This all looks seamless to you; there's no evident
page refresh. Uses for AJAX in Your Own Business ApplicationsSo you're probably wondering how you can improve your business applications with AJAX. Here are just a few ideas:
How Does AJAX Work?Remember that AJAX stands for Asynchronous JavaScript and XML. So there are three main components to AJAX technology: Asynchronous"Asynchronous" describes the communication method between the browser and the server. It lets the browser and server talk to each other in the background while the user interacts with the page. You could deploy AJAX in synchronous mode (even though that's an oxymoron), but in this mode, the user must wait while the browser locks up and waits for a server response. For this reason, most implementations of AJAX are asynchronous. The AJAX object, which is a component of the browser, has properties that make it state-aware, so your code can monitor when it is in a ready state after making a server request. Once in a ready state, the AJAX object can notify some client-side script that it's time to update some page content. JavascriptAJAX requires the use of some Javascript to initiate an AJAX request to the server and to process the resulting response. In my next AJAX article, I'll explain how to write a set of self-contained Javascript that can be included as an external file in any of your Web pages to implement AJAX. XMLThis part is a little deceiving, because you don't actually have to use any XML at all to deploy AJAX. However, the AJAX object named XMLHTTPRequest (in Mozilla or Firefox) is the central piece in making an AJAX request and receiving the response, hence the X in AJAX. This object can return plain text (with or with HTML tags embedded) or XML that can be parsed. (Microsoft IE also has an object--with a different implementation but a similar name--that does the same thing). Among the browsers that support AJAX are Microsoft Internet Explorer, Mozilla Firefox, Opera, Konqueror, and Apple Safari. What About Server Workload?Does using AJAX place an excessive workload on the Web server? In most
cases, no. In fact, in many cases, the workload is often considerably
less than it would be if an entire page of data had to be reloaded. Can You Implement AJAX on the iSeries?AJAX is very simple to implement. Since it requires only changes to
the way you write client-side code, you can easily include AJAX in iSeries-based
Web applications (CGIs or Java-based). You can package the code to implement
AJAX as a Javascript include file and create a black box that you can
include in any of your Web applications. You write the server-side components
of your Web applications in the same manner, whether they are communicating
with an AJAX object or not. Duncan Kenzie is President and CTO of BCD Technical Support, the development and support group for WebSmart a popular iSeries Web development tool, and Nexus, a portal product specifically designed for iSeries, i5, and AS/400 servers. Duncan has 29 years of experience on the midrange systems platform creating software for both green-screen and native Web environments. |
|
|||||||||||||||||||