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 java.io.IOException;
17  
18  import javax.servlet.ServletException;
19  import javax.servlet.http.HttpServlet;
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.eclipse.jetty.server.Handler;
24  import org.eclipse.jetty.server.HandlerContainer;
25  import org.eclipse.jetty.server.Server;
26  import org.eclipse.jetty.server.handler.ContextHandlerCollection;
27  import org.eclipse.jetty.server.handler.HandlerCollection;
28  import org.eclipse.jetty.server.handler.StatisticsHandler;
29  import org.eclipse.jetty.servlet.DefaultServlet;
30  import org.eclipse.jetty.servlet.ServletContextHandler;
31  import org.eclipse.jetty.servlet.ServletHolder;
32  
33  public class ManyServletContexts
34  {
35      public static void main(String[] args)
36          throws Exception
37      {
38          Server server = new Server(8080);
39          
40          ContextHandlerCollection contexts = new ContextHandlerCollection();
41          server.setHandler(contexts);
42          
43          ServletContextHandler root = new ServletContextHandler(contexts,"/",ServletContextHandler.SESSIONS);
44          root.addServlet(new ServletHolder(new HelloServlet("Hello")), "/");
45          root.addServlet(new ServletHolder(new HelloServlet("Ciao")), "/it/*");
46          root.addServlet(new ServletHolder(new HelloServlet("Bonjoir")), "/fr/*");
47          
48          ServletContextHandler other = new ServletContextHandler(contexts,"/other",ServletContextHandler.SESSIONS);
49          other.addServlet(DefaultServlet.class.getCanonicalName(), "/");
50          other.addServlet(new ServletHolder(new HelloServlet("YO!")), "*.yo");
51          
52          server.start();
53          System.err.println(server.dump());
54          server.join();
55      }
56  
57      public static class HelloServlet extends HttpServlet
58      {
59          String greeting="Hello";
60          public HelloServlet()
61          {}
62          
63          public HelloServlet(String hi)
64          {greeting=hi;}
65          
66          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
67          {
68              response.setContentType("text/html");
69              response.setStatus(HttpServletResponse.SC_OK);
70              response.getWriter().println("<h1>"+greeting+" SimpleServlet</h1>");
71              response.getWriter().println("session="+request.getSession(true).getId());
72          }
73      }
74  }