2

如果我在浏览器中输入http://example.com/index.html,jetty应该在指定的目录中查找文件 index.html。

如何使用嵌入式码头实现这一目标?

这是我启动码头的代码:

InetSocketAddress socketAddress =InetSocketAddress.createUnresolved("0.0.0.0", 80);
Server server = new Server(socketAddress);
server.start();
server.join();
4

1 回答 1

4

在http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty找到了一个很好的描述

public class FileServer{

    public static void main(String[] args) throws Exception{

        Server server = new Server();
        SelectChannelConnector connector = new SelectChannelConnector();
        connector.setPort(8080);
        server.addConnector(connector);

        ResourceHandler resource_handler = new ResourceHandler();
        resource_handler.setDirectoriesListed(true);
        resource_handler.setWelcomeFiles(new String[]{ "index.html" });

        resource_handler.setResourceBase(".");

        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
        server.setHandler(handlers);

        server.start();
        server.join();
    }
}
于 2012-06-08T10:18:48.343 回答