浏览器访问http://localhost:8080/dwr 会出现生成的js的路径 web.xml 把dwr实例demo复制过来 <listener> <listener-class>org.directwebremoting.servlet.DwrListener</listener-class> </listener> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <!-- This should NEVER be present in live --> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <!-- For more information on these parameters, see: - http://getahead.org/dwr/server/servlet - http://getahead.org/dwr/reverse-ajax/configuration --> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
dwr.xml
<dwr>
<allow>
<!-- intro - for the test on index.html -->
<create creator="new" javascript="service" scope="application">
<param name="class" value="com.Test"/>
</create>
</allow>
package com;
public class Test {
/**
* @param args
*/
public String helloWorld(String hello){
System.out.println("调用了"+hello);
return "hello world"+hello;
}
public static void main(String[] args) {
}
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type='text/javascript' src='../dwr/engine.js'> </script>
<script type='text/javascript' src='../dwr/util.js'> </script>
<script type='text/javascript' src='../dwr/interface/service.js'>
service.js自动生成。通过http://localhost:8080/webapp/dwr/可以看到这个js
</script>
<script type="text/javascript">
function hello(){
service.helloWorld("helloWOrld",resultHandle);//调用远程注册回调函数
}
function resultHandle(data){
alert(data);
}
</script>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<button οnclick="hello()"> </button>
This is my JSP page. <br>
</body>
</html>