Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HTML5 Server-Sent Events + Java Servlet example
package net.viralpatel.servlets;
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;
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/event-stream");
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
for (int i = 0; i < 20; i++) {
writer.write("data: "+ System.currentTimeMillis() +"\n\n");
writer.flush();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
writer.close();
}
}
<!DOCTYPE HTML>
<html>
<head>
<title>Server-Sent Events Servlet example</title>
<style>
body {
font-family: sans-serif;
}
</style>
</head>
<body>
Time: <span id="foo"></span>
<br><br>
<button onclick="start()">Start</button>
<script>
function start() {
var eventSource = new EventSource("HelloServlet");
eventSource.onmessage = function(event) {
document.getElementById('foo').innerHTML = event.data;
};
</script>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>ServerSentEvent_HttpServlet_example</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<display-name>HelloServlet</display-name>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>net.viralpatel.servlets.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
@edubey

This comment has been minimized.

Copy link

@edubey edubey commented Apr 2, 2015

Hi,
It seems you have missed the closing bracket of the start() function in JS.

@jeb2239

This comment has been minimized.

Copy link

@jeb2239 jeb2239 commented Jun 7, 2015

How am I supposed to run this?

@marinhoysmael

This comment has been minimized.

Copy link

@marinhoysmael marinhoysmael commented Apr 1, 2016

Muito Bom! Obrigado Pela Sua contribuição! Ajudou bastante!
Very good! Thank you for your contribution! It helped!

@sheldonshen

This comment has been minimized.

Copy link

@sheldonshen sheldonshen commented Jun 23, 2016

Thank you so much,help me a lot !

@mababu

This comment has been minimized.

Copy link

@mababu mababu commented Mar 30, 2018

After 20 messages, a new eventSource connection has been established. I am unable to find who is establishing this new connection after the writer has been closed in the servlet.

@lterrac

This comment has been minimized.

Copy link

@lterrac lterrac commented Apr 2, 2019

After 20 messages, a new eventSource connection has been established. I am unable to find who is establishing this new connection after the writer has been closed in the servlet.

This is due to the fact that the connection on the client side is not closed. The magic of server sent events is that if the connection is closed for whatever reason (unless you do not close it intentionally) the client tries to reconnect to the server after a default time out ( 3 seconds) if you don't call eventSource.close() on the client side.

@InBrewJ

This comment has been minimized.

Copy link

@InBrewJ InBrewJ commented Nov 22, 2020

This just saved me all the time in the world. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment