dwr推技术实现即时消息一

DWR是很好用的一个Ajax框架闲话不说 如果你找这个技术可定时了解Dwr的

Index页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getcontextpath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!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>

<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>

bean类

package com.dragon.dwr;

import java.util.Collection;

import org.directwebremoting.ScriptSession; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; import org.directwebremoting.proxy.dwr.Util;

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); } }

show页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getcontextpath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>">

<title>My JSP 'show.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,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>

<script type="text/javascript"> function init() { //启用Ajax反转 dwr.engine.setActiveReverseAjax(true); JavascriptChat.inits(); }

window.onload=init; </script> </head>

<body> <div id="div1"> </div> </body> </html>

WEB.XML配置

<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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>

<servlet> <!-- 指定DWR核心ServletName --> <servlet-name>dwr-invoker</servlet-name> <!-- 指定DWR实现类 --> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

<!-- 指定DWR核心Servlet处于调试状态 --> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param>

<!-- 启用Ajax反转技术 如果删除则不启用 --> <init-param> <param-name>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param>

<!-- 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>

<!-- 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>

<!-- For more information on these parameters,see: - http://getahead.org/dwr/server/servlet - http://getahead.org/dwr/reverse-ajax/configuration -->

<load-on-startup>1</load-on-startup> </servlet>

<servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> </web-app>

dwr.xml配置

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">

<dwr>

<allow>

<!-- <filter class="org.getahead.dwrdemo.monitor.MonitoringAjaxFilter"/> <filter class="org.directwebremoting.filter.ExtraLatencyAjaxFilter"> <param name="delay" value="200"/> </filter> -->

<!-- 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"/>

<!-- 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>

</dwr>

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...