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 : Page 3

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.

Common Creators
The create element has the required attributes creator and javascript and an optional attribute, scope:
<!ATTLIST create
  javascript CDATA #REQUIRED
  scope (application|session|request|page) #IMPLIED

The creator attribute specifies which creator is used to instantiate the Java class. The most commonly used creator is new. The javascript attribute specifies the JavaScript class library that is created from the Java class, and the scope attribute specifies the scope in which the JavaBean is available: application, page, session, and request. The default value of the optional scope attribute is page. Specify the create element for the application example:

  <create creator="new" javascript="UserRegistration">
    <param name="class" value="dwr.UserRegistration"/>

The create element has zero or more each of param, include, exclude, and auth subelements:

<!ELEMENT create ((param | include | exclude | auth)*)>

The param element specifies configuration for different types of creators. The new creator requires the type of object on which to invoke new. For example, to invoke new on the Java class UserRegistration, use a param element:

<param name="class" value="dwr.UserRegistration"/>

The include element specifies which methods are to be included for remoting in JavaScript, and the default is all methods. The exclude element specifies which Java class methods are excluded for remoting. By default, none of the methods are excluded. Here is the dwr.xml for the DWR application example:

  "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd">
    <create creator="new" javascript="UserRegistration">
      <param name="class" value="dwr.UserRegistration"/>

Copy dwr.xml to the JDeveloper project. At this point you also need to modify the web.xml deployment descriptor to specify the DWRServlet (see Listing 1). Copy web.xml to the JDeveloper project.

The Java class, UserRegistration.java, to be remoted specifies two methods: validate(String userId) and updateUserTable(String userId, String password). The validate() method validates a user ID specified in the user registration entry form. Its return type is boolean. Using the datasource configured in JBoss, a connection with the MySQL database is first obtained in the validate() method:

InitialContext initialContext = new InitialContext();
  javax.sql.DataSource ds = (javax.sql.DataSource)initialContext.lookup("java:MySqlDS");
  java.sql.Connection conn = ds.getConnection();

A SQL query is run in the MySQL database using the user ID specified in the user registration entry form:

Statement stmt = conn.createStatement();
  String query = "SELECT * from UserTable WHERE userId=" + "'" + userId + "'";
      ResultSet rs = stmt.executeQuery(query);

If the result set isn't empty, the user ID specified is defined already in the database and is therefore not valid. If the result set has data, create a boolean variable with the value false:

if (rs.next()) {
  valid  = false;
    return valid;

If the result set is empty, the user ID is valid, and a new user registration entry can be created. Return true if the result set is empty. If the user ID is valid, invoke the updateUserTable() method to create a new user entry. Listing 2 shows the UserRegistration.java class. Copy UserRegistration.java to the JDeveloper project.

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