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