Monday, April 10, 2017

Document Web Routing


Direct Web Remoting (DWR) technology developed by Joe Walker and maintained by the small IT consultancy Getahead in UK.

DWR is a RPC library which makes it easy to call Java functions from JavaScript and to call JavaScript functions from Java (a.k.a Reverse Ajax).

DWR takes a novel approach to Ajax by dynamically generating JavaScript code based on Java classes.

DWR has a number of features like call batching, marshalling of virtually any data-structure between Java and Javascript (including binary file uploading and downloading), exception handling, advanced CSRF protection and deep integration with several Java server-side technologies like Spring and Guice.

DWR consists of two main parts:
  • 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.
DWR works by dynamically generating Javascript based on Java classes. The code 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.
This method of remoting functions from Java to JavaScript gives DWR users a feel much like conventional RPC mechanisms like RMI or SOAP, with the benefit that it runs over the web without requiring web-browser plug-ins.
The DWR project is developing a method of automatically creating Java versions of JavaScript APIs which developers can use to control browsers from the server. A server-side version of the TIBCO GI library is currently in alpha release, and the DWR project aims to expand this to cover other client side APIs including the Dojo Toolkit, JQuery, YUI, Ext and others.
DWR integration with Maven:-

Project Structure 
Before we start adding some code to our application lets take a look at overall project structure. Create a simple 'web application' project in 'Eclipse' and add all code files to it as mentioned in part of this blog.



pom.xml
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.project.in</groupId>
  5. <artifactId>DemoProject</artifactId>
  6. <version>DemoProject</version>
  7. <packaging>war</packaging>
  8. <dependencies>
  9. <dependency>
  10. <groupId>commons-logging</groupId>
  11. <artifactId>commons-logging</artifactId>
  12. <version>1.1.1</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>commons-beanutils</groupId>
  16. <artifactId>commons-beanutils</artifactId>
  17. <version>1.8.3</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.directwebremoting</groupId>
  21. <artifactId>dwr</artifactId>
  22. <version>2.0.10</version>
  23. </dependency>
  24. </dependencies>
  25. </project>


/webapp/WEB-INF/web.xml
First of all add some entries to your 'web.xml' so that the 'tomcat container' could understand the behavior of application. Add a servlet entry point for 'DWRServlet' to make the container understand that all dwr configuration is done in it and going to be used accordingly. Now add 'url-mapping' to 'DWRServlet' , this tells the container that all requests with /'.html' extention are going to be handled by spring itself. At the end of the file a entry for welcome file is added that indicates the starting point of the application.


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. version="2.5">
  6. <display-name>DemoProject</display-name>
  7. <servlet>
  8. <servlet-name>dwr-invoker</servlet-name>
  9. <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
  10. <init-param>
  11. <param-name>debug</param-name>
  12. <param-value>true</param-value>
  13. </init-param>
  14. <init-param>
  15. <param-name>crossDomainSessionSecurity</param-name>
  16. <param-value>false</param-value>
  17. </init-param>
  18. </servlet>
  19. <servlet-mapping>
  20. <servlet-name>dwr-invoker</servlet-name>
  21. <url-pattern>/dwr/*</url-pattern>
  22. </servlet-mapping>
  23. <welcome-file-list>
  24. <welcome-file>index.jsp</welcome-file>
  25. </welcome-file-list>
  26. </web-app>


/webapp/WEB-INF/dwr.xml


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://directwebremoting.org/schema/dwr20.dtd">
  3. <dwr>
  4. <allow>
  5. <convert match="com.np.in.Employee" converter="bean"
  6. javascript="crmDwr">
  7. <param name="include" value="id,name"></param>
  8. </convert>
  9. <create creator="new" javascript="crmDwr">
  10. <param name="class" value="com.np.in.DwrManagerImpl" />
  11. </create>
  12. <convert converter="exception" match="java.lang.Exception" />
  13. <convert converter="bean" match="java.lang.StackTraceElement" />
  14. </allow>
  15. </dwr>