*
The moose likes Tomcat and the fly likes Configuring webapp to use subdirectories Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Configuring webapp to use subdirectories" Watch "Configuring webapp to use subdirectories" New topic
Author

Configuring webapp to use subdirectories

Derek Murphy
Greenhorn

Joined: Mar 26, 2010
Posts: 25
Hi,
I've been trying to figure out how to do this for a while, and it just doesn't make sense. I'm a novice with tomcat/java so bear with me if the question is silly.

I have an app called infocenter.war. Currently, I deploy it within $tomcat_home/webapps/ what i'd like to do, is deploy it(only) in a subdirectory.. so I'll have the following.

$tomcat_home/webapps/app1.war
$tomcat_home/webapps/app1/META-INF
$tomcat_home/webapps/app1/WEB-INF
$tomcat_home/webapps/subdirectory/infocenter.war
$tomcat_home/webapps/subdirectory/infocenter/META-INF
$tomcat_home/webapps/subdirectory/infocenter/WEB-INF
$tomcat_home/webapps/app2.war
$tomcat_home/webapps/app2/META-INF
$tomcat_home/webapps/app2/WEB-INF


I'll have apache in front of this, proxying requests to all the applications. Right now my relevant apache rules/config are as follows

RewriteRule ^/old/infocenter/?$ http://www.newsite.com/infocenter/index.jsp [R=301,L]
RewriteRule ^/infocenter/index.jsp http://www.newsite.com/infocenter/index.jsp [R=301,L]

