A Field Guide to Java Direct Web Remoting (DWR)
Introduction to DWR
Setting Up DWR
If you are familiar with the main JEE project structure, setting up DWR is relatively simple on any Java application server. To add Direct Web Remoting to any JEE project, there are three main steps. First, put the dwr.jar in the application path (this is usually the lib folder under the WEB-INF); second, add mapping to the special DWR Servlet in the main web.xml; and third, add a special dwr.xml descriptor file in the same folder as the web.xml file. The dwr.xml is the xml file that describes what objects and methods are "exposed" for the direct web remoting calls.
This is really all needed to set up the DWR on the server. The client-side is where the "exposed" methods are called. As you can see, the server setup does not require any changes to the existing Java code or server configuration code except extra mapping to just one Servlet.
Here is the directory structure in Eclipse:
The Servlet mapping in WEB-INF/web.xml can look like this:
The WEB-INF/dwr.xml can look like this, and I will follow up on this later.
<script src='/[WEBAPP-NAME]/dwr/engine.js'></script> <script src='/[WEBAPP-NAME]/dwr/util.js'></script>
Note that the second parameter is the ID, and the first is a callback function that will be invoked when the response comes back with the data from the server.
The test client provided by the DWR Servlet also let you invoke methods, but it is a visual interface that hides the true method signature, and can only verify if the correct result returns. Nevertheless, it is a very useful tool (see next section).
Here is the flow diagram:
Page 1 of 2