我有两个 jetty 嵌入式服务器, localhost:9001/WebApp1 和 localhost:9002/WebApp2, 如您所见,它们位于不同的端口。 我希望他们在创建服务器期间共享同一个端口 是否可以? (顺便说一句,它们也是两个独立的 jar 文件)。 那么我可以做这样的事情吗 localhost:9001/WebApp1 和 localhost:9001/WebApp2
还是我坚持制作 war 文件然后拥有它们 由 tomcat/glassfish 服务器包含
在创建服务器的过程中,我通常会看到这个
ContextHandler context = new ContextHandler();
context.setContextPath("/WebApp1");
context.setHandler(new WebApp1());
Server server = new Server(9001);
server.setHandler(context);
server.start();
server.join();
在第二个应用程序上,我想要一个看起来像这样的东西
ContextHandler context = new ContextHandler();
context.setContextPath("/WebApp2");
context.setHandler(new WebApp2());
Server server = getExistingServer(9001);
server.addHandler(context);
我看到有这样的方法 server.getHandlers();它返回一个处理程序数组我如何将新的处理程序添加到现有列表,或者让现有的 jetty 服务器在端口 9001 上运行
最佳答案
Jetty 是一个标准的servlet 容器,当然可以处理不同的上下文。 参见 Embedding Contexts 部分在 Jetty 文档的第 24 章中。
这是 ManyContexts example (Jetty 文档的一部分):
public class ManyContexts
{
public static void main( String[] args ) throws Exception
{
Server server = new Server(8080);
ContextHandler context = new ContextHandler("/");
context.setContextPath("/");
context.setHandler(new HelloHandler("Root Hello"));
ContextHandler contextFR = new ContextHandler("/fr");
contextFR.setHandler(new HelloHandler("Bonjoir"));
ContextHandler contextIT = new ContextHandler("/it");
contextIT.setHandler(new HelloHandler("Bongiorno"));
ContextHandler contextV = new ContextHandler("/");
contextV.setVirtualHosts(new String[] { "127.0.0.2" });
contextV.setHandler(new HelloHandler("Virtual Hello"));
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler[] { context, contextFR, contextIT, contextV });
server.setHandler(contexts);
server.start();
server.join();
}
}
关于java - 两个上下文一个服务器 jetty ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32155852/