反向Ajax技术实例(二)

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

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

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

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

3.创建一个SendingMessage.java类

copy

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

4.创建dwr.xml文件

copy

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

猜你在找的Ajax相关文章