JkMount /infocenter worker2
JkMount /infocenter/* worker2
JkMount /app1 worker2
JkMount /app1/* worker2
JkMount /app2 worker2
JkMount /app2/* worker2

The rewrite rules will have to change to something along the lines of...
RewriteRule ^/old/infocenter/?$ http://www.newsite.com/subdirectory/infocenter/index.jsp [R=301,L]
RewriteRule ^/infocenter/index.jsp http://www.newsite.com/subdirectory/infocenter/index.jsp [R=301,L]

What do I need to do to achieve this? How do my JkMounts change, and what has to be done to tomcat to achieve this?

Running apache 2.0.63 and Tomcat 6.0.18.

a real world example of what I want to achieve is: http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/index.jsp

Thanks in advance!!
Cheers,
-Derek
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60764
    
  65

Why the added complexity of fronting with Apache?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Derek Murphy
Greenhorn

Joined: Mar 26, 2010
Posts: 25
The tomcat apps actually don't live on the same server as the webserver.

So.. I have webservers (dmz) -> appservers (internal).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60764
    
  65

That didn't really answer my question. Why the 2-level setup in the first place? Is there something like PHP you need from Apache that Tomcat can't serve?
Derek Murphy
Greenhorn

Joined: Mar 26, 2010
Posts: 25
The webservers are generic webservers that host a bunch of different urls. This is one vHost in question. Our standard architecture is webservers = dmz, appservers and db servers = internal. That way the webserver is the only one exposed, and it only talks to the appserver.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60764
    
  65

Sounds like an over-complcation to me, but data center isn't my specialty,

In any case, it you want to put the war files under Tomcat's webapp folder to have them automatically deployed, they need to go directly in that folder.

If you want them somewhere else in the folder structure, you can set up the document base in the context XML to point to anywhere you want.
Derek Murphy
Greenhorn

Joined: Mar 26, 2010
Posts: 25
Bear Bibeault wrote:
If you want them somewhere else in the folder structure, you can set up the document base in the context XML to point to anywhere you want.


This is my question. I don't know exactly how to set that up in context xml.

Unless what you're saying is... it wont automatically unpack if it's not directly under webapps and I'd have to unpack it myself?

Does the below look correct? In apache would my JkMount be JkMount /subdirectory/infocenter/* worker2


< Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false" >
< Context path="/subdirectory/infocenter" docBase="webapps/subdirectory/infocenter" / >
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60764
    
  65

I prefer to use standalone context files rather than placing them in server.xml.
Derek Murphy
Greenhorn

Joined: Mar 26, 2010
Posts: 25
Bear Bibeault wrote:I prefer to use standalone context files rather than placing them in server.xml.


I've never really used context files, period. How does a standalone work? Do I need to read it into server.xml? What is the format of the file? Does it just contain the 1 context line that I had in my previous post?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60764
    
  65

The Tomcat docs cover all this.

The context file contains just the Context element (can span more than one line) and goes in the hierarchy under conf/Catalina.

That way, you can make changes without touching the server.xml and forcing a server restart.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

I'm reading this fast and sloppy, so forgive me if I miss something, but a couple of things jumped out at me.

First of all, it's been a LONG time since putting a Context into server.xml was considered a good practice. Tomcat 3, to be precise, and one of the major bones I have to pick with the Eclipse WTP package is that they DO put Context in server.xml even though Tomcat3 was end-of-lifed something like 5 years ago.

Context files are treated as though their contents were included into server.xml. However, you can update a context file and just that one webapp will reload. When you put the context directly into server.xml, the entire Tomcat server requires a restart. Also, if you like to use the Tomcat management webapps, they're going to create/modify context files, so you could have some serious grief if they collide wit a webapp Context element in server.xml.

That's point #1.

Point #2 is the definition of webapp context itself. As I say rather often, a WEB server is NOT a FILE server. webapps are (according to the spec) WAR files, which are single files in an enhanced ZIP format. Yes, ZIP files have an internal directory structure that looks like a filesystem directory structure. And yes, recent OS filesystems make it LOOK a lot like they are directly accessing files within that directory structure. And yes, Tomcat supports an "exploded WAR", where the file is actually unzipped and placed as files and directories under the TOMCAT_HOME/webapps direcotry. But they're faking it. And URLs are only mapped to filesystem paths when the architecture has nothing better to do with them.

In particular, regarding TOMCAT_HOME/webapps, each WAR is anchored directly under the webapps directory. No subdirectories, no WARs-within-WARs. That's not how the J2EE spec works.

But, as I said before, a URL is not a filesystem path, no matter how much it may resemble one. And Apache can do some very interesting transformations of URLs using its rewriting and proxying modules. The upshot of it is that you can't do exactly what you're proposing, but you can still provide the URL structure you'd like to present.


Customer surveys are for companies who didn't pay proper attention to begin with.
Derek Murphy
Greenhorn

Joined: Mar 26, 2010
Posts: 25
Tim Holloway wrote:
But, as I said before, a URL is not a filesystem path, no matter how much it may resemble one. And Apache can do some very interesting transformations of URLs using its rewriting and proxying modules. The upshot of it is that you can't do exactly what you're proposing, but you can still provide the URL structure you'd like to present.


I think you got everything. Your last point was more or less what I was trying to verify. I didn't think I could do exactly what I wanted with tomcat (I can with jboss and other appservers) so I understand all that.

So I guess my only real course of action is something along these lines, and I'll just have to make sure apache doesn't actually change my url. Correct?

Rewrite Big/Long/Url/To/App shortappname/

jkmount shortappname/ worker1

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

I'd actually have to print it out and draw circles-and-arrows to be 100% certain, but it looks like you're on the right track.

There are ways to get Tomcat to rewrite URLs as well, but if you're dealing with Apache being a multi-platform public face to your app suite, it's easier to let Apache do the heavy lifting.
Derek Murphy
Greenhorn

Joined: Mar 26, 2010
Posts: 25
yep, I looked into tomcat rewrites before and they didn't seem like a simple solution.

I kind of already have the rewrite /big/long/url shorturl/ thing in place.. except currently it changes the URL. I just have to fix that.

Thank you very much for your help.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15951
    
  19

Just remember that the only part of a URL in a J2EE webapp that's "real" is the part that follows the http://servername:port/contextname. The only time the front-end (routing) part of the URL matters is when you're pushing links back to the client and they need the whole path.
Mattias Axelsson
Greenhorn

Joined: Feb 12, 2013
Posts: 1
If you name your WAR file "subdirectory#infocenter.war", it will be unpacked as "subdirectory#infocenter" and accessible as http://www.example.com/subdirectory/infocenter.
 
Consider Paul's rocket mass heater.
 
subject: Configuring webapp to use subdirectories
 
Similar Threads
creating website basic question
passing session values between two web application
Problem with referencing session information from 2 contexts, one a subfolder of another
Help with Struts
getting set up apache/tomcat