0
votes

I'm making an application which loads modules dynamically based on configuration files. Each module has its own servlet and its own path.

It works, but so far it's only serving content I annotate with @Path in my classes (rest services), thanks to the ServerProperties.PROVIDER_PACKAGES property:

Context:

    ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS );
    context.setContextPath( "/" );

Each module/servlet is initialized like this:

    ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class, "/" );
    jerseyServlet.setInitOrder( 0 );
    jerseyServlet.setInitParameter( ServerProperties.PROVIDER_PACKAGES, "com.my.packages.rest.server.root" );

This gives me a url path per module - great.

But I also have some static html in the resource/modulename folder of each module, which I don't know how to serve...

With a DefaultServlet, I can do it like this:

    DefaultServlet defaultServlet = new DefaultServlet();
    ServletHolder staticAppServlet = new ServletHolder( "default", defaultServlet );
    staticAppServlet.setInitParameter( "resourceBase", "./src/main/resources/modulename/" );
    context.addServlet( staticAppServlet, "/path" );

But I don't know how to do it with a jetty ServletHolder.

Any idea?

1

1 Answers

1
votes

Don't reuse the name "default", make a new name for each module.

It is also important that you use a fully qualified path to your Resource Base, either as a full file system path, or as an absolute URL.

Here's an example from the embedded-jetty-cookbook example called DefaultServletMultipleBases.java

// add special pathspec of "/alt/" content mapped to the altPath
ServletHolder holderAlt = new ServletHolder("static-alt", DefaultServlet.class);
holderAlt.setInitParameter("resourceBase",altPath.toUri().toASCIIString());
holderAlt.setInitParameter("dirAllowed","true");
holderAlt.setInitParameter("pathInfoOnly","true");
context.addServlet(holderAlt,"/alt/*");