src/main/java/js/net/EventStreamServlet.java
@Override
	protected void handleRequest(RequestContext context) throws IOException {
		log.trace("handleRequest(RequestContext)");
		final HttpServletResponse httpResponse = context.getResponse();
		final String sessionID = getEventStreamSessionID(context.getRequestPath());

		EventStream eventStream = eventStreamManager.createEventStream(sessionID);
		if (eventStream == null) {
			// a bad or heavy loaded client may send this request after session expiration or attempt to reuse old session ID
			sendBadRequest(context);
			return;
		}

		// 1. headers and content type should be initialized before response commit
		// 2. it seems that ServletResponse#getWriter() updates character set
		// excerpt from apidoc:
		// If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method
		// just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.

		// so need to be sure headers are updated before writer opened

		httpResponse.setContentType("text/event-stream;charset=UTF-8");
		// no need to explicitly set character encoding since is already set by content type
		// httpResponse.setCharacterEncoding("UTF-8");

		httpResponse.setHeader(HttpHeader.CACHE_CONTROL, HttpHeader.NO_CACHE);
		httpResponse.addHeader(HttpHeader.CACHE_CONTROL, HttpHeader.NO_STORE);
		httpResponse.setHeader(HttpHeader.PRAGMA, HttpHeader.NO_CACHE);
		httpResponse.setDateHeader(HttpHeader.EXPIRES, 0);

		httpResponse.setHeader(HttpHeader.CONNECTION, HttpHeader.KEEP_ALIVE);

		eventStream.setRemoteHost(context.getRemoteHost());
		eventStream.setWriter(httpResponse.getWriter());
		try {
			eventStream.onOpen();
			log.debug("Event stream |%s| opened.", eventStream);
			while (eventStream.loop()) {
			}
		} finally {
			eventStream.onClose();
			eventStreamManager.destroyEventStream(eventStream);
			log.debug("Event stream |%s| closed.", sessionID);
		}
	}
@Override
	protected void handleRequest(RequestContext context) throws IOException {
		log.trace("handleRequest(RequestContext)");
		final HttpServletResponse httpResponse = context.getResponse();
		final String sessionID = getEventStreamSessionID(context.getRequestPath());

		EventStream eventStream = eventStreamManager.createEventStream(sessionID);
		if (eventStream == null) {
			// a bad or heavy loaded client may send this request after session expiration or attempt to reuse old session ID
			sendBadRequest(context);
			return;
		}

		// 1. headers and content type should be initialized before response commit
		// 2. it seems that ServletResponse#getWriter() updates character set
		// excerpt from apidoc:
		// If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method
		// just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.

		// so need to be sure headers are updated before writer opened

		httpResponse.setContentType("text/event-stream;charset=UTF-8");
		// no need to explicitly set character encoding since is already set by content type
		// httpResponse.setCharacterEncoding("UTF-8");

		httpResponse.setHeader(HttpHeader.CACHE_CONTROL, HttpHeader.NO_CACHE);
		httpResponse.addHeader(HttpHeader.CACHE_CONTROL, HttpHeader.NO_STORE);
		httpResponse.setHeader(HttpHeader.PRAGMA, HttpHeader.NO_CACHE);
		httpResponse.setDateHeader(HttpHeader.EXPIRES, 0);

		httpResponse.setHeader(HttpHeader.CONNECTION, HttpHeader.KEEP_ALIVE);

		eventStream.setRemoteHost(context.getRemoteHost());
		eventStream.setWriter(httpResponse.getWriter());
		try {
			eventStream.onOpen();
			log.debug("Event stream |%s| opened.", eventStream);
			while (eventStream.loop()) {
			}
		} finally {
			eventStream.onClose();
			eventStreamManager.destroyEventStream(eventStream);
			log.debug("Event stream |%s| closed.", sessionID);
		}
	}
@ Override protected void handleRequest ( RequestContext context ) throws IOException { log . trace ( "handleRequest(RequestContext)" ) ; final HttpServletResponse httpResponse = context . getResponse ( ) ; final String sessionID = getEventStreamSessionID ( context . getRequestPath ( ) ) ; EventStream eventStream = eventStreamManager . createEventStream ( sessionID ) ; if ( eventStream == null ) { // a bad or heavy loaded client may send this request after session expiration or attempt to reuse old session ID
 sendBadRequest ( context ) ; return ; } // 1. headers and content type should be initialized before response commit
 // 2. it seems that ServletResponse#getWriter() updates character set
 // excerpt from apidoc:
 // If the response's character encoding has not been specified as described in getCharacterEncoding (i.e., the method
 // just returns the default value ISO-8859-1), getWriter updates it to ISO-8859-1.
 // so need to be sure headers are updated before writer opened
 httpResponse . setContentType ( "text/event-stream;charset=UTF-8" ) ; // no need to explicitly set character encoding since is already set by content type
 // httpResponse.setCharacterEncoding("UTF-8");
 httpResponse . setHeader ( HttpHeader . CACHE_CONTROL , HttpHeader . NO_CACHE ) ; httpResponse . addHeader ( HttpHeader . CACHE_CONTROL , HttpHeader . NO_STORE ) ; httpResponse . setHeader ( HttpHeader . PRAGMA , HttpHeader . NO_CACHE ) ; httpResponse . setDateHeader ( HttpHeader . EXPIRES , 0 ) ; httpResponse . setHeader ( HttpHeader . CONNECTION , HttpHeader . KEEP_ALIVE ) ; eventStream . setRemoteHost ( context . getRemoteHost ( ) ) ; eventStream . setWriter ( httpResponse . getWriter ( ) ) ; try { eventStream . onOpen ( ) ; log . debug ( "Event stream |%s| opened." , eventStream ) ; while ( eventStream . loop ( ) ) { } } finally { eventStream . onClose ( ) ; eventStreamManager . destroyEventStream ( eventStream ) ; log . debug ( "Event stream |%s| closed." , sessionID ) ; } }
Create {@link EventStream} for session ID from request URI and run {@link EventStream#loop()} as far it returns true.
This method takes also care to handle connections errors due to network or client failure. At event stream loop exit,
graceful or due to error, unbind event stream from manager.
<p>
If event stream cannot be created due to bad session ID responds with bad request, error code 400.
<p>
Note that this method does not return as long client requesting for event stream is connected, keeping HTTP request
thread and servlet allocated, even there is no event to send for this particular client.

@param context HTTP request context.
@throws IOException if HTTP response output stream fail to open.
Create { @link EventStream } for session ID from request URI and run { @link EventStream#loop () } as far it returns true . This method takes also care to handle connections errors due to network or client failure . At event stream loop exit graceful or due to error unbind event stream from manager . <p > If event stream cannot be created due to bad session ID responds with bad request error code 400 . <p > Note that this method does not return as long client requesting for event stream is connected keeping HTTP request thread and servlet allocated even there is no event to send for this particular client .