Question

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;
    }

}
Was it helpful?

Solution

The error is telling you that the variable dwr does not exists in your JavaScript context.

It doesn't look like you have correctly set up DWR. This is confirmed by your comment on the previous answer: Shows message "NetworkError: 404 Not Found - localhost:8080/dwr/engine.js". If engine.js is not found then you aren't going to get very far!

Have you followed all the steps listed here to set up your environment so DWR is available to call?: http://directwebremoting.org/dwr/introduction/getting-started.html

OTHER TIPS

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>

Run localhost:8080/DWR_tomcat/dwr/ after implementing dwr.xml ,corresponding java class and dwr servlet mapping in web.xml . Then it will display the classes known to DWR.Then click on our class name, it will shows the declared methods in the class and generate java script tags to be add in html file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top