refined jetty embedded examples
This commit is contained in:
parent
616713fc5f
commit
9234f3ec90
|
@ -18,60 +18,37 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.embedded;
|
package org.eclipse.jetty.embedded;
|
||||||
|
|
||||||
import org.eclipse.jetty.server.Connector;
|
|
||||||
import org.eclipse.jetty.server.Handler;
|
import org.eclipse.jetty.server.Handler;
|
||||||
import org.eclipse.jetty.server.Server;
|
import org.eclipse.jetty.server.Server;
|
||||||
import org.eclipse.jetty.server.ServerConnector;
|
|
||||||
import org.eclipse.jetty.server.handler.ContextHandler;
|
import org.eclipse.jetty.server.handler.ContextHandler;
|
||||||
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
|
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
|
||||||
|
|
||||||
/* ------------------------------------------------------------ */
|
|
||||||
/**
|
|
||||||
* A {@link ContextHandlerCollection} handler may be used to direct a request to
|
|
||||||
* a specific Context. The URI path prefix and optional virtual host is used to
|
|
||||||
* select the context.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class ManyContexts
|
public class ManyContexts
|
||||||
{
|
{
|
||||||
public final static String BODY=
|
|
||||||
"<a href='/'>root context</a><br/>"+
|
|
||||||
"<a href='http://127.0.0.1:8080/context'>normal context</a><br/>"+
|
|
||||||
"<a href='http://127.0.0.2:8080/context'>virtual context</a><br/>";
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception
|
public static void main(String[] args) throws Exception
|
||||||
{
|
{
|
||||||
Server server = new Server();
|
Server server = new Server(8080);
|
||||||
ServerConnector connector = new ServerConnector(server);
|
|
||||||
connector.setPort(8080);
|
|
||||||
server.setConnectors(new Connector[]
|
|
||||||
{ connector });
|
|
||||||
|
|
||||||
ContextHandler context0 = new ContextHandler();
|
ContextHandler context = new ContextHandler("/");
|
||||||
context0.setContextPath("/");
|
context.setContextPath("/");
|
||||||
Handler handler0 = new HelloHandler("Root Context",BODY);
|
context.setHandler(new HelloHandler("Root Hello"));
|
||||||
context0.setHandler(handler0);
|
|
||||||
|
|
||||||
ContextHandler context1 = new ContextHandler();
|
ContextHandler contextFR = new ContextHandler("/fr");
|
||||||
context1.setContextPath("/context");
|
contextFR.setHandler(new HelloHandler("Bonjoir"));
|
||||||
Handler handler1 = new HelloHandler("A Context",BODY);
|
|
||||||
context1.setHandler(handler1);
|
|
||||||
|
|
||||||
ContextHandler context2 = new ContextHandler();
|
ContextHandler contextIT = new ContextHandler("/it");
|
||||||
context2.setContextPath("/context");
|
contextIT.setHandler(new HelloHandler("Bongiorno"));
|
||||||
context2.setVirtualHosts(new String[]
|
|
||||||
{ "127.0.0.2" });
|
ContextHandler contextV = new ContextHandler("/");
|
||||||
Handler handler2 = new HelloHandler("A Virtual Context",BODY);
|
contextV.setVirtualHosts(new String[]{ "127.0.0.2" });
|
||||||
context2.setHandler(handler2);
|
contextV.setHandler(new HelloHandler("Virtual Hello"));
|
||||||
|
|
||||||
ContextHandlerCollection contexts = new ContextHandlerCollection();
|
ContextHandlerCollection contexts = new ContextHandlerCollection();
|
||||||
contexts.setHandlers(new Handler[]
|
contexts.setHandlers(new Handler[] { context, contextFR, contextIT, contextV });
|
||||||
{ context0, context1, context2 });
|
|
||||||
|
|
||||||
server.setHandler(contexts);
|
server.setHandler(contexts);
|
||||||
|
|
||||||
server.start();
|
server.start();
|
||||||
System.err.println(server.dump());
|
|
||||||
server.join();
|
server.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ public class MinimalServlets
|
||||||
ServletHandler handler = new ServletHandler();
|
ServletHandler handler = new ServletHandler();
|
||||||
server.setHandler(handler);
|
server.setHandler(handler);
|
||||||
|
|
||||||
handler.addServletWithMapping("org.eclipse.jetty.embedded.MinimalServlets$HelloServlet","/");
|
handler.addServletWithMapping(HelloServlet.class,"/*");
|
||||||
|
|
||||||
server.start();
|
server.start();
|
||||||
server.join();
|
server.join();
|
||||||
|
|
|
@ -37,10 +37,8 @@ public class OneServletContext
|
||||||
holder.setInitParameter("resourceBase","/tmp");
|
holder.setInitParameter("resourceBase","/tmp");
|
||||||
holder.setInitParameter("pathInfoOnly","true");
|
holder.setInitParameter("pathInfoOnly","true");
|
||||||
|
|
||||||
// Serve some hello world servlets
|
// A Dump Servlet
|
||||||
context.addServlet(new ServletHolder(new HelloServlet()),"/*");
|
context.addServlet(new ServletHolder(new DumpServlet()),"/*");
|
||||||
context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")),"/it/*");
|
|
||||||
context.addServlet(new ServletHolder(new HelloServlet("Bonjour le Monde")),"/fr/*");
|
|
||||||
|
|
||||||
server.start();
|
server.start();
|
||||||
server.join();
|
server.join();
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
|
|
||||||
package org.eclipse.jetty.embedded;
|
package org.eclipse.jetty.embedded;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.security.HashLoginService;
|
||||||
import org.eclipse.jetty.server.Connector;
|
import org.eclipse.jetty.server.Connector;
|
||||||
import org.eclipse.jetty.server.Server;
|
import org.eclipse.jetty.server.Server;
|
||||||
import org.eclipse.jetty.server.ServerConnector;
|
import org.eclipse.jetty.server.ServerConnector;
|
||||||
|
@ -27,36 +28,18 @@ public class OneWebApp
|
||||||
{
|
{
|
||||||
public static void main(String[] args) throws Exception
|
public static void main(String[] args) throws Exception
|
||||||
{
|
{
|
||||||
Server server = new Server();
|
Server server = new Server(8080);
|
||||||
|
|
||||||
ServerConnector connector = new ServerConnector(server);
|
|
||||||
connector.setPort(Integer.getInteger("jetty.port",8080).intValue());
|
|
||||||
server.setConnectors(new Connector[]
|
|
||||||
{ connector });
|
|
||||||
|
|
||||||
|
|
||||||
//If you're running this from inside Eclipse, then Server.getVersion will not provide
|
|
||||||
//the correct number as there is no manifest. Use the command line instead to provide the path to the
|
|
||||||
//test webapp
|
|
||||||
String war = args.length > 0?args[0]: "../test-jetty-webapp/target/test-jetty-webapp-"+Server.getVersion();
|
|
||||||
String path = args.length > 1?args[1]:"/";
|
|
||||||
|
|
||||||
System.err.println(war + " " + path);
|
|
||||||
|
|
||||||
WebAppContext webapp = new WebAppContext();
|
WebAppContext webapp = new WebAppContext();
|
||||||
webapp.setContextPath(path);
|
webapp.setContextPath("/");
|
||||||
webapp.setWar(war);
|
webapp.setWar("../../tests/test-webapps/test-jetty-webapp/target/test-jetty-webapp-9.0.0-SNAPSHOT.war");
|
||||||
|
server.setHandler(webapp);
|
||||||
|
|
||||||
//If the webapp contains security constraints, you will need to configure a LoginService
|
// Configure a LoginService
|
||||||
if (war.contains("test-jetty-webapp"))
|
HashLoginService loginService = new HashLoginService();
|
||||||
{
|
|
||||||
org.eclipse.jetty.security.HashLoginService loginService = new org.eclipse.jetty.security.HashLoginService();
|
|
||||||
loginService.setName("Test Realm");
|
loginService.setName("Test Realm");
|
||||||
loginService.setConfig("src/test/resources/realm.properties");
|
loginService.setConfig("src/test/resources/realm.properties");
|
||||||
webapp.getSecurityHandler().setLoginService(loginService);
|
server.addBean(loginService);
|
||||||
}
|
|
||||||
|
|
||||||
server.setHandler(webapp);
|
|
||||||
|
|
||||||
server.start();
|
server.start();
|
||||||
server.join();
|
server.join();
|
||||||
|
|
Loading…
Reference in New Issue