It's not a secret anymore!
The moose likes Tomcat and the fly likes Make root directory an application folder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Make root directory an application folder" Watch "Make root directory an application folder" New topic

Make root directory an application folder

Rob Berrones

Joined: Jun 21, 2010
Posts: 2
Hi all,
Here's the low-down:

I inherited a beast of an application. I am trying to make it more manageable, but in order to do that, I need to break it apart.

In my C:\Tomcat 6.0\webapps\MyApp project, I need to do some absolute references due to folder jumping and global includes. (long story)

I have a file in C:\Tomcat 6.0\webapps\MyApp\jsp\example\filename.jsp an example link in this file would be

/includes/headers/header.jsp. (which makes the path: C:\Tomcat 6.0\webapps\includes\headers\header.jsp)

I need the path to be:
C:\Tomcat 6.0\webapps\MyApp\includes\headers\header.jsp

In other words, any files in the MyApp folder need to have MyApp be its root, not webapps. I've tried everything, and nothing has worked. please help me.

I know this probably isn't explained very well, but I'm new to this and don't really know what I'm doing.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

Welcome to the JavaRanch, Rob. I sing my old song, for your new ears:

A file server is not a web server!

A J2EE webapplication server is a service that when presented with a URL and related information will digest it, (usually) take some action, then return a response. In certain cases, that response will be to search a web application's resources and copy one back to the client, but that's not the same thing as being a file server. For example, you can't use an OS "fopen" call on a web server the same way you can with a fileserver's shared files and directories.

Each web application has a URL context. That's the part of the URL that allows Tomcat to determine which of the webapps deployed in the tomcat server to send the URL request to. There's a default context (the root context), located at "/", which is what you'll invoke if you don't specify a more specific context (such as /myapp).

webapp resource references are done relative to the webapp's WAR root. So instead of C:\Tomcat 6.0\webapps\MyApp\jsp\example\filename.jsp, what you'd actually specify is a path of /jsp/example/filename.jsp. Just as a side note, It's better to use forward slashes in pathnames under Java, and doubly so in URLs, Java or otherwise.

Now the actual Uniform Resource Locator (URL) is made up of the following parts:

protocol (http/https)
server/domain name (//
port (80, 8080, 443, etc.)
context (/MyApp)
resource (/jsp/example/filename.jsp)

String them all together and you get:

And because it's a JSP, Tomcat won't actually copy the resource to the client, but will instead compile it into java code, execute the java code, and return the output.

However, there's one real annoyance. URLs can be absolute or relative, just like file paths. Except that an absolute URL is absolute relative to the server, not to the webapp. Meaning that a URL of "http:/filename.jsp" would be passed to the default webapp, not to the webapp that the reference came from.

There's an HTML tag that can be used to select an alternate URL base for relative URL references on a returned page. Not all J2EE environments have a place for it, though.

The other alternative is to encode the JSP URL with dynamic content that gets the application context and inserts it into the "absolute" URL so that the returned URL is "http:/MyApp/filename.jsp".

An IDE is no substitute for an Intelligent Developer.
Rob Berrones

Joined: Jun 21, 2010
Posts: 2
Thanks for the quick reply. I'm a front end guy, so I'm familiar with how it all works, I just needed to know if I was missing a server configuration somewhere. The solutions posed by you won't work for this particular project. They left me in a cluster. Thanks again for the help.
I agree. Here's the link:
subject: Make root directory an application folder
It's not a secret anymore!