wood burning stoves
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
JavaRanch » Java Forums » Java » JSP
Bookmark "Receive Server-Sent Event Notifications" Watch "Receive Server-Sent Event Notifications" New topic

Receive Server-Sent Event Notifications

Senthilamrita Kumar

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"

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

var source=new EventSource("ServerSendEvent");
document.getElementById("result").innerHTML+=event .data + "<br />";
document.getElementById("result").innerHTML="Sorry , your browser does not support server-sent events...";



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 {

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");


PrintWriter out = response.getWriter();

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


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


public String getServletInfo() {
return "Short description";


Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63838

So what's your question?

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link: http://aspose.com/file-tools
subject: Receive Server-Sent Event Notifications
jQuery in Action, 3rd edition