1、将dwr的包拷贝到lib
2、配置web.xml
<span style="font-size:18px;"><span style="font-size:18px;"><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>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<!-- dwr的comet控制 -->
<init-param>
<param-name>pollAndCometEnabled</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> </span></span>
3、配置dwr.xml该文件应该放置在WEB-INF中
<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd">
<dwr>
<allow>
<!--
<filter class="com.example.dwr.monitor.MonitoringAjaxFilter"/>
<filter class="org.directwebremoting.filter.ExtraLatencyAjaxFilter">
<param name="delay" value="200"/>
</filter>
-->
<!-- intro - for the test on index.html -->
<create creator="new">
<param name="class" value="com.coke.dwr.model.MyDwr"/>//对应的类
</create>
<convert converter="bean" match="com.coke.dwr.model.User"/>
<convert converter="bean" match="com.coke.dwr.model.Group"/>
<convert match="java.lang.Exception" converter="exception"/>
<convert converter="bean" match="java.lang.StackTraceElement"/>
</allow>
</dwr></span>
4、在页面中引入相应的javascript
<span style="font-size:18px;"><%@ 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 'MyJsp.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="<%=basePath%>/dwr/engine.js"></script>
<script type="text/javascript" src="<%=basePath%>/dwr/interface/MyDwr.js"></script>
<script type="text/javascript">
MyDwr.hello("你妹!!!",function(data){
alert(data);
});
</script>
</head>
<body>
This is my JSP page. <br>
</body>
</html>
</span>
5.以MyDwr.java为例
package com.coke.dwr.model;
import java.util.ArrayList;
import java.util.List;
public class MyDwr {
public String hello(String world){
System.out.println("MyDwr.hello()"+world);
return "hello "+world;
}
public void deleteObj(){
throw new MyException("在删除对象的时候出错!!");
}
public List<User> list() {
List<User> users = new ArrayList<User>();
users.add(new User(1,"张三",new Group(1,"财务处")));
users.add(new User(2,"李四",new Group(2,"科技处")));
users.add(new User(3,"王五",new Group(3,"宣传部")));
return users;
}
}