13.2. DWR

Direct Web Remoting (DWR) is an Ajax framework for Java/JSP available for download at http://getahead.ltd.uk/dwr. DWR works similarly to JPSpan in that it uses Java's version of reflection to examine Java bean classes and then create JavaScript wrappers to call the various methods. Also like JPSpan, DWR includes its own JavaScript library for cross-browser Ajax communication, freeing the developer from worrying about browser incompatibilities. DWR assumes the use of Apache Tomcat (http://tomcat.apache.org).

NOTE

DWR expects that the classes you use will be Java beans, meaning that they can be created without passing any information to the constructor. This is important because the server-side objects don't persist from request to request and need to be created from scratch each time.

13.2.1. Using DWR

Setting up DWR in your web application is very simple. The first step is to place the dwr.jar file into the WEB-INF/lib directory. The next step is to edit the web.xml file contained in WEB-INF. There are two sections that need to be added.

  • The first section describes the DWR invoker servlet:

    <servlet>
      <servlet-name>dwr-invoker</servlet-name>
      <display-name>DWR Servlet</display-name>
      <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
      <init-param>
         <param-name>debug</param-name>
         <param-value>true</param-value>
      </init-param>
    </servlet>

    This code needs to go with the other <servlet/> tags inside of web.xml.

  • The second section that needs to be added is as follows: ...

Get Professional Ajax, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.