About this book

AJAX enables rich desktop-like user interfaces in the browser and enables interactive interfaces that can even replace traditional user interfaces. DWR (Direct Web Remoting) is an Open Source Java framework for building AJAX applications. DWR's main idea is to hide AJAX implementation details, like XMLHttpRequest, from developers. By using DWR, you can concentrate on developing the application and business objects and leave AJAX details behind the scenes where they belong. DWR allows server-side Java classes to be used in the browser and JavaScript functions to be used on the server (Reverse AJAX).

This book will show competent Java programmers how to work with DWR to create powerful AJAX applications. It starts with a tutorial on DWR's main features and functions. Then it covers setting up the development environment. Finally, it concludes with some sample applications.

The later chapters are full of example code for sample applications, to aid comprehension.

Publication date:
October 2008
Publisher
Packt
Pages
228
ISBN
9781847192936

 

Chapter 1. Introduction

Learning by doing is a key if you want to benefit from this book. Since the target audience is the developer community, much of this book consists of examples using DWR in action. Chapters 1 to 3 introduce the main features of DWR, and discuss how to get the development work started. Chapters 4 to 7 are full of sample code, and focus on the source code samples and applications.

This chapter introduces AJAX technology and a widely used Java framework for building AJAX applications: Direct Web Remoting, commonly known as DWR. The introductory sections on both AJAX and DWR are brief since AJAX is already a well-known technology and most of us have at least heard about it and know what it stands for. The introduction to DWR is presented in a short "executive summary" before we dive into more details and examples on DWR in the later chapters.

The following sections are discussed in this chapter:

  • What is AJAX?

  • DWR: AJAX for Java Developers

  • The DWR Community

What is AJAX?

