hmm, tricky one - i wouldn't have thought it was possible to capture a web browser closing event so getting this right might be a bit of a hack. Generally speaking infinite loops are poor coding so as csamuels hints at you need some condition that may exits the loop.
My initial thought is if you have a loop like you do why not just chuck a timer in there too: Presumably you have some action listenter somewher or some way for the user to interact with the applet - if you reset the timer each time an action happens then you can time out the applet at say 10minutes of inactivity, or whatever you see suitable. i.e.
Code:
while(time<X){
/* do some stuff */
}
Although clearly this is a bit of a hack and there is the potential for a user to open loads of instances of this applet in a very short space of time and potentially starving the server of resources for 10 minutes....
Ideally you need a way of determining if the browser window is open or not, otherwise if there is something in your code which is only ever going to happen if the user has navigated away - perhaps even just opening it in another popup window - this way you can probably tell if the window is open or closed... possibly! - having not written many applets i cant say any more. good luck though! let me know how you get on