DWR: Direct Web Remoting

Notes from my Recent Research on DWR

DWR is an open source Java library which helps client side AJAX code to call and use Server side Java code as if it is available locally. The communication can be two way, on one hand, JavaScript can call Java methods and on the other hand Java can call JavaScript methods (Reverse AJAX)

For getting started download the latest jar from http://directwebremoting.org/dwr/download

DWR consists of two main parts:
A Java Servlet running on the server that processes requests and sends responses back to the browser.

Let’s create a simple DWR application.

Step1: Add these lines to web.xml



Step 2: Create dwr.xml

“-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN”

<create creator="new" javascript="JDate">
<param name="class" value="java.util.Date"/>
<create creator="new" javascript="Employee">
<param name="class" value="testPkg.EmployeeBean"/>

Step 3. Create EmployeeBean. Java

package testPkg;

public class EmployeeBean {

private static String name;
private static String EmployeeNum;
private static String Address;

public String getAddress() {
return Address;
public void setAddress(String address) {
Address = address;
public String getEmployeeNum() {
return EmployeeNum;
public void setEmployeeNum(String employeeNum) {
EmployeeNum = employeeNum;
public String getName() {
return name;
public void setName(String name) {
System.out.println(“called with:”+name);
this.name = name;

Step 4: Here is the final HTML code (change the path for JS files as per the project)

<script type='text/javascript'
<script type='text/javascript' src='/[Path]/dwr/engine.js'></script>
<script type='text/javascript' src='/[Path]/dwr/util.js'></script>
First Set the Employee Name:
<input type="textbox" id="setname" value="Name Here" />
<input type="button"
onclick='Employee.setName(document.getElementById("setname").value, reply8);'
value="Set Name" />
<script type='text/javascript'>
var reply8 = function(data)
if (data != null && typeof data == 'object') alert(dwr.util.toDescriptiveString(data, 2));
else alert("data set!");
<input type='button' onclick='Employee.getName(reply);' value='Get Name'
title='Calls Employee.getName(). View source for details.' />
<script type='text/javascript'>
var reply = function(data)
if (data != null && typeof data == 'object') alert(dwr.util.toDescriptiveString(data, 2));
else alert("set data was: "+data);

Now go to your dwr.html and see the magic