ResourceHandler
class is used to serve static content. Following example explains you, how to
serve static content.
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.server.handler.gzip.GzipHandler; public class FileServer { public static void main(String[] args) throws Exception { Server server = new Server(8080); ResourceHandler resource_handler = new ResourceHandler(); resource_handler.setDirectoriesListed(true); resource_handler.setResourceBase("."); GzipHandler gzip = new GzipHandler(); HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() }); gzip.setHandler(handlers); server.setHandler(gzip); server.start(); server.join(); } }
Run above
application and hit the url ‘http://localhost:8080/’.
Server server = new Server(8080);
Instantiate
server, that listens at port 8080.
ResourceHandler resource_handler = new
ResourceHandler();
Initialize
ResourceHandler, it is used to serve static content.
GzipHandler gzip = new GzipHandler();
Initialize
GzipHandler, used to send compressed responses.
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] {
resource_handler, new DefaultHandler() });
gzip.setHandler(handlers);
Above
statements add resource_handler, DefultHandler to GzipHandler. DefaultHandler
generates a good 404 response for any requests that do not match a static
resource.
No comments:
Post a Comment