jetty embedded examples

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@590 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2009-07-27 08:02:35 +00:00
parent a65431c1c5
commit c0a92fa463
16 changed files with 347 additions and 329 deletions

View File

@ -13,47 +13,31 @@
package org.eclipse.jetty.embedded; 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.Handler;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerList; import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler; 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 ]] * File server
* @author gregw
* *
*/ */
public class FileServer public class FileServer
{ {
public static void main(String[] args) public static void main(String[] args) throws Exception
throws Exception
{ {
Server server = new Server(args.length==0?8080:Integer.parseInt(args[0])); Server server = new Server(8080);
ResourceHandler resource_handler=new ResourceHandler() ResourceHandler resource_handler = new ResourceHandler();
{ resource_handler.setDirectoriesListed(true);
protected void doDirectory(HttpServletRequest request, HttpServletResponse response, Resource resource) throws IOException resource_handler.setWelcomeFiles(new String[]{ "index.html" });
{
String listing=resource.getListHTML(request.getRequestURI(),request.getPathInfo().lastIndexOf("/")>0); resource_handler.setResourceBase(".");
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(); HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{resource_handler,new DefaultHandler()}); handlers.setHandlers(new Handler[] { resource_handler, new DefaultHandler() });
server.setHandler(handlers); server.setHandler(handlers);
server.start(); server.start();

View File

@ -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("<h1>"+_greeting+"</h1>");
response.getWriter().println("from " + ((Request)request).getConnection().getConnector().getName());
response.getWriter().println("<br/>");
if (_body!=null)
response.getWriter().println(_body);
}
}

View File

@ -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("<h1>" + greeting + " SimpleServlet</h1>");
response.getWriter().println("session=" + request.getSession(true).getId());
}
}

View File

@ -13,14 +13,13 @@
package org.eclipse.jetty.embedded; 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.HashLoginService;
import org.eclipse.jetty.security.LoginService;
import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.NCSARequestLog; import org.eclipse.jetty.server.NCSARequestLog;
import org.eclipse.jetty.server.Server; 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.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection; import org.eclipse.jetty.server.handler.HandlerCollection;
@ -31,8 +30,7 @@ import org.eclipse.jetty.util.thread.QueuedThreadPool;
public class LikeJettyXml public class LikeJettyXml
{ {
public static void main(String[] args) public static void main(String[] args) throws Exception
throws Exception
{ {
String jetty_home = System.getProperty("jetty.home","../jetty-distribution/target/distribution"); String jetty_home = System.getProperty("jetty.home","../jetty-distribution/target/distribution");
System.setProperty("jetty.home",jetty_home); System.setProperty("jetty.home",jetty_home);
@ -43,47 +41,49 @@ public class LikeJettyXml
threadPool.setMaxThreads(100); threadPool.setMaxThreads(100);
server.setThreadPool(threadPool); server.setThreadPool(threadPool);
Connector connector=new SelectChannelConnector(); Connector connector = new SelectChannelConnector();
connector.setPort(8080); connector.setPort(8080);
connector.setMaxIdleTime(30000); 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.setPort(8443);
ssl_connector.setKeystore(jetty_home+"/etc/keystore"); ssl_connector.setKeystore(jetty_home + "/etc/keystore");
ssl_connector.setPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"); ssl_connector.setPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
ssl_connector.setKeyPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g"); ssl_connector.setKeyPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
ssl_connector.setTruststore(jetty_home+"/etc/keystore"); ssl_connector.setTruststore(jetty_home + "/etc/keystore");
ssl_connector.setTrustPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"); ssl_connector.setTrustPassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
server.addConnector(ssl_connector); server.addConnector(ssl_connector);
HandlerCollection handlers = new HandlerCollection(); HandlerCollection handlers = new HandlerCollection();
ContextHandlerCollection contexts = new ContextHandlerCollection(); ContextHandlerCollection contexts = new ContextHandlerCollection();
RequestLogHandler requestLogHandler = new RequestLogHandler(); RequestLogHandler requestLogHandler = new RequestLogHandler();
handlers.setHandlers(new Handler[]{contexts,new DefaultHandler(),requestLogHandler}); handlers.setHandlers(new Handler[]
{ contexts, new DefaultHandler(), requestLogHandler });
server.setHandler(handlers); server.setHandler(handlers);
ContextDeployer deployer0 = new ContextDeployer(); ContextDeployer deployer0 = new ContextDeployer();
deployer0.setContexts(contexts); deployer0.setContexts(contexts);
deployer0.setConfigurationDir(jetty_home+"/contexts"); deployer0.setConfigurationDir(jetty_home + "/contexts");
deployer0.setScanInterval(1); deployer0.setScanInterval(1);
server.addBean(deployer0); server.addBean(deployer0);
WebAppDeployer deployer1 = new WebAppDeployer(); WebAppDeployer deployer1 = new WebAppDeployer();
deployer1.setContexts(contexts); deployer1.setContexts(contexts);
deployer1.setWebAppDir(jetty_home+"/webapps"); deployer1.setWebAppDir(jetty_home + "/webapps");
deployer1.setParentLoaderPriority(false); deployer1.setParentLoaderPriority(false);
deployer1.setExtract(true); deployer1.setExtract(true);
deployer1.setAllowDuplicates(false); deployer1.setAllowDuplicates(false);
deployer1.setDefaultsDescriptor(jetty_home+"/etc/webdefault.xml"); deployer1.setDefaultsDescriptor(jetty_home + "/etc/webdefault.xml");
server.addBean(deployer1); server.addBean(deployer1);
HashLoginService login = new HashLoginService(); HashLoginService login = new HashLoginService();
login.setName("Test Realm"); login.setName("Test Realm");
login.setConfig(jetty_home+"/etc/realm.properties"); login.setConfig(jetty_home + "/etc/realm.properties");
server.addBean(login); 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); requestLog.setExtended(false);
requestLogHandler.setRequestLog(requestLog); requestLogHandler.setRequestLog(requestLog);

View File

@ -13,68 +13,51 @@
package org.eclipse.jetty.embedded; 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.Connector;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server; 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.nio.SelectChannelConnector;
import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.util.thread.QueuedThreadPool;
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* A Jetty server can have multiple connectors. * A Jetty server with multiple connectors.
* *
*/ */
public class ManyConnectors public class ManyConnectors
{ {
public static void main(String[] args) public static void main(String[] args) throws Exception
throws Exception
{ {
Server server = new Server(); Server server = new Server();
SelectChannelConnector connector0 = new SelectChannelConnector(); SelectChannelConnector connector0 = new SelectChannelConnector();
connector0.setPort(8080); connector0.setPort(8080);
connector0.setMaxIdleTime(5000); connector0.setMaxIdleTime(30000);
connector0.setName("connector 0"); connector0.setRequestHeaderSize(8192);
SelectChannelConnector connector1 = new SelectChannelConnector(); SelectChannelConnector connector1 = new SelectChannelConnector();
connector1.setHost("127.0.0.1"); connector1.setHost("127.0.0.1");
connector1.setPort(8888); connector1.setPort(8888);
connector1.setName("connector 1"); connector1.setThreadPool(new QueuedThreadPool(20));
connector1.setName("admin");
SocketConnector connector2 = new SocketConnector(); SslSelectChannelConnector ssl_connector = new SslSelectChannelConnector();
connector2.setHost("127.0.0.2"); String jetty_home = System.getProperty("jetty.home","../jetty-distribution/target/distribution");
connector2.setPort(8888); System.setProperty("jetty.home",jetty_home);
connector2.setThreadPool(new QueuedThreadPool()); ssl_connector.setPort(8443);
connector2.setName("connector 2"); 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,connector2}); server.setConnectors(new Connector[]
{ connector0, connector1, ssl_connector });
server.setHandler(new HelloHandler()); server.setHandler(new HelloHandler());
server.start(); server.start();
server.join(); 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("<h1>Hello OneHandler</h1>");
response.getWriter().println("from "+((Request)request).getConnection().getConnector().getName());
((Request)request).setHandled(true);
}
}
} }

View File

@ -21,52 +21,55 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Connector; import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConnection;
import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.handler.AbstractHandler;
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;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.nio.SelectChannelConnector; import org.eclipse.jetty.server.nio.SelectChannelConnector;
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* A {@link ContextHandlerCollection} handler may be used to * A {@link ContextHandlerCollection} handler may be used to direct a request to
* direct a request to a specific Context. The URI path prefix * a specific Context. The URI path prefix and optional virtual host is used to
* and optional virtual host is used to select the context. * select the context.
* *
*/ */
public class ManyContexts public class ManyContexts
{ {
public static void main(String[] args) public final static String BODY=
throws Exception "<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(); Server server = new Server();
Connector connector=new SelectChannelConnector(); Connector connector = new SelectChannelConnector();
connector.setPort(8080); connector.setPort(8080);
server.setConnectors(new Connector[]{connector}); server.setConnectors(new Connector[]
{ connector });
ContextHandler context0 = new ContextHandler(); ContextHandler context0 = new ContextHandler();
context0.setContextPath("/"); context0.setContextPath("/");
Handler handler0=new HelloHandler("Root Context"); Handler handler0 = new HelloHandler("Root Context",BODY);
context0.setHandler(handler0); context0.setHandler(handler0);
ContextHandler context1 = new ContextHandler(); ContextHandler context1 = new ContextHandler();
context1.setContextPath("/context"); context1.setContextPath("/context");
Handler handler1=new HelloHandler("A Context"); Handler handler1 = new HelloHandler("A Context",BODY);
context1.setHandler(handler1); context1.setHandler(handler1);
ContextHandler context2 = new ContextHandler(); ContextHandler context2 = new ContextHandler();
context2.setContextPath("/context"); context2.setContextPath("/context");
context2.setVirtualHosts(new String[]{"127.0.0.2"}); context2.setVirtualHosts(new String[]
Handler handler2=new HelloHandler("A Virtual Context"); { "127.0.0.2" });
Handler handler2 = new HelloHandler("A Virtual Context",BODY);
context2.setHandler(handler2); context2.setHandler(handler2);
ContextHandlerCollection contexts = new ContextHandlerCollection(); ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler[]{context0,context1,context2}); contexts.setHandlers(new Handler[]
{ context0, context1, context2 });
server.setHandler(contexts); server.setHandler(contexts);
@ -75,26 +78,4 @@ public class ManyContexts
server.join(); 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("<h1>"+_welcome+" "+request.getContextPath()+"</h1>");
response.getWriter().println("<a href='/'>root context</a><br/>");
response.getWriter().println("<a href='http://127.0.0.1:8080/context'>normal context</a><br/>");
response.getWriter().println("<a href='http://127.0.0.2:8080/context'>virtual context</a><br/>");
}
}
} }

