As a Java web developer, I recommend that you take advantage of an advanced servlet component: the Servlet Filter.
A Servlet filter is an object that can intercept HTTP requests targeted at your web application.
This article from journaldev.com does a good job of walking you through the development process. Enjoy!
Java Servlet Filters Tutorial