DWR是一個可以允許你去創建AJAX WEB 站點的JAVA 開源庫。
它可以讓你在瀏覽器中的Javascript代碼調用Web服務器上的Java 代碼,就像在Java代碼就在瀏覽器中一樣。
DWR包含 2個主要部分:
一個運行在服務器端的Java Servlet,它處理請求并且向瀏覽器發回響應。
一個運行在瀏覽器端的JavaScript,它發送請求而且還能動態更新網頁。
DWR工作原理是通過動態把Java類生成為Javascript。它的代碼就像Ajax魔法一樣,你感覺調用就像發
生在瀏覽器端,但是實際上代碼調用發生在服務器端,DWR負責數據的傳遞和轉換。這種從Java 到
JavaScript的遠程調用功能的方式使DWR用起來有種非常像RMI或者SOAP的常規RPC機制,而且DWR
的優點在于不需要任何的網頁瀏覽器插件就能運行在網頁上。
Java從根本上講是同步機制,然 AJAX卻是異步的。所以你調用遠程方法時,當數據已經從網絡上返回
的時候,你要提供有反調 (callback) 功能的DWR。
第 1個 DWR 例子:Hello World
1) 從官方網站下載dwr.jar包。然后將它放在你 webapp 的 WEB-INF/lib目錄下。
2) 修改web.xml,如下
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="dwr">
<display-name>DWR (Direct Web Remoting)</display-name>
<description>A Simple Demo DWR</description>
<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>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
3) 在WEB-INF下新建dwr.xml,內容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="service">
<param name="class"
value="helloworld.Service"/>
</create>
</allow>
</dwr>
4) 啟動web服務器,訪問http://localhost/工程/dwr ,頁面結果顯示為
Classes known to DWR:
* service (helloworld.Service)
5) 點擊進入 *service,看到提示....
Methods For: service (helloworld.Service)
To use this class in your javascript you will need the following script includes:
<script type='text/javascript' src='/TestDWR/dwr/interface/service.js'></script>
<script type='text/javascript' src='/TestDWR/dwr/engine.js'></script>
In addition there is an optional utility script:
<script type='text/javascript' src='/TestDWR/dwr/util.js'></script>
Replies from DWR are shown with a yellow background if they are simple or in an alert box otherwise.
The inputs are evaluated as Javascript so strings must be quoted before execution.
There are 10 declared methods:
* sayHello( );
* hashCode( );
(Warning: hashCode() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* getClass( );
(Warning: No Converter for java.lang.Class. See below)
(Warning: getClass() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* wait( );
(Warning: overloaded methods are not recommended. See below)
(Warning: wait() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* wait( , );
(Warning: overloaded methods are not recommended. See below)
(Warning: wait() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* wait( );
(Warning: overloaded methods are not recommended. See below)
(Warning: wait() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* equals( );
(Warning: No Converter for java.lang.Object. See below)
(Warning: equals() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* notify( );
(Warning: notify() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* notifyAll( );
(Warning: notifyAll() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* toString( );
(Warning: toString() is excluded: Methods defined in java.lang.Object are not accessible. See below)
6)創建jsp,內容如下
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<script type='text/javascript' src='dwr/interface/service.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
</script>
<script type="text/javascript">
function firstDwr(){
service.sayHello(" Test ",callBackHello);
}
function callBackHello(data){
alert(data);
}
</script>
</head>
<body>
<input type="button" name="button" value="測試" onclick="firstDwr()">
</body>
</html>
當點擊"測試"時,就出現結果了.
總結:
1.在web.xml中加入dwr servlet
2.在drw.xml中指明你要調用的類,并指明生成的javascript名
<create creator="new" javascript="service">
<param name="class" value="helloworld.Service"/>
</create>
3.完成對應的類
4.在html/jsp中寫javascript函數,調用java服務器端的方法,并寫處理結果的回調函數