View File

@ -33,54 +33,54 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
import org.eclipse.jetty.server.handler.RequestLogHandler; import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.eclipse.jetty.util.ajax.JSON; import org.eclipse.jetty.util.ajax.JSON;
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* Frequently many handlers are combined together to handle * Frequently many handlers are combined together to handle different aspects of
* different aspects of a request. A handler may:<ul> * a request. A handler may:
* <ul>
* <li>handle the request and completely generate the response * <li>handle the request and completely generate the response
* <li>partially handle the request, but defer response generation * <li>partially handle the request, but defer response generation to another
* to another handler. * handler.
* <li>select another handler to pass the request to. * <li>select another handler to pass the request to.
* <li>use business logic to decide to do one of the above. * <li>use business logic to decide to do one of the above.
* </ul> * </ul>
* *
* Multiple handlers may be combined with:<ul> * Multiple handlers may be combined with:
* <li>{@link HandlerWrapper} which will nest one handler inside another. * <ul>
* In this example, the HelloHandler is nested inside a HandlerWrapper * <li>{@link HandlerWrapper} which will nest one handler inside another. In
* that sets the greeting as a request attribute. * this example, the HelloHandler is nested inside a HandlerWrapper that sets
* <li>{@link ListHandler} which will call a collection of handlers * the greeting as a request attribute.
* until the request is marked as handled. In this example, a list * <li>{@link ListHandler} which will call a collection of handlers until the
* is used to combine the param handler (which only handles the * request is marked as handled. In this example, a list is used to combine the
* request if there are parameters) and the wrapper handler. * param handler (which only handles the request if there are parameters) and
* Frequently handler lists are terminated with the {@link DefaultHandler}, * the wrapper handler. Frequently handler lists are terminated with the
* which will generate a suitable 404 response if the request has * {@link DefaultHandler}, which will generate a suitable 404 response if the
* not been handled. * request has not been handled.
* <li>{@link HandlerCollection} which will call each handler * <li>{@link HandlerCollection} which will call each handler regardless if the
* regardless if the request has been handled or not. Typically * request has been handled or not. Typically this is used to always pass a
* this is used to always pass a request to the logging handler. * request to the logging handler.
* </ul> * </ul>
*/ */
public class ManyHandlers public class ManyHandlers
{ {
public static void main(String[] args) public static void main(String[] args) throws Exception
throws Exception
{ {
Server server = new Server(8080); Server server = new Server(8080);
// create the handlers // create the handlers
Handler param=new ParamHandler(); Handler param = new ParamHandler();
HandlerWrapper wrapper = new HandlerWrapper() HandlerWrapper wrapper = new HandlerWrapper()
{ {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException,
ServletException
{ {
request.setAttribute("welcome","Hello"); request.setAttribute("welcome","Hello");
super.handle(target,baseRequest,request, response); super.handle(target,baseRequest,request,response);
} }
}; };
Handler hello=new HelloHandler(); Handler hello = new HelloHandler();
Handler dft=new DefaultHandler(); Handler dft = new DefaultHandler();
RequestLogHandler log=new RequestLogHandler(); RequestLogHandler log = new RequestLogHandler();
// configure logs // configure logs
log.setRequestLog(new NCSARequestLog(File.createTempFile("demo","log").getAbsolutePath())); log.setRequestLog(new NCSARequestLog(File.createTempFile("demo","log").getAbsolutePath()));
@ -91,8 +91,10 @@ public class ManyHandlers
// link them all together // link them all together
wrapper.setHandler(hello); wrapper.setHandler(hello);
list.setHandlers(new Handler[]{param,wrapper,dft}); list.setHandlers(new Handler[]
handlers.setHandlers(new Handler[]{list,log}); { param, wrapper, dft });
handlers.setHandlers(new Handler[]
{ list, log });
server.setHandler(handlers); server.setHandler(handlers);
@ -100,13 +102,12 @@ public class ManyHandlers
server.join(); server.join();
} }
public static class ParamHandler extends AbstractHandler public static class ParamHandler extends AbstractHandler
{ {
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{ {
Map params = request.getParameterMap(); Map params = request.getParameterMap();
if (params.size()>0) if (params.size() > 0)
{ {
response.setContentType("text/plain"); response.setContentType("text/plain");
response.getWriter().println(JSON.toString(params)); response.getWriter().println(JSON.toString(params));
@ -114,15 +115,4 @@ public class ManyHandlers
} }
} }
} }
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("<h1>"+request.getAttribute("welcome") +" ManyHandler</h1>");
((Request)request).setHandled(true);
}
}
} }

