Question

I am using a servlet as my server to send updates to my jsp client(Using HTML5 SSE). This is my jsp code

<script type="text/javascript">

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

This is my servlet code

public void doPost(HttpServletRequest request, HttpServletResponse response) {

    try{
    System.out.println("Begin for session: " + request.getSession(true).getId() + " " + response.getWriter());  
response.setHeader("pragma", "no-cache,no-store");  
response.setHeader("cache-control", "no-cache,no-store,max-age=0,max-stale=0");  

response.setContentType("text/event-stream");  

PrintWriter out = response.getWriter();  

int messagesSent = 0;  
while (true) {  
    out.print("data: {" + messagesSent++ + "}\n\n");  
    out.flush();  
     }} catch(Exception e){System.out.println(e);}

There is no response from server. I have deployed my servlet in jboss and the servlet is not at all being called by jsp file. The print statement at the beginning of servlet is not executing.

Was it helpful?

Solution

Are you able to access the Servlet directly i.e. not from the javascript script?

You need to double check first that this Servlet is accessible regardless of the call from javascript.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top