反向Ajax技术是服务器向客户端主动的发关数据,很大的减少了服务的负担。
@H_301_3@
学习新的知识点就要多写些自己的Demo,在这里写一个最简单的例子,以作笔记:
@H_301_3@
@H_301_3@sendMsg.jsp负责向表中添加数据(更新数据库),showMsg.jsp用来实时显示前者添加的信息。
@H_301_3@dwr.xml
@H_301_3@<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr//dwr30.dtd"> <dwr>
@H_301_3@ <allow>
@H_301_3@ <create creator="new" javascript="SendMsg" scope="session">
@H_301_3@ <param name="class" value="com.dwr.bean.SendMsg" />
@H_301_3@ </create>
@H_301_3@ </allow>
@H_301_3@</dwr>
@H_301_3@SendMsg.java
@H_301_3@package com.dwr.bean;
@H_301_3@
@H_301_3@import java.util.Collection;
@H_301_3@import java.util.LinkedList;
@H_301_3@import org.directwebremoting.WebContext;
@H_301_3@import org.directwebremoting.WebContextFactory;
@H_301_3@import org.directwebremoting.proxy.dwr.Util;
@H_301_3@/**
@H_301_3@* DWR反向Ajax示例
@H_301_3@* @author ︶ㄣ旺
@H_301_3@* @version 1.0
@H_301_3@* */
@H_301_3@public class SendMsg
@H_301_3@{
@H_301_3@ public static WebContext wctx = null;
@H_301_3@ //用一个List代表数据库 来储存消息
@H_301_3@ private LinkedList list = new LinkedList();
@H_301_3@ //调用添加和显示方法
@H_301_3@ public void sendMsg(String msg)
@H_301_3@ {
@H_301_3@ list.addFirst(msg);
@H_301_3@ //最多保留10条聊天记录
@H_301_3@ if(list.size()>10){
@H_301_3@ list.removeLast();
@H_301_3@ }
@H_301_3@ wctx = WebContextFactory.get();
@H_301_3@ Util utilThis = new Util(wctx.getScriptSession());
@H_301_3@ //使用utilThis重置 Id 属性为 msg 的文本框的内容
@H_301_3@ utilThis.setValue("msg","请输入信息");
@H_301_3@ String currentPage = "/login/showMsg.jsp"; //要推信息的页面地址
@H_301_3@ //获得所有已经打开此页面的会话
@H_301_3@ Collection sessions = wctx.getScriptSessionsByPage(currentPage);
@H_301_3@ Util utilAll = new Util(sessions);
@H_301_3@ //将消息从LinkedList中取出来,放放到一个字符串数组中
@H_301_3@ String[] msgs = new String[list.size()];
@H_301_3@ msgs = (String[]) list.toArray(msgs);
@H_301_3@ //先清空页面的消息内容,去除ul元素下所有元素
@H_301_3@ utilAll.removeAllOptions("ul");
@H_301_3@ //向页面添加消息内容
@H_301_3@ utilAll.addOptions("ul",msgs);
@H_301_3@}
@H_301_3@sendMsg.jsp
@H_301_3@<%@ page language="java" pageEncoding="UTF-8"%>
@H_301_3@<%@ page isELIgnored="false" %>
@H_301_3@<html>
@H_301_3@ <head>
@H_301_3@ <title>DWR反向Ajax示例</title>
@H_301_3@ <Meta http-equiv="pragma" content="no-cache">
@H_301_3@ <Meta http-equiv="cache-control" content="no-cache">
@H_301_3@ <script type='text/javascript' src='${pageContext.request.contextPath}/dwr/interface/SendMsg.js'></script>
@H_301_3@ <script type='text/javascript' src='${pageContext.request.contextPath}/dwr/engine.js'></script>
@H_301_3@ <script type='text/javascript' src='${pageContext.request.contextPath}/dwr/util.js'></script>
@H_301_3@ <script type="text/javascript">
@H_301_3@ function sendMessage()
@H_301_3@ var msg = $("msg").value;
@H_301_3@ SendMsg.sendMsg(msg);
@H_301_3@ </script>
@H_301_3@ </head>
@H_301_3@ <body>
@H_301_3@ DWR反向Ajax示例信息添加<br>
@H_301_3@ 输入信息:<input type="text" id="msg" name="msg" onkeypress="dwr.util.onReturn(event,sendMessage)">
@H_301_3@ </body>
@H_301_3@</html>
@H_301_3@showMsg.jsp
@H_301_3@ <body onload="dwr.engine.setActiveReverseAjax(true);">
@H_301_3@ DWR反向Ajax示例信息显示<br>
@H_301_3@ ul:
@H_301_3@ <ul id="ul">
@H_301_3@ </ul>
@H_301_3@运行效果如图:
@H_301_3@这时showMsg.jsp一直处于失去焦点状态,却能实时地显示服务器端更新的数据
原文链接:https://www.f2er.com/ajax/164386.html