That's about the most commonly asked question in this forum - click on "search" and see what others have said.
As far as running a servlet on a timed basis, that's not to be expected on ANY platform. Servlets are run in response to HTTP requests. You can have a task send an HTTP request periodically, but without receiving an HTTP request, a servlet doesn't run (startup code doesn't count).
The idea of trying to do something when a user "exits the application" just doesn't work on a web server. Aside from the fundamental architectural differences, web clients often "exit" by crashing (TOO often
). Try getting an event from THAT!
This is why modern servlet/JSP servers have support for session timeouts.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.