Remote calls with DWR(Direct Web Remoting) | HCLTech

Remote calls with DWR(Direct Web Remoting)

Remote calls with DWR(Direct Web Remoting)
April 27, 2022

This is my blog for Java web applications to handle the communication between the web server and web browser. This API is easy for AJAX to call a Java method from JavaScript.

Remote calls with DWR

DWR is Java API that provides easy interaction with web servers. It minimizes steps to communicate from a web browser. It is very helpful in AJAX calls while calling a java method to get an object from the server-side. If you are familiar with the web application, the DWR can appear to be conceptually very similar.

DWR technology quietly gained popularity among Java developers. DWR is freely available for download and uses in our application.

DWR is a separate API that can be integrated with Spring, Struts and other web frameworks. These are the following steps that must be followed in our java web project.

1. Add the DWR JAR file –

Download the DWR jar file and place it in the lib folder and add it to the project build path.

2. Add the Commons Logging JAR file

Also, we need to add the Commons Logging jar into the project build path. DWR depends on this jar.

3. Add the DWR servlet and mapping to the deployment descriptor web.xml file.

We need to add the servlet-mapping in the web.xml file. This is the servlet that enables DWR for our web application. It loads the DWR configuration file for our web application.

First, configure the servlet. This is done using the element. Here we give the servlet a name and write the class name of the servlet.

Remote calls with DWR

4. Add the DWR configuration file (dwr.xml) and include the section below.

Here, we can provide mapping of multiple java files which must be enabled with dwr. This will generate a js (JavaScript) file corresponding to each mapping. Thus, we create a Message.java file in a java project.

Remote calls with DWR

As in the above code, I have added the create tag, which will tell the DWR engine to include the JavaScript object named “Message”, using which we can call the server-side methods.

5. Add JavaScript file

It must include engine.js and utils.js in our JSP/HTML pages

DWR Project directory structure-

Remote calls with DWR

Here, we are calling the getMessage() method from the JavaScript function update(), which will return string messages. We must include the JavaScript library in our HTML file. It will use compilation and generation of JavaScript file corresponding to java class mapping in the DWR configuration file.

Remote calls with DWR

Diagram JavaScript call to Java class method.

Remote calls with DWR

Direct Web Remoting will minimize steps to communicate from a Web browser and very helpful in AJAX calls while calling a java method to get an object from the server-side

The above sample will invoke getMessage(), and it will directly show the message “Hello DWR from Web Server ” on the Web Page.

Here is a detailed example of where we can integrate DWR APIs.

Web URL:

http://localhost:8080/DWRTestProject/registration.html

Here we can access the registration page, and added student details.

Remote calls with DWR

On the same web page, we have the button below where we can access student details from the webserver using DWR.

Remote calls with DWR

In the asynchronous model, the result data is only available sometime 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 the CallBack Method. The value returned from the java function will be passed to the callback method.

References:

http://directwebremoting.org/dwr/index.html

Get HCLTech Insights and Updates delivered to your inbox

More from Kuldeep Singh