Apparently, ResourceHandler
stop hosting files with jetty 9 - 404 not found error (works fine with jetty 8). Here is the code:
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setDirectoriesListed(true);
resourceHandler.setResourceBase("some_resource_base");
HandlerList handlerList = new HandlerList();
handlerList.setHandlers(new Handler[]{servletHandler, resourceHandler});
server.setHandler(handlerList);
server.start();
This quistion with the accepted answer does not seem to work against jetty 9 - Serving static files w/ embedded Jetty
Assuming that servletHandler
is a ServletContextHandler
(Note: it best not be an actual ServletHandler
, as that's an internal class, not meant to be instantiated directly)
Then the resourceHandler
will never be called, as the DefaultServlet
processing (or Default404Servlet
) at the end of the ServletContextHandler
chain will always respond, not allowing resourceHandler
to even execute.
If you have a ServletContextHandler
, do not use ResourceHandler
use the DefaultServlet
in that ServletContextHandler
to setup and serve your static files.
In case somebody is looking for a working example, this is how I combined a ResourceHandler with a ContextHandler (partially based on current Jetty docs: Jetty documentation )
srv = new Server();
ServerConnector srvConn = new ServerConnector(srv);
srvConn.setHost("localhost");
srvConn.setPort(8080);
srvConn.setIdleTimeout(30000);
srv.addConnector(srvConn);
//used for webSocket comm later:
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
//for static content:
ResourceHandler resH = new ResourceHandler();
resH.setDirectoriesListed(true);
resH.setWelcomeFiles(new String[]{ "index.html" });
resH.setResourceBase("./my/web/root");
ContextHandler resCtx = new ContextHandler();
resCtx.setHandler(resH);
//Add both ContextHandlers to server:
ContextHandlerCollection handlers = new ContextHandlerCollection(resCtx, context);
srv.setHandler(handlers);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.