Richard Bair of Sun Microsystems has
created XMLHttpRequest and JSONHttpRequest beans, both part of the
SwingX-WS project.
Interestingly Richard "ased the design and implementation on the W3C Working Draft Specification for XMLHttpRequest. So the good news is, for those of you familiar with XHR, you have essentially the same API available now for your Swing apps."
This is pretty pragmatic. We all know that XHR may not be the best API in the world, but Richard see's that a lot of people now know it, so he created something similar on the Swing side (Maybe we will see ProtoSwing or DojoSwing to give a nice wrapper?).
Here is an example in code. Java code remember:
final HttpRequest req = new HttpRequest();
req.addOnReadyStateChangedListener(new
PropertyChangeListener() {
public void propertyChange(
PropertyChangeEvent evt) {
if (evt.getNewValue() == ReadyState.LOADED) {
String response = req.getResponseText();
//do what you like with the response here
//for example, if the XML was XMLDecoder
//compatible:
java.beans.XMLDecoder decoder = new java.beans.XMLDecoder(
new
ByteArrayInputStream(response.getBytes()));
Customer c = (Customer)decoder.readObject();
decoder.close();
}
}
});
try {
req.open(HttpMethod.GET, new
URL("http://MyCompany.com/someService?customer=\"A2342DAD\"/"));
req.send();
} catch (
Exception e) {
e.printStackTrace();
}

Sanjiv Jivan has written about how he has
integrated an ajax based login form to the
Acegi Spring module for Auth and Auth.
His implementation contains a ServletFilter in J2EE land, and JavaScript in the client:
JAVASCRIPT:
-
-
function ajaxLogin() {
-
Element.update('loginMessage', 'Sending request ...');
-
Element.show('loginMessage');
-
var opt = {
-
-
method: 'post',
-
-
postBody: Form.serialize($('loginForm')) + '&ajax=true',
-
-
-
onSuccess: function(response) {
-
var msg = response.responseText;
-
if ("error:" == msg.substr(0, 6)) {
-
var fp = "<font color='red'>" + msg.substring(6, msg.length) + '</font><br /><a xhref="forgetPwd.html" mce_href="forgetPwd.html">Forget Password?</a>';
-
Element.update('loginMessage', fp);
-
} else if ("url:" == msg.substr(0, 4)) {
-
location.href = msg.substring(4, msg.length);
-
} else if ("message:" == msg.substr(0, 8)) {
-
Element.update('loginMessage', msg.substring(8, msg.length));
-
}
-
}
-
}
-
new Ajax.Request('j_acegi_security_check', opt);
-
}
-
On the Java.net blog of Greg Murray, he
demonstrates a method of creating a proxy client in Java for an XMLHttpRequest of your choice.
One drawback of working with AJAX is that an AJAX-based client cannot make calls to URLs outside of its domain, which means that it cannot access services located on another server. To overcome these problems, you need a generic proxy that can communicate with external services on your client’s behalf. The proxy passes a call from your client application to the service, receives the content in response from the service, and returns the content to your client. You can then use this content in your AJAX-based application.
With the
brief explaination out of the way, Greg gets started with the code/application. First off, he includes a graphic (sequence diagram) to show how the flow of the application will go - in his example, a request to the geocoding interface at Yahoo. His framework of choice to work with is the
Project jMaki proxy functionality.
Where the
blog post gets a little sparse on the details,
this doucmentation on the Project jMaki site provides the complete story - code and all. Check it out!
The use of J2EE in development work has been growing at a staggering pace over the last few years. Developers are finding its environment and capaitilities to be just what they need. One thing that it’s missing though, is a good method to interact with the user. Sure, there are the usual web interaction methods, but why use those when you can incorporate Ajax into your application? Not sure how? Check out this new tutorial from DevArticles for the full scoop on using Ajax either client-side or as a servlet at server-side.
AJAX provides asynchronous communication service through JavaScript and XML. Thus a good combination can be formed by using AJAX at client-side and a servlet at server-side, providing a non-obtrusive, responsive and highly interactive web experience.
In this discussion, I will focus on utilizing such a winning combination. The first sections will detail the steps required for setting up an application for utilizing AJAX along with a servlet. In the last section I will develop a registration module that will use AJAX to check the availability of the username.
They start off with the basics - how to set up the XMLHttpRequest object and create an interface to pass the information to it. Next up is handling the state change of the object (when something, anything happens) and the generation of the XML response. Finally, they thie things together into a chunk of code that can send a message and retrieve the results from the request.
All Java developers can spot the look and feel of JavaDoc a mile away. The new
JavaRef (developed with the Apache Tapestry Java web framework) aims to give you another take, allowing you to search JavaDoc of over 80 projects.
On the Ajax side it provides:
- Class/package/method search with auto-complete
- Tabbed display of information for classes that brings in information as requested
- Navigation across libraries for class references
- Usage information
- Floating method navigator
