web.xml文件的配置
1. <?xml version="1.0" encoding="GBK"?>
2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
6. <!-- 配置DWR的核心Servlet -->
7. <servlet>
8. <!-- 指定DWR核心Servlet的名字 -->
9. <servlet-name>dwr-invoker</servlet-name>
10. <!-- 指定DWR核心Servlet的实现类 -->
11. <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
12. <!-- 指定DWR核心Servlet处于调试状态 -->
13. <init-param>
14. <param-name>debug</param-name>
15. <param-value>true</param-value>
16. </init-param>
17. <!-- 设置启用反向Ajax技术 -->
18. <init-param>
19. <param-name>pollAndCometEnabled</param-name>
20. <param-value>true</param-value>
21. </init-param>
22. <load-on-startup>1</load-on-startup>
23. </servlet>
24. <!-- 指定核心Servlet的URL映射 -->
25. <servlet-mapping>
26. <servlet-name>dwr-invoker</servlet-name>
27. <!-- 指定核心Servlet映射的URL -->
28. <url-pattern>/dwr/*</url-pattern>
29. </servlet-mapping>
30. </web-app>
Java处理类
1. package com.lbx.dwr.chat;
2.
3. import java.util.Collection;
4. import java.util.LinkedList;
5.
6. import org.directwebremoting.WebContext;
7. import org.directwebremoting.WebContextFactory;
8. import org.directwebremoting.proxy.dwr.Util;
9.
10. public class JavaChat
11. {
12. //保存聊天信息的List对象
13. private LinkedList<ChatMsg> messages = new LinkedList<ChatMsg>();
14.
15. public void addMessage(String text)
16. {
17. if (text != null && text.trim().length() > 0)
18. {
19. messages.addFirst(new ChatMsg(text));
20. //最多保留10条聊天记录
21. while (messages.size() > 10)
22. {
23. messages.removeLast();
24. }
25. }
26. WebContext wctx = WebContextFactory.get();
27. //以当前WebContext关联的ScriptSession创建Util
28. Util utilThis = new Util(wctx.getScriptSession());
29. //使用utilThis清除text文本框的内容
30. utilThis.setValue("text", "");
31. //获取当前页面的url
32. String currentPage = wctx.getCurrentPage();
33. //获取正在浏览当前页的所有浏览器会话
34. Collection sessions = wctx.getScriptSessionsByPage(currentPage);
35. //以sessions创建Util对象
36. Util utilAll = new Util(sessions);
37. //删除chatlog列表里的所有列表项
38. utilAll.removeAllOptions("chatlog");
39. //使用messages集合里集合元素的text属性为chatlog添加列表项
40. utilAll.addOptions("chatlog" , messages , "text");
41. }
42. }
1. package com.lbx.dwr.chat;
2.
3. public class ChatMsg
4. {
5. //ChatMsg包装的字符串
6. private String text;
7. public ChatMsg()
8. {
9. }
10.
11. public ChatMsg(String text)
12. {
13. this.text = text;
14. }
15. //text属性的setter和gette方法
16. public void setText(String text)
17. {
18. this.text = text;
19. }
20. public String getText()
21. {
22. return text;
23. }
24. }
dwr.xml文件的配置
1. <?xml version="1.0" encoding="UTF-8"?>
2. <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
3. "http://getahead.ltd.uk/dwr/dwr20.dtd">
4. <dwr>
5. <allow>
6. <create creator="new" javascript="chat" scope="application">
7. <param name="class" value="com.lbx.dwr.chat.JavaChat"/>
8. </create>
9. <convert converter="bean" match="com.lbx.dwr.chat.ChatMsg"/>
10. </allow>
11. </dwr>
客户端JSP代码
1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2. <%
3. String path = request.getContextPath();
4. String basePath = request.getScheme() + "://"
5. + request.getServerName() + ":" + request.getServerPort()
6. + path + "/";
7. %>
8.
9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
10. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
11. <html xmlns="http://www.w3.org/1999/xhtml">
12. <head>
13. <title>反向Ajax的聊天室</title>
14. <meta name="website" content="http://www.crazyit.org" />
15. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
16. <script type='text/javascript' src='dwr/engine.js'></script>
17. <script type='text/javascript' src='dwr/interface/chat.js'></script>
18. <script type='text/javascript' src='dwr/util.js'></script>
19. <script type="text/javascript">
20. function sendMessage()
21. {
22. //调用远程Java方法,无需回调函数
23. chat.addMessage(dwr.util.getValue("text"));
24. }
25. </script>
26. </head>
27. <!-- 本页面启用反向Ajax -->
28. <body onload="dwr.engine.setActiveReverseAjax(true);">
29.
30. <h3>
31. 反向Ajax的聊天室
32. </h3>
33. <div style="width: 460px; height: 200px; border: 1px solid #999999">
34. <ul id="chatlog"></ul>
35. </div>
36. <br />
37. 输入您的聊天信息:
38. <input id="text" onkeypress="dwr.util.onReturn(event, sendMessage);"/>
39. <input type="button" value="发送" onclick="sendMessage()" />
40. </body>
41. </html>
【联系】谭经理
【QQ】2017523306
【公司】深圳市无穷大软件技术有限公司
【网站】http://www.wqdsoft.com
【聊天产品】http://im.wqdsoft.com
【电话】0755-23733742