servlet: writing to servlet stream that is closed

P

Pawel

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.
 
F

Filip Larsen

Pawel wrote
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,
 
W

William Brogden

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 ?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top