I am trying to create a simple demo application with embedded Jetty that serves static files from the "html" directory, which is a subdirectory of the current working directory. The idea is that a directory with a demo bank and content can be moved to a new location and still work.
I tried the following options, but I keep getting 404s.
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); context.getInitParams().put( "org.eclipse.jetty.servlet.Default.resourceBase", "html"); context.addServlet(new ServletHolder(new DefaultServlet()), "/html"); Server jetty = new Server(8080); jetty.setHandler(context); jetty.start();
Update: here is the solution described in the Jetty tutorial. As mentioned in the correct answer, it uses a ResourceHandler instead of a ServletContextHandler :
Server server = new Server(); SelectChannelConnector connector = new SelectChannelConnector(); connector.setPort(8080); server.addConnector(connector); ResourceHandler resource_handler = new ResourceHandler(); resource_handler.setDirectoriesListed(true); resource_handler.setWelcomeFiles(new String[]{ "index.html" }); resource_handler.setResourceBase("."); HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() }); server.setHandler(handlers); server.start(); server.join();
java jetty embedded-jetty
HolySamosa Apr 23 2018-12-12T00: 00Z
source share