zoukankan      html  css  js  c++  java
  • Jetty 开发指南:嵌入式开发示例

    Jetty具有嵌入各种应用程序的丰富历史。 在本节中,我们将向您介绍我们的git存储库中的embedded-jetty-examples项目下的一些简单示例。

    重要:生成此文档时,将直接从我们的git存储库中提取这些文件。 如果行号不对齐,请随时在github中修复此文档,并向我们提供拉取请求,或者至少打开一个问题以通知我们该差异。

    简单的文件服务器

    此示例显示如何在Jetty中创建简单文件服务器。 它非常适合需要实际Web服务器来获取文件的测试用例,它可以很容易地配置为从src / test / resources下的目录提供文件。 请注意,这在文件缓存中没有任何逻辑,无论是在服务器内还是在响应上设置适当的标头。 它只是几行,说明了提供一些文件是多么容易。

    //
    //  ========================================================================
    //  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 org.eclipse.jetty.server.Handler;
    import org.eclipse.jetty.server.Server;
    import org.eclipse.jetty.server.handler.DefaultHandler;
    import org.eclipse.jetty.server.handler.HandlerList;
    import org.eclipse.jetty.server.handler.ResourceHandler;
    
    /**
     * Simple Jetty FileServer.
     * This is a simple example of Jetty configured as a FileServer.
     */
    public class FileServer
    {
        public static void main(String[] args) throws Exception
        {
            // Create a basic Jetty server object that will listen on port 8080.  Note that if you set this to port 0
            // then a randomly available port will be assigned that you can either look in the logs for the port,
            // or programmatically obtain it for use in test cases.
            Server server = new Server(8080);
    
            // Create the ResourceHandler. It is the object that will actually handle the request for a given file. It is
            // a Jetty Handler object so it is suitable for chaining with other handlers as you will see in other examples.
            ResourceHandler resource_handler = new ResourceHandler();
    
            // Configure the ResourceHandler. Setting the resource base indicates where the files should be served out of.
            // In this example it is the current directory but it can be configured to anything that the jvm has access to.
            resource_handler.setDirectoriesListed(true);
            resource_handler.setWelcomeFiles(new String[]{ "index.html" });
            resource_handler.setResourceBase(".");
    
            // Add the ResourceHandler to the server.
            HandlerList handlers = new HandlerList();
            handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
            server.setHandler(handlers);
    
            // Start things up! By using the server.join() the server thread will join with the current thread.
            // See "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()" for more details.
            server.start();
            server.join();
        }
    }

    启动后你应该可以导航到http:// localhost:8080 / index.html(假设一个在资源库目录中)。

    Maven坐标

    要在项目中使用此示例,您需要声明以下Maven依赖项。

    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>${project.version}</version>
    </dependency>

    Split File Server

    此示例构建在简单文件服务器上,以显示如何将多个ResourceHandler链接在一起可以聚合多个目录以在单个路径上提供内容,以及如何将这些目录与ContextHandler链接在一起。

    //
    //  ========================================================================
    //  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();
        }
    }

    启动后,你应该可以导航到http:// localhost:8090 / index.html(假设一个在资源库目录中),你可以正常访问。 任何文件请求都将在第一个资源处理程序中查找,然后在第二个资源处理程序中查找,依此类推。

    Maven Coordinates

    要在项目中使用此示例,您需要声明以下maven依赖项。 我们建议您不要在实际应用程序中使用工具链依赖项。

    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty.toolchain</groupId>
      <artifactId>jetty-test-helper</artifactId>
      <version>2.2</version>
    </dependency>

    参考资料:http://www.eclipse.org/jetty/documentation/9.4.x/embedded-examples.html

  • 相关阅读:
    2019 蓝桥杯国赛 B 组模拟赛 D. 程序设计:公约数
    2019长安大学ACM校赛网络同步赛 M LCM (数论)
    CCPC-Wannafly Winter Camp Day1 (Div2, onsite)
    CCPC-Wannafly Winter Camp Day1 流流流动 (树形dp)
    CCPC-Wannafly Winter Camp Day1 (Div2) 吃豆豆 (DP)
    CCPC-Wannafly Winter Camp Day1 (Div2, onsite) 夺宝奇兵
    CCPC-Wannafly Winter Camp Day1 爬爬爬山 (最短路)
    Comet OJ
    Comet OJ
    hdu-Danganronpa(AC自动机)
  • 原文地址:https://www.cnblogs.com/mymelody/p/9668803.html
Copyright © 2011-2022 走看看