Connecting Flash and Javascript via BlazeDS and DWR – 2/2
Juten Tach,
in this second post, i describe, how to access the same Java class – which i already made accessible through BlazeDS for my flash client – with HTML/Javascript through the DWR framework. We have used DWR successfully in another project for a popular rim manufacturer and it worked smoothly.
Putting DWR to work on the server side is also very easy. Simply download the dwr.jar from their website, put it in the lib folder of the web application folder and in the main web.xml add the following lines:
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-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>
You can leave the debug param if you want, of course. These lines setup the dwr service on for your tomcat. Now you have to add one more file, the dwr.xml file, which is the configuration file for DWR. You put it directly next to the web.xml file. In my case, it looks like this:
<!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="
de.ghost23.blaze_dwr_test.Calculator"/>
</create>
</allow>
</dwr>
This is quite similar to the remoting-config.xml we wrote for BlazeDS. Note, that here, we define, how the Class will be made visible to javascript, resulting in an javascript object, which will be called Calculator as well, and will the same methods, as my java class. Very elegant.
Now on the client side, things couldn’t be more easy. You simply write a tiny html file, and put it on the server, in my case, i put my client files in the “client” folder. My html file then looks like this:
<html> <head> <script src='/sbusse/dwr/interface/Calculator.js'></script> <script src='/sbusse/dwr/engine.js'></script> <script language="Javascript"> function init() { Calculator.multiply(4, 5, handleGetData); } function handleGetData(result) { alert(result); } </script> </head> <body onload="init();"> </body> </html>
Note, that both javascript files are not static, but generated from DWR at runtime, so do not search for them on the server. Using the server-side java class from within javascript is very easy, as you can see.
Well, that’s it. As you can see, i have written a java class, that knows nothing about the connectors, that make it accessible via remoting to my clients, so this solution is very transparent in this term, which was important to us for our current project. We now can use the same java class from within Flash via BlazeDS and from within Javascript in HTML via DWR.
Source code for this test can be found <a href=/blog/wp-content/uploads/tomcat_webapp_blazeds_dwr.zip” target=_blank”>here</a>.

