Or just use one ServletContextHandler and setup an Alternate DefaultServlet to serve static content.
Server server = new Server();
int securePort = 8180;
HttpConfiguration https = new HttpConfiguration();
https.setSecurePort(securePort); // <-- IMPORTANT
https.addCustomizer(new SecureRequestCustomizer());
SslContextFactory ssl = new SslContextFactory();
ssl.setKeyStorePath(getKeyStorePath());
ssl.setKeyStoreType("JKS");
ssl.setKeyStorePassword("password");
ssl.setKeyManagerPassword("password");
ServerConnector connector = new ServerConnector(server, new SslConnectionFactory(ssl, HttpVersion.HTTP_1_1.toString()), new HttpConnectionFactory(https));
connector.setPort(securePort);
connector.setIdleTimeout(500000);
server.addConnector(connector);
HashSessionManager session_manager = new HashSessionManager();
session_manager.setMaxInactiveInterval(3600);
SessionHandler session_handler = new SessionHandler(session_manager);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/"); // <-- IMPORTANT
context.setResourceBase("/path/to/main/resource/base"); // <-- IMPORTANT
context.setHandler(session_handler);
context.addFilter(new FilterHolder(SessionFilter.class), "/*", EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST));
context.addServlet(AppServlet.class, "/Echo");
// Fist, add special pathspec of "/static/" content mapped to the staticPath
ServletHolder holderStatic = new ServletHolder("static", DefaultServlet.class);
holderStatic.setInitParameter("resourceBase",System.getProperty("user.dir") + File.separator + "static" + File.separator);
holderStatic.setInitParameter("dirAllowed","true");
holderStatic.setInitParameter("pathInfoOnly","true");
context.addServlet(holderStatic,"/static/*");
server.setHandler(context);
try
{
server.start();
server.dump(System.err); // <-- Provides useful information for us to help troubleshoot
server.join();
}
catch (Throwable t)
{
t.printStackTrace(System.err);
}
Or don't use the alternate DefaultServlet and just restructure your static resource a little bit (directory wise)
Start with a new empty directory, and copy the /static/ folder into it.
Then use this new directory (with just /static/ in it) as your main resource base with normal DefaultServlet.
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setResourceBase(System.getProperty("main.resource.dir"));
context.setHandler(session_handler);
context.addFilter(new FilterHolder(SessionFilter.class), "/*", EnumSet.of(DispatcherType.INCLUDE, DispatcherType.REQUEST));
context.addServlet(AppServlet.class, "/Echo");
context.addServlet(DefaultServlet.class, "/");
server.setHandler(context);
This is probably the more sane approach, and you can then also setup some niceties for your website to help with analytics of your traffic (such as the various root path analytics company registration files). or use the root path resources for other web things like the your favicon or website icon (think chrome and firefox bookmark manager large format icons). could even use it for the sitemap (boost those google search results!).