Serving static files with embedded Jetty

耗尽温柔 提交于 2019-11-27 03:42:38
Sane Dog

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

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

If you need filters, the only way to go about it is using a ServletContextHandler, adding filters to it, then adding a DefaultServlet and finally, setting the base Resource.

The base Resource represents a resourceBase path a ResourceHandler would be initialised with. If serving static resources from multiple directories, use a ResourceCollection (which is still a Resource) and initialise it with an array of resourceBase strings:

ResourceCollection resourceCollection = new ResourceCollection();
resourceCollection.setResources(getArrayOfResourceBaseDirs());

In my small web server I have two files, a index.html and a info.js locate under /src/webapp and I want them to be served from the embedded jetty 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!

I managed to achieve something similar by adding a mapping for the "css" directory in web.xml. Explicitly telling it to use 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>

This is 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 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 that 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 this file being served.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!