4

I'm using the ExtJS framework which has a bulk of js and css files that are used for all apps. I intend to share these between a number of web applications (different war files).

For this reason I would like to serve ExtJS js and css directly from the web server, in my case Tomcat6, which can be used to serve static files, as in this helpful link.

Therefore I put my files under /var/lib/tomcat6/webapps/ROOT/extjs/. The static files that are directly under that directory are served correctly, e.g. /extjs/ext.js correctly serves the file at /var/lib/tomcat6/webapps/ROOT/extjs/ext.js.

However files in lower-level directories, for example /extjs/welcome/css/welcome.css, which should serve the file at /var/lib/tomcat6/webapps/ROOT/extjs/welcome/css/welcome.css, return a 404.

TL/DR

Tomcat serves static files only at top-level directory. A 404 is returned for files deeper in the hierarchy.

Config file contents:

FIXED - SOLUTION:

The issue was that I was placing the clause:

<Context docBase="/path/to/extjs-static-files" path="/extjs"/>

within Tomcat's server.xml <Engine> clause. Once I placed it within the <Host> tag it started working. Reading this doc was very helpful in identifying what I was doing wrong.

2 Answers2

1

I'm running a RESTful web service that serves some static content stored in a directory outside of Tomcat. The directory is specified by a servlet parameter in web.xml You could do something similar and just specify an absolute path to the files you need - place them in Tomcat somewhere or wherever road you like. I can post some sample code if you like. Of course all other files that reference the javascript and CSS would need to change because these files will be at a different URL directory than the web apps that use them. For instance, src="restAppContextName/serve/youJavaScript.js" where serve is a @Path for the REST web service and whatever follows is a @PathParam. To the browser, /serve will be treated like it's a directory, so a relative reference from one js file to another should work.

Thorn
  • 163
  • 1
  • 2
  • 9
  • Thank you for your help, I got around it in a different way (updated the question with the "solution". +1'ed your answer since it still is valuable feedback. – Joseph Victor Zammit Jun 28 '12 at 09:29
0

This is a very basic setup to give you better understanding of what-is-what:

server.xml

<Context docBase="appname" path="/appname" reloadable="true" source="org.eclipse.jst.jee.server:appname"/>

docBase is the application root folder (webapp)

web.xml

  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/res/*</url-pattern>
  </servlet-mapping>

res is just the URI distinguisher; useful if you have another one like /* pointing to a smarter servlet.

The default servlet serves statics from the root of the application. Basically, if the file test.js is in the folder js, that corresponds to the project's:

src/main/webapp/js/test.js

It can be now reached by the URL:

http://localhost:8080/appname/res/js/test.js