显然,ResourceHandler
停止使用jetty 9托管文件 - 404 not found error (对于jetty 8可以正常工作)。这是代码:
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();
,这种对已接受答案的回答似乎不起作用
答案 0 :(得分:3)
假设servletHandler
是ServletContextHandler
(注意:最好不要是实际的ServletHandler
,因为这是一个内部类,并不是要直接实例化)
然后永远不会调用resourceHandler
,因为DefaultServlet
链末尾的Default404Servlet
处理(或ServletContextHandler
)将始终响应,而不允许{{ 1}}甚至执行。
如果您有resourceHandler
,请不要使用ServletContextHandler
使用ResourceHandler
中的DefaultServlet
来设置和提供静态文件。
答案 1 :(得分:3)
万一有人在寻找可行的示例,这就是我将ResourceHandler与ContextHandler结合的方法(部分基于当前的Jetty文档: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);