web.xml
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>com.wxl.app.service.MyDwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>logLevel</param-name>
<param-value>DEBUG</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
dwr.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="service">
<param name="class" value="com.wxl.app.service.HelloWorldService" />
</create>
</allow>
</dwr>
java
package com.wxl.app.service;
public class HelloWorldService {
public String sayHello(String yourName) {
// 可以是访问数据库的复杂代码
return "Hello World " + yourName;
}
}
jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type='text/javascript' src='/dwr/dwr/engine.js'></script>
<script type='text/javascript' src='/dwr/dwr/interface/service.js'></script>
<script type='text/javascript' src='/dwr/dwr/util.js'></script>
<script type="text/javascript">
function firstDwr() {
service.sayHello("王星乐", callBackHello);
}
function callBackHello(data) {
alert(data);
}
</script>
</head>
<body>
This is my JSP page.
<br>
<input type="button" name="button" value="测试" οnclick=
firstDwr();
>
</body>
</html>