DWR: dwr is not defined

Master Mind picture Master Mind · Oct 17, 2012 · Viewed 8.3k times · Source

I'm a starter in DWR. According to tutorial in (http://directwebremoting.org/dwr-demo/simple/text.html), i placed dwr script my html file. But when i run the application it show the following message.

ReferenceError: dwr is not defined
[Break On This Error]       
var name = dwr.util.getValue("demoName");

My HTML file is:

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type='text/javascript' src='/dwr/engine.js'>    
</script>
<script type='text/javascript' src='/dwr/util.js'>

</script>
<title>Hello</title>
<script type="text/javascript">
    function update() {
        var name = dwr.util.getValue("demoName");
        Demo.sayHello(name, function(data) {
            dwr.util.setValue("demoReply", data);
        });
    }
</script>
</head>

<body>
    <p>
        Name: <input type="text" id="demoName" /> <input value="Send"
            type="button" onclick="update()" /> <br /> Reply: <span
            id="demoReply"></span>
    </p>
</body>
</html>

My dwr.xml file is :

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

<dwr>
    <allow>
        <create creator="new" javascript="Demo">
            <param name="class" value="com.dwr.my.Demo" />
        </create>
    </allow>
</dwr>

Class file is Demo.java

package com.dwr.my;

public class Demo {

    public String sayHello(String name) {
        return "Hello, " + name;
    }

}

Answer

Tribhuwan picture Tribhuwan · Oct 17, 2012

Please make sure to add dwr servlet mapping in web.xml

<servlet>
        <servlet-name>dwr-invoker</servlet-name>
        <display-name>DWR Servlet</display-name>
        <description>Direct Web Remoter Servlet</description>
        <servlet-class>
            org.directwebremoting.servlet.DwrServlet
        </servlet-class>

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

and add this in html file

<script type='text/javascript' src='/dwr/demo.js'>    
</script>