DWR的push技术是让服务器每次发送广播时,把这个广播推送给每个客户端,不用客户端去刷新,据说DWR的推送是基于长连接的,性能不错。可以在后台通过 ScriptSession 来执行一些 javascript 的 function 或者是 style 等操作,可以在服务器上将一些数据推送到客户端机器上。
修改web.xml,在dwr的servlet中启用dwr的Reverse Ajax,即加入:
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
Web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4"
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</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>Sender</servlet-name>
<servlet-class>com.dwr.servlet.Sender</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Sender</servlet-name>
<url-pattern>/notify.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
创建一个请求页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>index页面</title>
</head>
<body>
<form action="notify.do">
<input type="text" name="message" />
<input type="submit" value="提交">
</form>
</body>
</html>
创建一个发送广播的Servlet
packagecom.dwr.servlet;
importjava.io.IOException;
importjava.util.Collection;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.directwebremoting.ScriptBuffer;
importorg.directwebremoting.ScriptSession;
importorg.directwebremoting.ServerContext;
importorg.directwebremoting.ServerContextFactory;
public class Sender extends HttpServlet {
private static final long serialVersionUID = 221673813297384002L;
protected voiddoGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ServerContext serverContext = ServerContextFactory
.get(getServletContext());
Collection<ScriptSession> scriptSessions = serverContext
.getAllScriptSessions();
// Collection<ScriptSession> sessions =
// wctx.getScriptSessionsByPage("/poject/test.html");
String message = request.getParameter("message");
if (message == null) {
message = "empty";
}
ScriptBuffer scriptBuffer = new ScriptBuffer();
scriptBuffer.appendScript("showMessage(").appendData(message)
.appendScript(");");
for (ScriptSession scriptSession : scriptSessions) {
scriptSession.addScript(scriptBuffer);
}
}
}
添加一个html页面,用来接收服务端广播:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Client1的页面</title>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type="text/javascript">
dwr.engine.setActiveReverseAjax(true);
functionshowMessage(msg) {
alert(msg);
}
</script>
</head>
<body>
</body>
</html>
打开添加的html页面,然后让Sender发送一个广播, html页面就会自动弹出一个javascript对话框,到此,dwr的推送就完成了。