• Post Reply Bookmark Topic Watch Topic
  • New Topic

server sent event using jsp not working

 
manohar gunturu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

is it not compatible with jsp .

please tell me where the problem is
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why - in the name of sanity - are you trying to do this in JSP?

JSP just naturally writes HTML, not "text/event-stream" - therefore you should be making a servlet.

Bill

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65528
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!