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