In the first article in this series, I introduced the building blocks of Ajax:
How to send an asynchronous request from a Web page to a server using a JavaScript XMLHttpRequest object.
How to use Java servlet processing and response requests (returning an XML document to the client).
How to update the page view with a response document on the client.
This time, I will continue to discuss the basics of Ajax development, but will focus on the issues that many Java Web developers are most concerned with: generating data for clients.
Most Java developers have applied the model-view-controller (MVC) pattern to their WEB applications. In a traditional WEB application, the view component consists of a JSP or other presentation technology, such as the Velocity template. These presentation components update the user interface by dynamically generating completely new HTML pages, replacing the pages that the user was previously viewing. However, in the case of Java WEB applications using the Ajax UI, JavaScript client code is ultimately responsible for updating what the user sees, based on the data received from the XMLHttpRequest response. From the server's perspective, the view becomes the data representation it sends in response to a client request.
This article focuses on techniques that can be used to generate data-centric views of Java objects. I'll demonstrate the various ways you can turn JavaBeans into XML documents, and discuss the pros and cons of each approach. You'll see why XML isn't always the best way: for simple Ajax requests, it's better to transfer plain text. Finally, I'll introduce JavaScript object annotations (JSON). JSON allows data to be transmitted in the form of serialized JavaScript object graphs, which is extremely easy to handle serialized JavaScript object graphs in client code.
About the example
I'll use a sample application and several use cases to illustrate the technical features and techniques discussed here. The very simple data model shown in Figure 1 can represent the sample use case. This model represents the customer account in the online store. The customer has a collection of previous orders, each containing several items.
Figure 1. A Simple object model
Although XMLHttpRequest has no restrictions on the format used to send data, it is appropriate for most purposes to send only traditional form data, so my discussion is focused on the server's response. The response can also have text-based formatting, but as its name indicates, XMLHttpRequest has the built-in ability to process XML response data. This makes XML the default choice for Ajax responses, so we start with the discussion from XML format.
Generating XML from Java classes
There are many reasons to pass AJAX responses as XML: Each AJAX-enabled browser has a way to navigate XML documents, and there are many server-side technologies that can process XML data. By developing a scenario that describes the type of document to be exchanged, it is easy to define a contract between the Ajax client and the server, and if the server-side architecture is in a service-oriented manner, using XML can also allow non-AJAX clients to use the data that you provide.
I'll consider three ways to generate XML data from Java objects and discuss the pros and cons of each method.