AJAX Basics

Note: I wrote this back some time ago (27th, February 2007) on another site but thought this warranted a re-post. Much has been done by way of AJAX frameworks, IDEs, stadards etc but I feel that the basics are still important and provide the foundation for understanding.

I’ve been intending to write this article for a while now. This desire started close to a year ago after attending several break-out sessions at Java One. It was one of these specifically that the core of what AJAX was and is was detailed out. The following is an attempt to give a basic tutorial on how to get started. Please note that I’m assuming a decent knowledge of Java Script and events.

Contrary to what I found in more than one Google search, AJAX is rather simple. What i found was that the majority of it all is related to one simple and small key piece. XMLHTTPRequest (or for those that are forced to code for I.E. the ActiveX object Microsoft.XMLHTTP) is the key component that make this whole thing go. The rest is just the “extras”.

First off, this “event” will be captured using the onChange() Java Script function within the <select> tag. If you are not familiar with Java Script events or at least this event, please check out the tutorial at QuirksMode or a Google search for JavaScript Events.

Now let’s assume that we have already created a web accessible script or something that produces a document that is structured like the following.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<code>ID or Code</code>
<display>Display Value</display>
</response>

And next we make a request to a server for information based on this data. Now we see where XMLHTTPRequest comes into play. This simple “function” will look to retrieve XML from a specified source and return it all without reloading the browser window. The following code will give you an idea of what’s going on:

if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = processReqChangeXML;
req.open("GET", sendURL, true);
req.send(null);
}

This brings us to the step of capturing the event using the ReadyState value from our “req” variable earlier. The following is a snippet from a library I created and detail below.

if (req.readyState == 4) {
//-- only if "OK" from web server
if (req.status == 200) {
response  = req.responseXML.documentElement;
response.getElementsByTagName('display')[a].firstChild.data;
}
}

At this point you should be looking at two functions and a script that produces an XML document. The rest of our work is simply putting all the pieces together. Now at this point you may be lost so let’s get some actual code together. Start off by downloading this AJAX library written for a series of applications over the last few months. Make sure you have your XML producing script ready and running. Paste the following in an HTML page and add a couple of values for the dropDown1 select box.

<select name="dropDown1"
id="dropDown1"
onChange="loadXMLDoc(this.form, 'dropDownCode', this.value, '/scriptName.php');" >
<option value=""></option>
</select>
<select name="dropDownSelect" id="dropDownSelect">
<option value="">Please select from above list</option>
</select>

Please let me know what issues you run into. The AjaxLib.js file is meant to be minimalistic and also meant as a tutorial aid. Good luck and have fun.

JSR-168 Portlet Specification

What is it about the JSR-168 spec that is so hard for vendors to implement. Too vague? Too free? What?
I just wasted the last week trying to figure out why portlets were not rendering with a particular structure. Turns out with the folder in web containing the view, edit and help.jsp files can not start with a capital letter. What the hell?

Xmind

How to run XMind from http://www.xmind.net on a Fedora 9 install as a Launcher or in general from outside the install folder.

First off, XMind is a mind mapping program that I started using on a Mac, transferred to an Eclipse 3.4 plugin and have since downloaded and installed on any computer I have including my Linux machines at home. That said, there is a bit of an oddity when running it under linux. I couldn’t find any information on how to run the executable from outside of the unzipped folder. I have found that fix and wanted to share for anyone else who has had this issue. Below is what I have in place on a Fedora 9 install with Sun JDK 1.5.0…

  1. Download the XMind Portable from their site
  2. Unzip to /usr/local/xmind-portable/
  3. Rename XMind for Linux to XMind-Linux
  4. Open the config.ini file from XMind-Linux folder
  5. Edit any paths to reflect your location of /usr/local/xmind-portable/XMind-Linux/
  6. Copy all the values in this file and edit the lines to be a single line
  7. Create your shortcut to /usr/local/xmind-portable/XMind-Linux/xmind and append the single line from step 6
  8. Done!

Architecture and Portals

I just spent the day discussing architecture and Portal platforms. Specifically the uPortal and Liferay platforms were the topic of discussion along with the institution specific needs of who I work for. I miss this. I really need to focus on the technical aspects of my job and realize that I simply don’t like and hate to deal with the political B.S. of working for a large organization.