Hi
Having created a Servlet, I want to be sure, that the content written to
the HTTP-response is flushed all the way through the TCP connection
carrying the HTTP-request and -response.
In case the TCP connection to the HTTP-client has been closed - e.g. if
the client has timed out - I would like to see some form of IOException
within my servlet, when the Servlet ends up performing a
HttpServletResponse.flushBuffer() (or
HttpServletResponse.getOutputStream().flush()).
The below fragment of Servlet-code writes a line of text, sleeps 10
seconds and then writes another line of text. When initiating a request
from IE and aborting the request after about five seconds, the
Servlet-code ends up catching an IOException - in Tomcat it becomes a
ClientAbortException wrapping a SocketException with the text
"Connection reset by peer: socket write error". This makes my Servlet
aware of the connection to the client being cut.
When initiating a request from some Java code by using HttpURLConnection
with a read-timeout of five seconds, I see the same type of reaction
within the Servlet if and only if I do no end up doing a proper cleanup
containing a call to HttpURLConnection.getInputStream().close(). If the
call to close() is not made, the Servlet catches a IOException. But if
the call to close() is made, the Servlet does *not* detect the
HTTP-client cutting or closing the connection; no IOException is seen
within the Servlet. Why is this?
If I instead of IE use FireFox and initate a request and abort this
request after about five seconds, the Servlet also does not detect the
stream carrying the HTTP-reponse being cut or closed. Why is this?
(As far as I can tell from the net, both FireFox and Opera closes the
TCP connection in a proper way.)
If e.g. the FireFox browser correctly closes the TCP/IP connection, the
Servlet-container must somehow detect this. But why does this not imply
an IOException being generated within the Servlet, when the Servlet
performs explicit flushing?
Surely, a cut or closed TCP connection can not be ignored by Tomcat?
?
Regards
Morten Sabroe Mortensen
-----
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws
IOException,
ServletException
{
try
{
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
Writer w=response.getWriter();
w.write("Hello, this is the servlet speaking!");
w.write("\n");
w.flush();
Thread.sleep(10*1000);
w.write("Goodbye.");
w.write("\n");
w.flush();
response.flushBuffer();
System.out.println(":> response.isCommitted():
"+response.isCommitted());
}
catch (IOException ex)
{
System.out.println(":> *** VUUPS! ***");
ex.printStackTrace(System.out);
IOException exception=new IOException();
exception.initCause(ex);
throw exception;
}
catch (Throwable ex)
{
ex.printStackTrace(System.out);
throw new RuntimeException(ex);
}
}
-----
---------------------------------------------------------------------
To start a new topic, e-mail: [email protected]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]