I have the following simple embedded Jetty 9 server:
final Server server = new Server();
final ServerConnector connector = new ServerConnector(server);
connector.setPort(443);
server.setConnectors(new Connector[] { connector });
server.setHandler(new FooBarHandler());
server.start();
server.join();
Requests to both https://foo.bar.com/ and https://baz.bar.com/ are handled by this code. I want to change it so that:
I'm familiar with "running multiple java jetty instances with same port (80)" and http://wiki.eclipse.org/Jetty/Howto/Configure_Virtual_Hosts#Configuring_Virtual_Hosts but can't seem to get it right programmatically.
First of all, as in the xml-based configuration, the virtualHost property is within org.eclipse.jetty.server.handler.ContextHandler.setVirtualHosts(String[] vhosts). So, my guest is that the straightforward way is:
ContextHandler fooContextHandler = new ContextHandler("/");
fooContextHandler.setVirtualHosts(new String[]{"foo"});
fooContextHandler.setHandler(new FooBarHandler());
ContextHandler bazContextHandler = new ContextHandler("/");
bazContextHandler.setVirtualHosts(new String[]{"baz"});
bazContextHandler.setHandler(new BazBarHandler());
HandlerCollection handler = new HandlerCollection();
handler.addHandler(fooContextHandler);
handler.addHandler(bazContextHandler);
server.setHandler(handler);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With