AJAX is the abbreviation for Asynchronous JavaScript and XML. This gives an almost comprehensive explanation of the technology, except that XML is not required. The term AJAX surfaced around February 2005 and was first used by Jesse James Garrett (http://www.adaptivepath.com/ideas/essays/archives/000385.php), long after the building blocks of AJAX, JavaScript and XML, were available and in wide use.

The principle idea of AJAX is in the word "Asynchronous". This feature enables rich a desktop-like user interface in the browser and enables interactive interfaces that can even replace traditional user interfaces. Communication between browser and server is done in the background. Moreover, as only the data is transferred between the browser and the server, AJAX applications are actually fast and responsive to users. The following figures display how a typical request-response application works (upper diagram), and how AJAX applications work compared to the request-response application (lower diagram).

The main component of AJAX technology is XmlHttpRequest, which is a JavaScript object, first supported by Mozilla in 2002. The concept was originally developed by Microsoft in 1999 for Internet Explorer 5.0 and it was then called XMLHTTP. The following link provides the details and history of XmlHttpRequest: http://en.wikipedia.org/wiki/XMLHttpRequest.

XmlHttpRequest is used to transfer data between client and server asynchronously. The following figure shows the AJAX sequence diagram and how XmlHttpRequest is used:

AJAX is dealt with in detail in many books such as Head Rush Ajax, a "brain friendly guide" to AJAX and also on Internet sites. Further, the assumption is that most of you already know AJAX and you are reading this book for reasons other than just learning AJAX basics.

 

What is AJAX?


AJAX is the abbreviation for Asynchronous JavaScript and XML. This gives an almost comprehensive explanation of the technology, except that XML is not required. The term AJAX surfaced around February 2005 and was first used by Jesse James Garrett (http://www.adaptivepath.com/ideas/essays/archives/000385.php), long after the building blocks of AJAX, JavaScript and XML, were available and in wide use.

The principle idea of AJAX is in the word "Asynchronous". This feature enables rich a desktop-like user interface in the browser and enables interactive interfaces that can even replace traditional user interfaces. Communication between browser and server is done in the background. Moreover, as only the data is transferred between the browser and the server, AJAX applications are actually fast and responsive to users. The following figures display how a typical request-response application works (upper diagram), and how AJAX applications work compared to the request-response application (lower diagram).

The main component of AJAX technology is XmlHttpRequest, which is a JavaScript object, first supported by Mozilla in 2002. The concept was originally developed by Microsoft in 1999 for Internet Explorer 5.0 and it was then called XMLHTTP. The following link provides the details and history of XmlHttpRequest: http://en.wikipedia.org/wiki/XMLHttpRequest.

XmlHttpRequest is used to transfer data between client and server asynchronously. The following figure shows the AJAX sequence diagram and how XmlHttpRequest is used:

AJAX is dealt with in detail in many books such as Head Rush Ajax, a "brain friendly guide" to AJAX and also on Internet sites. Further, the assumption is that most of you already know AJAX and you are reading this book for reasons other than just learning AJAX basics.

 

DWR: AJAX for Java Developers


Direct Web Remoting (http://www.directwebremoting.org), is an Open Source Java framework, licensed under commercial-friendly Apache Software License v2 (http://www.apache.org/licenses/LICENSE-2.0.html) for building AJAX applications. DWR's main idea is to hide AJAX implementation details such as XMLHttpRequest from developers. Developers can concentrate on developing the application and business objects and leave AJAX details behind the scenes where they belong.

DWR allows server-side Java classes to be used in a browser (it's like RPC between JavaScript functions and server-side Java) and also allows JavaScript functions to be used in a server (Reverse AJAX). Through an XML-based configuration, DWR dynamically generates JavaScript functions of Java classes, which can be called from the browser via a DWR JavaScript library. A DWR servlet on the server side receives requests and calls the actual Java implementation.

The following figure displays the positioning of DWR in user applications and is taken from the DWR website (http://directwebremoting.org/dwr/overview/dwr).

In the previous figure, the JavaScript function eventHandler() responds to some browser event like clicking a button. Event handlers use the AjaxService object and call the getOptions() method just as if AjaxService were a normal JavaScript object. A developer can implement client-side and server-side code and leave the communication between the client and the server to DWR.

Calling server-side Java from JavaScript causes a lot of things to happen and also requires a lot to happen behind the scenes:

  • When a DWR-enabled web page is requested, DWR dynamically generates JavaScript functions from Java classes based on configuration.

    When the function eventHandler() gets called, say when clicking a button, the developer calls a dynamically generated function (AjaxService.getOptions(populateList) as shown in the previous figure) and the DWR JavaScript library takes the parameters, serializes them, and calls the DWR servlet on the server.

  • The DWR servlet receives a request and, based on configuration, the servlet instantiates a Java object such as AjaxService, and calls the required method, for example, AjaxService.getOptions(populateList).

  • When the Java method is finished, the return value (a String array) is returned to the DWR servlet, and servlet serializes the return value and sends the response to the DWR JavaScript function on the browser.

  • The DWR JavaScript function receives the response on the browser and based on the parameter, populateList (this is the name of the callback function in the AjaxService.getOptions() function), DWR calls the populateList function with the return value from the Java method as the parameter. The AjaxService.getOptions() JavaScript function is generated dynamically by DWR, and it communicates with the DWR servlet behind the scenes.

  • The callback function (written by the developer) does the desired thing with the return value such as updating the browser page and adding new options to the drop-down field.

DWR is well-suited for Java developers, because this kind of approach is very easy to get into.

 

The DWR Community


DWR has very active mailing lists available for DWR users:

These mailing lists have lots of information, which would be helpful to any DWR user, and any one can participate in them.

There is a mailing list available for DWR security, where DWR security issues are discussed. In order to participate, application to list must be made separately and it must include corporate email addresses and titles or other means of identification. Separate applications are made so that DWR security issues can be discussed without public dessemination (it is not intended to keep security issues a secret). Application to join security mailing list can be done from http://groups.google.com/group/dwr-security

DWR development mailing list are as follows:

he blog of DWR creator Joe Walker (http://directwebremoting.org/blog/joe) includes news about DWR and thoughts about web development.

Commercial support is also available from SitePen (http://sitepen.com/services/support.php). There are several support packages available, and there are also possibilities to customize support packages to specific needs.

DWR is used by thousands of developers, and it has been used by companies such as American Airlines, Walmart, Citigroup, Mastercard, and many others. DWR is used both in public sites and Intranet sites.

DWR is also part of the Dojo Foundation, which is an independent legal entity that provides infrastructure for development. The members include IBM and SitePen. In addition to DWR, Dojo Foundation also sponsors Dojo Toolkit, OpenRecord, and Cometd projects. The following website has more information about the Dojo Foundation:

http://dojotoolkit.org/foundation

 

Summary


This chapter briefly introduced AJAX and DWR. AJAX is a fundamental technology that uses browser-based JavaScript to build Internet applications with better user experience than typical request-response web applications.

DWR is a Java framework for building AJAX applications, and it is targeted mainly at Java developers who don't want to implement the low-level "stuff" that makes AJAX work.

The rest of the book is about DWR features and sample code that can be used in your own projects. The examples provided in the later chapters show some of the common situations in which to use DWR and how to use it.

About the Author

  • Sami Salkosuo

    Sami Salkosuo is a Software IT Architect at IBM Software Group in Finland. He has over ten years of experience in Java, web, and integration technologies.

    Sami has written several articles for IBM developerWorks and he is also a coauthor of an IBM Redbook: Portalizing Domino Applications.

    Browse publications by this author
Book Title
Unlock this book and the full library for FREE
Start free trial