DefaultServlet intended for viewing request URI after contextPath .
In your sample code, when you change the url template of your servlet from / to /foo/* resulting file viewed on disk now includes the /foo/ .
In other words, the request URI /css/main.css causes the file (on disk) to be found as ./src/webapp/foo/css/main.css
There are several drawbacks in your example. It is not wise to have an empty resource base for your ServletContextHandler , since the ServletContext itself needs access to this configuration value.
You would fix it by deleting ...
holderPwd.setInitParameter("resourceBase", "./src/webapp/");
and using ServletContextHandler.setBaseResource (Resource) instead ...
ctx.setResourceBase(Resource.newResource(new File("./src/webapp")));
This will allow you to use the following ServletContext methods (used by countless servlet libraries)
String getRealPath(String path)URL getResource(String path)InputStream getResourceAsStream(String path)Set<String> getResources(String path)
Finally, to make this setting workable in ServletContextHandler , you add the name of the default servlet to the " default url template", which appears to be implemented as DefaultServlet .
Like this:
// Lastly, the default servlet for root content // It is important that this is added last. String defName = "default"; // the important "default" name ServletHolder holderDef = new ServletHolder(defName, DefaultServlet.class); holderDef.setInitParameter("dirAllowed","true"); ctx.addServlet(holderDef,"/"); // the servlet spec "default url-pattern"
Now, if you also need to use static content from the request URI /foo/* from a directory that does not belong to webapp, you can also do this. This will require the installation of another DefaultServlet that is not involved in the ServletContext .
An example of this setting ...
package jetty; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.util.resource.PathResource; import java.io.File; import java.nio.file.Path; public class ManyDefaultServlet { public static void main(String[] args) throws Exception { Server server = new Server(); ServerConnector connector = new ServerConnector(server); connector.setPort(8080); server.addConnector(connector);
This uses the second DefaultServlet , using a unique resource base for that DefaultServlet and matching the url pattern that ends with /* .
Finally, the init parameter for this second DefaultServlet prompted to use the pathInfo request URI, rather than breaking it into contextPath, as usual.
For more information that this is the entire pathInfo, request URIs, contextPath and url patterns ending in /* everything, see the helpful answer at @ 30thh
This stand-alone DefaultServlet declaration is not involved in the ServletContext , and libraries will not be able to see or access the contents of this DefaultServlet using the ServletContext methods. However, all incoming HTTP client requests can easily request content through this url template.