Upload
hussulinux
View
10.365
Download
1
Embed Size (px)
DESCRIPTION
Tutorial on how to DWR
Citation preview
DWR: Direct Web Remoting
Hussain [email protected]
About Me
Open Source Advocate
Believe in coding with style
Blogger(hussulinux.blogspot.com)
Pure Vegetarian
DWR:Agenda
What is DWR? FAQ Advantages Tutorial
DWR: Definition
Easy Ajax for Java. Export your Java code to a browser and include the results in your pages.
DWR:Definition
In other words its nothing butAn RPC library which makes it easy to call Java functions from JavaScript and to call JavaScript functions from Java (a.k.a Reverse Ajax).
Some features
Virtually any data-structure between Java and Javascript (including binary file uploading and downloading)
exception handling advanced CSRF(Cross site request forgery)
protection Integration with Spring and Guice
JS to Java
Java to JS: Reverse Ajax
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
Ingredients
A Java Servlet running on the server that processes requests and sends responses back to the browser.
JavaScript running in the browser that sends requests and can dynamically update the webpage
Dynamic JS
DWR generates dynamic JS based on the Java Classes
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.
Advantages
Call Java Functions like RMI or Soap Automatically create Java versions of
JS Expose business methods through JS
Tutorial
Calculator Example What you need? Eclipse Step by Step Run
Calulator Example
We will write a Java class which has the following methods
int add(int a, int b); int subtract(int a , int b); int divide(int a, int b); int multiply (int a, int b);
What you need?
Download DWR.jar from : http://getahead.org/dwr/download
Add the Jar
Add Dependency
Apache Commons:http://commons.apache.org/downloads/download_logging.cgi
Add Jars to Project
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>
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;} elsereturn (float)((float)a / (float)b);}
}
Add dwr.xml along with web.xml
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<dwr> <allow> <create creator="new" javascript="Calculator"> <param name="class" value="org.hussulinux.samples.Calculator"/> </create> </allow></dwr>
Open Default page to check
Create front end
index.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>My First DWR Example</title>
<script type='text/javascript' src='/CalculatorDWR/dwr/interface/Calculator.js'></script> <script type='text/javascript' src='/CalculatorDWR/dwr/engine.js'></script> <script type='text/javascript' src='/CalculatorDWR/dwr/util.js'></script> </head> <body> A:<input type ="text" id="a"><br/> B:<input type ="text" id="b"><br/> <hr/> <br/> <input type ="button" value="Add" onClick="Calculator.add(a.value,b.value,replyfunc)" ><br/> <input type ="button" value="Subtract" onClick='Calculator.subtract($("a").value,$("b").value,replyfunc);' ><br/> <input type ="button" value="Multiply" onClick="Calculator.multiply(a.value,b.value,replyfunc)" ><br/> <input type ="button" value="Divide" onClick="Calculator.divide(a.value,b.value,replyfunc)" ><br/> <script type="text/javascript"> var replyfunc=function(data){ if (data!=null && typeof data=='object') alert(dwr.util.toDescriptiveString(data,2)); else dwr.util.setValue("answerdiv", dwr.util.toDescriptiveString(data,1)); } </script> <hr/> Answer = <div id ="answerdiv"> </div> </body> </html>
Run
Queries
Thanks!
Joe Walker for starting this as open source project
TIBCO for supporting this project You can email me at:
[email protected] Visit: http://hussulinux.blogspot.com