View Javadoc

1   //
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.
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  import org.eclipse.jetty.server.handler.gzip.GzipHandler;
27  
28  /** 
29   * Simple Jetty FileServer.
30   * This is a simple example of Jetty configured as a FileServer.
31   */
32  public class FileServer
33  {
34      public static void main(String[] args) throws Exception
35      {
36          // Create a basic Jetty server object that will listen on port 8080.  Note that if you set this to port 0
37          // then a randomly available port will be assigned that you can either look in the logs for the port,
38          // or programmatically obtain it for use in test cases.
39          Server server = new Server(8080);
40  
41          // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
42          // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
43          ResourceHandler resource_handler = new ResourceHandler();
44          // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
45          // In this example it is the current directory but it can be configured to anything that the jvm has access to.
46          resource_handler.setDirectoriesListed(true);
47          resource_handler.setWelcomeFiles(new String[]{ "index.html" });
48          resource_handler.setResourceBase(".");
49  
50          // Add the ResourceHandler to the server.
51          GzipHandler gzip = new GzipHandler();
52          server.setHandler(gzip);
53          HandlerList handlers = new HandlerList();
54          handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
55          gzip.setHandler(handlers);
56  
57          // Start things up! By using the server.join() the server thread will join with the current thread.
58          // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
59          server.start();
60          server.join();
61      }
62  }