View File

@ -13,27 +13,17 @@
package org.eclipse.jetty.embedded; 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;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HandlerContainer;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.handler.StatisticsHandler;
import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
public class ManyServletContexts public class ManyServletContexts
{ {
public static void main(String[] args) public static void main(String[] args) throws Exception
throws Exception
{ {
Server server = new Server(8080); Server server = new Server(8080);
@ -41,34 +31,16 @@ public class ManyServletContexts
server.setHandler(contexts); server.setHandler(contexts);
ServletContextHandler root = new ServletContextHandler(contexts,"/",ServletContextHandler.SESSIONS); ServletContextHandler root = new ServletContextHandler(contexts,"/",ServletContextHandler.SESSIONS);
root.addServlet(new ServletHolder(new HelloServlet("Hello")), "/"); root.addServlet(new ServletHolder(new HelloServlet("Hello")),"/");
root.addServlet(new ServletHolder(new HelloServlet("Ciao")), "/it/*"); root.addServlet(new ServletHolder(new HelloServlet("Ciao")),"/it/*");
root.addServlet(new ServletHolder(new HelloServlet("Bonjoir")), "/fr/*"); root.addServlet(new ServletHolder(new HelloServlet("Bonjoir")),"/fr/*");
ServletContextHandler other = new ServletContextHandler(contexts,"/other",ServletContextHandler.SESSIONS); ServletContextHandler other = new ServletContextHandler(contexts,"/other",ServletContextHandler.SESSIONS);
other.addServlet(DefaultServlet.class.getCanonicalName(), "/"); other.addServlet(DefaultServlet.class.getCanonicalName(),"/");
other.addServlet(new ServletHolder(new HelloServlet("YO!")), "*.yo"); other.addServlet(new ServletHolder(new HelloServlet("YO!")),"*.yo");
server.start(); server.start();
System.err.println(server.dump()); System.err.println(server.dump());
server.join(); server.join();
} }
public static 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("<h1>"+greeting+" SimpleServlet</h1>");
response.getWriter().println("session="+request.getSession(true).getId());
}
}
} }

