26
DWR: Direct Web Remoting Hussain Fakhruddin [email protected]

Direct Web Remoting : DWR

Embed Size (px)

DESCRIPTION

Tutorial on how to DWR

Citation preview

Page 1: Direct Web Remoting : DWR

DWR: Direct Web Remoting

Hussain [email protected]

Page 2: Direct Web Remoting : DWR

About Me

Open Source Advocate

Believe in coding with style

Blogger(hussulinux.blogspot.com)

Pure Vegetarian

Page 3: Direct Web Remoting : DWR

DWR:Agenda

What is DWR? FAQ Advantages Tutorial

Page 4: Direct Web Remoting : DWR

DWR: Definition

Easy Ajax for Java. Export your Java code to a browser and include the results in your pages.

Page 5: Direct Web Remoting : DWR

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).

Page 6: Direct Web Remoting : DWR

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

Page 7: Direct Web Remoting : DWR

JS to Java

Page 8: Direct Web Remoting : DWR

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

Page 9: Direct Web Remoting : DWR

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

Page 10: Direct Web Remoting : DWR

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.

Page 11: Direct Web Remoting : DWR

Advantages

Call Java Functions like RMI or Soap Automatically create Java versions of

JS Expose business methods through JS

Page 12: Direct Web Remoting : DWR

Tutorial

Calculator Example What you need? Eclipse Step by Step Run

Page 13: Direct Web Remoting : DWR

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);

Page 14: Direct Web Remoting : DWR

What you need?

Download DWR.jar from : http://getahead.org/dwr/download

Page 15: Direct Web Remoting : DWR

Add the Jar

Page 16: Direct Web Remoting : DWR

Add Dependency

Apache Commons:http://commons.apache.org/downloads/download_logging.cgi

Page 17: Direct Web Remoting : DWR

Add Jars to Project

Page 18: Direct Web Remoting : DWR

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>

Page 19: Direct Web Remoting : DWR

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);}

}

Page 20: Direct Web Remoting : DWR

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>

Page 21: Direct Web Remoting : DWR

Open Default page to check

Page 22: Direct Web Remoting : DWR

Create front end

Page 23: Direct Web Remoting : DWR

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>

Page 24: Direct Web Remoting : DWR

Run

Page 25: Direct Web Remoting : DWR

Queries

Page 26: Direct Web Remoting : DWR

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