Hallo
I have the following Servlet's service code:
public void service( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException {
PrintStream out = new PrintStream( res.getOutputStream() );
try {
while(true) {
out.println( "" );
// then sleep for 10 sec
}
} catch ( IOException e ) {
System.out.println( "problem interacting with servlet caller. Reason: " + e.getMessage() + ". Exiting..." );
return;
}
}
When I invoke that servlet from my browser I can see new lines, but when
I close it, and servlet tries to write to `out' it does not throw an exception
, it just keeps writing (!?).
I wonder how to detect that the browser was closed after my servlet was woken up.
Do You know the answer ?
Greetings.
Filip Larsen - 12 Apr 2006 12:08 GMT
> When I invoke that servlet from my browser I can see new lines, but when
> I close it, and servlet tries to write to `out' it does not throw an exception
> , it just keeps writing (!?).
> I wonder how to detect that the browser was closed after my servlet was woken up.
I have a system where a servelet feeds data to a java client and here
the servlet (using Tomcat 4) gets a SocketException soon after the
client is terminated.
In a very few cases (like 1 out of 1000 disconnects and only in the
production environment) the servlet thread either hangs or terminates so
that the feed loop in the servlet is not terminated by an exception. To
handle this situation a central monitoring using timeouts is employed to
properly release resources for the servlet in question.
Also, the client never receive any exception if the server is terminated
so here the servlet sends ping on regular intervals if it has nothing
else to send to allow client to be able to detect disconnects. This may
of course not be a concern in your setup.
Regards,

Signature
Filip Larsen
William Brogden - 12 Apr 2006 14:44 GMT
> Hallo
> I have the following Servlet's service code:
[quoted text clipped - 24 lines]
> woken up.
> Do You know the answer ?
simple - a PrintStream never throws an exception, it swallows them all
- look at the JavaDocs
- perhaps you could use the checkError method.
while( !out.checkError() )
Bill