Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Develop a Web application using Ajax with Dojo and DB2

A practical case study

Leons Petrazickis (leonsp@ca.ibm.com), Database Technology Advocate, IBM
Photo: Leons Petrazickis
Leons Petrazickis is a Database Technology Advocate at IBM, where he writes tutorials and prepares sample applications. He has a decade of experience in web development across a swath of platforms. He comes from a Computer Science background at University of Toronto, and his programming language of choice is Python, and he blogs at http://lpetr.org/blog/.

Summary:  Follow this case study for quickly creating an elegant, responsive Web application using IBM® DB2® 9 for Linux®, UNIX®, and Windows®, XQuery, PHP, and the Dojo JavaScript Framework. The case study is based on IOD Planner 2006, a Web application that was used for the IBM Information On Demand 2006 Global Conference. Learn the advantages of the Ajax approach and see, step by step, how to implement this kind of application.

Date:  15 Feb 2007
Level:  Intermediate PDF:  A4 and Letter (447 KB | 26 pages)Get Adobe® Reader®

Activity:  11483 views
Comments:  

Before you start

About this tutorial

The objective of this tutorial is two-fold -- to showcase the Ajax/Web 2.0 approach to Web development, and to showcase the strengths of DB2 9 pureXML™. With this in mind, I wrote the Information On Demand 2006 Scheduler application, in conjunction with the Information On Demand 2006 Global Conference. It is a simple application for planning your conference itinerary. It takes advantage of the Ajax -- Asynchronous JavaScript and XML -- approach to Web programming to make user interaction as intuitive and painless as possible. It also takes advantage of the speedy pureXML capabilities of IBM DB2 9.

Figure 1 illustrates the development of the IOD Planner application. All loading is done in the background to avoid the refresh lag, and rich widgets are used for form fields, dialog boxes, and drop-down menus.


Figure 1. IOD Planner 2006
Search results in the Planner


Prerequisites

You should be reasonably familiar with Web development and databases. The tutorial "DB2 XML evaluation guide" (developerWorks, June 2006) gives an excellent introduction to DB2 9, while "Query DB2 XML Data with XQuery" (developerWorks, April 2006) does the same for XQuery.

Requirements

To deploy the source code, you will need a Windows or Linux operating system. On it, you will need Zend Core for IBM (see Resources for a download link). Zend Core conveniently bundles the DB2 9 Express-C data server, Apache2 Web server, PHP scripting language, DB2 extensions for PHP, and a powerful administration console.


Why Ajax?


Figure 2. Traditional Web model and Ajax model
Ajax model loads content in background


The key tenet of the Ajax philosophy is to avoid loading new Web pages. Instead, you load new content in the background and then show it within the existing page. This avoids the wasteful reloading of basic infrastructure and the frustration of waiting for the browser to render the same navigation and design. Your users never see a blank, loading screen. The final effect is that of a speedy, very responsive application.

Requests to the server are sent using an XMLHTTPRequest object, and responses are handled asynchronously as they come in. Contrary to the name, they are not limited to XML. Any plain text format, from JavaScript (JSON) to CSV to pre-generated HTML, can be received by this mechanism. We chose the latter one -- HTML generated on the server.

Major Ajax-style Web applications include Bloglines, Del.icio.us, Digg, Flickr, Google Maps, Gmail, and Reddit.


Why Dojo JavaScript Framework?

In the past two years, JavaScript has matured a great deal as a language and as a platform. The language does not have one official library, framework, or API, so several industry-supported and open-source alternatives have appeared. They offer easy Ajax, rich widgets, data structures, helper functions, effects, and layout assistance. One of these toolkits is Dojo.

Dojo is an open source JavaScript framework backed by IBM, Sun, AOL, JotSpot, and others. Ajax for IBM WebSphere® ships Dojo 0.4.1. Dojo isn't tied to any specific server-side toolkit -- meaning it can be used with PHP, Java servlets, .NET, or any other back end. There is an active user and developer community, with hundreds of daily messages in the dojo-interest mailing list.

Dojo has a modular design, with extra functionality loaded on demand.


Figure 3. JavaScript framework adoption (Ajaxian.com, 2006)
43% Prototype, 33% Script.aculo.us, 19% Dojo, all others less than 12%, multiple frameworks allowed


As the number of contenders in Figure 3 suggests, Dojo is a new field. It's a quickly consolidating one. Both Dojo and Script.aculo.us made large mindshare gains in the past year. Of the two, Dojo has a more robust architecture and greater industry backing.


Why DB2 9 pureXML?

A relational database back end for a Web application is a given. We've chosen DB2 Express-C, which is a free-to-download, free-to-deploy, and free-to-distribute version of the DB2 9 data server. It also has native XML capabilities, which calls for some elaboration.


Figure 4. Historical ways of combining XML with a relational database
Dirty files, CLOBs, and shredding


Any sufficiently large enterprise application tends to have XML in it. Often, XML is the most natural form for the data to take. This means that XML is either stored in files, stored wholesale in database CLOBs, or shredded into distinct columns.

Alas, mixing individual XML files with a relational database is hard to scale; XML in CLOBs is hard to query and slow; shredding XML into relational columns means that the relational schemas have to be synchronized with XML schemas -- every change to XML structure triggers expensive regression testing on the whole database.


Figure 5. Native XML in DB2
Native XML in DB2


DB2 9 solves the problem. Its native XML data type is easy to query with SQL and W3C-standard XQuery. It scales with the database, and XML-specific optimizations give it better performance than CLOBs. Finally, it keeps XML and relational schemas separate -- there's no need to regression test everything when you've only changed the XML structure.


Why XQuery?


Figure 6. XQuery and DB2
The architecture of DB2 9 pureXML


XQuery is a W3C standard for querying data from XML files. The WHERE clauses in XQuery and SQL are similar, and the FOR loop is intuitive to casual programmers. XQuery code is faster to write, easier to read, and generally friendlier than equivalent XSL.

FLWOR ("flower") is often mentioned in the context of XQuery. It refers to FOR, LET, WHERE, ORDER BY, and RETURN statements, the foundations of XQuery.

For an excellent introduction, please consult the article"Query DB2 XML Data with XQuery."


Why PHP and XSL?

PHP is a popular and easy-to-learn server-side scripting language. It has a lower learning curve than Java servlets or .NET, while offering the same powerful, advanced features for the experienced professional. As an interpreted scripting language, it is very friendly to iterative development -- there's no recompiling after a single change, and you can see the impact of your code changes in real time.

Needless to say, you should have separate development, testing, and production environments. Live editing is not recommended.

We chose XSL for its powerful transformations. In hindsight, it would have been better to rely exclusively on XQuery. Its facilities are also excellent, and our code would have been shorter and more elegant. XQuery is a friendlier, side-effect-free language tailored for transforming from one XML structure to another. We are already using XQuery to generate well-formed XML, so we could have bypassed the second transformation entirely.

1 of 7 | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Information Management, Open source, Web development
ArticleID=195902
TutorialTitle=Develop a Web application using Ajax with Dojo and DB2
publish-date=02152007
author1-email=leonsp@ca.ibm.com
author1-email-cc=