《HTML5資料推送應用開發》源碼java版之一-----helloworld,html5源碼

來源:互聯網
上載者:User

《HTML5資料推送應用開發》源碼java版之一-----helloworld,html5源碼

1.建立servelet,然後進行編碼,最後編碼完後的源碼如下:

package com.heetian.servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.Date;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class SSEEchoServlet */@WebServlet("/SSEEchoServlet")public class SSEEchoServlet extends HttpServlet {private static final long serialVersionUID = 1L;/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse *      response) */protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubresponse.setContentType("text/event-stream");//設定事件流response.setCharacterEncoding("UTF-8");//設定編碼                //擷取最新時間並返回PrintWriter writer = response.getWriter();String string = new Date().toString();System.out.println(string);// send SSEwriter.write("data: " + string + "\n\n");try {   //設定間隔時間Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

2.建立index.html檔案,然後進行編碼。編碼後的檔案如下:


        <!doctype html>        <html>          <head>            <meta charset="UTF-8">            <title>Basic SSE Example</title>          </head>          <body>            <pre id="x">初始化...</pre><!--之所以用pre,沒用div或p,是為了確保(包括換行的資料能夠以它被接收時的格式呈現,而不會被修改或格式化。)-->            <script>            var es = new EventSource("SSEEchoServlet");//建立EventSource對象,將要串連的URL作為它唯一的參數。這裡是串連到basic_sse.php            es.addEventListener("message", function(e){              document.getElementById("x").innerHTML += "\n" + e.data;//運態修改頁面內容。              },false);            </script>。          </body>        </html>

3. 運行後,可以看到瀏覽器中不停輸出時間。效果如下:

初始化...Sun Nov 23 13:56:22 CST 2014Sun Nov 23 13:56:27 CST 2014Sun Nov 23 13:56:31 CST 2014Sun Nov 23 13:56:35 CST 2014Sun Nov 23 13:56:39 CST 2014Sun Nov 23 13:56:43 CST 2014Sun Nov 23 13:56:47 CST 2014Sun Nov 23 13:56:51 CST 2014Sun Nov 23 13:56:55 CST 2014Sun Nov 23 13:56:59 CST 2014Sun Nov 23 13:57:03 CST 2014。


說明

1. 本系列是根據《Data Push Apps with HTML5 SSE》一書的源碼修改而成,該書源碼為PHP版,本人在學習過程中,根據個人需求及理解,修改成了JAVA版。所以有關例子的細節,請參閱該書。

2.本系列工程使用maven管理。如果對maven不瞭解,請自行google或baidu。或者自行將maven工程轉化為普通java工程,項目所依賴jar包,請參見pom.xml。

3.本系列部落格為原創,謝絕轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.