web.xml文件的编写
<servlet> <servlet-name>dwr-invoker</servlet-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> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN" "http://www.getahead.ltd.uk/dwr/dwr10.dtd"> <dwr> <allow> <create creator="new" javascript="Demo"> <param name="class" value="com.Demo"/> </create> </allow> </dwr>
java类文件
package com; public class Demo { public String sayHello(String name) { return "Hello," + name; } }
html文件demo.xtml
<title>Text Demo</title> <script src='dwr/interface/Demo.js'></script> <!--此处的Demo.js是dwr.xml中create标签的javascript的值--> <script src='dwr/engine.js'></script> <script type='text/javascript' src='dwr/util.js'></script> <script> var allname; function update() { var name = dwr.util.getValue("demoName"); <!--在这里直接类名.方法就可以调用java方法了--> Demo.sayHello(name,function(data){ dwr.util.setValue("demoReply",data); allname=data; }); alert(allname); } </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>
还要导入Dwr所需的jar文件,可以从dwr的官网下载