"HTML5 Data push application development" one of the source Java version-----HelloWorld

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.