aspose file tools*
The moose likes Tomcat and the fly likes Giving each webapp its own context root Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Giving each webapp its own context root" Watch "Giving each webapp its own context root" New topic
Author

Giving each webapp its own context root

David Sheltby
Ranch Hand

Joined: Oct 19, 2011
Posts: 34
Hi,

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:

href="/MyWebApp/css/styles.css"

I want to give each app it own root if possible so that I can instead reference like:

href="/css/styles.css"

I remember doing something similar many years back on a J2EE server, but cannot find a solution for Tomcat.

Thanks!
David Sheltby
Ranch Hand

Joined: Oct 19, 2011
Posts: 34
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.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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:
C:\Tomcat\web\

Then you would have the 'default' web application named ROOT:
C:\Tomcat\web\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:
C:\Tomcat\web\MyApp1\
C:\Tomcat\web\MyApp2\
etc...

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.


Steve
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Setting up the context for each web app is covered in detail in Tomcat's documentation.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Sheltby
Ranch Hand

Joined: Oct 19, 2011
Posts: 34
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?

thank you!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

Of course it's possible with Tomcat. Otherwise, how could multiple web apps be hosted on a single server?

Place the context files in Tomcat's conf/Catalina/hostname/ folder.

For example: conf/Catalina/localhost for the local host.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

On my localhost: http://localhost:8080/docs/appdev/deployment.html
David Sheltby
Ranch Hand

Joined: Oct 19, 2011
Posts: 34
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:
Catalina/localhost/app1
Catalina/localhost/app2
Catalina/localhost/app3

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.

Hope thats a little more clear
Thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

David Sheltby wrote: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.


And why is that? Surely what you said about using "/images/myimage.jpg" instead of "/app1/images/myimage.jpg" also applies to "/app2/images/myimage.jpg" and "/app3/images/myimage.jpg" and so on?
David Sheltby
Ranch Hand

Joined: Oct 19, 2011
Posts: 34
Yes it does. And I would want to address this for each of my webapps, not just one. So I'm not sure what your getting at.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

David Sheltby wrote:Yes it does. And I would want to address this for each of my webapps, not just one. So I'm not sure what your getting at.


That's what I just said. I'm not sure what you're getting at. So you're going to have to explain what you want to do.

(And pointing to what you already said isn't going to help, since what you already said is simply solved by using the root application for the shared resources.)
David Sheltby
Ranch Hand

Joined: Oct 19, 2011
Posts: 34
Can I make each application a root/default application?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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.
David Sheltby
Ranch Hand

Joined: Oct 19, 2011
Posts: 34
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.

thanks for the help
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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?
David Sheltby
Ranch Hand

Joined: Oct 19, 2011
Posts: 34
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??
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Oh, is that all this thread is about? Have a look at this FAQ entry: RelativeLinks.
David Sheltby
Ranch Hand

Joined: Oct 19, 2011
Posts: 34
Haha, took a while. I was hoping there was a way to configure tomcat to imply the context for each webapp, to avoid all the getContextPath code for each path.

thanks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

I've found that if you use the <c:url> tag from JSTL, it automatically inserts the context path for you. However that's just differently cumbersome.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

I believe that for raw page links, the HTML <BASE> tag can help set the relative path root for a page. IIRC, there's a Struts equivalent to that tag.

JSF tags mostly add the webapp context automatically, but I have taken to defining a global context-path variable for JSTL to reference in order to cover the cases where that's not sufficient.

Ideally, there'd be a core JSP variable in the J[2]EE standard for this, however.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Giving each webapp its own context root