h1

Introducing DWR 2 Projects (Direct Web Remoting)…

January 8, 2011

DWR, or Direct Web Remoting, is a Java open source library that helps developers write web sites that include Ajax technology. It allows code in a web browser to use Java functions running on a web server as if those functions were within the browser.

It consists of two main parts:

  • Code to allow JavaScript to retrieve data from a servlet-based web server using Ajax principles.
  • A JavaScript library that makes it easier for the web site developer to dynamically update the web page with the retrieved data.

DWR is a RPC library which makes it easy to call Java functions from JavaScript and to call JavaScript functions from Java (a.k.a Reverse Ajax).

It has a large user-base, active mailing list and has been used in many projects including the Walmart shopping site and American Airlines flight booking site.

DWR has a number of features like call batching, marshalling of virtually any data-structure between Java and Javascript (including binary file uploading and downloading), exception handling, advanced CSRF protection and deep integration with several Java server-side technologies like Spring and Guice.

Picture below: High level pictorial representation of DWR in action.

Getting ready for the DWR Development Environtment.

DWR 2.0  require JDK 1.3 or higher , and a servlet  spec 2.2 or higher. DWR works in numerous containers and application servers including Tomcat , WebLogic , WebSphere , JBoss , Jetty , Resin and GlassFish. One caveat is that if you have a web server in front of your app server , and the web server alters the URLs , DWR may (and probably will ) fail.

DWR requires:-

1. Sun Java SDK 1.6.0_03

2. Apache Tomcat 6.0.13

3. Apache Ant 1.7.0

4. Two environtment variable set: JAVA_HOME , which should point to the directory of your SDK was installed to , and ANT_HOME , which should point to the directory Ant was installed to. In addition, you’ll want to add <JAVA_HOME>/bin and <ANT_HOME>/bin to your path. Lastly, as a recommendation , I highly suggest having no CLASSPATH environtment variable set. This will greatly reduce the chance of any kind of classpath issues bitting you.

Example below:-

Name:

Reply:

Source Code:-

HTML source:

<p>
  Name:
  <input type="text" id="demoName"/>
  <input value="Send" type="button" onclick="update()"/>
  <br/>
  Reply: <span id="demoReply"></span>
</p>

Javascript source:

function update() {
  var name = dwr.util.getValue("demoName");
  Demo.sayHello(name, function(data) {
    dwr.util.setValue("demoReply", data);
  });
}

Java source:

package org.getahead.dwrdemo.simpletext;

public class Demo {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
    "http://getahead.org/dwr/dwr20.dtd">

<dwr>
  <allow>
    <create creator="new" javascript="Demo">
      <param name="class" value="org.getahead.dwrdemo.simpletext.Demo"/>
    </create>
  </allow>
</dwr>

  Well guys , for this week post , I'm just introducing you all the concept of DWR 2.
To explain more detail about the implementation and some of the DWR 2 projetcs , try
to check this website:-

  http://directwebremoting/dwr/index.html
 
I suggest you all to read the Practical DWR 2 Projects book , author by Frank W.Zammetti.
The publisher of the book is Apress. It got tons of example and deep explanation of
how to implement and write DWR 2 code and use it in our application. 


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: