3

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

}
Master Mind
  • 2,386
  • 3
  • 31
  • 48

3 Answers3

1

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>
Tribhuwan
  • 180
  • 1
  • 1
  • 11
  • how to generate demo.js and where toplace it. No problem in web.xml. I placed code as you mentioned but haven't any change. – Master Mind Oct 17 '12 at 11:52
  • It will be automatically generated just give reference in the html page – Tribhuwan Oct 17 '12 at 12:09
  • i gave it, but not working.Shows message "NetworkError: 404 Not Found - http://localhost:8080/dwr/engine.js"engine.js "NetworkError: 404 Not Found - http://localhost:8080/dwr/demo.js" demo.js"NetworkError: 404 Not Found - ttp://localhost:8080/dwr/util.js – Master Mind Oct 17 '12 at 12:21
  • No this is not what i asked, you need to add inside html's head tag. Please refer this [http://directwebremoting.org/dwr/introduction/getting-started.html](http://directwebremoting.org/dwr/introduction/getting-started.html) – Tribhuwan Oct 17 '12 at 12:34
  • yes i add the script tag in and no change. As u look on my qn, i followed the same site. – Master Mind Oct 17 '12 at 12:37
  • By the way in this url context name is missing [http://localhost:8080/[context name]/dwr/engine.js](http://localhost:8080/[context_name]/dwr/engine.js) – Tribhuwan Oct 17 '12 at 12:49
1

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

Community
  • 1
  • 1
Chris R
  • 2,464
  • 3
  • 25
  • 31
1

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.

Master Mind
  • 2,386
  • 3
  • 31
  • 48