0

i want to achieve server sent events using jsp but it's not working, My code is as given below but it not even displaying date also

date.jsp

  <%@ page import="java.io.*,java.util.*, javax.servlet.*" %>
  <%
    response.setContentType("text/event-stream;charset=UTF-8");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Connection", "keep-alive");

  %>


  <%
    Date date = new Date();
    out.write(+date.toString()+);
    out.flush();
    try {
    Thread.currentThread().sleep(5000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    %>

and my ex.html code is as fallows

<!DOCTYPE html>
<html>
<body>
     <h1>Getting server updates</h1>
     <div id="result"></div>

     <script>
     if(typeof(EventSource)!=="undefined")
     {
         var source=new EventSource("date.jsp");
         source.onmessage=function(event)
         {
             document.getElementById("result").innerHTML+=event.data + "<br>";
         };
     }
     else
     {
         document.getElementById("result").innerHTML="Sorry, your browser does not support   server-sent events...";
     }
    </script>

</body>
</html>

OUTPUT IS:

Getting server updates


not displaying the date also...

I am using tomact server,is there any problem in the code

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Manohar Gunturu
  • 676
  • 1
  • 10
  • 23

1 Answers1

0

you should use something runnable like body onLoad or button onClick listeners, you should use correct path to your jsp or servlet(placed not inside WEB-INF folder), you should use correct event data format, with "data:" prefix and "\n\n" suffix.

ex.html

<!DOCTYPE html>
<html>
    <body onLoad = RegisterSSE()>
          <h1>Getting server updates</h1>
          <div id="result"></div>
          <script>
              function RegisterSSE()
              {
                  alert("wtf");
                  if(typeof(EventSource) != "undefined")
                  {
                      var source = new EventSource ("http://localhost:8080/web_war_exploded/date.jsp");
                      source.onmessage = function(event)
                      {
                          document.getElementById("result").innerHTML += event.data + "<br/>";
                      };
                  }
                  else
                  {
                      document.getElementById("result").innerHTML = "Sorry, your browser does not support   server-sent events...";
                  }
              }
          </script>
    </body>
    </html>

date.jsp

<%
    response.setContentType("text/event-stream;charset=UTF-8");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Connection", "keep-alive");

    Date date = new Date();
    out.write("event: server-time\n\n");
    out.write("data: "+date.toString() + "\n\n");
    out.flush();
    try {
        Thread.currentThread().sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
%>

and after all you should use google, your question is nothing new, please see links below:

java-servlet-and-server-sent-events

push-notification-for-java-web-app

how-push-notification-java-servlet-for-web-application

Community
  • 1
  • 1
user3002379
  • 141
  • 1
  • 3