java 启动嵌入式 Jetty 服务器的最短代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1021971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:50:23  来源:igfitidea点击:

Shortest code to start embedded Jetty server

javajettyembedded-jetty

提问by PeterMmm

I'm writing some example code where an embedded Jetty server is started. The server must load exactly one servlet, send all requests to the servlet and listen on localhost:80

我正在编写一些示例代码,其中启动了嵌入式 Jetty 服务器。服务器必须只加载一个 servlet,将所有请求发送到 servlet 并侦听 localhost:80

My code so far:

到目前为止我的代码:

static void startJetty() {
        try {
            Server server = new Server();

            Connector con = new SelectChannelConnector();
            con.setPort(80);
            server.addConnector(con);

            Context context = new Context(server, "/", Context.SESSIONS);
            ServletHolder holder = new ServletHolder(new MyApp());
            context.addServlet(holder, "/*");

            server.start();
        } catch (Exception ex) {
            System.err.println(ex);
        }

    }

Can i do the same with less code/lines ? (Jetty 6.1.0 used).

我可以用更少的代码/行做同样的事情吗?(使用 Jetty 6.1.0)。

回答by workmad3

static void startJetty() {
    try {
        Server server = new Server();
        Connector con = new SelectChannelConnector();
        con.setPort(80);
        server.addConnector(con);
        Context context = new Context(server, "/", Context.SESSIONS);
        context.addServlet(new ServletHolder(new MyApp()), "/*");
        server.start();
    } catch (Exception ex) {
        System.err.println(ex);
    }
}

Removed unnecessary whitespace and moved ServletHolder creation inline. That's removed 5 lines.

删除了不必要的空格并内联移动了 ServletHolder 创建。删除了 5 行。

回答by Jon

You could configure Jetty declaratively in a Spring applicationcontext.xml, e.g:

您可以在 Spring applicationcontext.xml 中以声明方式配置 Jetty,例如:

http://roopindersingh.com/2008/12/10/spring-and-jetty-integration/

http://roopindersingh.com/2008/12/10/spring-and-jetty-integration/

then simply retrieve the server bean from the applicationcontext.xml and call start... I believe that makes it one line of code then... :)

然后简单地从 applicationcontext.xml 中检索服务器 bean 并调用 start...我相信这使它成为一行代码然后...:)

((Server)appContext.getBean("jettyServer")).start();

It's useful for integration tests involving Jetty.

它对于涉及 Jetty 的集成测试很有用。

回答by Janus Troelsen

Works with Jetty 8:

适用于 Jetty 8:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class Main {
    public static void main(String[] args) throws Exception {
            Server server = new Server(8080);
            WebAppContext handler = new WebAppContext();
            handler.setResourceBase("/");
            handler.setContextPath("/");
            handler.addServlet(new ServletHolder(new MyApp()), "/*");
            server.setHandler(handler);
            server.start();
    }
}

回答by Renato

I've written a library, EasyJetty, that makes it MUCH easier to embed Jetty. It is just a thin layer above the Jetty API, really light-weight.

我编写了一个库EasyJetty,它使嵌入 Jetty 变得更加容易。它只是 Jetty API 之上的一个薄层,真的很轻量级。

Your example would look like this:

您的示例如下所示:

import com.athaydes.easyjetty.EasyJetty;

public class Sample {

    public static void main(String[] args) {
        new EasyJetty().port(80).servlet("/*", MyApp.class).start();
    }

}

回答by sendon1982

        Server server = new Server(8080);
        Context root = new Context(server, "/");
        root.setResourceBase("./pom.xml");
        root.setHandler(new ResourceHandler());
        server.start();