简体   繁体   English

服务器使用jsp发送事件不起作用

[英]server sent event using jsp not working

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 我想使用jsp实现服务器发送的事件,但无法正常工作,我的代码如下,但它甚至不显示日期

date.jsp 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 和我的ex.html代码是休假

<!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 我正在使用tomact服务器,代码中是否有任何问题

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. 您应使用诸如body onLoad或按钮onClick侦听器之类的可运行内容,应使用指向jsp或servlet的正确路径(未放置在WEB-INF文件夹内),应使用正确的事件数据格式,并带有“ data:”前缀和“ \\ n \\ n“后缀。

ex.html 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 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: 毕竟您应该使用google,您的问题并不新鲜,请查看以下链接:

java-servlet-and-server-sent-events java-servlet和服务器发送事件

push-notification-for-java-web-app 推送通知Java Web应用程序

how-push-notification-java-servlet-for-web-application Web应用程序如何推送通知Java Servlet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM