The method names in the API pretty much sum it up.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContextListener.html You have two methods:
contextInitialized gets called before any servlets get loaded. This is a good place to put any application initialization code.
and:
contextDestroyed. This gets called after all the servlets have been destroyed but before your context is destroyed.
This is a good place to put any cleanup code.
Before context listeners, the only way to run initialization code for the app was to put it in a servlet and trigger it with the load-on-startup attribute in web.xml. This really didn't follow the spirit of what a servlet should do (listen for requests and issue a response). You also had to keep track of what servlets were there only to initialize the app and make sure that they had a load-on-startup value that was lower than any servlet which might need to have that initialization done in order to work.