dwr小例子
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>
package com;public class Demo { public String sayHello(String name) { return "Hello, " + name; } }
<title>Text Demo</title> <script src='dwr/interface/Demo.js'></script> <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"); 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>