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.