View File

@ -27,18 +27,18 @@ import org.eclipse.jetty.servlet.ServletHandler;
public class MinimalServlets public class MinimalServlets
{ {
public static void main(String[] args) public static void main(String[] args) throws Exception
throws Exception
{ {
Server server = new Server(); Server server = new Server();
Connector connector=new SocketConnector(); Connector connector = new SocketConnector();
connector.setPort(8080); connector.setPort(8080);
server.setConnectors(new Connector[]{connector}); server.setConnectors(new Connector[]
{ connector });
ServletHandler handler=new ServletHandler(); ServletHandler handler = new ServletHandler();
server.setHandler(handler); server.setHandler(handler);
handler.addServletWithMapping("org.eclipse.jetty.server.example.MinimalServlets$HelloServlet", "/"); handler.addServletWithMapping("org.eclipse.jetty.server.example.MinimalServlets$HelloServlet","/");
server.start(); server.start();
server.join(); server.join();

View File

@ -24,20 +24,16 @@ import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler; import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.server.handler.ContextHandler;
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
* A {@link ContextHandler} provides a common environment for * A {@link ContextHandler} provides a common environment for multiple Handlers,
* multiple Handlers, such as: URI context path, class loader, * such as: URI context path, class loader, static resource base.
* static resource base.
* *
* Typically a ContextHandler is used only when multiple contexts * Typically a ContextHandler is used only when multiple contexts are likely.
* are likely.
*/ */
public class OneContext public class OneContext
{ {
public static void main(String[] args) public static void main(String[] args) throws Exception
throws Exception
{ {
Server server = new Server(8080); Server server = new Server(8080);
@ -52,15 +48,4 @@ public class OneContext
server.start(); server.start();
server.join(); server.join();
} }
public static class HelloHandler extends AbstractHandler
{
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/html");
response.getWriter().println("<h1>Hello OneContext</h1>");
((Request)request).setHandled(true);
}
}
} }

View File

@ -13,24 +13,11 @@
package org.eclipse.jetty.embedded; 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.Handler;
import org.eclipse.jetty.server.HttpConnection;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.server.handler.AbstractHandler;
public class OneHandler public class OneHandler
{ {
public static void main(String[] args) public static void main(String[] args) throws Exception
throws Exception
{ {
Server server = new Server(8080); Server server = new Server(8080);
server.setHandler(new HelloHandler()); server.setHandler(new HelloHandler());
@ -38,16 +25,4 @@ public class OneHandler
server.start(); server.start();
server.join(); 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("<h1>Hello OneHandler</h1>");
((Request)request).setHandled(true);
}
}
} }

View File

@ -26,8 +26,7 @@ import org.eclipse.jetty.servlet.ServletHolder;
public class OneServletContext public class OneServletContext
{ {
public static void main(String[] args) public static void main(String[] args) throws Exception
throws Exception
{ {
Server server = new Server(8080); Server server = new Server(8080);
@ -35,20 +34,9 @@ public class OneServletContext
context.setContextPath("/"); context.setContextPath("/");
server.setHandler(context); server.setHandler(context);
context.addServlet(new ServletHolder(new HelloServlet()), "/*"); context.addServlet(new ServletHolder(new HelloServlet()),"/*");
server.start(); server.start();
server.join(); server.join();
} }
public static class HelloServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello SimpleServlet</h1>");
response.getWriter().println("session="+request.getSession(true).getId());
}
}
} }

View File

@ -20,21 +20,21 @@ import org.eclipse.jetty.webapp.WebAppContext;
public class OneWebApp public class OneWebApp
{ {
public static void main(String[] args) public static void main(String[] args) throws Exception
throws Exception
{ {
String jetty_home = System.getProperty("jetty.home",".."); String jetty_home = System.getProperty("jetty.home","..");
Server server = new Server(); Server server = new Server();
Connector connector=new SelectChannelConnector(); Connector connector = new SelectChannelConnector();
connector.setPort(Integer.getInteger("jetty.port",8080).intValue()); connector.setPort(Integer.getInteger("jetty.port",8080).intValue());
server.setConnectors(new Connector[]{connector}); server.setConnectors(new Connector[]
{ connector });
String war=args.length>0?args[0]:jetty_home+"/test-jetty-webapp/target/test-jetty-webapp-"+Server.getVersion(); String war = args.length > 0?args[0]:jetty_home + "/test-jetty-webapp/target/test-jetty-webapp-" + Server.getVersion();
String path=args.length>1?args[1]:"/"; String path = args.length > 1?args[1]:"/";
System.err.println(war+" "+path); System.err.println(war + " " + path);
WebAppContext webapp = new WebAppContext(); WebAppContext webapp = new WebAppContext();
webapp.setContextPath(path); webapp.setContextPath(path);

View File

@ -21,18 +21,18 @@ import org.eclipse.jetty.servlets.ProxyServlet;
public class ProxyServer public class ProxyServer
{ {
public static void main(String[] args) public static void main(String[] args) throws Exception
throws Exception
{ {
Server server = new Server(); Server server = new Server();
Connector connector=new SocketConnector(); Connector connector = new SocketConnector();
connector.setPort(8080); connector.setPort(8080);
server.setConnectors(new Connector[]{connector}); server.setConnectors(new Connector[]
{ connector });
ServletHandler handler=new ServletHandler(); ServletHandler handler = new ServletHandler();
server.setHandler(handler); server.setHandler(handler);
handler.addServletWithMapping(ProxyServlet.class, "/"); handler.addServletWithMapping(ProxyServlet.class,"/");
server.start(); server.start();
server.join(); server.join();

View File

@ -0,0 +1,29 @@
// ========================================================================
// Copyright (c) 2006-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 org.eclipse.jetty.server.Server;
/* ------------------------------------------------------------ */
/** The simplest possible Jetty server.
*/
public class SimplestServer
{
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);
server.start();
server.join();
}
}

View File

@ -58,6 +58,7 @@ public class ResourceHandler extends AbstractHandler
MimeTypes _mimeTypes = new MimeTypes(); MimeTypes _mimeTypes = new MimeTypes();
ByteArrayBuffer _cacheControl; ByteArrayBuffer _cacheControl;
boolean _aliases; boolean _aliases;
boolean _directory;
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public ResourceHandler() public ResourceHandler()
@ -98,6 +99,24 @@ public class ResourceHandler extends AbstractHandler
_aliases = aliases; _aliases = aliases;
} }
/* ------------------------------------------------------------ */
/** Get the directory option.
* @return true if directories are listed.
*/
public boolean isDirectoriesListed()
{
return _directory;
}
/* ------------------------------------------------------------ */
/** Set the directory.
* @param directory true if directories are listed.
*/
public void setDirectoriesListed(boolean directory)
{
_directory = directory;
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
public void doStart() public void doStart()
throws Exception throws Exception
@ -339,7 +358,14 @@ public class ResourceHandler extends AbstractHandler
protected void doDirectory(HttpServletRequest request,HttpServletResponse response, Resource resource) protected void doDirectory(HttpServletRequest request,HttpServletResponse response, Resource resource)
throws IOException throws IOException
{ {
response.sendError(HttpStatus.FORBIDDEN_403); if (_directory)
{
String listing = resource.getListHTML(request.getRequestURI(),request.getPathInfo().lastIndexOf("/") > 0);
response.setContentType("text/html; charset=UTF-8");
response.getWriter().println(listing);
}
else
response.sendError(HttpStatus.FORBIDDEN_403);
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */