【问题标题】:Shortest code to start embedded Jetty server启动嵌入式 Jetty 服务器的最短代码
【发布时间】:2010-11-04 13:14:20
【问题描述】:

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

到目前为止我的代码:

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);
        }

    }

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

【问题讨论】:

    标签: java jetty embedded-jetty


    【解决方案1】:

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

    您的示例如下所示:

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

    【讨论】:

      【解决方案2】:
              Server server = new Server(8080);
              Context root = new Context(server, "/");
              root.setResourceBase("./pom.xml");
              root.setHandler(new ResourceHandler());
              server.start();
      

      【讨论】:

        【解决方案3】:

        适用于 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();
            }
        }
        

        【讨论】:

          【解决方案4】:
          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);
              }
          }
          

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

          【讨论】:

            【解决方案5】:

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

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

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

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

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

            【讨论】:

            • 我在 OP 问题上没有看到 spring 标签。为什么要把一只重达 800 磅的大猩猩像春天一样扔在这么简单的东西上?!
            猜你喜欢
            • 2020-05-18
            • 1970-01-01
            • 1970-01-01
            • 2023-03-20
            • 1970-01-01
            • 2012-04-21
            • 2022-08-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多