jetty - 创建主要和资源servlet

时间:2017-02-12 12:33:29

标签: jetty embedded-jetty

我试图创建两个servlet。首先,主要的servlet与" /"路径和来自另一个路径的资源servlet。但是这两条路径都是从" /" (我的工作目录)

我写道:

    Server server = new Server(8001);
    ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(Servlet.class, "/");

    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase("./classes/static/");

    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] { resourceHandler,handler});
    server.setHandler(handlers);
    server.start();
    server.join();

但是一个处理程序与另一个处理程序重叠。

基本上我希望我的代码表现得像:

handler.addServletWithMapping(Servlet.class," / q"); (本地主机:8001 / Q)

而不是:

handler.addServletWithMapping(Servlet.class," /");

希望我足够清楚。

谢谢,

1 个答案:

答案 0 :(得分:0)

在这里找到它 https://examples.javacodegeeks.com/enterprise-java/jetty/jetty-resource-handler-example/

需要声明路径:

    Server server = new Server(8001);
    ServletHandler handler = new ServletHandler();
    handler.addServletWithMapping(Servlet.class, "/");


    ResourceHandler resourceHandler = new ResourceHandler();
    resourceHandler.setResourceBase("./classes/static/");
    resourceHandler.setDirectoriesListed(true);
    ContextHandler contextHandler= new ContextHandler("/static");
    contextHandler.setHandler(resourceHandler);



    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[] {contextHandler,handler});
    server.setHandler(handlers);
    server.start();
    server.join();