1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.embedded;
15
16 import org.eclipse.jetty.server.Connector;
17 import org.eclipse.jetty.server.Handler;
18 import org.eclipse.jetty.server.Server;
19 import org.eclipse.jetty.server.handler.ContextHandler;
20 import org.eclipse.jetty.server.handler.ContextHandlerCollection;
21 import org.eclipse.jetty.server.nio.SelectChannelConnector;
22
23
24
25
26
27
28
29
30 public class ManyContexts
31 {
32 public final static String BODY=
33 "<a href='/'>root context</a><br/>"+
34 "<a href='http://127.0.0.1:8080/context'>normal context</a><br/>"+
35 "<a href='http://127.0.0.2:8080/context'>virtual context</a><br/>";
36
37 public static void main(String[] args) throws Exception
38 {
39 Server server = new Server();
40 Connector connector = new SelectChannelConnector();
41 connector.setPort(8080);
42 server.setConnectors(new Connector[]
43 { connector });
44
45 ContextHandler context0 = new ContextHandler();
46 context0.setContextPath("/");
47 Handler handler0 = new HelloHandler("Root Context",BODY);
48 context0.setHandler(handler0);
49
50 ContextHandler context1 = new ContextHandler();
51 context1.setContextPath("/context");
52 Handler handler1 = new HelloHandler("A Context",BODY);
53 context1.setHandler(handler1);
54
55 ContextHandler context2 = new ContextHandler();
56 context2.setContextPath("/context");
57 context2.setVirtualHosts(new String[]
58 { "127.0.0.2" });
59 Handler handler2 = new HelloHandler("A Virtual Context",BODY);
60 context2.setHandler(handler2);
61
62 ContextHandlerCollection contexts = new ContextHandlerCollection();
63 contexts.setHandlers(new Handler[]
64 { context0, context1, context2 });
65
66 server.setHandler(contexts);
67
68 server.start();
69 System.err.println(server.dump());
70 server.join();
71 }
72 }