Comments
Patrick Collands wrote: collands (AT) gmail com I'd be very grateful for an invitation. Thank you.
Cloud Expo on Google News

SYS-CON.TV

2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Click For 2008 West
Event Webcasts
Meet AJAX: Intelligent Web Applications with AJAX
A peek into modern technologies for browser-based applications

The function ask() communicates with the server and assigns a callback to process the server's response (see the following code). Later, we'll look at the content of the dual-natured resolveZip.jsp that looks up the city or state information depending on the number of characters in the zip field. Importantly, ask() uses the asynchronous flavor of the XmlHttpRequest so that populating the state and city fields or coloring the zip border is done without slowing data entry down. First, we call request.open(), which opens the socket channel with the server using one of the HTTP verbs (GET or POST) as the first argument and the URL of the data provider as a second one. The last argument of the request.open() is set to true, which indicates the asynchronous nature of the request. Note that the request hasn't been submitted yet. That happens with the request.send() call, which can provide any necessary payload for POST. With asynchronous requests we have to assign the request's callback using the request.onreadystatechanged attribute. (If the request had been synchronous, we could have processed the results immediately after request.send, but we would have blocked the user until the request was completed.)


HTTPRequest = function () {
var xmlhttp=null;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (_e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (_E) { }
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
} }
return xmlhttp;
}

function ask(url, fieldToFill, lookupField) {
var http = new HTTPRequest();
http.open("GET", url, true);
http.onreadystatechange = function (){ handleHttpResponse(http, fieldToFill,
lookupField)};
http.send(null);
}

function handleHttpResponse(http, fieldToFill, lookupField) {
if (http.readyState == 4) {
result = http.responseText;
if ( -1 != result.search("null") ) {
lookupField.style.borderColor = "red";
fieldToFill.value = "";
} else {
lookupField.style.borderColor = "";
fieldToFill.value = result;
} } }
The HttpRequest() function (see above) used by ask() is a cross-browser constructor of an instance of the XMLHTTPRequest; we'll look at it a bit later. For now, note how the invocation of handleResponse() is wrapped by an anonymous function (a so-called closure) function (){ handleHttpResponse(http, fieldToFill, lookupField)}.

The code for that function is dynamically created and compiled every time we do an assignment to the http.onreadstatechange property. As a result, JavaScript creates a pointer to the context with all variables that the enclosing method - ask() - has access to. It's done so the anonymous function and handleResponse() are guaranteed full access to all context-hosted variables until the reference to the anonymous function is garbage-collected. In other words, whenever our anonymous function gets invoked, it can refer to the request, fieldToFill, and lookupField variables as seamlessly as if they were global. It's also true that every invocation of ask() will create a separate copy of the environment with the variables holding the values of the moment the closure was formed.

Let's look at the function handleResponse(). Since it can be invoked at different states of the request processing, the function ignores all cases except the one when the request processing is complete. This corresponds to the request.readyState property equal to 4 ("Completed"). At this point the function reads the server's response text. Contrary to what its name may suggest, neither the input nor the output of XmlHttpRequest has to be restrained to XML. In particular, our resolveZip.jsp (see Listing 1) returns plain text. If the return value is "unknown" the function assumes that the zip code was invalid and changes the border color of the lookup field (zip) to red. Otherwise, the return value is used to populate the fill field (state or city), and zip's border is assigned a default color.

XMLHttpRequest - the Transport Object
Let's return to our cross-browser implementation of XMLHTTPRequest. The last listing contains an HttpRequest() function that's upward-compatible with IE5.0 and Mozilla 1.8/FireFox. For simplicity's sake, we just try to create a Microsoft XMLHTTPRequest object - and if that fails we assume it's Firefox/Mozilla.

At the heart of this function is the XMLHTTPRequest - a native browser object, which facilitates anything that involves HTTP protocol in communicating with the server. It allows specifying any HTTP verbs, headers, and payload and works in either asynchronous or synchronous mode. No downloads or plugins are required, although in the case of IE, XMLHTTPRequest is an ActiveX integrated inside the browser. Accordingly, the "Run ActiveX Control and Plugins" default IE permission should be in place to use it.

