简体   繁体   English

码头中的多个 servlet

[英]multiple servlet in jetty

I am new to Jetty and trying to understand by online example program.我是 Jetty 的新手,并试图通过在线示例程序来理解。 Here is the sample program I used:这是我使用的示例程序:

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();

    }

}

With that I can use:有了它,我可以使用:

http://localhost:7070/example/ http://localhost:7070/example/

Now I want to add one more servlet URI现在我想再添加一个 servlet URI

http://localhost:7070/example2 http://localhost:7070/example2

How can I do this ?我该怎么做?

I can see some reference such as webapp, looking for a good approach.我可以看到一些参考,例如webapp,正在寻找一个好的方法。

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

Each addServlet creates a mapping.每个 addServlet 创建一个映射。 Jetty will create an instance of the Servlet that will be a singleton for each mapping, meaning that init(ServletConfig config) will only be called once in each instance and all requests to a mapping go to the same instance. Jetty 将创建一个 Servlet 实例,该实例将成为每个映射的单例,这意味着 init(ServletConfig config) 在每个实例中只会被调用一次,并且对映射的所有请求都将转到同一个实例。

Jetty provides a Web server and javax.servlet container. Jetty 提供了一个 Web 服务器和 javax.servlet 容器。

Your servlets are stored and served via jetty's embedded container to serve when needed.您的 servlet 通过 jetty 的嵌入式容器进行存储和服务,以便在需要时提供服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM