使用文档根设置嵌入式jetty

时间:2012-06-08 10:03:14

标签: java servlets webserver embedded-jetty

如果我在浏览器中输入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();

1 个答案:

答案 0 :(得分: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();
    }
}