How can I get the name of the current web app in Java EE?
I'm quite comfortable with stand-alone Java, but Java EE is new to me. I'm writing some custom code to plug in to a third-party Java EE reporting package. I have multiple instances deployed on the same Tomcat server, so I have something like:
C:\ +-- tomcat6 +-- webapps +-- app1 +-- app2
So when the user goes to, let's say, http://example.com/app1/viewReport, I want to be able to get "app1". (And not by parsing the URL, preferrably.)
Also, if there was a way to get the root of app1 (in this example, C:\tomcat6\webapps\app1), that would be great too.
Getting the "name"
=====
It's called the context path. If your code is running within the web app request context, you can get it by calling HttpServletRequest#getContextPath()
.
Accessing the physical/real resource ===== If you're trying to access the contents of a file/resource in your webapp, you're best of using one of:
It is also possible get the physical path on disk of a file/resource, given the path relative to the web app, using ServletContext#getRealPath(String)
, but it's not reliable (doesn't always work if you deploy you webapp as a WAR, for instance).
In your case, you'd probably want to use the latter, and then load the Properties file via Properties#load(InputStream).
Something along the lines of:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/reportCustom.properties"));