Shortest code to start embedded Jetty server

前端 未结 5 1213
醉酒成梦
醉酒成梦 2021-01-30 15:30

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

5条回答
  •  别跟我提以往
    2021-01-30 16:04

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

提交回复
热议问题