0

这是我的代码-

    Server server = new Server();
    ServerConnector pxy = new ServerConnector(server);
    pxy.setPort(8080);
    server.addConnector(pxy);

    HandlerCollection handlers = new HandlerCollection();

    ServletContextHandler test1 = new ServletContextHandler(handlers, "/test1", ServletContextHandler.NO_SESSIONS);
    ServletContextHandler test2 = new ServletContextHandler(handlers, "/test2", ServletContextHandler.NO_SESSIONS);


    ServletHolder rootServletHolder = new ServletHolder(root.class);      
    test1.addServlet(rootServletHolder, "/test1/*");

    ServletHolder testServletHolder = new ServletHolder(test.class);      
    test2.addServlet(testServletHolder, "/test2/*");


    server.setHandler(handlers);
    server.start();
    server.join();

当我转到http://domainName.com:8080/test1http://domainName.com:8080/test2时,我收到未找到错误消息。

码头启动看起来像这样-

2013-06-07 18:58:59.871:INFO:oejs.Server:main: jetty-9.0.3.v20130506 2013-06-07 18:58:59.895:INFO:oejsh.ContextHandler:main: 开始 oejsServletContextHandler@7c87c24a{ /test1,null,AVAILABLE} 2013-06-07 18:58:59.896:INFO:oejsh.ContextHandler:main: 启动 oejsServletContextHandler@34e4c825{/test2,null,AVAILABLE} 2013-06-07 18:58:59.919:INFO :oejs.ServerConnector:main: 启动 ServerConnector@3932f2b2{HTTP/1.1}{0.0.0.0:8080}

4

1 回答 1

1

您的配置有一个上下文"/test1"和一个 servlet 下"/test1/*"

这意味着您的 URL 将是http://domainName.com:8080/test1/test1/

只要这样想,它就会有意义。

  • ServletContextHandler == WebApp
  • ServletContextHandler.addServlet() == 向该 WebApp 添加一个 servlet

我认为你想要的是一个单一的 ServletContextHandler"/"并添加了 2 个 servlet。

例子:

Server server = new Server();
ServerConnector pxy = new ServerConnector(server);
pxy.setPort(8080);
server.addConnector(pxy);

// Use ContextHandlerCollection 
ContextHandlerCollection contexts = new ContextHandlerCollection();
// Don't forget to add it to the server!
server.setHandler(contexts);

ServletContextHandler testApp = new ServletContextHandler(contexts, "/",
    ServletContextHandler.NO_SESSIONS);

ServletHolder rootServletHolder = new ServletHolder(root.class);      
ServletHolder testServletHolder = new ServletHolder(test.class);      

testApp.addServlet(rootServletHolder, "/test1/*");
testApp.addServlet(testServletHolder, "/test2/*");


server.setHandler(handlers);
server.start();
server.join();

另外,看看嵌入式码头示例。ManyServletContext.java似乎最相关。

于 2013-06-08T02:12:24.833 回答