2

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)
{

}

1 Answer 1

1
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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.