Search code examples
htmltomcatserver-sent-events

HTML5 Server Sent Events using Java Servlet 3 and tomcat


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.

Here the two files:

<html>
<body onload ="registerSSE()" >
    <script>
        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 + "<br />";
            };
            /*source.addEventListener('server-time',function (e){
                alert('ea');
            },true);*/
        }
    </script>
    <output id ="result"></output>

</body>
</html>

the servlet :

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>10)
                break;
            }
            pw.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void doGet(HttpServletRequest request,HttpServletResponse response) {
        doPost(request,response);
    }

}

Which version of tomcat should I need ? and also which connector type should i require?

_ WEB.XML _

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>demo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
        <servlet-name>sse</servlet-name>
        <servlet-class>sse</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>sse</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

Solution

  • I don't know about EventResource, but I understand that, if you are able to open resource in browser, it should take you one step ahead.

    Make changes as under in web.xml :-

    1. <servlet-class> Give full path to your servlet with package name like - com.test.sse
    2. <url-pattern> You can make /* here so it will work for every URL.

    After making this changes, try to open resource in browser and see if this helps.