JEE never ceases to amaze me. Even when I think I’m on top and I know all there’s to know about webapps, I’m in for a surprise. Good news is, whatever you think you know about a subject, there’s still room for one more fact. Bad news is, I’m deeply disturbed by what I learned.
Fact is: web applications context root can contain the /
characte, meaning an URL such as http://localhost/multipart/context
can refer to the root of the multipart/context
webapp as well as to the context
servlet mapping of the http://localhost/multipart
webapp.
When I was told that, my first reaction was disbelief.
I immediately hurried to run a few tests on the JOnAS and JBoss application servers and it confirmed that was entirely possible.
In fact, if you look at the application.xml XML Schema, you see that the context-root is of a type that extends string (with an id) :
This means the context root can effectively contains anything, including slashes, backslashes and what have you.
The J2EE 1.4 specifications does not enforce additional constraints (p 125) :
Each web module must be given a distinct and non-overlapping name for its context root. […] See the servlet specification for detailed requirements of context root naming.
I did not see additional requirements in the Servlet 1.4 specifications for the context root.
Tomcat even takes this tweak into account when creating Context with individual XML files. It calls it "multi-level context paths" :
In individual files (with a ".xml" extension) in the
$CATALINA_HOME/conf/[enginename]/[hostname]/
directory. The name of the file (less the .xml) extension will be used as the context path. Multi-level context paths may be defined using #, e.g.foo#bar.xml
for a context path of/foo/bar
.
So, in order to deploy the context.war under the multipart/context
root, you’ll have to name the context XML file multipart#context.xml
.
The content of the file is the following:
<?xml version='1.0' encoding='utf-8'?>
<Context docBase="${catalina.home}/context.war" />
<!-- this means the context.war should be available at the root of Tomcat -->
In the tests I’ve made, if the multi-level context path shadows a classic context-root with a servlet mapping, the former takes precedence. I do not advise using this since the case is not specified in the specification, it could be handled differently from product to product.
OK, now we’ve established these facts, what is the point in knowing this, aside from setting you as the übergeek in a JEE geek convention? Since http://localhost/multipart/context
and http://localhost/multipart
are clearly separated web applications, they do not even share context.
In my case, that was the solution for a simple use-case.
Imagine monitoring web applications: you know the URL you have to monitor.
Now a product, developped in-house for diagnostics purpose, is deployed side-by-side with each webapp in webapp form.
It would be very nice if you could reach the diagnostics webapp without looking at the documentation to know its URL.
Let’s say it is the business webapp’s URL appended with /diag
.
So, if the main webapp’s URL is http://myserver.com/mywebapp
and the person in charge of monitoring knows about it, he knows he has to access http://myserver.com/mywebapp/diag
and he gets what he wants! On the development side, it means both webapps are different products, are developed by different teams and have different lifecycles.