Monday, August 11, 2008

Direct Web Remoting (DWR)

Direct Web Remoting (DWR) exports java code to the browser. It consists of:
A java servlet running on the server that processes requests and sends responses back to the browser

Javascript running in the browser sends requests and can dynamically update the webpage

Methods for installtion

1 .Download the DWR jar

2. Addition into web.xml
<display-name>DWR Servlet</display-name>

3.Add 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="Demo">
<param name="class" value=""/>

The DWR config file defines what classes DWR can create and remote for use by Javascript
http://localhost:8080/[YOUR-WEBAPP]/dwr/ will show all accesible java methods

Include links to the javascript files that make the magic happen:
<script src='/[YOUR-WEBAPP]/dwr/interface/[YOUR-SCRIPT].js'></script>
<script src='/[YOUR-WEBAPP]/dwr/engine.js'></script>

DWR generates Javascript code that is similar to the Java code that exported using dwr.xml


public class Remote {
public String getData(int index) { ... }

<script type="text/javascript"
src="[WEBAPP]/dwr/interface/Remote.js"> </script>
<script type="text/javascript"
src="[WEBAPP]/dwr/engine.js"> </script>

Remote.getData(42, function(str) { alert(str); });

It also helps in calling Meta-Data Objects ,handling Timeouts and Handling Errors and creating Creating Javascript objects to match Java objects.
Hence the java objects can be mapped to javascript and exchanged vise versa.

For more info refer : -

Free Domain Names @!