Most important, XMLHTTPRequest allows an RPC-style programmatic query to the server without any page refresh. It does it in a predictable, controlled way, offering complete access to all details of the HTTP protocol, including the headers and any custom formatting of the data. In future articles, we'll show you industrial protocols that you can run on top of this transport including Web Services and XML-RPC that greatly simplify developing and maintaining large-scale applications.

The Server-Side Logic
Finally, the server-side resolveZip.jsp is invoked from the function ask() as shown in Listing 1. The resolveZip.jsp is called in two separate scenarios differentiated by the current length of the zip code (see the zipChanged() function.) The value of the request parameter lookupType is either state or city. For simplicity's sake, we'll assume that two files, state.properties and city.properties, are located in the root directory of the c: drive of the server. The resolveZip.jsp logic is confined to returning the lookup value with the appropriate pre-loaded file - once in each case of course.

About Victor Rasputnis
Dr. Victor Rasputnis is a Managing Principal of Farata Systems. He's responsible for providing architectural design, implementation management and mentoring to companies migrating to XML Internet technologies. He holds a PhD in computer science from the Moscow Institute of Robotics. You can reach him at vrasputnis@faratasystems.com

About Anatole Tartakovsky
Anatole Tartakovsky is a Managing Principal of Farata Systems. He's responsible for creation of frameworks and reusable components. Anatole authored number of books and articles on AJAX, XML, Internet and client-server technologies. He holds an MS in mathematics. You can reach him at atartakovsky@faratasystems.com

About Igor Nys
Igor Nys is a Director of Technology Solutions at EPAM Systems, Inc, a company combining IT consulting expertise with advanced onshore-offshore software development practices. Igor has been working on many different computer platforms and languages including Java, C++, PowerBuilder, Lisp, Assembler since the mid 80's. Igor is currently managing a number of large distributed projects between US and Europe. In addition Igor is the author of the award-winning freeware system-management tools, and he was closely involved in the development of XMLSP technology - one of the AJAX pioneers.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Browser-based applications are widely used and we like the fact that we can access them from anywhere. But from the users' perspective, the productivity level of Web applications still doesn't approximate the productivity of desktop programs. The good news is the gap is closing: the accumulated potential of multiple technologies has boosted a whole new breed of HTML-based apps that are as powerful as the desktop ones. Meet AJAX.

I received number of questions on the future of the AJAX recently - tried to give my personal view on the possible development in the blog - http://anatolet.blogspot.com

You mention the need to destroy the request and callback objects, but my O'Reilly reference says there is no need to clean up in javascript. Which is correct.

How do you destroy the request and especially the callback objects (can I destroy the callback in the callback!!!)?

Nice, to the point, Ajax article.
Though some comments (nitpicking)... Wasn't the term 'Ajax' introduced in the Adaptive Path article from february 2005, not march 2004?
You add style attributes with values 'width:30', but you should add 'px' ('width:30px') to make it standards compliant and make firefox display it correctly (IE is way too lax).
Wouldn't you best upon readyState==4 also check the http status 200 before continuing?
You should best add an existing default url to the iframe (ex 'blank.html'), because when you would do this over https (without a url to begin with) you would get an ugly security message in IE, making you pull your hair out to solve this (I lost a bunch one time).
While you did the iframe example, you might have integrated it with the xmlhttprequest for gracefull degradation in an (corporate) IE environment with ActiveX disabled, no?
But like I said, nitpicking.
Looking forward for the upcoming Ajax articles.

Anand, you are right. The example was meant to be the most obvious, to avoid problems with getRealPath() or explanation around getResourseAsStream() etc. You have a good eye :), thanks again.

Thanks Victor. I had figured out application instead of using getServletContext() on BEA. However my problem was with the

properties.load(new java.io.FileInputStream(key + ".property"));

I had the file in the same folder and thought that I did not need to provide it with an explicit path such as c:\\ but I guess that is not the case. Also one more change to the html file that I figured in the line:
zip.length = 3 ? updateState(zip) : zip.length == 5 ? updateCity(zip):"";

should be

zip.length == 3 ? updateState(zip) : zip.length == 5 ? updateCity(zip):"";

Anand, getServletContext() is available in Tomcat JSP, but not in WebLogic (WL -getServletConfig().getServletContext()). Anyway, I replaced gSC() to _application_ for simplicity. Please download again. Most importantly, please also download the DATA files or ZIP will remain red, no matter what :).

Kind Regards,
Victor Rasputnis

