Integrating Java and JavaScript with the DWR, Java, and Dojo Toolbox
How many java™web development frameworks, libraries, and toolbox can you quickly tell? Yes, there are so many things that it's hard to figure out what their features are and which features can really help you solve the problem. However, if you are engaged in Ajax development, then you must know this library: Direct Web Remoting (DWR). It dramatically simplifies Ajax development with the Java language and Java Web technology, and sets the standard for how to seamlessly integrate Ajax into Java Web applications. In fact, DWR joined the Dojo Foundation, which brings together a number of popular Open-source Ajax technologies. In this article, learn about using DWR to easily develop Ajax.
This article is part 3rd and last in a 3-part series that describes the popular JavaScript libraries needed to create AJAX-supported applications. In the 1th part, you learned how to use the Prototype library to build a WEB application that manages songs. Part 2nd discusses how to use the Scriptaculous library to build a WEB application to manage photos. This article focuses on showing you how to use DWR to simplify Ajax development.
This article uses the DWR 2.0 version. Because generics and annotations are used, the example code requires Java 5+. The sample application combines MySQL 5.12 and Tomcat 6.0.14. However, you need to be able to easily switch to other implementations. The application also uses the Java persistence API (JPA) and OpenJPA 1.0 for data access and application JPA implementations, respectively. Again, this requires the ability to switch to other JPA implementations (such as Hibernate, Kodo, etc.). This article uses the Firefox Firebug plug-in, because it's a great Ajax debugging tool. Links to these tools are available in the Resources section.
Introduction to Direct Web Remoting (DWR)
Ajax applications look a little cryptic, but the process of developing an application is more intuitive. For each Ajax interaction, you must create an endpoint on the server (a term borrowed from a friend who is engaged in a WEB service) and create client code to invoke that endpoint. In addition, you must create all the code used to serialize the data flow between the client and the server. These server endpoints can be generic services or even REST-type endpoints. However, their creation is typically specific to the needs of the client. Tight coupling should sometimes be avoided, but sometimes it should be used. In the latter case, DWR is a package solution. It allows you to expose server-side code as an AJAX endpoint, and all templates are automatically generated. Now, let's look at how DWR works with a specific example.
Sample application: Ajax Message Board
The sample application used here is a simple walls. The data model has been greatly simplified so that you can focus on using DWR Ajax interactions. Let's look at the backend of the application and see how DWR can cascade it to enable AJAX functionality for the application.