package pw.scho.battleship.web;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.servlet.ServletContainer;

public class Main {

    public static void main(String[] args) throws Exception {
        int port = Integer.valueOf(System.getProperty("jetty.port"));

        System.out.println("Starting server on port " + port);

        Server server = new Server(port);

        HandlerList handlers = new HandlerList();

        handlers.setHandlers(new Handler[]{getStaticContentHandler(), getJerseyHandler(), new DefaultHandler()});

        server.setHandler(handlers);

        server.start();
        server.join();
    }

    private static Handler getStaticContentHandler() {
        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirectoriesListed(true);
        resourceHandler.setResourceBase("./src/main/webapp/");
        return resourceHandler;
    }

    private static Handler getJerseyHandler() {
        ServletHolder h = new ServletHolder(new ServletContainer());

        h.setInitParameter(ServerProperties.PROVIDER_PACKAGES, "pw.scho.battleship.web.resources");
        h.setInitParameter(ServerProperties.APPLICATION_NAME, "battleship");
        h.setInitOrder(1);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.addServlet(h, "/*");
        context.setContextPath("/api");
        return context;
    }

}
@@ -580,16 +580,47 @@
     int 
+port;%0A        try%7B%0A            
 port = I
@@ -668,16 +668,93 @@
 port%22));
+%0A        %7D catch(NumberFormatException e)%7B%0A            port = 8080;%0A        %7D
 %0A%0A      

Add default jetty port