Understanding the basics of Ajax programming is important, but if you're building a complex user interface, it's important to be able to work on a higher level of abstraction. In the 3rd article in the AJAX series for Java developers, I introduced a technique that avoids the tedious details of Java object serialization, based on last month's Ajax data serialization technology.
In the previous article, I introduced how to serialize data using JavaScript object annotations (JSON) in a format that is easy to translate into JavaScript objects on a client. With this setting, you can invoke the remote service in JavaScript code and receive a JavaScript object graph in the response, but not like a remote procedure call. This time, you'll learn how to take it a step further, using a framework to formalize the ability to make remote calls to server-side Java objects from JavaScript client code.
DWR is an open source solution that uses the Apache license agreement, which includes a server-side Java library, a DWR servlet, and a JavaScript library. Although DWR is not the only AJAX-RPC toolkit available on the Java platform, it is the most mature and provides a number of useful features. See Resources for downloading DWR before continuing with your studies.
What is DWR?
From the simplest point of view, DWR is an engine that exposes the method of server-side Java objects to JavaScript code. Using DWR can effectively eliminate all of the AJAX request-response loops from the application code. This means that client code no longer needs to deal directly with the XMLHttpRequest object or the server's response. You no longer need to write serialization code for an object or use a Third-party tool to turn an object into XML. You don't even need to write a servlet code to adjust the Ajax request to a call to a Java domain object.
DWR is deployed as a servlet in a WEB application. As a black box, this servlet has two main roles: first, for each class exposed, DWR dynamically generates JAVASCRIPT contained in a Web page. The generated JavaScript contains stub functions that represent the corresponding methods on the Java class and perform xmlhttprequest behind the scenes. These requests are sent to DWR, whose second role is to translate the request into a method call on a server-side Java object and send the method's return value in a servlet response to be sent back to the client, encoded as JavaScript. DWR also provides JavaScript tool functions that help you perform common user interface tasks.
About the example
Before I explain DWR in more detail, I want to introduce a simple example scenario. As in the previous article, I'll take a minimal model based on an online store that contains a basic product representation, a user's cart that can contain product items, and a data Access object (DAO) that queries products from the data store. The Item class is the same as used in the previous article, but no manual serialization method is implemented. Figure 1 illustrates this simple setting:
Figure 1. Class diagram that describes the Cart, Catalogdao, and Item classes
In this scenario, I'll demonstrate two very simple use cases. First, users can perform text searches in the directory and view matching items. Second, users can add goods to the shopping cart and view the total price of the goods in the cart.