Pushlet(一種comet 架構的實現)是基於Servlet 機制,資料從server端的Java 物件直接推送(push)
到客戶端瀏覽器的(動態)HTML 頁面,而無需任何Java applet 或者外掛的幫助。
- 在MyEclipse中建立工程。附件包含了從官方下載的Pushlet包已經本次的原始碼包,直接匯入到開發工具即可。
- 在建立的工程中增加pushlet.jar,log4j.jar。拷貝pushlet.properties,sources.properties到WEB-INF下。
- 拷貝ajax-pushlet-client.js到WebRoot下。
- 配置web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE web-app
- PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd">
- <!-- $Id: web.xml,v 1.7 2005/02/21 17:21:49 justb Exp $ -->
- <web-app>
- <!-- Define the pushlet servlet -->
- <servlet>
- <servlet-name>pushlet</servlet-name>
- <servlet-class>nl.justobjects.pushlet.servlet.Pushlet</servlet-class>
- <load-on-startup>3</load-on-startup>
- </servlet>
- <!-- Define the Servlet Mappings. -->
- <!-- The pushlet -->
- <servlet-mapping>
- <servlet-name>pushlet</servlet-name>
- <url-pattern>/pushlet.srv</url-pattern>
- </servlet-mapping>
- </web-app>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <!-- $Id: web.xml,v 1.7 2005/02/21 17:21:49 justb Exp $ --> <web-app> <!-- Define the pushlet servlet --> <servlet> <servlet-name>pushlet</servlet-name> <servlet-class>nl.justobjects.pushlet.servlet.Pushlet</servlet-class> <load-on-startup>3</load-on-startup> </servlet> <!-- Define the Servlet Mappings. --> <!-- The pushlet --> <servlet-mapping> <servlet-name>pushlet</servlet-name> <url-pattern>/pushlet.srv</url-pattern> </servlet-mapping> </web-app>
- 後臺程式碼HelloWorldPlushlet.java,該類包含一個內部類
- package com;
- import nl.justobjects.pushlet.core.Event;
- import nl.justobjects.pushlet.core.EventPullSource;
- public class HelloWorldPlushlet {
- static public class HwPlushlet extends EventPullSource {
- // 休眠五秒
- @Override
- protected long getSleepTime() {
- return 5000;
- }
- @Override
- protected Event pullEvent() {
- Event event = Event.createDataEvent("/cuige/he");
- event.setField("mess", "hello,world!Plushlet!");
- return event;
- }
- }
- }
package com;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventPullSource;
public class HelloWorldPlushlet {
static public class HwPlushlet extends EventPullSource {
// 休眠五秒
@Override
protected long getSleepTime() {
return 5000;
}
@Override
protected Event pullEvent() {
Event event = Event.createDataEvent("/cuige/he");
event.setField("mess", "hello,world!Plushlet!");
return event;
}
}
}
- sources.properties清理其他,定義自己釋出的內容
- source1=com.HelloWorldPlushlet$HwPlushlet
source1=com.HelloWorldPlushlet$HwPlushlet
- 在JSP中呼叫
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta http-equiv="Pragma" content="no-cache" />
- <script type="text/javascript" src="ajax-pushlet-client.js"></script>
- <script type="text/javascript">
- PL._init();
- PL.joinListen('/cuige/he');
- function onData(event) {
- alert(event.get("mess"));
- // 離開
- // PL.leave();
- }
- </script>
- </head>
- <body>
- <center>
- <h1>
- my first pushlet!
- </h1>
- </center>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Pragma" content="no-cache" /> <script type="text/javascript" src="ajax-pushlet-client.js"></script> <script type="text/javascript"> PL._init(); PL.joinListen('/cuige/he'); function onData(event) { alert(event.get("mess")); // 離開 // PL.leave(); } </script> </head> <body> <center> <h1> my first pushlet! </h1> </center> </body> </html>
- 將工程部署到Tomcat後訪問,該頁面以五秒鐘一個間隔,收到後臺傳來的資料!
工程部署後如下圖展示
每次訪問時,後臺都會列印增加到管理中的使用者資訊,當關閉瀏覽器後,伺服器會在超時後自動清理!