参考文章:http://blog.sina.com.cn/s/blog_58b9cb3a01014l71.html
1.> 添加基础文件
src : source.properties | pushlet.properties
js : ajax-pushlet-client.js | js-pushlet-client.js
两个Servlet,一个是pushlet 并且要提前初始化,另一个我们自己的Servlet
<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>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>zy.pushlet.web.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myServlet.do</url-pattern>
</servlet-mapping>
3.> 不需要再去修改 source.properties 再去注册事件源了.
4.> MyServlet
public class MyServlet extends HttpServlet {
private String meg;
protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//获取内容并转码
meg = request.getParameter("meg");
meg = new String(meg.getBytes("UTF-8"),"ISO-8859-1");
//初始化事件实例,并发送
Event event = Event.createDataEvent("/Pointer");
event.setField("data", meg);
Dispatcher.getInstance().multicast(event);
response.sendRedirect(request.getContextPath()+"/input.jsp");
}
}
5.> 发送端和接收端
<% request.setAttribute("ctx", request.getContextPath()); %>
<form action="${ctx}/myServlet.do">
消息内容 : <input type="text" name="meg" />
<input type="submit" value="发送" />
</form>
receive.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="ajax-pushlet-client.js"></script>
</head>
<body>
<div>
接收到的内容 :
<div id="receive">
</div>
</div>
<script type="text/javascript">
PL._init();
PL.joinListen('/Pointer');
var receive = document.getElementById("receive");
function onData(event) {
receive.innerHTML = receive.innerHTML + event.get("data") +"<br/>";
}
</script>
</body>
</html>
6.> Test
分别打开两个页面,input.jsp 和 receive.jsp