[prev in list] [next in list] [prev in thread] [next in thread] 

List:       tomcat-user
Subject:    Re: Tomcat 7 - Organizing web applications into sub directories
From:       André_Warnier <aw () ice-sa ! com>
Date:       2015-08-26 14:26:18
Message-ID: 55DDCC8A.10202 () ice-sa ! com
[Download RAW message or body]

On 26.08.2015 06:06, Srikanth Challa wrote:
> I am trying to organize my applications (multiple) into a specific
> hierarchy under the webapps folder.
> Something like this -
> webapps
>       dev
>            app1
>            app2
>       test
>            app1
>            app3
>
> When deploying (without WAR), I am getting a 404 error for servlets. Tried
> changing the web.xml servlet mapping, still no luck. It works perfectly
> when the folder is moved directly under webapps like below -
> webapps
>       app1
>
> Does tomcat have a limitation on organizing webapps under multiple levels
> of folders (under the webapp directory)?
>

Hi.
Without getting too technical :
It is not a limitation of Tomcat.  There /are/ ways of doing what you indicate above.  But 
the problem is that if you do that, you are going against the "natural" way in which URLs 
are mapped to web-applications, and that will force you further down the line, to do ever 
more complicated things to keep this working correctly (for example, if you want to easily 
move an application between the "dev" and the "test" areas above).

To map URLs to web-applications, Tomcat is following the basic principles outlined in the 
Servlet Specification 3.0, for example this :

-- quote --
10.5 Directory Structure
A Web application exists as a structured hierarchy of directories. The root of this
hierarchy serves as the document root for files that are part of the application. For
example, for a Web application with the context path /catalog in a Web container,
the index.html file at the base of the Web application hierarchy or in a JAR file
inside WEB-INF/lib that includes the index.html under META-INF/resources
directory can be served to satisfy a request from /catalog/index.html.
-- unquote --

(re: http://tomcat.apache.org/tomcat-8.0-doc/appdev/deployment.html)

For Tomcat, the "root" for all the applications within a specified <Host>, is the 
directory which is indicated by the "appBase" attribute of the corresponding <Host> tag.
Like this :
       <Host name="localhost"  appBase="(/somepath/)webapps"
             unpackWARs="true" autoDeploy="true">

and then under "(/somepath/)webapps/" you would have something like this :

(/somepath/)webapps/
        app1
          app1-sub1
          app1-sub2
        app2
          app2-sub1
          app2-sub2
etc..

This makes it clear to Tomcat that "app1" and "app2" are the distinct web-applications 
(also known as "context"), corresponding respectively to URLs such as :
   http://yourhost:port/app1
   http://yourhost:port/app2
and that the subdirectories "app1-sub1", "app1-sub2" etc.. are internal sub-divisions of 
these "app1" and "app2" web-applications, helping to map longer URLs to "things" inside 
these application (such as servlets, JSP pages, HTML pages etc.) (these further 
sub-mappings being described in the web.xml file of each web-application).

If you want to go against this "natural" interpretation of the directory structure by 
Tomcat, then you have to start telling Tomcat (in various places), that "app1/app1-sub1" 
is one application, and "app1/app1-sub2" is a different application etc.., which 
complicates things for you (for example, you'd have to name a WAR file like 
"app1#app1-sub1.war"). (And also, since it is not the "natural way", it will confuse 
orther people).

A more practical way of achieving what you want, would probably be to define 2 distinct 
<Host>'s, like this (in server.xml) :

  <Host name="mydevhost" appBase="(/somepath/)webapps-dev"
unpackWARs="true" autoDeploy="true">
...
</Host>

  <Host name="mytesthost" appBase="(/somepath/)webapps-test"
unpackWARs="true" autoDeploy="true">
...
</Host>

(and of course, both "mydevhost" and "mytesthost" map to the same IP address (in DNS)).

and then have a directory structure like this :

webapps-dev/
    app1
    app2

webapps-test/
    app1
    app2
    app3

corresponding to URLs like :
  http://mydevhost:port/app1  (maps to /somepath/webapps-dev/app1)
  http://mytesthost:port/app1 (maps to /somepath/webapps-test/app1)
  etc..

This way, the internal configuration and content of "app1" can be exactly the same for 
"dev" and "test", and you can move an application between the 2 Hosts (or anywhwere else, 
such as to another machine) without having to make any change at all inside the 
application or its configuration.



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic