<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"> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%></p><p style="text-indent:2em"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type='text/javascript' src='/JavaChatDwr/dwr/interface/JavascriptChat.js'></script>
<script type='text/javascript' src='/JavaChatDwr/dwr/engine.js'></script>
<script type='text/javascript' src='/JavaChatDwr/dwr/util.js'></script></p><p style="text-indent:2em"><script type="text/javascript">
function sendMessage(){
//获得用户输入的文本
var test = dwr.util.getValue("text");
//发送信息
JavascriptChat.sendMessage(test);
}
</script>
</head>
<body>
<input type="text" value="" name="" id="text"/>
<input type="button" value="发送" onclick="sendMessage()"/>
</body>
</html></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<ScriptSession> scriptSessions = wctx.getScriptSessionsByPage(currentPage);
}
public void sendMessage(String message){
System.out.println(message);
//获得当前打开的页面
String currentPage = wctx.getCurrentPage();
//清空对象
//获得打开当前页面的所有的session对象
Collection<ScriptSession> 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"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%></p><p style="text-indent:2em"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"></p><p style="text-indent:2em"><title>My JSP 'show.jsp' starting page</title></p><p style="text-indent:2em"><meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type='text/javascript'
src='/JavaChatDwr/dwr/interface/JavascriptChat.js'>
</script>
<script type='text/javascript' src='/JavaChatDwr/dwr/engine.js'>
</script></p><p style="text-indent:2em">
<script type='text/javascript' src='/JavaChatDwr/dwr/util.js'>
</script></p><p style="text-indent:2em">
<script type="text/javascript">
function init() {
//启用Ajax反转
dwr.engine.setActiveReverseAjax(true);
JavascriptChat.inits();
}</p><p style="text-indent:2em">window.onload=init;
</script>
</head></p><p style="text-indent:2em"><body>
<div id="div1">
</div>
</body>
</html>
</p></pre><pre></pre></pre><pre>WEB.XML配置</pre><pre></pre><pre></pre><pre></pre><pre><pre class="prettyprint"><p style="text-indent:2em"><?xml version="1.0" encoding="UTF-8"?>
<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>">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- web中配置Dwr -->
<display-name>DWR (Direct Web Remoting)</display-name>
<description>A Simple Demo DWR</description></p><p style="text-indent:2em">
<servlet>
<!-- 指定DWR核心ServletName -->
<servlet-name>dwr-invoker</servlet-name>
<!-- 指定DWR实现类 -->
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class></p><p style="text-indent:2em"> <!-- 指定DWR核心Servlet处于调试状态 -->
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param></p><p style="text-indent:2em"> <!-- 启用Ajax反转技术 如果删除则不启用 -->
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param></p><p style="text-indent:2em"> <!-- By default DWR creates application scope objects when they are first
used. This creates them when the app-server is started -->
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param></p><p style="text-indent:2em"> <!-- This enables full streaming mode. It's probably better to leave this
out if you are running across the internet -->
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>-1</param-value>
</init-param></p><p style="text-indent:2em"> <!--
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>
--></p><p style="text-indent:2em"> <load-on-startup>1</load-on-startup>
</servlet></p><p style="text-indent:2em"> <servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
</p></pre><pre></pre><pre>dwr.xml配置</pre><pre></pre><pre><pre class="prettyprint"><p style="text-indent:2em"><?xml version="1.0" encoding="UTF-8"?>
<!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>"></p><p style="text-indent:2em"><dwr></p><p style="text-indent:2em"> <allow></p><p style="text-indent:2em"> <!--
<filter class="org.getahead.dwrdemo.monitor.MonitoringAjaxFilter"/>
<filter class="org.directwebremoting.filter.ExtraLatencyAjaxFilter">
<param name="delay" value="200"/>
</filter>
--></p><p style="text-indent:2em">
<!-- chat -->
<create creator="new" javascript="JavascriptChat" scope="application">
<param name="class" value="com.dragon.dwr.JavaChat"/>
</create>
<!-- 设置类型转换 -->
<convert converter="bean" match="com.dragon.entity.User"/></p><p style="text-indent:2em">
<!-- this is a bad idea for live, but can be useful in testing -->
<convert converter="exception" match="java.lang.Exception"/>
<convert converter="bean" match="java.lang.StackTraceElement"/>
</allow></p><p style="text-indent:2em"></dwr>
</p></pre></pre></pre></pre>
<br></wbr>
分享到:
相关推荐
包含了dwr推送技术的详细介绍 和 一个基于dwr推送技术的聊天实例
Dwr推送技术实现BS即时通讯,项目源代码,功能实现
我不能把公司的代码贴出来,所以我自己写了一个DEMO,已经实现了对指定用户发送消息的功能,并且即时显示在指定的用户界面上面.当然你可以修改这些,让它弹出来显示,或者像QQ一样在你页面的某个角落让消息的图标闪动:)
使用dwr框架的服务器推技术也称为反向ajax或comet,将项目发布到ajax中输入http://localhost:8080/dwr即可聊天 我实现的时群聊
NULL 博文链接:https://yangxh101.iteye.com/blog/684776
dwr实现前台监控 后台推送即时信息 入门必看资料 里边含源码;
dwr反向ajax使用,即时提醒。WEB网页实现右下角时时弹出提醒DIV
没有用到数据库,就简单用session保存用户,也方便大家测试,经过...采用dwr服务器端推技术,减少了刷新,缓解服务器端压力。 请大家讲点网德,好东西就顶一个 谢谢,还有什么问题联系我。 qq:529757141,说明来意。
实现简单的功能以供学习,欢迎大家批评.主要涉及DWR框架技术。
dwr实现的在线即时聊天demo,比较简化的一个反向ajax例子, <br>everse Ajax主要是在BS架构中,从服务器端向多个浏览器主动推数据的一种技术。它的一种实现就是客户端向服务器请求后,服务器不立即回应,从而导致一...