<?xml version="1.0" encoding="utf-8"?>
<feed version="0.3" xmlns="http://purl.org/atom/ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xml:lang="en">
<title>Hariharasudhan Dhakshinamoorthy&apos;s Blog</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/hari_ustri07/" />
<modified>2008-03-30T15:32:06Z</modified>
<tagline></tagline>
<id>tag:weblogs.java.net,2008:/blog/hari_ustri07/452</id>
<generator url="http://www.movabletype.org/" version="3.01D">Movable Type</generator>
<copyright>Copyright (c) 2008, hari_ustri07</copyright>
<entry>
<title>RESTful POJO with DWR (Direct Web Remoting)</title>
<link rel="alternate" type="text/html" href="http://weblogs.java.net/blog/hari_ustri07/archive/2008/03/restful_pojo_wi_1.html" />
<modified>2008-03-30T15:32:06Z</modified>
<issued>2008-03-30T15:30:32Z</issued>
<id>tag:weblogs.java.net,2008:/blog/hari_ustri07/452.9444</id>
<created>2008-03-30T15:30:32Z</created>
<summary type="text/plain"> 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...</summary>
<author>
<name>hari_ustri07</name>

<email>Hariharasudhan.Dhakshinamoorthy@ust-global.com</email>
</author>

<content type="text/html" mode="escaped" xml:lang="en" xml:base="http://weblogs.java.net/blog/hari_ustri07/">
<![CDATA[<span style="font-size:85%;">
<span style="font-weight: bold;">Contents</span>
<br>
<pre>
* About this article
* What is REST?
* Direct Web Remoting
* Creating Restful Calculator Service
* Packaging/Deploying/Testing Calculator Service
* Conclusion
* Resources
</pre>
<span style="font-weight: bold;">About this article</span>
<pre>
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).
<pre><br>
<span style="font-weight: bold;">Direct Web Remoting</span>
<pre>
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.
</pre>
<br>
<span style="font-weight: bold;">Packaging/Deploying/Testing Calculator Service</span>
<br>
<span style="font-weight: bold;">Packaging / Deploying:</span>
<pre>
* 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
</pre><br>
&lt;dwr&gt;
&lt;allow&gt;
&lt;!-- Calculator Pojo Service --&gt;
&lt;create creator="new" javascript="Calculator"&gt;
&lt;param name="class" value="com.example.pojo.Calculator"/&gt;
&lt;/create&gt;
&lt;/allow&gt;
&lt;/dwr&gt;
<pre>
* step 5: configure servlet filter RESTFilter in web.xml
* step 6: copy client html appropriately
* step 7: package it and deploy it
</pre>
<span style="font-weight: bold;">Testing Calculator Service :</span>
<p><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://bp0.blogger.com/_1dIJcXQlT4g/R8_XaWPKRzI/AAAAAAAAAA8/zpxglApVrl4/s1600-h/addTwoNumbers.JPG"><img style="cursor: pointer;" src="http://bp0.blogger.com/_1dIJcXQlT4g/R8_XaWPKRzI/AAAAAAAAAA8/zpxglApVrl4/s320/addTwoNumbers.JPG" alt="" id="BLOGGER_PHOTO_ID_5174591344517728050" border="0" /></a>
<img src="http://www.geocities.com/hari_infra/addTwoNumbers.jpg" alt="" />
<i>Figure 1. addTwoNumbers</i>
</p>
<br>
<span style="font-weight: bold;">Conclusion</span>
<pre>
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.
</pre>
<br>
Resources <br>
* <a href="http://www.geocities.com/hari_infra/Calculator.java">Calculator.java </a> for this article
* <a href="http://www.geocities.com/hari_infra/RESTFilter.java%3ERESTFilter.java%3C/a%3E%20for%20this%20article%3Cbr%3E%20%20*%20%3Ca%20href=" com="" hari_infra="" zip="">dwr.xml</a> for this article
* <a href="http://www.geocities.com/hari_infra/client.html.zip">HTML Client</a> for this article

</span>]]>

</content>
</entry>

</feed>