一个简单的dwr推送程序
1、将dwr相关的jar包导入到工程
commons-logging-1.1.1.jar
dwr.jar
2、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 以设置下格式固定,可复制使用 -->
<display-name>ajaxDWR</display-name>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<!-- <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class> -->
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<!-- 实现调试,在release请设置为false -->
<description></description>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>allowScriptTagRemoting</param-name>
<param-value>true</param-value>
</init-param>
<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>
</web-app>
3、dwr.xml配置
<?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>
<!-- Hello为类名,ajax.lusm.Hello为类的真实地址 -->
<create creator="new" javascript="service">
<param name="class" value="helloWorld.Service" />
</create>
</allow>
</dwr>
4、编写暴露的接口
public class Service {
public String sayHello(String youName){
return "HelloWorld" + youName;
}
5、jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>DWR CASE</title>
<!-- 这两个是必须的,来自官方,路径的写法为相对路径,且开始第一个字符不为"/" -->
<script type='text/javascript' src='dwr/util.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<!-- 这个文件不存在,当必须设定Hello.js的名称为后台类名,且路径一定是dwr/interface/类名.js -->
<script type='text/javascript' src='dwr/interface/service.js'></script>
<!-- 这个是我们用户自己的定义的js,真实存在,存放我们自己写的js的代码,dwr2.0该代码有时候只有放当前
目录下,程序才能正确运行 -->
<!-- <script type="text/javascript" src='hello.js'></script> -->
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
dwr.engine.setActiveReverseAjax(true);
$("#sendt").click(function(){
service.sayHello($("#input").val(), callback);
})
});
function callback(data) {
//alert(data);
//DWRUtil.setValue("test", data);
$("#test").html(data);
}
</script>
</head>
<body>
大名2:
<input id="input" type="text" />
<!-- <input type='button' value='运行' onclick='hello();' /> -->
<input type='button' value='运行' id="sendt" />
<p>
<font color="red"><div id="test"></div>
</font>
</body>
</html>
6、js文件
function hello() {
var input = DWRUtil.getValue("input");
service.sayHello(input, callback);
}
function callback(data) {
//alert(data);
DWRUtil.setValue("test", data);
}
7、项目下载
链接:http://pan.baidu.com/s/1cIgHxc 密码:cp2w