Shortest code to start embedded Jetty server

前端 未结 5 1129
醉酒成梦
醉酒成梦 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:03

    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.

提交回复
热议问题