ResourceHandler停止托管文件与jetty 9 - 404找不到错误(与jetty 8一起正常工作)

时间:2015-02-05 14:24:03

标签: java eclipse jetty

显然,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();

对于码头9 - Serving static files w/ embedded Jetty

,这种对已接受答案的回答似乎不起作用

2 个答案:

答案 0 :(得分:3)

假设servletHandlerServletContextHandler

(注意:最好不要是实际的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);