码头中的多个servlet

时间:2016-05-05 11:07:36

标签: java servlets jetty-9

我是Jetty的新手并试图通过在线示例程序来理解。这是我使用的示例程序:

public class EmbeddedJettyMain {

    public static void main(String[] args) throws Exception {

        Server server = new Server(7070);
        ServletContextHandler handler = new ServletContextHandler(server, "/example");
        handler.addServlet(ExampleServlet.class, "/");
        server.start();

    }

}

我可以使用:

  

http://localhost:7070/example/

现在我想再添加一个servlet URI

  

http://localhost:7070/example2

我该怎么做?

我可以看到一些像webapp这样的参考,寻找一个好的方法。

感谢您的帮助, 阿肖克

2 个答案:

答案 0 :(得分:4)

Server server = new Server(7070);
ServletContextHandler handler = new ServletContextHandler(server, "/");
handler.addServlet(ExampleServlet.class, "/example");
handler.addServlet(ExampleServlet.class, "/example2");

每个addServlet都会创建一个映射。 Jetty将创建一个Servlet实例,它将成为每个映射的单例,这意味着init(ServletConfig配置)只会在每个实例中调用一次,并且所有对映射的请求都会转到同一个实例。

答案 1 :(得分:1)

  

Jetty提供了一个Web服务器和javax.servlet容器。

您的servlet通过jetty的嵌入式容器进行存储和提供,以便在需要时提供服务。