View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.embedded;
20  
21  import org.eclipse.jetty.server.Handler;
22  import org.eclipse.jetty.server.Server;
23  import org.eclipse.jetty.server.handler.DefaultHandler;
24  import org.eclipse.jetty.server.handler.HandlerList;
25  import org.eclipse.jetty.server.handler.ResourceHandler;
26  
27  /* ------------------------------------------------------------ */
28  /** Simple Jetty FileServer.
29   * This is a simple example of Jetty configured as a FileServer.
30   */
31  public class FileServer
32  {
33      public static void main(String[] args) throws Exception
34      {
35          // Create a basic Jetty server object that will listen on port 8080.  Note that if you set this to port 0
36          // then a randomly available port will be assigned that you can either look in the logs for the port,
37          // or programmatically obtain it for use in test cases.
38          Server server = new Server(8080);
39  
40          // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
41          // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
42          ResourceHandler resource_handler = new ResourceHandler();
43          // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
44          // In this example it is the current directory but it can be configured to anything that the jvm has access to.
45          resource_handler.setDirectoriesListed(true);
46          resource_handler.setWelcomeFiles(new String[]{ "index.html" });
47          resource_handler.setResourceBase(".");
48  
49          // Add the ResourceHandler to the server.
50          HandlerList handlers = new HandlerList();
51          handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
52          server.setHandler(handlers);
53  
54          // Start things up! By using the server.join() the server thread will join with the current thread.
55          // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
56          server.start();
57          server.join();
58      }
59  
60  }