| By Przemek Piotrowski, Mark Townsend | Article Rating: |
|
| July 26, 2007 10:45 AM EDT | Reads: |
19,814 |
This procedure will handle all the processing work for us by setting the content type to text/xml (required by the XmlHttpRequest object for DOM processing), setting null handling to output empty tags for null values, and disabling pretty printing for smaller response size. Also, when something goes wrong at the query level, e.g., empty result set or invalid query, the procedure manually outputs the hard-coded "No data found" message to indicate its state.
The AJAXE procedure will be a core dispatcher of responses. It will accept two parameters: q - the type of action requested, w - the extra parameter required for certain types of action (optional) (See Listing 3).
The functionality of the sample application will be divided into three basic use cases:
- Filling the SELECT list with elements obtained through the AJAX request and hooking the event handler to it
- Obtaining the results set and rendering it into a HTML table dynamically
- Using simple auto-complete on the HTML input field
Hands-on Document Object Model (DOM)
AJAX
extensively relies on a set of JavaScript programming techniques once
referred to as DHTML. But now they're much more powerful thanks to the
unification of the DOM implementation among Web browser vendors. Back
in the days of Netscape Navigator 4 and Microsoft Internet Explorer 4
you had to use a different syntax for each browser to manipulate page
content dynamically. No need to do that anymore.
The Document Object Model is a tree-like hierarchical representation of nodes/HTML elements (see Table 1). The root node of an HTML document is the <html> tag that usually has two children: <head> and <body> - the manipulations happen in the second. Since DOM is a vast subject, we'll focus only on the most useful properties and methods of JavaScript XML objects. For the complete DOM specification, see the W3C Web site at http://w3.org.
Again, remember that there are a number of other JavaScript methods for working with the DOM object. For purposes of this article, an ajaxTable function was created: it builds up the HTML table from the XML response obtained through the AJAX request. By default DBMS_XMLGEN returns a XML tree of the following structure:
<?xml version="1.0"?>
<ROWSET>
<ROW>
<COLUMN1></COLUMN1>
<COLUMN2></COLUMN2>
...
</ROW>
...
</ROWSET>
The code in Listing 4 represents the body of the ajaxTable function.
This function appends the DOM tree in Listing 5 to the DIV element of ID="q" (the TBODY element is required here to render properly under Internet Explorer).
AJAX = Asynchronous JavaScript and XML
Technologically, AJAX is nothing new. It owes its recent popularity to
the unification of the JavaScript standards that allow access to the
XmlHttpRequest object. XML isn't new either. It now plays a key role in
data and information interchange and is universally adopted by both the
enterprise and the open source community. The biggest problem with the
Web was that applications needed a full-page refresh after each user
action on the page. It didn't matter whether it was just sorting table
elements or downloading a whole new page.
There are a number of AJAX frameworks on the market, including the Dojo Toolkit, Prototype, or Microsoft Atlas to name a few. However, writing basic handlers alone helps enable a deeper understanding of what's happening under AJAX's hood. First, we need an XmlHttpRequest object that unfortunately still depends on the browser - Microsoft Internet Explorer continues handling it using an ActiveX control so the appropriate requests object creation code is presented in Listing 6. This is the only part that requires browser-compatibility code. Everything else here is cross-browser.
As for the handler, a dedicated function to handle all the asynchronous operations was created. It takes a request method, a URL, and callback functions as parameters. After the response is obtained the callback is immediately called with the fetched XML passed as an argument.
function async(method, url, callback) {
request.open(method, url, true);
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
callback(request.responseXML);
}
}
request.send(null);
}
The callback function will do all the DOM processing based on the XML content from the response. The callback works directly on the XMLDocument JavaScript object.
Putting It All Together
Now that we have all the building blocks of the solution, let's take a look at the logic behind it. Figure 1
separates client side and server side into three separate blocks:
Oracle XE, Web page, and browser client. Notice that the Web page has
been separated here because of its state-dependent nature - thanks to
AJAX it now incorporates the logic to handle user actions and transform
them into HTTP requests; the standard Web model assumes that it's the
browser that dispatches HTTP requests to the Web server. That is the
key concept in the solution. Take a look at Figure 1 to see the whole
interaction process.
1. The browser client sends a direct HTTP request to the Web server
(here: the Embedded PL/SQL Gateway of Oracle XE); the URL of the
request has been supplied in the location bar or through a link.
2. Oracle XE serves a static HTML page that includes AJAX scripts.
This static page is then rendered by the browser and now all the
requests and interaction happen indirectly, as AJAX requests.
3. When the user triggers an event on the Web page by interacting with
page elements such as forms and links, an event handler catches it and
sends the appropriate HTTP request through the AJAX engine.
4. The XMLHttpRequest object (Table 2)
requests a remote resource from the Embedded PL/SQL gateway. At this
point, the user doesn't have to wait for the script to finish fetching
responses from the server; it's handled in an asynchronous manner. The
script takes as much time as needed to fetch the response while the
user can further interact with the Web page.
5. The script performs DOM modifications in the background while the user can trigger the next events on the Web page.
You can now upload the attached HTML file (hr-ajax-demo.html) into your XE instance through FTP or WebDAV, which contains the complete code for the sample application. After putting it into the /public/ folder of XE, open http://localhost:8080/public/hr-ajax-demo.html with your Web browser. After carefully following all the steps of this tutorial the demo page should pre-load the list of departments and respond to user interaction almost immediately.
Advantages and Potential Problems
Although
programming AJAX isn't necessarily difficult, the question is whether
asynchronous querying techniques are safe and production-ready. AJAX
isn't always a sure choice over standard development methodologies.
There are at least several viewpoints to consider.
Advantages
• Better user experience
• Greater responsiveness
• Smaller server load, increased performance
• Network applications can be updated on-the-fly without redistributing updates
• Separation of content from presentation (easier maintenance)
Potential Problems
• Cross-browser incompatibilities
• Client-side security concerns
The Real World
Today, AJAX applications are
gaining a lot of steam. With Google leading this trend together and
Yahoo! and Microsoft trailing, the desktop experience is delivered to
users through increasingly dynamic Web pages. Since the early adoption
of AJAX it has matured to be a full-blown solution often chosen by
enterprises for commercial solutions.
After the unification of DOM implementations and the adoption of World Wide Web Consortium (W3C) standards among developers of Web browsers, it's now possible to create cross-platform Web applications more easily than ever before. With a wide range of available AJAX toolkits, developers can now upgrade Web applications to an asynchronous architecture at a low cost. AJAX is flexible enough to be plugged into existing solutions and greatly enhances usability. The movement of desktop applications to the Web is a matter of time and represents the next step in software evolution.
Published July 26, 2007 Reads 19,814
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Przemek Piotrowski
Przemek Piotrowski has been following Oracle 10g Express Edition (Oracle XE) development since its early beta stages back in 2005 and can easily distinguish new features of this first database from Oracle, free to develop, deploy and distribute. Oracle XE's small footprint doesn't stop him from taking advantage of many of its advanced features and plugging them into existing Web architectures.
More Stories By Mark Townsend
Mark Townsend is the vice president of database product management in Oracle's Server Technology Division. His responsibilities include requirement analysis, release planning, co-ordination of database product management activities, communication with analysts and press on database topics, and development and delivery of field technical training. He was also the product boss for Oracle XE. Mark has been with Oracle since 1991 and has specialized in the Oracle database for over 15 years.
- Kindle 2 vs Nook
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Windows 7 – Microsoft’s First Step to the Cloud
- Ulitzer Provides a Powerful Social Journalism Platform
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Open Source Mobile Cloud Sync and Push Email
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing on Gartner's Top 10 List and SYS-CON Events' 2010 Calendar
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- US Post Office Hops a Ride on NetSuite’s Cloud
- Moving Your RIA Apps into the Cloud: Seven Challenges
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Building a Drag-and-Drop Shopping Cart with AJAX
- What Is AJAX?
- Google Maps! AJAX-Style Web Development Using ASP.NET
- Flashback to January 2006: Exclusive SYS-CON.TV Interviews on "OpenAjax Alliance" Announcement
- AJAXWorld Conference & Expo to Take Place October 2-4, 2006, at the Santa Clara Convention Center, California
- AJAX Sponsor Webcasts Are Now Available at AJAXWorld Website
- How and Why AJAX, Not Java, Became the Favored Technology for Rich Internet Applications
- "Real-World AJAX" One-Day Seminar Arrives in Silicon Valley
- AJAXWorld University Announces AJAX Developer Bootcamp
- AJAX Support In JadeLiquid WebRenderer v3.1
- Where Are RIA Technologies Headed in 2008?
- Struts Validations Framework Using AJAX






































