sjallamander
sjallamander

Reputation: 439

How to add static web content (js/css) to embedded jetty?

How can I use static web content like js and css in my embedded web application?

If I have web content in src/main/webapp.

I've tried with:

ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
contextHandler.setContextPath("/mypath");

ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setBaseResource(Resource.newClassPathResource("/src/main/webapp/");
// I've also tried with resourceHandler.setResourceBase("/src/main/webapp/");

ContextHandlerCollection contexts = new ContextHandlerCollections();
contexts.setHandlers(new Handler[] { contextHandler, resourceHandler });

Server jettyServer = new Server(8080);
jettyServer.setHAndler(contexts);

try
{
    jettyServer.start();
    jettyServer.join();
}
catch (Exception e)
{

}

Upvotes: 2

Views: 1321

Answers (1)

mirmdasif
mirmdasif

Reputation: 6354

ResourceHandler resource_handler = new ResourceHandler();
resource_handler.setDirectoriesListed(true);
resource_handler.setWelcomeFiles(new String[]{ "index.html" });

resource_handler.setResourceBase(".");

HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
server.setHandler(handlers);

Upvotes: 1

Related Questions