Y
Y
ybotok2015-08-08 21:06:37
Java
ybotok, 2015-08-08 21:06:37

Jetty does not return html. Where is the crooked hand?

I'm using jetty and I'm facing the fact that it returns 404 to me

apiServer = new Server();
    ServerConnector connector = new ServerConnector(apiServer);
    connector.setPort(Constants.PORT);
    connector.setHost(Constants.HOST);
    apiServer.addConnector(connector);
    HandlerList apiHandlers = new HandlerList();
    //add page with visualisator;
  ServletContextHandler apiHandler = new ServletContextHandler();
   ServletHolder defaultServletHolder = new ServletHolder(new DefaultServlet());
   defaultServletHolder.setInitParameter("resourceBase", "ui");
   apiHandler.addServlet(defaultServletHolder, "/*");
   apiHandler.setWelcomeFiles(new String[]{"tester.html"});
    apiHandlers.addHandler(apiHandler);
    apiHandlers.addHandler(new DefaultHandler());

    apiServer.setHandler(apiHandlers);
    try {
        apiServer.start();

    } catch (Exception e) {
        e.printStackTrace();
    }

But when i use the
ClassLoader.getSystemResource("ui/tester.html")
file in jar is located

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Andreevich, 2016-02-17
@SibWol

Try like this

public static void main(String[] args) throws Exception{
   DefaultServlet defaultServlet = new DefaultServlett();
   ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
   context.addServlet(new ServletHolder(defaultServlet ),"/");
   ResourceHandler resourceHandler = new ResourceHandler();
   resourceHandler.setResourceBase("ui");
   HandlerList handlers = new HandlerList();
   handlers.setHandlers(new Handler[]{resourceHandler, context});
   Server server = new Server(Constants.PORT);
   server.setHandler(handlers);
   server.start();
   server.join();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question