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
http://directwebremoting.org/dwr/download

2. Addition into web.xml
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<servlet-class>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>

3.Add dwr.xml
<!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="Demo">
<param name="class" value="your.java.Bean"/>
</create>
</allow>
</dwr>


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

Example

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 : directwebremoting.org/ -

No comments:

 
Free Domain Names @ .co.nr!