| By Yakov Fain | Article Rating: |
|
| October 30, 2006 12:30 AM EST | Reads: |
31,294 |
From Farata Systems Blog
While the term Ajax was introduced in 2005, the technique of using XMLHttpRequest object was known since 1999 (this object became available in Internet Explorer 5). But up till now XMLHttpRequest object was never standardized by World Wide Web Consortium. This technically means that each Web Browser vendor can implement it differently.
Such Internet giants as Google, Yahoo, Amazon started using Ajax in their applications, which brought interest of business application developers who always wanted to make their Web applications less static and minimize page refreshes. A discussion on usability of Ajax for business applications is out of the scope of this article.
When I was learning how to work with AJAX, I went through a number of 101-type articles. The biggest problem with these tutorials is that the authors are trying to explain several things at once, which is confusing. I’ll try to offer you a very simple example of an Ajax application that will illustrate the “refreshless” nature of Ajax. Here’s a simple HTML page:

Click on the link, and the text area will be populated with the content of the server side file, which in our example has the text “Hello from the server!”
What’s the big deal? There is no entire Web page refresh! The XMLHttpRequest object sends an asynchronous request to the server, gets the data back and changes the content of just one object on this HTML page – the text area.

Here’s the code of the ajaxSample.html:
<html lang="en" dir="ltr">
<head>
<title>Ajax sample application</title>
<script type="text/javascript">
var myXHR= new ActiveXObject("Microsoft.XMLHTTP");
function goGetIt(){
myXHR.open("GET", "/theriabook/hello.txt",true);
myXHR.onreadystatechange=updateTheData;
myXHR.send();
}
function updateTheData(){
if (myXHR.readyState==4){
myForm.someText.value=myXHR.responseText;
}
}
</script>
</head>
<body>
<p>Click on <a href="javascript:goGetIt()"> this link</a> to populate the text area
below from the server side text file without the entire page refresh
<form name="myForm">
<textarea name="someText" rows="5" cols="30">
</textarea>
</form>
</body>
</html>
I deployed this file under Apache Tomcat that ran locally on my PC. In this example both ajaxSample.html and the data file hello.txt are located in directory webapps\theriabook. To test this program, make sure that Tomcat is up and running and direct your Internet Explorer to the URL of this HTML file, which in my case is http://localhost:8080/theriabook/ajaxSample.html.
In this example, the JavaScript code creates an instance of the XMLHttpRequest object in a way that is specific to Microsoft Internet Explorer. This sample will not work in other than IE browsers, because instantiation of the XMLHttpRequest object should have been done a little differently there, which is out of the scope of this article.
The click on the link calls the JavaScript function goGetIt() that starts with creating an HTTP GET request for the file hello.txt.
myXHR.open("GET", "/theriabook/hello.txt",true);
The third argument here is equal to true, which means that we want asynchronous communication with the server. The next line tells which function to call when the data arrive:
myXHR.onreadystatechange=updateTheData;
And finally, we send an asynchronous request to the server with no arguments:
myXHR.send();
When the request completes (the readystate is equal to 4) and the data arrive, the JavaScript function updateTheData() assigns the text received from the server to the text area called someText:
myForm.someText.value=myXHR.responseText;
For the sake of simplicity I did not include in this example multi-browser support or error processing, but this application illustrates the “refreshless” nature of Ajax Web applications.
And the last advice: always keep your fingers crossed when you start your Ajax application. Hey, you never know…
Published October 30, 2006 Reads 31,294
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Yakov Fain
Yakov Fain is a Managing Director of Farata Systems, consulting, training and product company. He has authored several Java books, dozens of technical articles. SYS-CON Books released his latest co-authored book , Rich Internet Applications with Adobe Flex and Java: Secrets of the Masters in Spring 2007. Sun Microsystems has nominated and awarded Yakov with the title Java Champion. He leads the Princeton Java Users Group. He is an Adobe Certified Flex Instructor. Yakov co-athored the O'Reilly book "Enterprise Application Development with Flex". He twits at twitter.com/yfain.
- It's the Java vs. C++ Shootout Revisited!
- Patterns for Building High Performance Applications
- Asynchronous Logging Using Spring
- Java for Programmers (2nd Edition)
- Cross-Platform Mobile Website Development – a Tool Comparison
- Three Buzzwords That Every CIO Hears but One They Should Listen To
- Write Once Run Anywhere or Cross Platform Mobile Development Tools
- Immersing into JavaScript Frameworks
- Workday Reportedly Prepping to Go Public
- Cloud Expo New York: The Java EE 7 Platform - Developing for the Cloud
- Book Review: Sams Teach Yourself Java in 24 Hours
- OpenOffice.com Lives
- Book Excerpt: Introducing HTML5
- Adobe Sends Flex to the Apache Foundation
- Five Years Waiting for JRE 7: Is It Justified? (Part 1)
- Book Excerpt: Java Application Profiling Tips and Tricks
- i-Technology in 2012: Five Industry Predictions
- It's the Java vs. C++ Shootout Revisited!
- Patterns for Building High Performance Applications
- OpenXava 4.3: Rapid Java Web Development
- The Next Web Architecture
- Asynchronous Logging Using Spring
- Java for Programmers (2nd Edition)
- Is Write Once Run Anywhere Ever Going to Be a Reality?
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- JavaServer Faces (JSF) vs Struts
- The i-Technology Right Stuff
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- What's New in Eclipse?
- i-Technology Predictions for 2007: Where's It All Headed?



