I tried doing this sample using both IE 6 and Firefox. Everytime I type in 3 digits (valid zip code) it paints the box red. It does not populate the city and state. Using BEA I got an error on the JSP page saying that getServletContext() was not recognized. On Tomcat I did not get any error but the program won't work.
Any suggestions ?


Your Feedback
SYS-CON India News Desk wrote: Browser-based applications are widely used and we like the fact that we can access them from anywhere. But from the users' perspective, the productivity level of Web applications still doesn't approximate the productivity of desktop programs. The good news is the gap is closing: the accumulated potential of multiple technologies has boosted a whole new breed of HTML-based apps that are as powerful as the desktop ones. Meet AJAX.
Anatole Tartakovsky wrote: I received number of questions on the future of the AJAX recently - tried to give my personal view on the possible development in the blog - http://anatolet.blogspot.com
Frank wrote: You mention the need to destroy the request and callback objects, but my O'Reilly reference says there is no need to clean up in javascript. Which is correct. How do you destroy the request and especially the callback objects (can I destroy the callback in the callback!!!)?
Halans wrote: Nice, to the point, Ajax article. Though some comments (nitpicking)... Wasn't the term 'Ajax' introduced in the Adaptive Path article from february 2005, not march 2004? You add style attributes with values 'width:30', but you should add 'px' ('width:30px') to make it standards compliant and make firefox display it correctly (IE is way too lax). Wouldn't you best upon readyState==4 also check the http status 200 before continuing? You should best add an existing default url to the iframe (ex 'blank.html'), because when you would do this over https (without a url to begin with) you would get an ugly security message in IE, making you pull your hair out to solve this (I lost a bunch one time). While you did the iframe example, you might have integrated it with the xmlhttprequest for gracefull degradation in an (corporate) IE environment with ActiveX disabled, no? But like I said,...
Victor Rasputnis wrote: Anand, you are right. The example was meant to be the most obvious, to avoid problems with getRealPath() or explanation around getResourseAsStream() etc. You have a good eye :), thanks again.
Anand wrote: Thanks Victor. I had figured out application instead of using getServletContext() on BEA. However my problem was with the properties.load(new java.io.FileInputStream(key + ".property")); I had the file in the same folder and thought that I did not need to provide it with an explicit path such as c:\\ but I guess that is not the case. Also one more change to the html file that I figured in the line: zip.length = 3 ? updateState(zip) : zip.length == 5 ? updateCity(zip):""; should be zip.length == 3 ? updateState(zip) : zip.length == 5 ? updateCity(zip):"";
Victor Rasputnis wrote: Anand, getServletContext() is available in Tomcat JSP, but not in WebLogic (WL -getServletConfig().getServletContext()). Anyway, I replaced gSC() to _application_ for simplicity. Please download again. Most importantly, please also download the DATA files or ZIP will remain red, no matter what :). Kind Regards, Victor Rasputnis
Anand wrote: I tried doing this sample using both IE 6 and Firefox. Everytime I type in 3 digits (valid zip code) it paints the box red. It does not populate the city and state. Using BEA I got an error on the JSP page saying that getServletContext() was not recognized. On Tomcat I did not get any error but the program won't work. Any suggestions ?
Latest Cloud Developer Stories
The Enterprise Cloud Requires a real time infrastructure and a management discipline that understands and can enforce service level discipline.
CloudBench Applications, Inc. announced its financial results for the three months and nine months ending September 30, 2009. All amounts are stated in Canadian dollars unless otherwise noted. Revenues from BasicGov, the Company's cloud computing solution for local government, gr...
The new contract is an industry first, with CSC being the first Microsoft partner to lead and win a cloud computing services agreement of this scale. Under terms of the contract, CSC will provide Royal Mail Group's 30,000 employees with access to new IT services using Microsoft's...
Operates in over 170 countries and is one of the world’s leading providers of communications solutions and services. Richard Tarboton talks for MeettheBoss.TV on his role as Head of Energy & Carbon for BT and what they are doing towards reducing carbon emissions.
CA is going to put its Agile Planner software on salesforce.com’s Force.com platform in the first half to accelerate development time and give users visibility over their development initiatives to reduce time-to-market. Customers are supposed to be able to accelerate the deploym...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE

Breaking Cloud Computing News
CloudBench Applications, Inc. announced its financial results for the three months and nine months e...