14

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

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).

PeterMmm
  • 24,152
  • 13
  • 73
  • 111

5 Answers5

14
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.

javanna
  • 59,145
  • 14
  • 144
  • 125
workmad3
  • 25,101
  • 4
  • 35
  • 56
3

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

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... :)

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

It's useful for integration tests involving Jetty.

Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150
  • 1
    I don't see the spring tag on the OP question. Why throw a 800 pound gorilla like spring at something so simple?! – philk Apr 17 '20 at 07:07
2

Works with 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();
    }
}
Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
2

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.

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

}
Renato
  • 12,940
  • 3
  • 54
  • 85
1
        Server server = new Server(8080);
        Context root = new Context(server, "/");
        root.setResourceBase("./pom.xml");
        root.setHandler(new ResourceHandler());
        server.start();
sendon1982
  • 9,982
  • 61
  • 44