Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java's HttpServletResponse doesn't have isClientConnected method

Tags:

java

servlets

I'm implementing a long poll http connection using java servlet.

How can I know that the http client is still active at any instance? Currently, what I do is to write a byte to the output stream and flush data. If there's an IO exception then the client is dead.

But in ASP.NET there is a property, Response.IsClientConnected which can find out if the client is active without writing anything to the output stream.

I want to know how if it is possible to develop in java servlet. I do not want to keep writing data into the http response stream as it may cost network.

Thanks in advance.

like image 718
PC. Avatar asked Sep 30 '11 05:09

PC.


People also ask

What is HttpServletResponse in Java?

All Superinterfaces: ServletResponse All Known Implementing Classes: HttpServletResponseWrapper public interface HttpServletResponse extends ServletResponse. Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response. For example, it has methods to access HTTP headers and cookies ...

Which of these is the HTTP response code when there is a resource forbidden error HttpServletResponse sc forbidden?

SC_FORBIDDEN. Status code (403) indicating the server understood the request but refused to fulfill it.

Which method sets the local of the response?

setLocale. Sets the locale of the response, if the response has not been committed yet. It also sets the response's character encoding appropriately for the locale, if the character encoding has not been explicitly set using setContentType(java. lang.


1 Answers

It will be difficult to achieve that using Servlet APIs. Though the low level Socket APIs provide this functionality (Socket.isConnected() ), but same functionality is not available through any higher level APIs. Not sure if you any compulsions of using Servlet APIs or you can use low level socket APIs.

like image 164
Santosh Avatar answered Nov 01 '22 10:11

Santosh