1. Create a new servelet, then encode it, and the source code after the last encoding is as follows:
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, HTTPSERVLETRESPO NSE * response) */protected void doget (httpservletrequest request,httpservletresponse response) throws Servletexcepti On, IOException {//TODO auto-generated method Stubresponse.setcontenttype ("Text/event-stream");// Set event Stream response.setcharacterencoding ("UTF-8");//Set encode//Get latest time and return printwriter writer = Response.getwriter (); String string = new Date (). toString (); System.out.println (string);//Send Ssewriter.write ("Data: "+ string +" \ n "); try {//Set interval time thread.sleep (1000);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
2. Create a new index.html file and encode it. The encoded files are as follows:
<!doctype html>
3. After running, you can see the output time in the browser. The effect is as follows:
Initialize ... Sun Nov 13:56:22 CST 2014Sun Nov 13:56:27 CST 2014Sun Nov 13:56:31 CST 2014Sun Nov 13:56:35 CST 2014Sun Nov 23 13:56:39 CST 2014Sun Nov 13:56:43 CST 2014Sun Nov 13:56:47 CST 2014Sun Nov 13:56:51 CST 2014Sun Nov 23 13:56:55 CST 2014Sun Nov 13:56:59 CST 2014Sun Nov 13:57:03 CST 2014.
Description:1. This series is based on "Data Push Apps with HTML5 SSE" the source of the revision of the book, the source code for the PHP version, I in the learning process, according to individual needs and understanding, modified into the Java version. So for the details of the example, please refer to the book.
2. This series works with Maven management. If you don't know about maven, please Google or Baidu yourself. Or you can convert your MAVEN project to a common Java project by itself, and the project depends on the jar package, see Pom.xml.
3. This series of blog is original, declined reprint.
HTML5 Data Push application development one of the source Java version-----HelloWorld