diff --git a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/FileServer.java b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/FileServer.java index bfd7ed8d7c8..0164fe5f188 100644 --- a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/FileServer.java +++ b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/FileServer.java @@ -13,49 +13,33 @@ package org.eclipse.jetty.embedded; -import java.io.IOException; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.ResourceHandler; -import org.eclipse.jetty.util.log.Log; -import org.eclipse.jetty.util.resource.Resource; /* ------------------------------------------------------------ */ -/** File server - * Usage - java org.eclipse.jetty.server.example.FileServer [ port [ docroot ]] - * @author gregw - * +/** + * File server + * */ public class FileServer { - public static void main(String[] args) - throws Exception + public static void main(String[] args) throws Exception { - Server server = new Server(args.length==0?8080:Integer.parseInt(args[0])); + Server server = new Server(8080); + + ResourceHandler resource_handler = new ResourceHandler(); + resource_handler.setDirectoriesListed(true); + resource_handler.setWelcomeFiles(new String[]{ "index.html" }); + + resource_handler.setResourceBase("."); - ResourceHandler resource_handler=new ResourceHandler() - { - protected void doDirectory(HttpServletRequest request, HttpServletResponse response, Resource resource) throws IOException - { - String listing=resource.getListHTML(request.getRequestURI(),request.getPathInfo().lastIndexOf("/")>0); - response.setContentType("text/html; charset=UTF-8"); - response.getWriter().println(listing); - } - }; - resource_handler.setWelcomeFiles(new String[]{"index.html"}); - - resource_handler.setResourceBase(args.length==2?args[1]:"."); - Log.info("serving "+resource_handler.getBaseResource()); HandlerList handlers = new HandlerList(); - handlers.setHandlers(new Handler[]{resource_handler,new DefaultHandler()}); + handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() }); server.setHandler(handlers); - + server.start(); server.join(); } diff --git a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/HelloHandler.java b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/HelloHandler.java new file mode 100644 index 00000000000..ba872e7760a --- /dev/null +++ b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/HelloHandler.java @@ -0,0 +1,60 @@ +// ======================================================================== +// Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== + +package org.eclipse.jetty.embedded; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.AbstractHandler; + +public class HelloHandler extends AbstractHandler +{ + final String _greeting; + final String _body; + + public HelloHandler() + { + _greeting="Hello World"; + _body=null; + } + + public HelloHandler(String greeting) + { + _greeting=greeting; + _body=null; + } + + public HelloHandler(String greeting,String body) + { + _greeting=greeting; + _body=body; + } + + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException + { + response.setContentType("text/html"); + response.setStatus(HttpServletResponse.SC_OK); + baseRequest.setHandled(true); + + response.getWriter().println("

"+_greeting+"

"); + response.getWriter().println("from " + ((Request)request).getConnection().getConnector().getName()); + response.getWriter().println("
"); + if (_body!=null) + response.getWriter().println(_body); + } +} \ No newline at end of file diff --git a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/HelloServlet.java b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/HelloServlet.java new file mode 100644 index 00000000000..fc98328be87 --- /dev/null +++ b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/HelloServlet.java @@ -0,0 +1,45 @@ +// ======================================================================== +// Copyright (c) 2009-2009 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== + + +package org.eclipse.jetty.embedded; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@SuppressWarnings("serial") +public class HelloServlet extends HttpServlet +{ + String greeting = "Hello"; + + public HelloServlet() + { + } + + public HelloServlet(String hi) + { + greeting = hi; + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException + { + response.setContentType("text/html"); + response.setStatus(HttpServletResponse.SC_OK); + response.getWriter().println("

" + greeting + " SimpleServlet

