refined jetty embedded examples
This commit is contained in:
parent
616713fc5f
commit
9234f3ec90
|
@ -18,60 +18,37 @@
|
|||
|
||||
package org.eclipse.jetty.embedded;
|
||||
|
||||
import org.eclipse.jetty.server.Connector;
|
||||
import org.eclipse.jetty.server.Handler;
|
||||
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.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 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
|
||||
{
|
||||
Server server = new Server();
|
||||
ServerConnector connector = new ServerConnector(server);
|
||||
connector.setPort(8080);
|
||||
server.setConnectors(new Connector[]
|
||||
{ connector });
|
||||
Server server = new Server(8080);
|
||||
|
||||
ContextHandler context0 = new ContextHandler();
|
||||
context0.setContextPath("/");
|
||||
Handler handler0 = new HelloHandler("Root Context",BODY);
|
||||
context0.setHandler(handler0);
|
||||
ContextHandler context = new ContextHandler("/");
|
||||
context.setContextPath("/");
|
||||
context.setHandler(new HelloHandler("Root Hello"));
|
||||
|
||||
ContextHandler context1 = new ContextHandler();
|
||||
context1.setContextPath("/context");
|
||||
Handler handler1 = new HelloHandler("A Context",BODY);
|
||||
context1.setHandler(handler1);
|
||||
ContextHandler contextFR = new ContextHandler("/fr");
|
||||
contextFR.setHandler(new HelloHandler("Bonjoir"));
|
||||
|
||||
ContextHandler contextIT = new ContextHandler("/it");
|
||||
contextIT.setHandler(new HelloHandler("Bongiorno"));
|
||||
|
||||
ContextHandler context2 = new ContextHandler();
|
||||
context2.setContextPath("/context");
|
||||
context2.setVirtualHosts(new String[]
|
||||
{ "127.0.0.2" });
|
||||
Handler handler2 = new HelloHandler("A Virtual Context",BODY);
|
||||
context2.setHandler(handler2);
|
||||
ContextHandler contextV = new ContextHandler("/");
|
||||
contextV.setVirtualHosts(new String[]{ "127.0.0.2" });
|
||||
contextV.setHandler(new HelloHandler("Virtual Hello"));
|
||||
|
||||
ContextHandlerCollection contexts = new ContextHandlerCollection();
|
||||
contexts.setHandlers(new Handler[]
|
||||
{ context0, context1, context2 });
|
||||
contexts.setHandlers(new Handler[] { context, contextFR, contextIT, contextV });
|
||||
|
||||
server.setHandler(contexts);
|
||||
|
||||
server.start();
|
||||
System.err.println(server.dump());
|
||||
server.join();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,12 +36,12 @@ public class MinimalServlets
|
|||
ServletHandler handler = new ServletHandler();
|
||||
server.setHandler(handler);
|
||||
|
||||
handler.addServletWithMapping("org.eclipse.jetty.embedded.MinimalServlets$HelloServlet","/");
|
||||
handler.addServletWithMapping(HelloServlet.class,"/*");
|
||||
|
||||
server.start();
|
||||
server.join();
|
||||
}
|
||||
|
||||
|
||||
public static class HelloServlet extends HttpServlet
|
||||
{
|
||||
@Override
|
||||
|
|
|
@ -37,10 +37,8 @@ public class OneServletContext
|
|||
holder.setInitParameter("resourceBase","/tmp");
|
||||
holder.setInitParameter("pathInfoOnly","true");
|
||||
|
||||
// Serve some hello world servlets
|
||||
context.addServlet(new ServletHolder(new HelloServlet()),"/*");
|
||||
context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")),"/it/*");
|
||||
context.addServlet(new ServletHolder(new HelloServlet("Bonjour le Monde")),"/fr/*");
|
||||
// A Dump Servlet
|
||||
context.addServlet(new ServletHolder(new DumpServlet()),"/*");
|
||||
|
||||
server.start();
|
||||
server.join();
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
package org.eclipse.jetty.embedded;
|
||||
|
||||
import org.eclipse.jetty.security.HashLoginService;
|
||||
import org.eclipse.jetty.server.Connector;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.ServerConnector;
|
||||
|
@ -27,36 +28,18 @@ public class OneWebApp
|
|||
{
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = new Server();
|
||||
|
||||
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);
|
||||
Server server = new Server(8080);
|
||||
|
||||
WebAppContext webapp = new WebAppContext();
|
||||
webapp.setContextPath(path);
|
||||
webapp.setWar(war);
|
||||
|
||||
//If the webapp contains security constraints, you will need to configure a LoginService
|
||||
if (war.contains("test-jetty-webapp"))
|
||||
{
|
||||
org.eclipse.jetty.security.HashLoginService loginService = new org.eclipse.jetty.security.HashLoginService();
|
||||
loginService.setName("Test Realm");
|
||||
loginService.setConfig("src/test/resources/realm.properties");
|
||||
webapp.getSecurityHandler().setLoginService(loginService);
|
||||
}
|
||||
|
||||
webapp.setContextPath("/");
|
||||
webapp.setWar("../../tests/test-webapps/test-jetty-webapp/target/test-jetty-webapp-9.0.0-SNAPSHOT.war");
|
||||
server.setHandler(webapp);
|
||||
|
||||
// Configure a LoginService
|
||||
HashLoginService loginService = new HashLoginService();
|
||||
loginService.setName("Test Realm");
|
||||
loginService.setConfig("src/test/resources/realm.properties");
|
||||
server.addBean(loginService);
|
||||
|
||||
server.start();
|
||||
server.join();
|
||||
|
|
Loading…
Reference in New Issue