DWR (Java)
Encyclopedia
DWR, or Direct Web Remoting, is a Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 open source
Open source
The term open source describes practices in production and development that promote access to the end product's source materials. Some consider open source a philosophy, others consider it a pragmatic methodology...

 library that helps developers write web sites that include Ajax
Ajax (programming)
Ajax is a group of interrelated web development methods used on the client-side to create asynchronous web applications...

 technology. It allows code in a web browser to use Java functions running on a web server as if those functions were within the browser.

It consists of two main parts:
  • Code to allow JavaScript
    JavaScript
    JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

     to retrieve data from a servlet-based web server using Ajax principles.
  • A JavaScript library that makes it easier for the web site developer to dynamically update the web page with the retrieved data.


DWR takes a novel approach to Ajax by dynamically generating JavaScript code based on Java classes. Thus the web developer can use Java code from JavaScript as if it were local to the web browser; whereas in reality the Java code runs in the web server and has full access to web server resources. For security reasons the web developer must configure exactly which Java classes are safe to export (which is often called web.xml or dwr.xml).

This method of remoting functions from Java to JavaScript gives DWR users a feel much like conventional RPC
Remote procedure call
In computer science, a remote procedure call is an inter-process communication that allows a computer program to cause a subroutine or procedure to execute in another address space without the programmer explicitly coding the details for this remote interaction...

 mechanisms like RMI
Java remote method invocation
The Java Remote Method Invocation Application Programming Interface , or Java RMI, is a Java application programming interface that performs the object-oriented equivalent of remote procedure calls ....

 or SOAP
SOAP
SOAP, originally defined as Simple Object Access Protocol, is a protocol specification for exchanging structured information in the implementation of Web Services in computer networks...

, with the benefit that it runs over the web without requiring web browser plug-ins.

DWR does not consider the web browser / web server protocol to be important, and prefers to ensure that the programmer's interface is natural. The greatest challenge to this is to marry the asynchronous nature of Ajax with the synchronous nature of normal Java method calls.

In the asynchronous model, result data is only available some time after the initial call is made. DWR solves this problem by allowing the web developer to specify a function to be called when the data is returned using an extra method parameter.This extra method is called CallBack Method.

Here is a sample Callback:

MJavaClassOnJs.getListProducts(selectedCategory,{
callback:function(returnedList){
dwr.util.addOptions(myComboId,returnedList,"productId","productName")
}
})


The callback is that function inside the Json
JSON
JSON , or JavaScript Object Notation, is a lightweight text-based open standard designed for human-readable data interchange. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects...

 object passed as an additional parameter to the remoted function.

With version 2.0 DWR supports Reverse Ajax where Java code running on the server can deliberately send dedicated JavaScript to a browser.

Joe Walker started the DWR project in 2004.
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK