Getting the virtual host names as a general collection from the Tomcat server.xml file is relatively straightforward. You can do anything from a mindless "grep" to a full-blown XSLT or Digester app.
Getting the virtual host names that a single webapp is running under
from inside the webapp is quite a different story.
Most Tomcat users think of webapps in terms of exploded WAR files, so there's one instance of the WAR directory for each deployed instance of that webapp. However, that's a Tomcat quirk. You can (and I routinely do) deploy a WAR in the strict
J2EE sense of an actual WAR
file. For example, I can deploy the file "/opt/javaranch/webapps/forum.war" as many times as I want into as many hosts as I want under as many Contexts as I want in as many Tomcat instances as I want (allowing for hardware limitations). There's absolutely no way that the webapp itself can enumerate all the virtualhost names that it is running under - especially in cases where there are several different Tomcat JVMs using it.