dwr.jar
1.test.html
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<link rel="stylesheet" type="text/css" href="generic.css" />
<title>DWR入门--DWR后台调用页面组件 蓝杰信息@NetJava.cn</title>
</head>
<!--引入dwr的js脚本-->
<script src="dwr/interface/ChatWithSamePage.js"></script>
<script src="dwr/engine.js" /></script>
<script src="dwr/util.js" /></script>
<script type="text/javascript">
function sendMessage() {
ChatWithSamePage.sendWebMessage(dwr.util.getValue("text"));
}
</script>
<body>
<pre>
发送内容:
<input type="text" id="text" οnkeypress="dwr.util.onReturn(event, sendMessage)"/>
<input type="button" value="Send" οnclick="sendMessage();"/>
<hr>
<ul id="chatlog" style="list-style-type:none;">
</ul>
</pre>
</body>
</html>
2.ChartwithSamPage.java
package cn.netjava.dwr;
import java.util.*;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
import org.directwebremoting.ScriptSession;
/**
* 通知消息调用java对象
*/
public class ChatWithSamePage {
/**
* @param text The new message text to add
*/
public void sendWebMessage(String text)
{
WebContext wctx = WebContextFactory.get();
String remoteAdd= wctx.getHttpServletRequest().getRemoteAddr();
text="来自"+remoteAdd+"的朋友说:"+text;
// Make sure we have a list of the list 10 messages
if (text != null && text.trim().length() > 0)
{
messages.addFirst(new WebMessage(text));
while (messages.size() > 10)
{
messages.removeLast();
}
}
//将队列中的消息通知到所有客户端
notifyAllClient(wctx,messages);
}
public void notifyAllClient(WebContext wctx,List msgQueue){
String currentPage = wctx.getCurrentPage();
// Clear the input box in the browser that kicked off this page only
Util utilThis = new Util(wctx.getScriptSession());
utilThis.setValue("text", "");
// For all the browsers on the current page:
Collection<ScriptSession> sessions = wctx.getScriptSessionsByPage(currentPage);
Util utilAll = new Util(sessions);
// Clear the list and add in the new set of messages
utilAll.removeAllOptions("chatlog");
utilAll.addOptions("chatlog", messages, "text");
}
/**
* The current set of messages
*/
private LinkedList messages = new LinkedList();
}
_________________________________________________
WebMessage.java
package cn.netjava.dwr;
public class WebMessage {
/**
* @param newtext the new message text
*/
public WebMessage(String newtext)
{
text = newtext;
if (text.length() > 256)
{
text = text.substring(0, 256)+". . .";
}
}
/**
* @return the message id
*/
public long getId()
{
return id;
}
/**
* @return the message itself
*/
public String getText()
{
return text;
}
/**
* When the message was created
*/
private long id = System.currentTimeMillis();
/**
* The text of the message
*/
private String text;
}
3.web.xml和上节的一样不变。
WEB-INF/dwr.xml
<dwr>
<!-- 聊天消息调用接口 -->
<create creator="new" javascript="ChatWithSamePage" scope="application">
<param name="class" value="cn.netjava.dwr.ChatWithSamePage"/>
</create>
<convert match="cn.netjava.dwr.WebMessage" converter="bean"/>
</allow>
</dwr>
1.test.html
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<link rel="stylesheet" type="text/css" href="generic.css" />
<title>DWR入门--DWR后台调用页面组件 蓝杰信息@NetJava.cn</title>
</head>
<!--引入dwr的js脚本-->
<script src="dwr/interface/ChatWithSamePage.js"></script>
<script src="dwr/engine.js" /></script>
<script src="dwr/util.js" /></script>
<script type="text/javascript">
function sendMessage() {
ChatWithSamePage.sendWebMessage(dwr.util.getValue("text"));
}
</script>
<body>
<pre>
发送内容:
<input type="text" id="text" οnkeypress="dwr.util.onReturn(event, sendMessage)"/>
<input type="button" value="Send" οnclick="sendMessage();"/>
<hr>
<ul id="chatlog" style="list-style-type:none;">
</ul>
</pre>
</body>
</html>
2.ChartwithSamPage.java
package cn.netjava.dwr;
import java.util.*;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
import org.directwebremoting.ScriptSession;
/**
* 通知消息调用java对象
*/
public class ChatWithSamePage {
/**
* @param text The new message text to add
*/
public void sendWebMessage(String text)
{
WebContext wctx = WebContextFactory.get();
String remoteAdd= wctx.getHttpServletRequest().getRemoteAddr();
text="来自"+remoteAdd+"的朋友说:"+text;
// Make sure we have a list of the list 10 messages
if (text != null && text.trim().length() > 0)
{
messages.addFirst(new WebMessage(text));
while (messages.size() > 10)
{
messages.removeLast();
}
}
//将队列中的消息通知到所有客户端
notifyAllClient(wctx,messages);
}
public void notifyAllClient(WebContext wctx,List msgQueue){
String currentPage = wctx.getCurrentPage();
// Clear the input box in the browser that kicked off this page only
Util utilThis = new Util(wctx.getScriptSession());
utilThis.setValue("text", "");
// For all the browsers on the current page:
Collection<ScriptSession> sessions = wctx.getScriptSessionsByPage(currentPage);
Util utilAll = new Util(sessions);
// Clear the list and add in the new set of messages
utilAll.removeAllOptions("chatlog");
utilAll.addOptions("chatlog", messages, "text");
}
/**
* The current set of messages
*/
private LinkedList messages = new LinkedList();
}
_________________________________________________
WebMessage.java
package cn.netjava.dwr;
public class WebMessage {
/**
* @param newtext the new message text
*/
public WebMessage(String newtext)
{
text = newtext;
if (text.length() > 256)
{
text = text.substring(0, 256)+". . .";
}
}
/**
* @return the message id
*/
public long getId()
{
return id;
}
/**
* @return the message itself
*/
public String getText()
{
return text;
}
/**
* When the message was created
*/
private long id = System.currentTimeMillis();
/**
* The text of the message
*/
private String text;
}
3.web.xml和上节的一样不变。
WEB-INF/dwr.xml
<dwr>
<!-- 聊天消息调用接口 -->
<create creator="new" javascript="ChatWithSamePage" scope="application">
<param name="class" value="cn.netjava.dwr.ChatWithSamePage"/>
</create>
<convert match="cn.netjava.dwr.WebMessage" converter="bean"/>
</allow>
</dwr>