Avalanche
Avalanche

Reputation: 386

Path to static content with Embedded Jetty

I'm using embedded Jetty to serve static content from the "public" folder in my project:

    Server server = new Server(9999);
    ServletContextHandler context = new ServletContextHandler();
    context.setContextPath("/");

    ServletHolder staticHolder = new ServletHolder(new DefaultServlet());
    staticHolder.setInitParameter("resourceBase", "./public");
    context.addServlet(staticHolder, "/*");

    server.setHandler(context);
    server.start();
    server.join();

Which works fine. However, if I change

    context.addServlet(staticHolder, "/*");

to

   context.addServlet(staticHolder, "/ui/*");

or

   context.addServlet(staticHolder, "/ui");

or anything other than "/*" I get a 404. Basically I can see my index page at http://127.0.0.1:9999/index.html, and I would like to change it to http://127.0.0.1:9999/ui/index.html.

Thanks

Upvotes: 4

Views: 4013

Answers (2)

InsertUserHere
InsertUserHere

Reputation: 101

Great answer Avalanche. Also if you serving static content you should add
staticHolder.setInitParameter("useFileMappedBuffer", "true"); to avoid Locked Files on Windows

Upvotes: 0

Avalanche
Avalanche

Reputation: 386

As posted here:

Serving static files from alternate path in embedded Jetty

I needed to add:

staticHolder.setInitParameter("pathInfoOnly", "true");

which allowed me to modify the path and have it behave correctly:

context.addServlet(staticHolder, "/ui/*");

allowing access to static content at http://127.0.0.1:9999/ui/index.html

Thanks!

Upvotes: 1

Related Questions