Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Direct Web Remoting : DWR


Published on

Tutorial on how to DWR

Published in: Business, Technology
  • Be the first to comment

Direct Web Remoting : DWR

  1. 1. DWR: Direct Web Remoting Hussain Fakhruddin [email_address]
  2. 2. About Me <ul><li>Open Source Advocate </li></ul><ul><li>Believe in coding with style </li></ul><ul><li>Blogger (‏ </li></ul><ul><li>Pure Vegetarian </li></ul>
  3. 3. DWR:Agenda <ul><li>What is DWR? </li></ul><ul><li>FAQ </li></ul><ul><li>Advantages </li></ul><ul><li>Tutorial </li></ul>
  4. 4. DWR: Definition <ul><li>Easy Ajax for Java. Export your Java code to a browser and include the results in your pages. </li></ul>
  5. 5. DWR:Definition <ul><li>In other words its nothing but An RPC library which makes it easy to call Java functions from JavaScript and to call JavaScript functions from Java (a.k.a Reverse Ajax). </li></ul>
  6. 6. Some features <ul><li>Virtually any data-structure between Java and Javascript (including binary file uploading and downloading) </li></ul><ul><li>exception handling </li></ul><ul><li>advanced CSRF(Cross site request forgery) protection </li></ul><ul><li>Integration with Spring and Guice </li></ul>
  7. 7. JS to Java
  8. 8. Java to JS: Reverse Ajax <ul><li>allows Java code running on the server to find out what clients are viewing certain pages, and to send to them JavaScript, generated either manually or using a Java API </li></ul>
  9. 9. Ingredients <ul><li>A Java Servlet running on the server that processes requests and sends responses back to the browser. </li></ul><ul><li>JavaScript running in the browser that sends requests and can dynamically update the webpage </li></ul>
  10. 10. Dynamic JS <ul><li>DWR generates dynamic JS based on the Java Classes </li></ul><ul><li>The DWR Engine then does some Ajax magic to make it feel like the execution is happening on the browser, but in reality the server is executing the code and DWR is marshalling the data back and forwards. </li></ul>
  11. 11. Advantages <ul><li>Call Java Functions like RMI or Soap </li></ul><ul><li>Automatically create Java versions of JS </li></ul><ul><li>Expose business methods through JS </li></ul>
  12. 12. Tutorial <ul><li>Calculator Example </li></ul><ul><li>What you need? </li></ul><ul><li>Eclipse Step by Step </li></ul><ul><li>Run </li></ul>
  13. 13. Calulator Example <ul><li>We will write a Java class which has the following methods </li></ul><ul><li>int add(int a, int b); </li></ul><ul><li>int subtract(int a , int b); </li></ul><ul><li>int divide(int a, int b); </li></ul><ul><li>int multiply (int a, int b); </li></ul>
  14. 14. What you need? <ul><li>Download DWR.jar from : </li></ul><ul><ul><li> </li></ul></ul>
  15. 15. Add the Jar
  16. 16. Add Dependency <ul><li>Apache Commons: http:// </li></ul>
  17. 17. Add Jars to Project
  18. 18. Edit the web.xml <servlet> <servlet-name>dwr-invoker</servlet-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>
  19. 19. Write your Business Logic package org.hussulinux.samples; public class Calculator { public Calculator() { } public int add( int a, int b) { return a + b; } public int subtract( int a, int b) { return a - b; } public int multiply( int a, int b) { return a * b; } public float divide( int a, int b) { if (b == 0) { return 0; } else return ( float )(( float )a / ( float )b); } }
  20. 20. Add dwr.xml along with web.xml <!DOCTYPE dwr PUBLIC &quot;-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN&quot; &quot;;> <dwr> <allow> <create creator=&quot;new&quot; javascript=&quot;Calculator&quot;> <param name=&quot;class&quot; value=&quot;org.hussulinux.samples.Calculator&quot;/> </create> </allow> </dwr>
  21. 21. Open Default page to check
  22. 22. Create front end
  23. 23. index.jsp <ul><li><!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;;> </li></ul><ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot;> </li></ul><ul><li><title>My First DWR Example</title> </li></ul><ul><li><script type='text/javascript' src='/CalculatorDWR/dwr/interface/Calculator.js'></script> </li></ul><ul><li><script type='text/javascript' src='/CalculatorDWR/dwr/engine.js'></script> </li></ul><ul><li><script type='text/javascript' src='/CalculatorDWR/dwr/util.js'></script> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li>A:<input type =&quot;text&quot; id=&quot;a&quot;><br/> </li></ul><ul><li>B:<input type =&quot;text&quot; id=&quot;b&quot;><br/> </li></ul><ul><li><hr/> </li></ul><ul><li><br/> </li></ul><ul><li><input type =&quot;button&quot; value=&quot;Add&quot; onClick=&quot;Calculator.add(a.value,b.value,replyfunc)&quot; ><br/> </li></ul><ul><li><input type =&quot;button&quot; value=&quot;Subtract&quot; onClick='Calculator.subtract($(&quot;a&quot;).value,$(&quot;b&quot;).value,replyfunc);' ><br/> </li></ul><ul><li><input type =&quot;button&quot; value=&quot;Multiply&quot; onClick=&quot;Calculator.multiply(a.value,b.value,replyfunc)&quot; ><br/> </li></ul><ul><li><input type =&quot;button&quot; value=&quot;Divide&quot; onClick=&quot;Calculator.divide(a.value,b.value,replyfunc)&quot; ><br/> </li></ul><ul><li><script type=&quot;text/javascript&quot;> </li></ul><ul><li>var replyfunc=function(data){ </li></ul><ul><li>if (data!=null && typeof data=='object')‏ </li></ul><ul><li>alert(dwr.util.toDescriptiveString(data,2)); </li></ul><ul><li>else </li></ul><ul><li>dwr.util.setValue(&quot;answerdiv&quot;, dwr.util.toDescriptiveString(data,1)); </li></ul><ul><li>} </li></ul><ul><li></script> </li></ul><ul><li><hr/> </li></ul><ul><li>Answer = <div id =&quot;answerdiv&quot;> </div> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  24. 24. Run
  25. 25. Queries
  26. 26. Thanks! <ul><li>Joe Walker for starting this as open source project </li></ul><ul><li>TIBCO for supporting this project </li></ul><ul><li>You can email me at: [email_address] </li></ul><ul><li>Visit: </li></ul>