Serving Static Files with Embedded Jetty

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(); 
+52
java jetty embedded-jetty
Apr 23 2018-12-12T00:
source share
5 answers
+33
Apr 23 '12 at 18:53
source share

There is an important difference between serving static content with the ResourceHandler and using the DefaultServlet (with the ServletContextHandler ).

When a ResourceHandler (or HandlerList containing multiple instances of the ResourceHandler ) is set as a context handler, it directly processes requests and ignores any registered instances of javax.servlet.Filter.

If you need filters, the only way to do this is to use the ServletContextHandler , add filters to it, then add the DefaultServlet and finally install the Resource base.

The Resource base is the path of the resource to which a ResourceHandler will be initialized. If you use static resources from multiple directories, use a ResourceCollection (which is still a Resource ) and initialize it with an array of resourceBase strings:

 ResourceCollection resourceCollection = new ResourceCollection(); resourceCollection.setResources(getArrayOfResourceBaseDirs()); 
+19
Oct 26 '13 at 10:10
source share

On my small web server, I have two files: index.html and info.js locate under /src/webapp , and I want to be served from the pier’s built-in web server.

This is how I solve the problem with static content.

 Server server = new Server(8080); ServletContextHandler ctx = new ServletContextHandler(); ctx.setContextPath("/"); DefaultServlet defaultServlet = new DefaultServlet(); ServletHolder holderPwd = new ServletHolder("default", defaultServlet); holderPwd.setInitParameter("resourceBase", "./src/webapp/"); ctx.addServlet(holderPwd, "/*"); ctx.addServlet(InfoServiceSocketServlet.class, "/info"); server.setHandler(ctx); 

Worked like a charm!

+15
Feb 26 '15 at 5:29
source share

I managed to achieve something similar by adding a mapping for the css directory in web.xml. Explicitly telling him to use the DefaultServlet:

 <servlet> <servlet-name>DefaultServlet</servlet-name> <servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DefaultServlet</servlet-name> <url-pattern>/css/*</url-pattern> </servlet-mapping> 
+2
Dec 15 '15 at 5:51 on
source share

This is the Main.java file:

 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 Main { public static void main(String[] args) throws Exception { Server server = new Server(8080); ResourceHandler resource_handler = new ResourceHandler(); resource_handler.setResourceBase("C:/Users/serge.klimkovitch/Documents/images"); HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() }); server.setHandler(handlers); server.start(); server.join(); } } 

=======================================

And this is the gradle.build file:

 apply plugin: 'java' apply plugin: 'application' mainClassName = 'SheetsQuickstart' sourceCompatibility = 1.7 targetCompatibility = 1.7 version = '1.0' repositories { mavenCentral() } dependencies { compile group: 'org.eclipse.jetty', name: 'jetty-server', version: '9.4.16.v20190411' } jar { manifest { attributes( 'Main-Class': 'SheetsQuickstart' ) } from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } } 

=======================================

Assuming the following file exists: C:\Users\serge.klimkovitch\Documents\images\image.html

Then run in Eclipse and go to http://localhost:8080/image.html in your browser to see that this file is being served.

0
Apr 18 '19 at 15:20
source share



All Articles