/**************************************************
*Author:Java619
*Time:2006-06
*E-Mail:ceun@163.com
*QQ:395676370
**************************************************/
1.把dwr对应的包
主要是
dwr.jar;(把DWR的演示war中的包,都复制到你的应用中)
另外还要xalan.jar包(这个包在DWR的演示war中未包含)
拷贝到应用的WEB-INF/lib目录下
2.修改web.xml文件,大概如下:
代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<description>Direct Web Remoter Servlet</description>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
3.编写dwr.xml文件保存到与web.xml同一目录下
代码:
<?xml version="1.0" encoding="gb2312"?>
<dwr>
<allow>
<create creator="new" javascript="Test" scope="application">
<param name="class"
value="com.ceun.Demo" />
</create>
</allow>
</dwr>
4.编写com.ceun.Demo类
代码:
package com.ceun;
public class Demo {
public String hello(){
return "你好";
}
}
5.编写jsp
代码:
<%@ page language="java" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>DWR 第一例</title>
<script type='text/javascript' src='dwr/engine.js'> </script>
<script type='text/javascript' src='dwr/interface/Test.js'></script>
<script type='text/javascript' src='dwr/util.js'> </script>
<script type="text/javascript">
function callBack(data){
alert(data);
}
</script>
</head>
<body>
<input type="Button" name="button3" value="测试" οnclick="Test.hello(callBack)"/>
</body>
</html>