How can you determine if a user hit cancel during boot from the Java servlet?

How can you find out if a user hit cancel during boot from the Java servlet? What seems to be happening to me with IE, println () output streams. Used click cancels, but is there any way to do this or something like that?

+4
source share
2 answers

Wrap OutputStream#write() , #flush() and #close() in a try-catch on an IOException . If it is caught, this usually means that the end user interrupted the request.

To take it one step further, you can also catch more exceptions related to the servlet container, such as ClientAbortException on Tomcat and clones (which is a subclass of IOException ), but this will make your webapp code not portable for other servlet containers.


Update : to return to the lock problem, you must call flush() after writing one block of data. This will actually send data to the client and will IOException when the main socket is closed.

+4
source

You cannot tell for sure. It depends on how your servlet container is implemented.

When the user clicks Cancel, the browser should close the socket. Of course, this will be detected by the server, but regardless of whether the server ServletOutputStream "plumbed" directly has an OutputStream socket, it depends on the implementation. If this happens, an IOException will be raised the next time the servlet tries to write data.

If the output buffers of the containers are output, the servlet may just end up writing data and never know that something went wrong.

+2
source

All Articles