反向Ajax技术实例(二)

前端之家收集整理的这篇文章主要介绍了反向Ajax技术实例(二)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

[c-sharp] view plain @L_502_1@
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appversion="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <!--dwr核心servlet-->
  8. <listener>
  9. <listener-class>org.directwebremoting.servlet.DwrListener</listener-class>
  10. </listener>
  11. <servlet>
  12. <!--dwr核心servelt实现类-->
  13. <servlet-name>dwr</servlet-name>
  14. <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  15. <!--设置dwr可调式-->
  16. <init-param>
  17. <param-name>debug</param-name>
  18. <param-value>true</param-value>
  19. </init-param>
  20. <!--设置ajax的反向技术-->
  21. <init-param>
  22. <param-name>activeReverseAjaxEnabled</param-name>
  23. <param-value>true</param-value>
  24. </init-param>
  25. <param-name>initApplicationScopeCreatorsAtStartip</param-name>
  26. <!--长连接时保持时间-->
  27. <param-name>maxWaitAfterWrite</param-name>
  28. <param-value>60</param-value>
  29. <load-on-startup>1</load-on-startup>
  30. </servlet>
  31. <servlet-mapping>
  32. <url-pattern>/dwr/*</url-pattern>
  33. </servlet-mapping>
  34. <welcome-file-list>
  35. <welcome-file>index.jsp</welcome-file>
  36. </welcome-file-list>
  37. </web-app>

3.创建一个SendingMessage.java类

copy

    packagecom.yjpeng.dwr;
  1. importjava.util.LinkedList;
  2. importjava.util.List;
  3. importjavax.naming.LinkLoopException;
  4. importorg.directwebremoting.Browser;
  5. importorg.directwebremoting.ScriptSession;
  6. importorg.directwebremoting.ScriptSessions;
  7. publicclassSendingMessage{
  8. privateList<String>messages=newLinkedList<String>();
  9. voidaddMessage(Stringmessage){
  10. messages.add(message);
  11. System.out.println("有客户请求消息:"+message);
  12. Browser.withCurrentPage(newRunnable(){
  13. voidrun(){
  14. ScriptSessions.addFunctionCall("receiveMessage",messages);
  15. }
  16. });
  17. }

4.创建dwr.xml文件

copy

    <!DOCTYPEdwrPUBLIC
  1. "-//GetAheadLimited//DTDDirectWebRemoting3.0//EN"
  2. "http://getahead.org/dwr/dwr30.dtd">
  3. <dwr>
  4. <allow>
  5. <createcreator="new"javascript="sendingMessage"scope="application">
  6. <paramname="class"value="com.yjpeng.dwr.SendingMessage"></param>
  7. </create>
  8. </allow>
  9. </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浏览器发出的消息。也就是消息的实时传递。

原文链接:https://www.f2er.com/ajax/163338.html

猜你在找的Ajax相关文章