反向Ajax技术实例

1.创建一个Web Project项目,导入dwr.jar包。

2.修改web.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- dwr核心servlet --> <listener> <listener-class>org.directwebremoting.servlet.DwrListener</listener-class> </listener> <servlet> <!-- dwr核心servelt实现类 --> <servlet-name>dwr</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <!-- 设置dwr可调式 --> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <!-- 设置ajax的反向技术 --> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>initApplicationScopeCreatorsAtStartip</param-name> <param-value>true</param-value> </init-param> <!-- 长连接时保持时间 --> <init-param> <param-name>maxWaitAfterWrite</param-name> <param-value>60</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

3.创建一个SendingMessage.java类

package com.yjpeng.dwr; import java.util.LinkedList; import java.util.List; import javax.naming.LinkLoopException; import org.directwebremoting.Browser; import org.directwebremoting.ScriptSession; import org.directwebremoting.ScriptSessions; public class SendingMessage { private List<String> messages = new LinkedList<String>(); public void addMessage(String message){ messages.add(message); System.out.println("有客户请求消息:" + message); Browser.withCurrentPage(new Runnable(){ public void run() { ScriptSessions.addFunctionCall("receiveMessage", messages); } }); } }

4.创建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> <create creator="new" javascript="sendingMessage" scope="application"> <param name="class" value="com.yjpeng.dwr.SendingMessage"></param> </create> </allow> </dwr>

6.创建一个getMessage.jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="dwr/interface/sendingMessage.js"></script>
<script src="dwr/engine.js"></script>
<script src="dwr/util.js"></script>
<script type="text/javascript">
function sendMessage(){
 var message = $("message").value;
 sendingMessage.addMessage(message);
}

function receiveMessage(messages){
 var chatlog = "";
 for(var data in messages){
   chatlog += "<div>" + messages[data] + "</div>";
 }
 dwr.util.setValue("list", chatlog, {escapeHtml:false});
}

</script>
</head>
<body onload="dwr.engine.setActiveReverseAjax(true);">
 <form method="get" action="getMessage.jsp" >
  input message:<input id="message" type="text"/>
  <input type="button" value="sendMessage" onclick="sendMessage()"/>
  <div id="list"></div>
 </form>
</body>
</html>

6.把web项目部署到服务器上,启动服务器。分别打开多个浏览器。最后可以看到,在a浏览器发消息,b浏览器会自动出现a浏览器发出的消息。也就是消息的实时传递。

你可能感兴趣的:(JavaScript,Ajax,浏览器,function,DWR,encoding)