Download:
jetty-distribution-9.4.11.v20180605.zip
jetty-http-9.4.11.v20180605.jar
jetty-io-9.4.11.v20180605.jar
jetty-server-9.4.11.v20180605.jar
jetty-servlet-9.4.11.v20180605.jar
jetty-util-9.4.11.v20180605.jar
servlet-api-3.1.jar
1. Simple HTTP File Server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | package 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; public class HttpServer { Server server; public HttpServer() throws Exception { } void stop() throws Exception { server.stop(); } void startDirList() throws Exception { server = new Server(8070); ResourceHandler resource_handler = new ResourceHandler(); resource_handler.setDirectoriesListed(true); resource_handler.setWelcomeFiles(new String[]{ "index.html" }); resource_handler.setResourceBase("D:\\"); HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() }); server.setHandler(handlers); server.start(); server.join(); } public static void testDirList() throws Exception { HttpServer http = new HttpServer(); new Thread( () -> { for( int i = 0; i < 10; i++ ) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } try { http.stop(); } catch (Exception e) { e.printStackTrace(); } }).start(); http.startDirList(); } public static void main( String[] args ) throws Exception { testDirList(); } } |
2. HTTP Server with Handler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletHandler; public class HttpServer { Server server; public HttpServer() throws Exception { } void stop() throws Exception { server.stop(); } @SuppressWarnings("serial") public static class HelloServlet extends HttpServlet { @Override protected void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println("<h1>Hello from HelloServlet</h1>"); } } void startServlet() throws Exception { Server server = new Server(8080); ServletHandler handler = new ServletHandler(); server.setHandler(handler); handler.addServletWithMapping(HelloServlet.class, "/*"); server.start(); server.join(); } public static void testServlet() throws Exception { HttpServer http = new HttpServer(); http.startServlet(); } public static void main( String[] args ) throws Exception { testServlet(); } } |