I have a series of webapps, and for each I would like to set a context root so that when I reference a resource (css, js, etc.) from a web page in one of these apps, I can reference it through a relative root path without having to put the webapp path in the url. For example, as it is now, a root relative path has to be:
I want to give each app it own root if possible so that I can instead reference like:
I remember doing something similar many years back on a J2EE server, but cannot find a solution for Tomcat.
Joined: Oct 19, 2011
If there is a way to do it for each webapp, then that would be great but if I'm forced to solve this by defining one root/default webapp then that would be acceptable since I only want to do this for one of the webapps at the moment. Question now becomes how to do this? Tomcat 6 does not recommend defining Context elements in the Host element of server.xml (which is the only way I know how); instead to use a ROOT.xml to accomplish it. The docs on this are pretty vague in my opinion.
Each of your WebApps should be its own WAR file for deployment, so they get their own deployment descriptor and directory structure. You don't necessarily have to produce the WAR file, but you do have to produce the entire exploded web folder structure (including a WEB-INF\ and web.xml file).
If I recall if the ROOT of web directory was here:
Then you would have the 'default' web application named ROOT:
Which has a a WEB-INF folder (or if not, just inherits deployment from the server.xml).
You then add you individual web applications parallel to the ROOT:
Each of those should have its own WEB-INF and web.xml file. There is a setting that will let Tomcat auto-detect these new webapps, but I believe you have to restart the server. If you deploy via WARs (into a specific directory) then Tomcat auto-explodes them and you do not have to restart the server.
Again, been a while, but I believe that is the crux of it.
Ya I can get it work as a default webapp, by either using the ROOT method, or defining a Context element in the Host element of server.xml (using path and docBase attributes). However in retrospect, the ideal situation for me would be to not have a default webapp, but be able to define the root/base for each webapp. I basically just want to eliminate the requirement for root relative paths within the webapp to all begin with the webapp name. If for some reason I have to rename the app, then I'm forced to change the path for a whole lot of paths (and in many cases i'm forced to use a root relative path).
I'm certain that I've accomplished this in the past (albeit with a J2EE server) where the default config was that root relative paths would point only to the app itself, not the appBase. I haven't come across anything in the docs that would help... and perhaps its not possible with Tomcat. Any feedback?
Perhaps I'm not being clear about what I would like to accomplish. I'm well aware that you can have multiple webapps hosted on a single server. Currently my conf directory looks like this:
I can access each app without issue. But going back to my original post; currently if I was on http://localhost/app1/index.html, and index.html has an image tag, the root relative path to that image has to be: "/app1/images/myimage.jpg". I'd like to know if its possible through some sort of configuration, to access that image with a root relative path by only entering "/images/myimage.jpg". In other words, any request coming from app1 would know that its root is app1 and not require "/app1" prefixed to every root relative path.
The only way I know how to accomplish this, is to set a root application, but this is not ideal since it only solves the problem for one application.
David Sheltby wrote:Can I make each application a root/default application?
You can only have one root application.
My original understanding was that you just wanted the images and CSS and other resources to be in the root application, where they could be referred to by all of your webapps. That's certainly possible and easy to do. But now you seem to have some other requirement which you are unable to describe.
Joined: Oct 19, 2011
Ok, we're getting closer to what I'm trying to accomplish. I see the misunderstanding. Basically I don't want any relationship or reuse of resources among applications... they are completely independent of each other. But I still want each app to have their own root, in a way. A request from a page in app1, for a resource in app1, should not have to prefix "app1" to every url. And a request from a page in app2, for a resource in app2, should not have to prefix "app2" to every url.
I refer again to my earlier post: a request coming from a webapp should know its own root context (that of the app that the request came from) and not require the app name prefixed to every root relative path. I don't think i can make it any more clear.
There's only one root application. How could it be otherwise when the root application is identified by a zero-character string? You seem to be asking for an unlimited number of zero-character strings.
So can we go back to the beginning... what is the reason you can't just put the images and so on into the webapps that own them?
Joined: Oct 19, 2011
They are. My original post says exactly this. If I am on http://localhost/webapp1/pages/index.html, and I want to reference images in webapp1/images from pages/index.html with a root relative path, then my img tag has to look like this:
<img src="/webapp1/images/myimage.jpg" />
It won't work if I put
<img src="/images/myimage.jpg" />
All I'm asking is for a root relative path to implicity know that it starts from /webapp1... the request is coming from a page inside webapp1 after all, so why am i forced to prefix my src attribute with /webapp1??