title: HTML5 Server Sent Events using Java Servlet 3 and tomcat
body: <p>I tried to run this code in Eclipse   with Tomcat but I don't get any output by the server in the client side. Should I include some libriries (jar files, js files ... ) to the project ? Thank you. </p>
  
  <p><strong>Here the two files:</strong> </p>
  
  <pre><code>&lt;html&gt;
  &lt;body onload ="registerSSE()" &gt;
      &lt;script&gt;
          function registerSSE()
          {
              alert('test 1');
              var source = new EventSource('http://localhost:8080/hello/sse');  
              alert('Test2');
              source.onmessage=function(event)
              {
                  document.getElementById("result").innerHTML+=event.data + "&lt;br /&gt;";
              };
              /*source.addEventListener('server-time',function (e){
                  alert('ea');
              },true);*/
          }
      &lt;/script&gt;
      &lt;output id ="result"&gt;&lt;/output&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>the servlet :</p>
  
  <pre><code>import java.io.*;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  public class sse extends HttpServlet {
     public void doPost(HttpServletRequest request, HttpServletResponse response) {
          try {
              System.out.println("SSE Demo");
              response.setContentType("text/event-stream");
  
              PrintWriter pw = response.getWriter();
              int i=0;
              while(true) {
                  i++;
                  pw.write("event: server-time\n\n");  //take note of the 2 \n 's, also on the next line.
                  pw.write("data: "+ i + "\n\n");
                  System.out.println("Data Sent!!!"+i);
                  if(i&gt;10)
                  break;
              }
              pw.close();
          } catch(Exception e) {
              e.printStackTrace();
          }
      }
  
      public void doGet(HttpServletRequest request,HttpServletResponse response) {
          doPost(request,response);
      }
  
  }
  </code></pre>
  
  <p>Which version of tomcat should I need ? and also which connector type should i require?</p>
  
  <p><em><strong></em>_ WEB.XML <em>_</em></strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"&gt;
    &lt;display-name&gt;demo&lt;/display-name&gt;
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
  
    &lt;servlet&gt;
          &lt;servlet-name&gt;sse&lt;/servlet-name&gt;
          &lt;servlet-class&gt;sse&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;sse&lt;/servlet-name&gt;
          &lt;url-pattern&gt;*.html&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>