3

I have a WAR (being served through Embedded Jetty) that I'd like to include static files outside the WAR. My goals are:

  • Serve static files from a path outside the WAR, relative to the directory the Embedded Jetty is running. So, if I invoke the jar in /srv/app, I'd like to configure a URL of http://myapp.com/static/js/my.js to serve /src/app/public/js/my.js.
  • Secure: No directory listings or ../ path escapes
  • Not reinvent the wheel: I don't want to write a new Servlet, but rather harness existing capabilities
  • Ideally (not required): Shadow the static content in the WAR, so that if I have a /src/app/public/index.html, that file will be served, and, if not existent, the WAR's index.html will be served.

Here's what I examined, and where I'm stuck:

  1. DefaultServlet: Seems ideal, but, I can't find any way to have it serve files outside the WAR. Is there a way for the DefaultServlet to serve static content outside the WAR, with paths relative to the app's pwd?
  2. Configuring Jetty, as described in https://www.eclipse.org/jetty/documentation/current/static-content-deployment.html ; this would seem to be ideal, but, when copied in, didn't seem to do anything (it was a NOOP).
  3. ResourceHandler: Is this the right way to do it? I wasn't able to figure out how to use this to make my goals
SRobertJames
  • 8,210
  • 14
  • 60
  • 107

1 Answers1

0

ResourceHandler: Is this the right way to do it? I wasn't able to figure out how to use this to make my goals

Don't use ResourceHandler, its inferior to DefaultServlet.

Configuring Jetty, as described in https://www.eclipse.org/jetty/documentation/current/static-content-deployment.html ; this would seem to be ideal, but, when copied in, didn't seem to do anything (it was a NOOP).

This only works if you don't share the same contextPath as your deployed webapp.

Is there a way for the DefaultServlet to serve static content outside the WAR, with paths relative to the app's pwd?

Yes, and this has been documented on stackoverflow in a few different ways. Key is, you'll use extra DefaultServlet entries, defined with their own "Resource Base Path" (which has to be a fully qualified path entry, no relative paths), on different url-patterns.

See:

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • Can you elaborate how this should be done? I'm new to Jetty and Servlets, and would like to use your answer to guide my implementation. – SRobertJames May 19 '19 at 15:59