I basically have the same issue as this question:
Setting Jetty resourcebase to static file embedded in the same jar file
where I am using embedded Jetty, and I want to access some static HTML files in the same JAR file.
Here is how the Jetty server is set up:
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
String res = ApiServer.class.getClassLoader().getResource("res").toExternalForm();
context.setResourceBase(res);
jettyServer = new Server(port);
jettyServer.setHandler(context);
ServletHolder jerseyServlet = context.addServlet(ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
// Tells the Jersey Servlet which REST service/class to load.
String classes = new StringJoiner(",")
.add(MyClass1.class.getCanonicalName())
.add(MyClass2.class.getCanonicalName())
.toString();
jerseyServlet.setInitParameter(ServerProperties.PROVIDER_CLASSNAMES, classes);
The folder structure of the JAR is like this:
root
| src (Java classes in here)
| res
| index.html
However it just doesn't work. I have tried to access the URL in various ways:
http://localhost:12345/res/index.html
or
http://localhost:12345/index.html
but neither works.
What am I doing wrong?