在开发前请先配好你的DWR包和配置文件
dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting
2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="JavaChat">
<param name="class" value="cn.com.thinkbank.service.JavaChat" />
</create>
<convert converter="bean" match="cn.com.thinkbank.bean.ChatMsg">
</convert>
</allow>
</dwr>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
ChatMsg.java
package cn.com.thinkbank.bean;
public class ChatMsg {
private String text;
/**
* @return 返回 text。
*/
public String getText() {
return text;
}
/**
* @param text
* 要设置的 text。
*/
public void setText(String text) {
this.text = text;
}
public ChatMsg() {
// TODO 自动生成构造函数存根
}
public ChatMsg(String text) {
// TODO 自动生成构造函数存根
this.text = text;
}
}
Chat.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>My JSP 'chat.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">
<script. type="text/javascript" src="/Chat/dwr/util.js"></script>
<script. type="text/javascript" src="/Chat/dwr/engine.js"></script>
<script. type="text/javascript" src="/Chat/dwr/interface/JavaChat.js"></script>
<script. type="text/javascript">
<!--
function sendMessage(){
JavaChat.addMessage(dwr.util.getValue("text"));
}
//-->
</script>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body nload="DWREngine.setActiveReverseAjax(true)">
<h1>
反向Ajax的聊天室
</h1>
<DIV style="width:460px;height: 200px;border:1px solid;#999999">
<UL id="charLog"></UL>
</DIV>
<P>
输入您的聊天信息
<INPUT type="text" id="text" nkeypress="dwr.util.onReturn(event,sendMessage)" />
<INPUT type="button" value="发送" nclick="sendMessage()" />
</p>
</body>
</html>
JavaChat.java
package cn.com.thinkbank.service;
import java.util.Collection;
import java.util.LinkedList;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
import cn.com.thinkbank.bean.ChatMsg;
public class JavaChat {
private LinkedList<ChatMsg> messages = new LinkedList<ChatMsg>();
public void addMessage(String text){
if(text !=null && text.trim().length()>0){
messages.addFirst(new ChatMsg(text));
System.out.println(messages.size());
while(messages.size()>10){
messages.removeLast();
}
}
// 获得WebContext实例
WebContext wctx = WebContextFactory.get();
// 获得当前页面的Util实例,用以***作当前页面的HTML元素
Util utilThis = new Util(wctx.getScriptSession());
// 清除输入文本框的内容
utilThis.setValue("text","");
String currentPage = wctx.getCurrentPage();
Collection sessions = wctx.getScriptSessionsByPage(currentPage);
Util utilAll = new Util(sessions);
// 将所有页面中的charlog的所有元素删除
utilAll.removeAllOptions("charlog");
// 将messages中的元素的text属性添加成选项
utilAll.addOptions("Charlog",messages,"text");
}
}
dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting
2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="JavaChat">
<param name="class" value="cn.com.thinkbank.service.JavaChat" />
</create>
<convert converter="bean" match="cn.com.thinkbank.bean.ChatMsg">
</convert>
</allow>
</dwr>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
</web-app>
ChatMsg.java
package cn.com.thinkbank.bean;
public class ChatMsg {
private String text;
/**
* @return 返回 text。
*/
public String getText() {
return text;
}
/**
* @param text
* 要设置的 text。
*/
public void setText(String text) {
this.text = text;
}
public ChatMsg() {
// TODO 自动生成构造函数存根
}
public ChatMsg(String text) {
// TODO 自动生成构造函数存根
this.text = text;
}
}
Chat.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>My JSP 'chat.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">
<script. type="text/javascript" src="/Chat/dwr/util.js"></script>
<script. type="text/javascript" src="/Chat/dwr/engine.js"></script>
<script. type="text/javascript" src="/Chat/dwr/interface/JavaChat.js"></script>
<script. type="text/javascript">
<!--
function sendMessage(){
JavaChat.addMessage(dwr.util.getValue("text"));
}
//-->
</script>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body nload="DWREngine.setActiveReverseAjax(true)">
<h1>
反向Ajax的聊天室
</h1>
<DIV style="width:460px;height: 200px;border:1px solid;#999999">
<UL id="charLog"></UL>
</DIV>
<P>
输入您的聊天信息
<INPUT type="text" id="text" nkeypress="dwr.util.onReturn(event,sendMessage)" />
<INPUT type="button" value="发送" nclick="sendMessage()" />
</p>
</body>
</html>
JavaChat.java
package cn.com.thinkbank.service;
import java.util.Collection;
import java.util.LinkedList;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
import cn.com.thinkbank.bean.ChatMsg;
public class JavaChat {
private LinkedList<ChatMsg> messages = new LinkedList<ChatMsg>();
public void addMessage(String text){
if(text !=null && text.trim().length()>0){
messages.addFirst(new ChatMsg(text));
System.out.println(messages.size());
while(messages.size()>10){
messages.removeLast();
}
}
// 获得WebContext实例
WebContext wctx = WebContextFactory.get();
// 获得当前页面的Util实例,用以***作当前页面的HTML元素
Util utilThis = new Util(wctx.getScriptSession());
// 清除输入文本框的内容
utilThis.setValue("text","");
String currentPage = wctx.getCurrentPage();
Collection sessions = wctx.getScriptSessionsByPage(currentPage);
Util utilAll = new Util(sessions);
// 将所有页面中的charlog的所有元素删除
utilAll.removeAllOptions("charlog");
// 将messages中的元素的text属性添加成选项
utilAll.addOptions("Charlog",messages,"text");
}
}