View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
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   * A {@link ContextHandlerCollection} handler may be used to direct a request to
26   * a specific Context. The URI path prefix and optional virtual host is used to
27   * select the context.
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  
73  }