 |
Hariharasudhan Dhakshinamoorthy's Blog
Hariharasudhan Dhakshinamoorthy is an Architect working for UST Global. Before joining UST Global he was with Sonoa Systems, a SOA appliance company. And prior to Sonoa Systems he was with Infravio (a/k/a) Webmethods (a/k/a) SoftwareAG, a SOA Intermediatory and Registry product company. His areas of interest include SOA , Web2.0 technologies. When not at work he is interested in bikes, books, fishing.
RESTful POJO with DWR (Direct Web Remoting)
Posted by hari_ustri07 on March 30, 2008 at 07:30 AM | Permalink
| Comments (3)
Contents
* 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
http://ustri/resources/library/books
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/Calculator.java
* step 2 : create a ServletFilter ,
refer Resources/RESTFilter.java
* 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 RESTFilter.java,Calculator.java,copy them inside \WEB-INF\classes\
* step 4: edit \WEB-INF\dwr.xml
<dwr>
<allow>
<!-- Calculator Pojo Service -->
<create creator="new" javascript="Calculator">
<param name="class" value="com.example.pojo.Calculator"/>
</create>
</allow>
</dwr>
* 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
Conclusion
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.
Resources
* Calculator.java for this article
* dwr.xml for this article
* HTML Client for this article
 |
 |
June 2008
| Sun |
Mon |
Tue |
Wed |
Thu |
Fri |
Sat |
| 1 |
2 |
3 |
4 |
5 |
6 |
7 |
| 8 |
9 |
10 |
11 |
12 |
13 |
14 |
| 15 |
16 |
17 |
18 |
19 |
20 |
21 |
| 22 |
23 |
24 |
25 |
26 |
27 |
28 |
| 29 |
30 |
|
|
|
|
|
Search this blog:
Categories
Archives
March 2008
Recent Entries
RESTful POJO with DWR (Direct Web Remoting)

|