RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Apply DWR to Data Validation in an AJAX Application

Learn how to use the open source Direct Web Remoting (DWR) library to dynamically generate JavaScript that includes a callback function parameter for web page updates in AJAX applications.

irect Web Remoting (DWR) is a Java open source library for developing AJAX applications that consists of two components: JavaScript running in the browser that sends requests and dynamically updates the web page with the response, and a servlet running on the server that processes requests and sends responses back to the browser. Remoting in DWR implies that Java class methods are remoted as JavaScript functions in the browser. DWR dynamically generates JavaScript corresponding to Java classes, and the JavaScript may be run in the browser just as any other JavaScript class library.

The generated JavaScript functions that correspond to Java class methods have a callback function as one of the parameters. The remote methods are invoked in the browser using the callback function, and the request is sent to the server using AJAX. When the request is complete AJAX is used to return a response to the browser. The callback function specified in the remote method is invoked with the data returned from the server, and the web page may be updated with the server response.

Download the DWR 1.1.4 JAR file, dwr.jar. Most browsers support DWR, and the application example described here will employ Microsoft Internet Explorer 6. DWR requires the Java Developer Kit (JDK) 1.3 or later and a servlet container that supports servlet specification 2.2 or later. Install a JDK if one is not installed already. JDK 1.5.0_10 was used for the application example discussed here.

DWR has been tested on these web servers: Tomcat, WebLogic, WebSphere, JBoss, Jetty, Resin, Sun ONE, and Glassfish. For this application use JBoss 4.05. Install JBoss 4.x, if it's not installed already. This DWR application will also use the MySQL database; installing MySQL 5.0 is therefore recommended. Create a database table named UserTable in the MySQL database using this script:

CREATE TABLE UserTable(userid VARCHAR(25) 
PRIMARY KEY, password VARCHAR(25));

INSERT INTO UserTable VALUES('dvohra', 'ajaxdwr');

INSERT INTO UserTable VALUES('jsmith', 'smith');
Configure JBoss with MySQL by copying the mysql-ds.xml file from the \JBoss\jboss-4.0.5.GA\docs\examples\jca directory to the \JBoss\jboss-4.0.5.GA\server\default\deploy directory. The MySQL datasource is specified as MySqlDS in the mysql-ds.xml file. Specify the connection URL and driver class:
A password is not required for the root user. Specify the user-name and password this way:
Download the MySQL JDBC driver JAR file, mysql-connector-java-5.0.4-bin.jar, and copy it to the \jboss-4.0.5.GA\server\default\lib directory. For this discussion the DWR web application example was developed in the JDeveloper IDE. You can download the JDeveloper ZIP file, and extract it to a separate directory to install it.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date