Monday, June 29, 2009

DWR - Direct Web Remoting

DWR stands for Direct Web Remoting. From the name it implies that Directly making remote procedure calls.

It is a technique by which interaction between

client and server takes place by marshalling objects to and fro (Java script object to Java object and vice versa).

The actual remote calls takes place using Ajax with out even user know about it.

It can call any java objects directly from Java script without having own servlet class, its inbuilt DWRServlet do your work. It has inbuilt Ajax,Object Serialization etc., and it also provides

utility scripts to format display data in the web page.

The learning curve of DWR is steep. User just have to know about using config file and using utility classes provided by DWR.

Here is one of the simple way to implment DWR.

Have these java classes in the classpath

DataManager.Java

public List getCustomers(){

List list = new ArrayList();

list.add(new Customer(1, "Bill", "Los Angeles", "000000001"));
list.add(new Customer(2, "Kumar", "Chennai", 00000000020"));
list.add(new Customer(3, "Tom", "Chicago", "000000003"));
list.add(new Customer(4, "Chian", "Kualalumpur", 000000004"));
list.add(new Customer(5, "Hong", "Singapore", "000000005"));
list.add(new Customer(6, "Jade", "Holborn", "0000000006"));

return list;

}

Customer.java

private int id; private String name; private String city; private String phone;

//have getter/setter methods for above.

DWR Configuration

Have this in claspath, preferably under WEB-INF

<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.org/dwr/dwr20.dtd">

<dwr>
<allow>
<create creator="new" javascript="JDate">
<param name="class" value="java.util.Date"/>
</create>

<create creator="new" javascript="DataManager" scope="session">
<param name="class" value="com.dwrtest.data.DataManager" />
</create>
<convert converter="bean" match="com.dwrtest.data.Customer"/>

</allow>
</dwr>

Add DWRServlet to web.xml

<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<description>DWR Servlet</description>
<servlet-lass>

org.directwebremoting.servlet.DwrServlet

</servlet-class>

<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

Testing

http://localhost:<port>/<your-webapp-context>/dwr/index.html

you should be able to see

DataManager
JDate

which is defined in dwr.xml

Clicking on DataManager will show methods defined in that. You can test methods by passing required parameters if any by clicking 'Execute' buttons.

If that works you can go to next step creating your jsp.

test.jsp

Have the below given scripts



<script type='text/javascript' src='/dwrtest/dwr/engine.js'></script>
<script type='text/javascript' src='/dwrtest/dwr/util.js'></script>
<script type='text/javascript' src='/dwrtest/dwr/interface/DataManager.js'></script>

//DataManager.jsp corresponds to the name defined in dwr.xml

function handleData(dat) {
dwr.util.addRows( "mytable",dat,[function(dat){{return dat.name}},function(dat){{return dat.city}},function(dat){{return dat.phone}}]);
dwr.util.addOptions("myselect",dat,"name","name");

}

function loadData(){
DataManager.getCustomers(handleData);

}







<table id='mytable' border="1">
</table>

Optons:<br/>

<select id='myselect'>
</select>

Output

Table :
































Bill Los Angeles 000000001
Kumar Chennai 00000000020
Tom Chicago 000000003
Chian Kualalumpur 0000000004
Hong Singapore 000000005
Jade Holborn 0000000006

Options


Download : DWR

Reference : Documentation