Hello World to Direct Web Remoting

This is a simple article that how to hello world to DWR (Direct Web Remoting).

First, go http://directwebremoting.org/dwr/download.html download dwr.jar, for me I download 2.0.6 version in order it can be supported in Google Appengine. Put dwr.jar in WEB-INF/lib

Second, download commons-logging-1.1.1.jar, put this jar in WEB-INF/lib also.

Third, modify web.xml to add this content of

    <servlet>
  <servlet-name>dwr-invoker</servlet-name>
  <display-name>DWR Servlet</display-name>
  <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  <init-param>
     <param-name>debug</param-name>
     <param-value>true</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>dwr-invoker</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

Fourth, create dwr.xml and with the content of

<!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
    "
http://getahead.org/dwr/dwr20.dtd">

<dwr>
  <allow>
    <create creator="new" javascript="DWRStuff">
      <param name="class" value="com.fyhao.secs.DWRStuff"/>
    </create>
  </allow>
</dwr>

Then you have to create DWRStuff.java file with the content of

package com.fyhao.secs;

public class DWRStuff {

    public String shownum(int a) {
        return "" + (a * 3);
    }
}

Create a JSP file, called ajax.jsp with the content of

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src=’dwr/interface/DWRStuff.js’></script>
<script src=’dwr/engine.js’></script>
<script type="text/javascript">
function update() {
    DWRStuff.shownum(5, function(data) {
                                 alert(data);
                                 });
}
</script>
</head>

<body>
<div id="num"></div>
<script>update();
</script>
</body>
</html>

Remember the DWRStuff.js shown in the script source there.

Now, you can try to see if it is success.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.