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.
4. Add the DWR configuration file (dwr.xml) and include the section below.
It must include engine.js and utils.js in our JSP/HTML pages
DWR Project directory structure-
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.
Here we can access the registration page, and added student details.
On the same web page, we have the button below where we can access student details from the webserver using 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.