"); + response.getWriter().println("session=" + request.getSession(true).getId()); + } +} \ No newline at end of file diff --git a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java index 02dcf2ab545..f68f3cc6230 100644 --- a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java +++ b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/LikeJettyXml.java @@ -13,14 +13,13 @@ package org.eclipse.jetty.embedded; +import org.eclipse.jetty.deploy.ContextDeployer; +import org.eclipse.jetty.deploy.WebAppDeployer; import org.eclipse.jetty.security.HashLoginService; -import org.eclipse.jetty.security.LoginService; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.NCSARequestLog; import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.deploy.ContextDeployer; -import org.eclipse.jetty.deploy.WebAppDeployer; import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerCollection; @@ -31,67 +30,68 @@ import org.eclipse.jetty.util.thread.QueuedThreadPool; public class LikeJettyXml { - public static void main(String[] args) - throws Exception + public static void main(String[] args) throws Exception { String jetty_home = System.getProperty("jetty.home","../jetty-distribution/target/distribution"); System.setProperty("jetty.home",jetty_home); - + Server server = new Server(); - + QueuedThreadPool threadPool = new QueuedThreadPool(); threadPool.setMaxThreads(100); server.setThreadPool(threadPool); - - Connector connector=new SelectChannelConnector(); + + Connector connector = new SelectChannelConnector(); connector.setPort(8080); connector.setMaxIdleTime(30000); - server.setConnectors(new Connector[]{connector}); + server.setConnectors(new Connector[] + { connector }); - SslSelectChannelConnector ssl_connector=new SslSelectChannelConnector(); + SslSelectChannelConnector ssl_connector = new SslSelectChannelConnector(); ssl_connector.setPort(8443); - ssl_connector.setKeystore(jetty_home+"/etc/keystore"); + ssl_connector.setKeystore(jetty_home + "/etc/keystore"); ssl_connector.setPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"); ssl_connector.setKeyPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g"); - ssl_connector.setTruststore(jetty_home+"/etc/keystore"); + ssl_connector.setTruststore(jetty_home + "/etc/keystore"); ssl_connector.setTrustPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"); server.addConnector(ssl_connector); - + HandlerCollection handlers = new HandlerCollection(); ContextHandlerCollection contexts = new ContextHandlerCollection(); RequestLogHandler requestLogHandler = new RequestLogHandler(); - handlers.setHandlers(new Handler[]{contexts,new DefaultHandler(),requestLogHandler}); + handlers.setHandlers(new Handler[] + { contexts, new DefaultHandler(), requestLogHandler }); server.setHandler(handlers); - + ContextDeployer deployer0 = new ContextDeployer(); deployer0.setContexts(contexts); - deployer0.setConfigurationDir(jetty_home+"/contexts"); + deployer0.setConfigurationDir(jetty_home + "/contexts"); deployer0.setScanInterval(1); - server.addBean(deployer0); - + server.addBean(deployer0); + WebAppDeployer deployer1 = new WebAppDeployer(); deployer1.setContexts(contexts); - deployer1.setWebAppDir(jetty_home+"/webapps"); + deployer1.setWebAppDir(jetty_home + "/webapps"); deployer1.setParentLoaderPriority(false); deployer1.setExtract(true); deployer1.setAllowDuplicates(false); - deployer1.setDefaultsDescriptor(jetty_home+"/etc/webdefault.xml"); + deployer1.setDefaultsDescriptor(jetty_home + "/etc/webdefault.xml"); server.addBean(deployer1); - + HashLoginService login = new HashLoginService(); login.setName("Test Realm"); - login.setConfig(jetty_home+"/etc/realm.properties"); + login.setConfig(jetty_home + "/etc/realm.properties"); server.addBean(login); - - NCSARequestLog requestLog = new NCSARequestLog(jetty_home+"/logs/jetty-yyyy_mm_dd.log"); + + NCSARequestLog requestLog = new NCSARequestLog(jetty_home + "/logs/jetty-yyyy_mm_dd.log"); requestLog.setExtended(false); requestLogHandler.setRequestLog(requestLog); - + server.setStopAtShutdown(true); server.setSendServerVersion(true); - + server.start(); server.join(); } - + } diff --git a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java index 0bbc9d9260d..6d890deb572 100644 --- a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java +++ b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyConnectors.java @@ -13,68 +13,51 @@ package org.eclipse.jetty.embedded; -import java.io.IOException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.eclipse.jetty.server.Connector; -import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.bio.SocketConnector; -import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.nio.SelectChannelConnector; +import org.eclipse.jetty.server.ssl.SslSelectChannelConnector; import org.eclipse.jetty.util.thread.QueuedThreadPool; - /* ------------------------------------------------------------ */ /** - * A Jetty server can have multiple connectors. + * A Jetty server with multiple connectors. * */ public class ManyConnectors { - public static void main(String[] args) - throws Exception + public static void main(String[] args) throws Exception { Server server = new Server(); - + SelectChannelConnector connector0 = new SelectChannelConnector(); connector0.setPort(8080); - connector0.setMaxIdleTime(5000); - connector0.setName("connector 0"); - - + connector0.setMaxIdleTime(30000); + connector0.setRequestHeaderSize(8192); + SelectChannelConnector connector1 = new SelectChannelConnector(); connector1.setHost("127.0.0.1"); connector1.setPort(8888); - connector1.setName("connector 1"); - - SocketConnector connector2 = new SocketConnector(); - connector2.setHost("127.0.0.2"); - connector2.setPort(8888); - connector2.setThreadPool(new QueuedThreadPool()); - connector2.setName("connector 2"); - - server.setConnectors(new Connector[]{connector0,connector1,connector2}); - + connector1.setThreadPool(new QueuedThreadPool(20)); + connector1.setName("admin"); + + SslSelectChannelConnector ssl_connector = new SslSelectChannelConnector(); + String jetty_home = System.getProperty("jetty.home","../jetty-distribution/target/distribution"); + System.setProperty("jetty.home",jetty_home); + ssl_connector.setPort(8443); + ssl_connector.setKeystore(jetty_home + "/etc/keystore"); + ssl_connector.setPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"); + ssl_connector.setKeyPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g"); + ssl_connector.setTruststore(jetty_home + "/etc/keystore"); + ssl_connector.setTrustPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"); + server.addConnector(ssl_connector); + + server.setConnectors(new Connector[] + { connector0, connector1, ssl_connector }); + server.setHandler(new HelloHandler()); - + server.start(); server.join(); } - - public static class HelloHandler extends AbstractHandler - { - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException - { - response.setContentType("text/html"); - response.setStatus(HttpServletResponse.SC_OK); - response.getWriter().println("

Hello OneHandler

"); - response.getWriter().println("from "+((Request)request).getConnection().getConnector().getName()); - - ((Request)request).setHandled(true); - } - } } diff --git a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java index c645df6c2dd..ecdf5d08164 100644 --- a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java +++ b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyContexts.java @@ -21,80 +21,61 @@ import javax.servlet.http.HttpServletResponse; import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Handler; -import org.eclipse.jetty.server.HttpConnection; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.handler.ContextHandlerCollection; -import org.eclipse.jetty.server.handler.DefaultHandler; -import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.nio.SelectChannelConnector; - /* ------------------------------------------------------------ */ /** - * 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. - * + * 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 static void main(String[] args) - throws Exception + public final static String BODY= + "root context
"+ + "normal context
"+ + "virtual context
"; + + public static void main(String[] args) throws Exception { Server server = new Server(); - Connector connector=new SelectChannelConnector(); + Connector connector = new SelectChannelConnector(); connector.setPort(8080); - server.setConnectors(new Connector[]{connector}); - + server.setConnectors(new Connector[] + { connector }); + ContextHandler context0 = new ContextHandler(); context0.setContextPath("/"); - Handler handler0=new HelloHandler("Root Context"); + Handler handler0 = new HelloHandler("Root Context",BODY); context0.setHandler(handler0); ContextHandler context1 = new ContextHandler(); context1.setContextPath("/context"); - Handler handler1=new HelloHandler("A Context"); - context1.setHandler(handler1); + Handler handler1 = new HelloHandler("A Context",BODY); + context1.setHandler(handler1); ContextHandler context2 = new ContextHandler(); context2.setContextPath("/context"); - context2.setVirtualHosts(new String[]{"127.0.0.2"}); - Handler handler2=new HelloHandler("A Virtual Context"); - context2.setHandler(handler2); - + context2.setVirtualHosts(new String[] + { "127.0.0.2" }); + Handler handler2 = new HelloHandler("A Virtual Context",BODY); + context2.setHandler(handler2); + ContextHandlerCollection contexts = new ContextHandlerCollection(); - contexts.setHandlers(new Handler[]{context0,context1,context2}); - + contexts.setHandlers(new Handler[] + { context0, context1, context2 }); + server.setHandler(contexts); - + server.start(); System.err.println(server.dump()); server.join(); } - public static class HelloHandler extends AbstractHandler - { - String _welcome; - - HelloHandler(String welcome) - { - _welcome=welcome; - } - - public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException - { - ((Request)request).setHandled(true); - response.setStatus(HttpServletResponse.SC_OK); - response.setContentType("text/html"); - response.getWriter().println("

"+_welcome+" "+request.getContextPath()+"

"); - response.getWriter().println("root context
"); - response.getWriter().println("normal context
"); - response.getWriter().println("virtual context
"); - - } - } - } diff --git a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java index a6e72d7ab16..48400d3dc78 100644 --- a/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java +++ b/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/ManyHandlers.java @@ -33,80 +33,81 @@ import org.eclipse.jetty.server.handler.HandlerWrapper; import org.eclipse.jetty.server.handler.RequestLogHandler; import org.eclipse.jetty.util.ajax.JSON; - /* ------------------------------------------------------------ */ /** - * Frequently many handlers are combined together to handle - * different aspects of a request. A handler may: