5

I apologize if this is a poor question, but I'm using Windows and looking to see if there's a way to run a webapp via Tomcat where the docBase is multiple folders.

A little more background is that we have our Eclipse project set up in a way that the web content is broken into separate folders. One folder represents what our SDK provides, one is a folder which has SDK patches, and a third is the project-specific components -- either of the last two folders could have subfolders/files that "erase" items from the first two.

I'd like to try and have my context file point back into my dev environment so that I don't need to build/deploy in order to see the changes I'm making. Is this possible?

A couple other notes:

  • We thought about using symlinks via mklink, but didn't want to create complexity if simplicity was out there.
  • This is just for running locally so simple hacks would be allowed. When we create the WAR which gets deployed to the real environment the ANT script creates a single web root.
Matt Felzani
  • 785
  • 10
  • 24

2 Answers2

3

I use it this way:

    <Context docBase="jquery"                path="/js/jquery" />
    <Context docBase="foobar/www/javascript" path="/js" />
    <Context docBase="foobar/www/css"        path="/css" />
    <Context docBase="foobar"                path="/" />

Important for the concrete context is the path-attribute. A request is processed from top to bottom.

So a request for /css/default.css is only processed from the 3rd context.

A different order may catches a different context.

This is wrong:

    <Context docBase="foobar"                path="/" />
    <Context docBase="jquery"                path="/js/jquery" />
    <Context docBase="foobar/www/javascript" path="/js" />
    <Context docBase="foobar/www/css"        path="/css" />

Because /css/default.css will be catched by the first context, not the fourth.

Edit 2013-08-10: (Not by the answer-author) It is important to note that while the above technique will work, much of it is actually incorrect. Please see comments for details.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
Grim
  • 1,938
  • 10
  • 56
  • 123
  • BTW, this Configuration may be part of the Project, so you may put it into `/META-INF/context.xml` – Grim Jul 22 '13 at 13:50
  • not to be a meatball, but for each i create does that mean i need to list my JNDI resources in each? – Matt Felzani Jul 22 '13 at 14:29
  • No, jndi-entries are for Webapps. Not every *context* must be a web-app. A valid *context* can be an empty folder. For the example above: `foobar/www/css`-folder has only one file: `standard.css` – Grim Jul 22 '13 at 14:33
  • We can join http://chat.stackoverflow.com/rooms/139/java if you already have questions. – Grim Jul 22 '13 at 14:43
  • thanks, that does help a lot. so, can i just do the following: ? i ask only because i don't really have a fine-grained need for the seek. basically if the file is in the first folder use it, otherwise look in the second, finally the third. – Matt Felzani Jul 22 '13 at 14:45
  • Hm ... I dont understand the question. a,b and c are served to the docbase "foobar". So they always have the same content. – Grim Jul 22 '13 at 14:52
  • ha, got docBase/path backwards, sorry. i get it now. so i have some work to do on my side to look at my project structure ... and to create Context entries to prescribe the pecking order of where to find the files. in the end though, i should be able to achieve what i'm after. – Matt Felzani Jul 22 '13 at 15:23
  • Ah Ok! There also is a (anti-)pattern to serve-cascade resources by **either `myapp/WEB-INF/classes/META-INF/resources/css/standard.css` or `myapp/WEB-INF/libs/*.jar!/META-INF/resources/css/standard.css` or `myapp/css/standard.css`** in `servlet-3.0` that may better fit your requirements. Anyway i prefer the usage of the `` solution because your Project is not depended of `servlet-3.0`. – Grim Jul 23 '13 at 08:14
  • 2
    Servlet spec says that longest-match wins when mapping URLs to web applications, so a request for `/css/whatever.css` should be processed by the webapp with a context path of `/css` rather than the ROOT context (which is incorrectly configured in this answer: path should be "" not "/"). – Christopher Schultz Jul 25 '13 at 14:35
  • Also, you should not be configuring `` elements in `server.xml`. Instead, use `META-INF/context.xml` files in each individual webapp. – Christopher Schultz Jul 25 '13 at 14:36
  • @ChristopherSchultz either the order or longest-match of ``, can you please evaluate? – Grim Jul 30 '13 at 21:32
  • Read Java Servlet Specification version 3.0, section 12.1, sentence two. – Christopher Schultz Aug 02 '13 at 15:57
3

Tomcat can do this for you, you just need a little extra configuration.

You are looking for VirtualDirContext which allows you to specify a list of extraResourcePaths which will be searched (in order) for additional files. You can use that to merge static resources, JSPs, directories of JAR files, etc.

Just remember that each path you add makes every file lookup potentially take longer -- especially if the file can't be found at all.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77