2 //========================================================================
3 //Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4 //------------------------------------------------------------------------
5 //All rights reserved. This program and the accompanying materials
6 //are made available under the terms of the Eclipse Public License v1.0
7 //and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 //You may elect to redistribute this code under either of these licenses.
16 //========================================================================
20 import org.eclipse.jetty.server.Handler;
21 import org.eclipse.jetty.server.Server;
22 import org.eclipse.jetty.server.handler.DefaultHandler;
23 import org.eclipse.jetty.server.handler.HandlerList;
24 import org.eclipse.jetty.server.handler.ResourceHandler;
26 public class JettyExamplesDir
30 * Simple Jetty FileServer. This is a simple example of Jetty configured as a
34 public static void main(String[] args) throws Exception
36 // Create a basic Jetty server object that will listen on port 8080. Note
37 // that if you set this to port 0
38 // then a randomly available port will be assigned that you can either look
39 // in the logs for the port,
40 // or programmatically obtain it for use in test cases.
41 Server server = new Server(8080);
43 // Create the ResourceHandler. It is the object that will actually handle
44 // the request for a given file. It is
45 // a Jetty Handler object so it is suitable for chaining with other handlers
46 // as you will see in other examples.
47 ResourceHandler resource_handler = new ResourceHandler();
48 // Configure the ResourceHandler. Setting the resource base indicates where
49 // the files should be served out of.
50 // In this example it is the current directory but it can be configured to
51 // anything that the jvm has access to.
52 resource_handler.setDirectoriesListed(true);
53 resource_handler.setWelcomeFiles(new String[]
55 resource_handler.setResourceBase(".");
57 // Add the ResourceHandler to the server.
58 // GzipHandler gzip = new GzipHandler();
59 // server.setHandler(gzip);
60 HandlerList handlers = new HandlerList();
61 handlers.setHandlers(new Handler[]
62 { resource_handler, new DefaultHandler() });
63 server.setHandler(handlers);
65 // Start things up! By using the server.join() the server thread will join
66 // with the current thread.
68 // "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()"