HTML5 data push Application Development (java) source code ----- helloworld, html5 source code

Source: Internet
Author: User

HTML5 data push Application Development (java) source code ----- helloworld, html5 source code

1. Create a New servelet and encode it. The source code after 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, HttpServletResponse * response) */protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubresponse. setContentType ("text/event-stream"); // sets the event stream response. setCharacterEncoding ("UTF-8"); // set the encoding // get the 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 the interval Thread. sleep (1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}

2.create an index.html file and encode it. The encoded file is as follows:


<! Doctype html> 

3. After running, you can see that the browser does not stop outputting time. The effect is as follows:

Initialization... 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.


Description:

1. this series is based on the source code of the book "Data Push Apps with HTML5 SSE". The source code of this book is the PHP version. During my learning process, based on my personal needs and understanding, changed to JAVA. For more information about the examples, see the book.

2. This series of projects are managed using maven. If you are not familiar with maven, google or baidu. You can also convert a maven project to a java project. For details about jar packages that the project depends on, see pom. xml.

3. This series of blogs is original and will not be reproduced.

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.