Version: 9.4.12.v20180830 |

Resource Handler
Previous | Chapter 18. Provided Servlets, Filters, and Handlers Home | Next

##### Contact the core Jetty developers at www.webtide.com

private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services for sponsored feature development

## Resource Handler

### Info

- Classname: `org.eclipse.jetty.server.handler.ResourceHandler`
- Maven Artifact: org.eclipse.jetty:jetty-server
- Javadoc: http://www.eclipse.org/jetty/javadoc/9.4.12.v20180830/org/eclipse/jetty/server/handler/ResourceHandler.html

### Usage

This handler will serve static content and handle If-Modified-Since headers and is suitable for simple serving of static content.

> ### Important

> There is no caching done with this handler, so if you are looking for a more fully featured way of serving static content look to the Default Servlet.

> ### Note

> Requests for resources that do not exist are let pass (Eg no 404’s).

### Improving the Look and Feel

The resource handler has a default stylesheet which you can change by calling `setStyleSheet(String location)` with the location of a file on the system that it can locate through the resource loading system. The default css is called `jetty-dir.css` and is located in the `jetty-util` package, pulled as a classpath resource from the `jetty-util` jar when requested through the `ResourceHandler`.

### Embedded Example

The following is an example of a split fileserver, able to serve static content from multiple directory locations. Since this handler does not return 404’s on content you are able to iteratively try multiple resource handlers to resolve content.

    //
    //  ========================================================================
    //  Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd.
    //  ------------------------------------------------------------------------
    //  All rights reserved. This program and the accompanying materials
    //  are made available under the terms of the Eclipse Public License v1.0
    //  and Apache License v2.0 which accompanies this distribution.
    //
    //      The Eclipse Public License is available at
    //      http://www.eclipse.org/legal/epl-v10.html
    //
    //      The Apache License v2.0 is available at
    //      http://www.opensource.org/licenses/apache2.0.php
    //
    //  You may elect to redistribute this code under either of these licenses.
    //  ========================================================================
    //

    package org.eclipse.jetty.embedded;

    import java.io.File;

    import org.eclipse.jetty.server.Connector;
    import org.eclipse.jetty.server.Handler;
    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.server.ServerConnector;
    import org.eclipse.jetty.server.handler.ContextHandler;
    import org.eclipse.jetty.server.handler.ContextHandlerCollection;
    import org.eclipse.jetty.server.handler.ResourceHandler;
    import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
    import org.eclipse.jetty.util.resource.Resource;

    /**
     * A {@link ContextHandlerCollection} handler may be used to direct a request to
     * a specific Context. The URI path prefix and optional virtual host is used to
     * select the context.
     */
    public class SplitFileServer
    {
        public static void main( String[] args ) throws Exception
        {
            // Create the Server object and a corresponding ServerConnector and then
            // set the port for the connector. In this example the server will
            // listen on port 8090. If you set this to port 0 then when the server
            // has been started you can called connector.getLocalPort() to
            // programmatically get the port the server started on.
            Server server = new Server();
            ServerConnector connector = new ServerConnector(server);
            connector.setPort(8090);
            server.setConnectors(new Connector[] { connector });

            // Create a Context Handler and ResourceHandler. The ContextHandler is
            // getting set to "/" path but this could be anything you like for
            // builing out your url. Note how we are setting the ResourceBase using
            // our jetty maven testing utilities to get the proper resource
            // directory, you needn't use these, you simply need to supply the paths
            // you are looking to serve content from.
            ResourceHandler rh0 = new ResourceHandler();

            ContextHandler context0 = new ContextHandler();
            context0.setContextPath("/");
            File dir0 = MavenTestingUtils.getTestResourceDir("dir0");
            context0.setBaseResource(Resource.newResource(dir0));
            context0.setHandler(rh0);

            // Rinse and repeat the previous item, only specifying a different
            // resource base.
            ResourceHandler rh1 = new ResourceHandler();

            ContextHandler context1 = new ContextHandler();
            context1.setContextPath("/");
            File dir1 = MavenTestingUtils.getTestResourceDir("dir1");
            context1.setBaseResource(Resource.newResource(dir1));
            context1.setHandler(rh1);

            // Create a ContextHandlerCollection and set the context handlers to it.
            // This will let jetty process urls against the declared contexts in
            // order to match up content.
            ContextHandlerCollection contexts = new ContextHandlerCollection();
            contexts.setHandlers(new Handler[] { context0, context1 });

            server.setHandler(contexts);

            // Start things up!
            server.start();

            // Dump the server state
            System.out.println(server.dump());

            // The use of server.join() the will make the current thread join and
            // wait until the server is done executing.
            // See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join()
            server.join();
        }
    }

Previous | Top | Next
Cross Origin Filter | Home | Debug Handler

See an error or something missing? Contribute to this documentation at Github!(Generated: 2018-08-30)