File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Receive Server-Sent Event Notifications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Receive Server-Sent Event Notifications" Watch "Receive Server-Sent Event Notifications" New topic
Author

Receive Server-Sent Event Notifications

Senthilamrita Kumar
Greenhorn

Joined: Aug 17, 2012
Posts: 1

Server-Sent Events - One Way Messaging
-------------------------------------------------------------------

A server-sent event is when a web page automatically gets updates from a server.

This was also possible before, but the web page would have to ask if any updates were available. With server-sent events, the updates come automatically.

Examples: Facebook/Twitter updates, stock price updates, news feeds, sport results, etc.

1) Create a new EventSource object, and specify the URL of the page sending the updates (in this example "ServerSendEvent" Servelet page)
2) Each time an update is received, the onmessage event occurs
3) When an onmessage event occurs, put the received data into the element with id="result"

Create index.html
------------------------
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Getting server updates</h1>
<div id="result"></div>

<script>
if(typeof(EventSource)!=="undefined")
{
var source=new EventSource("ServerSendEvent");
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>

-------------------------------------------------------------------------

Create New servlet page in the name of 'ServerSendEvent'

-------------------------------------------------------------------------
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author Senthil Kumar
*/
public class ServerSendEvent extends HttpServlet {


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
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();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
}
System.out.println("Sent " + messagesSent);
}

}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

}


@Override
public String getServletInfo() {
return "Short description";
}

}
------------------------------------------------------------------------------

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

So what's your question?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Receive Server-Sent Event Notifications