Pushlet是一种comet实现:在Servlet机制下,数据从server端的Java对象直接推送(push)到(动态)HTML页面,而无需任何Java applet或者插件的帮助。
缺点:服务的类在服务器启动时开始一直运行,对服务器的压力很大。
准备工作:
1. 下载pushlet
http://cdnetworks-kr-2.dl.sourceforge.net/project/pushlets/pushlets/2.0.4/pushlet-2.0.4.zip
eclipse or myeclipse 搭建
1. 创建工程
a) File à new à projectàweb project 项目名称为 : pushlet-demo
2. 添加依赖文件
a) 在下载文件的webapps 演示文件夹中找寻文件
b) 在 src 目录中添加 log4j.properties pushlet.properties sources.properties ,添加后修改sources.properties 清空演示配置,准备添加自己的配置
source1=nl.justobjects.pushlet.test.TestEventPullSources$TemperatureEventPullSource
source2=nl.justobjects.pushlet.test.TestEventPullSources$SystemStatusEventPullSource
source3=nl.justobjects.pushlet.test.TestEventPullSources$PushletStatusEventPullSource
source4=nl.justobjects.pushlet.test.TestEventPullSources$AEXStocksEventPullSource
source5=nl.justobjects.pushlet.test.TestEventPullSources$WebPresentationEventPullSource
source6=nl.justobjects.pushlet.test.TestEventPullSources$PingEventPullSource
c) 添加依赖jar包
在下载文件的 lib 目录下 copy pushlet.jar pushletclient.jar 导入到/WEB-INF/lib
d) 配置 web.xml
<servlet>
<servlet-name>pushlet</servlet-name>
<servlet-class>nl.justobjects.pushlet.servlet.Pushlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>pushlet</servlet-name>
<url-pattern>/pushlet.srv</url-pattern>
</servlet-mapping>
3. 添加实例
a) 创建数据源
package com.source;
import java.io.Serializable;
import nl.justobjects.pushlet.core.Event;
import nl.justobjects.pushlet.core.EventPullSource;
public class HelloWorldEventPullSource implements Serializable{
private static final long serialVersionUID = 1L;
static public class HelloWorldEvent extends EventPullSource{
@Override
protected long getSleepTime() {
return 1000; //刷新时间
}
@Override
protected Event pullEvent() {
Event event =Event.createDataEvent("/source/event");//事件标识
event.setField("msg", "hello,world");//封装参数
return event;
}
}
}
b) 配置数据源
sources.properties 进行数据源配置
添加source1=com.source.HelloWorldEventPullSource$HelloWorldEvent
c) 页面调用
Index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/ajax-pushlet-client.js"></script>
<!—js 从演示项目的 lib 下copy -->
<script type="text/javascript">
PL._init();
PL.joinListen('/source/event'); //事件标识 在数据源中引用
function onData(event) {
alert(event.get("msg"));
}
</script>
</head>
<body>
</body>
</html>
4. 测试 http://localhost:8080/pushlet-demo/index.html
要更深入的了解好好研究下 pushlet 的演示程序