HTML5中的服务器‘推送’技术 -Server-Sent Events
html5服务器推送技术DEMO:http://viralpatel.net/blogs/html5-server-sent-events-java-servlets-example/
java 代码:
package com.forms; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MovementEventSource extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/event-stream"); // encoding must be set to UTF-8 response.setCharacterEncoding("UTF-8"); PrintWriter writer = response.getWriter(); for (int i = 0; i < 10; i++) { writer.write("data: " + System.currentTimeMillis() + "\n\n"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } writer.close(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(req, resp); } }
web.xml端配置:
<servlet> <servlet-name>sendServlet</servlet-name> <servlet-class>com.forms.MovementEventSource</servlet-class> </servlet> <servlet-mapping> <servlet-name>sendServlet</servlet-name> <url-pattern>/testSendServlet.do</url-pattern> </servlet-mapping>
jsp页面代码:
<body> Time: <span id="foo"></span> <br><br> <button onclick="start()">Start</button> <script type="text/javascript"> function start() { var eventSource = new EventSource("testSendServlet.do"); eventSource.onmessage = function(event) { document.getElementById('foo').innerHTML = event.data; }; } start(); </script> </body>
复杂的html5服务器推送技术:
java代码:
package net.viralpatel.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/event-stream"); response.setCharacterEncoding("UTF-8"); PrintWriter writer = response.getWriter(); int upVote = 0; int downVote = 0; for (int i = 0; i < 20; i++) { upVote = upVote + (int) (Math.random() * 10); downVote = downVote + (int) (Math.random() * 10); writer.write("event:up_vote\n"); writer.write("data: " + upVote + "\n\n"); writer.write("event:down_vote\n"); writer.write("data: " + downVote + "\n\n"); writer.flush(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } writer.close(); } }
jsp页面代码:
var eventSource = new EventSource("HelloServlet"); eventSource.addEventListener('up_vote', function(event) { document.getElementById('up').innerHTML = event.data; }, false); eventSource.addEventListener('down_vote', function(event) { document.getElementById('down').innerHTML = event.data; }, false);
相关推荐
wusiye 2020-10-23
表格的现在还是较为常用的一种标签,但不是用来布局,常见处理、显示表格式数据。在HTML网页中,要想创建表格,就需要使用表格相关的标签。<table> <tr> <td>单元格内的文字</td> ...
gufudhn 2020-08-09
nercon 2020-08-01
swiftwwj 2020-07-21
nercon 2020-07-16
饮马天涯 2020-07-05
Lophole 2020-06-28
gufudhn 2020-06-12
csstpeixun 2020-06-11
huzijia 2020-06-09
WebVincent 2020-06-06
行吟阁 2020-05-30
qsdnet我想学编程 2020-05-26
gufudhn 2020-05-25
qsdnet我想学编程 2020-05-19
suixinsuoyu 2020-05-15
HSdiana 2020-05-15
PioneerFan 2020-05-15