Skip to main content

RESTful POJO with DWR (Direct Web Remoting)

Posted by hari_ustri07 on March 30, 2008 at 7:30 AM PDT


* About this article
* What is REST?
* Direct Web Remoting
* Creating Restful Calculator Service
* Packaging/Deploying/Testing Calculator Service
* Conclusion
* Resources

About this article
This article talks about how REST architectural style be used in DWR , to invoke
pojo services.

Adhering to the REST principles we will make your pojo's Restful.
What is REST?

The web is built on an architectural style called REST.
Any URL is a representation of some resource and REST refers to a
set of principles that outline how resources can be defined
and addressed.

REST is not a standard , it uses standards like

* HTTP , URL , XML,HTML,GIF,JPEG,etc (Resource Representations)
* text/xml, text/html, image/gif, image/jpeg, etc (MIME Types
Lets consider a library ,the library may define library resource.
The resource might be represented by a URL something like


A representation of the resource is returned in any of the standard mime type
(text/xml, text/html,image/gif, image/jpeg, etc).

Direct Web Remoting

Go through DWR if you haven't done.

Its good to have an understanding of these things listed below, before we start with
the Calculator Service.

* Basic of DWR
* DWR xml configuration
* DWR request types
* Examples

Creating Restful Calculator Service

* step 1 : create a calculator class with add,subtract,mutiply,divide methods,
refer Resources/
* step 2 : create a ServletFilter ,
refer Resources/
* step 3 : Configure dwr.xml ,
  refer Resources/dwr.xml
* step 4 : write client html ,
  refer Resources/index.html

The methods inside Calculator

* addTwoNumbers(int,int)
* subtractTwoNumbers(int,int)
* multiplyTwoNumbers(int,int)
* divideTwoNumbers(int,int)

In a REST style representation of the above listed methods
* addTwoNumbers (http://..../addTwoNumbers/100/10/)
* subtractTwoNumbers (http://..../subtractTwoNumbers/100/10/)
* multiplyTwoNumbers (http://..../multiplyTwoNumbers/100/10/)
* divideTwoNumbers (http://..../divideTwoNumbers/100/10/)

This url http://host:port/restws/addTwoNumbers/100/10/ is a representation of
"addTwoNumbers",100/10 are the values which are passed to it.

when a request is made to above url it passes through the servlet filter,
which inturn forwards the request to a client html page.
Servlet Filter acts as a Handler.

Packaging/Deploying/Testing Calculator Service

Packaging / Deploying:
* step 1: download dwr.war
* step 2: extract the war
* step 3: compile,,copy them inside \WEB-INF\classes\
* step 4: edit \WEB-INF\dwr.xml

* step 5: configure servlet filter RESTFilter in web.xml
* step 6: copy client html appropriately
* step 7: package it and deploy it

Testing Calculator Service :

Figure 1. addTwoNumbers


Good thing about DWR

* Easy way to expose server-side java methods as services.
* Calling client doesn't deal with XMLHTTPRequest.
* No need to write object serialization code.


* for this article
* dwr.xml for this article
* HTML Client for this article


For those interested DWR 3 will support REST style URLs out-of-the-box. The implementation differs from what is shown here though.