`
koudailidexiaolong
  • 浏览: 90009 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

dwr推技术实现即时消息(一)

 
阅读更多
<wbr><p style="text-indent:2em">DWR是很好用的一个Ajax框架闲话不说 如果你找这个技术可定时了解Dwr的</p> <p style="text-indent:2em">Index页面</p> <p></p> <pre class="prettyprint"><p style="text-indent:2em"> &lt;%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%&gt; &lt;% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %&gt;</p><p style="text-indent:2em">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt; &lt;html&gt; &lt;head&gt; &lt;base href="&lt;%=basePath%&gt;"&gt; &lt;title&gt;My JSP 'index.jsp' starting page&lt;/title&gt; &lt;meta http-equiv="pragma" content="no-cache"&gt; &lt;meta http-equiv="cache-control" content="no-cache"&gt; &lt;meta http-equiv="expires" content="0"&gt; &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt; &lt;meta http-equiv="description" content="This is my page"&gt; &lt;!-- &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt; --&gt; &lt;script type='text/javascript' src='/JavaChatDwr/dwr/interface/JavascriptChat.js'&gt;&lt;/script&gt; &lt;script type='text/javascript' src='/JavaChatDwr/dwr/engine.js'&gt;&lt;/script&gt; &lt;script type='text/javascript' src='/JavaChatDwr/dwr/util.js'&gt;&lt;/script&gt;</p><p style="text-indent:2em">&lt;script type="text/javascript"&gt; function sendMessage(){ //获得用户输入的文本 var test = dwr.util.getValue("text"); //发送信息 JavascriptChat.sendMessage(test); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input type="text" value="" name="" id="text"/&gt; &lt;input type="button" value="发送" onclick="sendMessage()"/&gt; &lt;/body&gt; &lt;/html&gt;</p><p style="text-indent:2em"> </p></pre> <pre><pre>bean类 </pre><pre class="prettyprint"><p style="text-indent:2em">package com.dragon.dwr;</p><p style="text-indent:2em">import java.util.Collection;</p><p style="text-indent:2em">import org.directwebremoting.ScriptSession; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; import org.directwebremoting.proxy.dwr.Util;</p><p style="text-indent:2em">public class JavaChat { //获得WebContext 工厂实例 WebContext wctx = WebContextFactory.get(); /** * 保存用户打开的页面的session */ public void inits(){ wctx = WebContextFactory.get(); //获得当前打开的页面 String currentPage = wctx.getCurrentPage(); //清空对象 //获得打开当前页面的所有的session对象 Collection&lt;ScriptSession&gt; scriptSessions = wctx.getScriptSessionsByPage(currentPage); } public void sendMessage(String message){ System.out.println(message); //获得当前打开的页面 String currentPage = wctx.getCurrentPage(); //清空对象 //获得打开当前页面的所有的session对象 Collection&lt;ScriptSession&gt; scriptSessions = wctx.getScriptSessionsByPage(currentPage); //创建Dwr的util对象 Util utils = new Util(scriptSessions); //对id为Div1的标签赋值 utils.setValue("div1", message); } } </p></pre><pre></pre><pre>show页面</pre><pre><pre class="prettyprint"><p style="text-indent:2em"> </p><p style="text-indent:2em">&lt;%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%&gt; &lt;% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %&gt;</p><p style="text-indent:2em">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt; &lt;html&gt; &lt;head&gt; &lt;base href="&lt;%=basePath%&gt;"&gt;</p><p style="text-indent:2em">&lt;title&gt;My JSP 'show.jsp' starting page&lt;/title&gt;</p><p style="text-indent:2em">&lt;meta http-equiv="pragma" content="no-cache"&gt; &lt;meta http-equiv="cache-control" content="no-cache"&gt; &lt;meta http-equiv="expires" content="0"&gt; &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt; &lt;meta http-equiv="description" content="This is my page"&gt; &lt;!-- &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt; --&gt; &lt;script type='text/javascript' src='/JavaChatDwr/dwr/interface/JavascriptChat.js'&gt; &lt;/script&gt; &lt;script type='text/javascript' src='/JavaChatDwr/dwr/engine.js'&gt; &lt;/script&gt;</p><p style="text-indent:2em"> &lt;script type='text/javascript' src='/JavaChatDwr/dwr/util.js'&gt; &lt;/script&gt;</p><p style="text-indent:2em"> &lt;script type="text/javascript"&gt; function init() { //启用Ajax反转 dwr.engine.setActiveReverseAjax(true); JavascriptChat.inits(); }</p><p style="text-indent:2em">window.onload=init; &lt;/script&gt; &lt;/head&gt;</p><p style="text-indent:2em">&lt;body&gt; &lt;div id="div1"&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </p></pre><pre></pre></pre><pre>WEB.XML配置</pre><pre></pre><pre></pre><pre></pre><pre><pre class="prettyprint"><p style="text-indent:2em">&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app version="2.5" xmlns="<a target="_blank" href="http://java.sun.com/xml/ns/javaee" rel="nofollow">http://java.sun.com/xml/ns/javaee</a>" xmlns:xsi="<a target="_blank" href="http://www.w3.org/2001/XMLSchema-instance" rel="nofollow">http://www.w3.org/2001/XMLSchema-instance</a>" xsi:schemaLocation="<a target="_blank" href="http://java.sun.com/xml/ns/javaee" rel="nofollow">http://java.sun.com/xml/ns/javaee</a> <a target="_blank" href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" rel="nofollow">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>"&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;!-- web中配置Dwr --&gt; &lt;display-name&gt;DWR (Direct Web Remoting)&lt;/display-name&gt; &lt;description&gt;A Simple Demo DWR&lt;/description&gt;</p><p style="text-indent:2em"> &lt;servlet&gt; &lt;!-- 指定DWR核心ServletName --&gt; &lt;servlet-name&gt;dwr-invoker&lt;/servlet-name&gt; &lt;!-- 指定DWR实现类 --&gt; &lt;servlet-class&gt;org.directwebremoting.servlet.DwrServlet&lt;/servlet-class&gt;</p><p style="text-indent:2em"> &lt;!-- 指定DWR核心Servlet处于调试状态 --&gt; &lt;init-param&gt; &lt;param-name&gt;debug&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/init-param&gt;</p><p style="text-indent:2em"> &lt;!-- 启用Ajax反转技术 如果删除则不启用 --&gt; &lt;init-param&gt; &lt;param-name&gt;activeReverseAjaxEnabled&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/init-param&gt;</p><p style="text-indent:2em"> &lt;!-- By default DWR creates application scope objects when they are first used. This creates them when the app-server is started --&gt; &lt;init-param&gt; &lt;param-name&gt;initApplicationScopeCreatorsAtStartup&lt;/param-name&gt; &lt;param-value&gt;true&lt;/param-value&gt; &lt;/init-param&gt;</p><p style="text-indent:2em"> &lt;!-- This enables full streaming mode. It's probably better to leave this out if you are running across the internet --&gt; &lt;init-param&gt; &lt;param-name&gt;maxWaitAfterWrite&lt;/param-name&gt; &lt;param-value&gt;-1&lt;/param-value&gt; &lt;/init-param&gt;</p><p style="text-indent:2em"> &lt;!-- For more information on these parameters, see: - <a target="_blank" href="http://getahead.org/dwr/server/servlet" rel="nofollow">http://getahead.org/dwr/server/servlet</a> - <a target="_blank" href="http://getahead.org/dwr/reverse-ajax/configuration" rel="nofollow">http://getahead.org/dwr/reverse-ajax/configuration</a> --&gt;</p><p style="text-indent:2em"> &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt;</p><p style="text-indent:2em"> &lt;servlet-mapping&gt; &lt;servlet-name&gt;dwr-invoker&lt;/servlet-name&gt; &lt;url-pattern&gt;/dwr/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </p></pre><pre></pre><pre>dwr.xml配置</pre><pre></pre><pre><pre class="prettyprint"><p style="text-indent:2em">&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "<a target="_blank" href="http://getahead.org/dwr/dwr20.dtd" rel="nofollow">http://getahead.org/dwr/dwr20.dtd</a>"&gt;</p><p style="text-indent:2em">&lt;dwr&gt;</p><p style="text-indent:2em"> &lt;allow&gt;</p><p style="text-indent:2em"> &lt;!-- &lt;filter class="org.getahead.dwrdemo.monitor.MonitoringAjaxFilter"/&gt; &lt;filter class="org.directwebremoting.filter.ExtraLatencyAjaxFilter"&gt; &lt;param name="delay" value="200"/&gt; &lt;/filter&gt; --&gt;</p><p style="text-indent:2em"> &lt;!-- chat --&gt; &lt;create creator="new" javascript="JavascriptChat" scope="application"&gt; &lt;param name="class" value="com.dragon.dwr.JavaChat"/&gt; &lt;/create&gt; &lt;!-- 设置类型转换 --&gt; &lt;convert converter="bean" match="com.dragon.entity.User"/&gt;</p><p style="text-indent:2em"> &lt;!-- this is a bad idea for live, but can be useful in testing --&gt; &lt;convert converter="exception" match="java.lang.Exception"/&gt; &lt;convert converter="bean" match="java.lang.StackTraceElement"/&gt; &lt;/allow&gt;</p><p style="text-indent:2em">&lt;/dwr&gt; </p></pre></pre></pre></pre> <br></wbr>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics