A context listener is exactly what you need.
In years past, Servlet and
JSP developers were always running into just this very problem. The solution typically was to code everything into a Servlet that loads at startup, but as you say, that is sloppy.
Similarly, the other problem that existed was "how do you make sure a user has something inside the session as soon as it is created." To address this question, they now provide a session listener.
When you code this listener, don't forget to update the deployment descriptor. A
Java file alone won't be recognized by the web container. I put together an online tutorial for coding a listener, just in case you're interested:
How to Create a ContextListener, and other Servlet Lifecycle Listeners Cheers!
-Cameron McKenzie