9

Is it possible using embedded Jetty to serve static files from directory X but mapped to URL Y? I have static files stored under directory "web", but I want the URL be something like http://host/myapp.

I have already successfully ran a server configured with ResourceHandler in the following way:

ResourceHandler ctx = new ResourceHandler();
ctx.setResourceBase("path-to-web");
HandlerList list = new HandlerList();
list.addHandler(ctx);
...
server.setHandler(list);

But the result is serving the files under /web and not under the desired URL mapping.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Little Bobby Tables
  • 5,261
  • 2
  • 39
  • 49

2 Answers2

20

The ResourceHandler has no context configurable, but you can simply wrap it in a ContextHandler to achieve that.

Try this instead:

ContextHandler ctx = new ContextHandler("/my-files"); /* the server uri path */
ResourceHandler resHandler = new ResourceHandler();
resHandler.setResourceBase("path-to-web");
ctx.setHandler(resHandler);
server.setHandler(ctx);

That will serve /my-files as the ResourceHandler content of the filesystem path-to-web

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
2

The above doesn't work for Jetty 9, but this does:

ContextHandler contextHandler = new ContextHandler("/my-files");
contextHandler.setResourceBase("/tmp/static");

ResourceHandler resourceHandler = new ResourceHandler();
contextHandler.setHandler(resourceHandler);

server.setHandler(contextHandler);
  • this seems not working with jetty 9. Could you provide a link to some doc? – Ilya Buziuk Feb 05 '15 at 13:54
  • I have created a separate question for jetty 9 - http://stackoverflow.com/questions/28346438/resourcehandler-stop-hosting-files-with-jetty-9-404-not-found-error-works-fin Will be glad if you give me a hint – Ilya Buziuk Feb 05 '15 at 14:42