Summary
Direct Web Remoting (DWR) allows browser-based JavaScript to interact with Java classes on the server, and to control page content with results from the server, making it easy to AJAX-enable a Java Web app. The biggest new feature of 2.0 is "reverse Ajax," allowing the server to send asynchronous calls back to the client.
Advertisement
The Direct Web Remoting (DWR) project on java.net provides an open source library to help develop Ajax web sites with Java on the server.
DWR consists of a server-side component—a Java servlet that processes requests from a browser and sends replies back to the browser, and JavaScript functions that run inside a browser that interact with the servlet and update the contents of the page based on results from the server.
DWR works like an RPC mechanism: JavaScript code running in the browser can invoke Java "methods" as if the Java classes defining those methods resided in the browser's address space. DWR takes care of dispatching those invocations to the server, including marshalling the request and the response. Moreover, DWR dynamically generates JavaScript based on Java class definitions. The result is somewhat similar to Java RMI, but because the client-side uses JavaScript, the client requires no applets or plugins.
DWR's 1.0 and 1.1 releases extended a typically asynchronous AJAX server-side invocation with a listener mechanism: The client can register a listener when it invokes a server-side function, and when the server responds, that listener notifies client-side code of the response contents.
Many rich-client applications—and most non-trivial apps with an HTML UI as well—require some form of client-side state. For instance, displaying a set of inventory items in an HTML table means that the browser, in effect, maintains a client-side cache of the displayed items. The same issue holds for a Swing application that maintains a data model behind each Swing component. A key aspect of architecting such applications is to ensure synchronization between the client- and server-sides of the application's state.
DWR version 2.0's biggest new feature helps achieve such synchronization for AJAX apps: "reverse Ajax" lets the server make asynchronous calls back to the client. For instance, if the server-side state of an application changes—a new item is added to inventory or the price of some goods change—the server can just call the client, which can then update its state of the application.
What do you think of DWR's RMI-like remote call mechanism for AJAX? And how do you synchronize state between client and server in a Web app?
The biggest problem we faced when using dwr was during performance testing. Load runnner could not record the data going back and forth between web pages and server side code. This manifested itself as failed transactions