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:
parent
a65431c1c5
commit
c0a92fa463
|
@ -13,47 +13,31 @@
|
|||
|
||||
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()
|
||||
{
|
||||
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"});
|
||||
ResourceHandler resource_handler = new ResourceHandler();
|
||||
resource_handler.setDirectoriesListed(true);
|
||||
resource_handler.setWelcomeFiles(new String[]{ "index.html" });
|
||||
|
||||
resource_handler.setResourceBase(".");
|
||||
|
||||
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();
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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,8 +30,7 @@ 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);
|
||||
|
@ -43,47 +41,49 @@ public class LikeJettyXml
|
|||
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);
|
||||
|
||||
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);
|
||||
|
||||
|
|
|
@ -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");
|
||||
connector1.setThreadPool(new QueuedThreadPool(20));
|
||||
connector1.setName("admin");
|
||||
|
||||
SocketConnector connector2 = new SocketConnector();
|
||||
connector2.setHost("127.0.0.2");
|
||||
connector2.setPort(8888);
|
||||
connector2.setThreadPool(new QueuedThreadPool());
|
||||
connector2.setName("connector 2");
|
||||
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,connector2});
|
||||
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("<h1>Hello OneHandler</h1>");
|
||||
response.getWriter().println("from "+((Request)request).getConnection().getConnector().getName());
|
||||
|
||||
((Request)request).setHandled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,52 +21,55 @@ 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=
|
||||
"<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();
|
||||
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");
|
||||
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.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);
|
||||
|
||||
|
@ -75,26 +78,4 @@ public class ManyContexts
|
|||
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/>");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,54 +33,54 @@ 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:<ul>
|
||||
* Frequently many handlers are combined together to handle different aspects of
|
||||
* a request. A handler may:
|
||||
* <ul>
|
||||
* <li>handle the request and completely generate the response
|
||||
* <li>partially handle the request, but defer response generation
|
||||
* to another handler.
|
||||
* <li>partially handle the request, but defer response generation to another
|
||||
* handler.
|
||||
* <li>select another handler to pass the request to.
|
||||
* <li>use business logic to decide to do one of the above.
|
||||
* </ul>
|
||||
*
|
||||
* Multiple handlers may be combined with:<ul>
|
||||
* <li>{@link HandlerWrapper} which will nest one handler inside another.
|
||||
* In this example, the HelloHandler is nested inside a HandlerWrapper
|
||||
* that sets the greeting as a request attribute.
|
||||
* <li>{@link ListHandler} which will call a collection of handlers
|
||||
* until the request is marked as handled. In this example, a list
|
||||
* is used to combine the param handler (which only handles the
|
||||
* request if there are parameters) and the wrapper handler.
|
||||
* Frequently handler lists are terminated with the {@link DefaultHandler},
|
||||
* which will generate a suitable 404 response if the request has
|
||||
* not been handled.
|
||||
* <li>{@link HandlerCollection} which will call each handler
|
||||
* regardless if the request has been handled or not. Typically
|
||||
* this is used to always pass a request to the logging handler.
|
||||
* Multiple handlers may be combined with:
|
||||
* <ul>
|
||||
* <li>{@link HandlerWrapper} which will nest one handler inside another. In
|
||||
* this example, the HelloHandler is nested inside a HandlerWrapper that sets
|
||||
* the greeting as a request attribute.
|
||||
* <li>{@link ListHandler} which will call a collection of handlers until the
|
||||
* request is marked as handled. In this example, a list is used to combine the
|
||||
* param handler (which only handles the request if there are parameters) and
|
||||
* the wrapper handler. Frequently handler lists are terminated with the
|
||||
* {@link DefaultHandler}, which will generate a suitable 404 response if the
|
||||
* request has not been handled.
|
||||
* <li>{@link HandlerCollection} which will call each handler regardless if the
|
||||
* request has been handled or not. Typically this is used to always pass a
|
||||
* request to the logging handler.
|
||||
* </ul>
|
||||
*/
|
||||
public class ManyHandlers
|
||||
{
|
||||
public static void main(String[] args)
|
||||
throws Exception
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = new Server(8080);
|
||||
|
||||
// create the handlers
|
||||
Handler param=new ParamHandler();
|
||||
Handler param = new ParamHandler();
|
||||
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");
|
||||
super.handle(target,baseRequest,request, response);
|
||||
super.handle(target,baseRequest,request,response);
|
||||
}
|
||||
};
|
||||
Handler hello=new HelloHandler();
|
||||
Handler dft=new DefaultHandler();
|
||||
RequestLogHandler log=new RequestLogHandler();
|
||||
Handler hello = new HelloHandler();
|
||||
Handler dft = new DefaultHandler();
|
||||
RequestLogHandler log = new RequestLogHandler();
|
||||
|
||||
// configure logs
|
||||
log.setRequestLog(new NCSARequestLog(File.createTempFile("demo","log").getAbsolutePath()));
|
||||
|
@ -91,8 +91,10 @@ public class ManyHandlers
|
|||
|
||||
// link them all together
|
||||
wrapper.setHandler(hello);
|
||||
list.setHandlers(new Handler[]{param,wrapper,dft});
|
||||
handlers.setHandlers(new Handler[]{list,log});
|
||||
list.setHandlers(new Handler[]
|
||||
{ param, wrapper, dft });
|
||||
handlers.setHandlers(new Handler[]
|
||||
{ list, log });
|
||||
|
||||
server.setHandler(handlers);
|
||||
|
||||
|
@ -100,13 +102,12 @@ public class ManyHandlers
|
|||
server.join();
|
||||
}
|
||||
|
||||
|
||||
public static class ParamHandler extends AbstractHandler
|
||||
{
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||
{
|
||||
Map params = request.getParameterMap();
|
||||
if (params.size()>0)
|
||||
if (params.size() > 0)
|
||||
{
|
||||
response.setContentType("text/plain");
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,27 +13,17 @@
|
|||
|
||||
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.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.ServletContextHandler;
|
||||
import org.eclipse.jetty.servlet.ServletHolder;
|
||||
|
||||
public class ManyServletContexts
|
||||
{
|
||||
public static void main(String[] args)
|
||||
throws Exception
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = new Server(8080);
|
||||
|
||||
|
@ -41,34 +31,16 @@ public class ManyServletContexts
|
|||
server.setHandler(contexts);
|
||||
|
||||
ServletContextHandler root = new ServletContextHandler(contexts,"/",ServletContextHandler.SESSIONS);
|
||||
root.addServlet(new ServletHolder(new HelloServlet("Hello")), "/");
|
||||
root.addServlet(new ServletHolder(new HelloServlet("Ciao")), "/it/*");
|
||||
root.addServlet(new ServletHolder(new HelloServlet("Bonjoir")), "/fr/*");
|
||||
root.addServlet(new ServletHolder(new HelloServlet("Hello")),"/");
|
||||
root.addServlet(new ServletHolder(new HelloServlet("Ciao")),"/it/*");
|
||||
root.addServlet(new ServletHolder(new HelloServlet("Bonjoir")),"/fr/*");
|
||||
|
||||
ServletContextHandler other = new ServletContextHandler(contexts,"/other",ServletContextHandler.SESSIONS);
|
||||
other.addServlet(DefaultServlet.class.getCanonicalName(), "/");
|
||||
other.addServlet(new ServletHolder(new HelloServlet("YO!")), "*.yo");
|
||||
other.addServlet(DefaultServlet.class.getCanonicalName(),"/");
|
||||
other.addServlet(new ServletHolder(new HelloServlet("YO!")),"*.yo");
|
||||
|
||||
server.start();
|
||||
System.err.println(server.dump());
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,18 +27,18 @@ import org.eclipse.jetty.servlet.ServletHandler;
|
|||
|
||||
public class MinimalServlets
|
||||
{
|
||||
public static void main(String[] args)
|
||||
throws Exception
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = new Server();
|
||||
Connector connector=new SocketConnector();
|
||||
Connector connector = new SocketConnector();
|
||||
connector.setPort(8080);
|
||||
server.setConnectors(new Connector[]{connector});
|
||||
server.setConnectors(new Connector[]
|
||||
{ connector });
|
||||
|
||||
ServletHandler handler=new ServletHandler();
|
||||
ServletHandler handler = new ServletHandler();
|
||||
server.setHandler(handler);
|
||||
|
||||
handler.addServletWithMapping("org.eclipse.jetty.server.example.MinimalServlets$HelloServlet", "/");
|
||||
handler.addServletWithMapping("org.eclipse.jetty.server.example.MinimalServlets$HelloServlet","/");
|
||||
|
||||
server.start();
|
||||
server.join();
|
||||
|
|
|
@ -24,20 +24,16 @@ import org.eclipse.jetty.server.Server;
|
|||
import org.eclipse.jetty.server.handler.AbstractHandler;
|
||||
import org.eclipse.jetty.server.handler.ContextHandler;
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
* A {@link ContextHandler} provides a common environment for
|
||||
* multiple Handlers, such as: URI context path, class loader,
|
||||
* static resource base.
|
||||
* A {@link ContextHandler} provides a common environment for multiple Handlers,
|
||||
* such as: URI context path, class loader, static resource base.
|
||||
*
|
||||
* Typically a ContextHandler is used only when multiple contexts
|
||||
* are likely.
|
||||
* Typically a ContextHandler is used only when multiple contexts are likely.
|
||||
*/
|
||||
public class OneContext
|
||||
{
|
||||
public static void main(String[] args)
|
||||
throws Exception
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = new Server(8080);
|
||||
|
||||
|
@ -52,15 +48,4 @@ public class OneContext
|
|||
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.setStatus(HttpServletResponse.SC_OK);
|
||||
response.setContentType("text/html");
|
||||
response.getWriter().println("<h1>Hello OneContext</h1>");
|
||||
((Request)request).setHandled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,24 +13,11 @@
|
|||
|
||||
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.bio.SocketConnector;
|
||||
import org.eclipse.jetty.server.handler.AbstractHandler;
|
||||
|
||||
public class OneHandler
|
||||
{
|
||||
public static void main(String[] args)
|
||||
throws Exception
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = new Server(8080);
|
||||
server.setHandler(new HelloHandler());
|
||||
|
@ -38,16 +25,4 @@ public class OneHandler
|
|||
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("<h1>Hello OneHandler</h1>");
|
||||
|
||||
((Request)request).setHandled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,8 +26,7 @@ import org.eclipse.jetty.servlet.ServletHolder;
|
|||
|
||||
public class OneServletContext
|
||||
{
|
||||
public static void main(String[] args)
|
||||
throws Exception
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = new Server(8080);
|
||||
|
||||
|
@ -35,20 +34,9 @@ public class OneServletContext
|
|||
context.setContextPath("/");
|
||||
server.setHandler(context);
|
||||
|
||||
context.addServlet(new ServletHolder(new HelloServlet()), "/*");
|
||||
context.addServlet(new ServletHolder(new HelloServlet()),"/*");
|
||||
|
||||
server.start();
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,21 +20,21 @@ import org.eclipse.jetty.webapp.WebAppContext;
|
|||
|
||||
public class OneWebApp
|
||||
{
|
||||
public static void main(String[] args)
|
||||
throws Exception
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
String jetty_home = System.getProperty("jetty.home","..");
|
||||
|
||||
Server server = new Server();
|
||||
|
||||
Connector connector=new SelectChannelConnector();
|
||||
Connector connector = new SelectChannelConnector();
|
||||
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 path=args.length>1?args[1]:"/";
|
||||
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]:"/";
|
||||
|
||||
System.err.println(war+" "+path);
|
||||
System.err.println(war + " " + path);
|
||||
|
||||
WebAppContext webapp = new WebAppContext();
|
||||
webapp.setContextPath(path);
|
||||
|
|
|
@ -21,18 +21,18 @@ import org.eclipse.jetty.servlets.ProxyServlet;
|
|||
|
||||
public class ProxyServer
|
||||
{
|
||||
public static void main(String[] args)
|
||||
throws Exception
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
Server server = new Server();
|
||||
Connector connector=new SocketConnector();
|
||||
Connector connector = new SocketConnector();
|
||||
connector.setPort(8080);
|
||||
server.setConnectors(new Connector[]{connector});
|
||||
server.setConnectors(new Connector[]
|
||||
{ connector });
|
||||
|
||||
ServletHandler handler=new ServletHandler();
|
||||
ServletHandler handler = new ServletHandler();
|
||||
server.setHandler(handler);
|
||||
|
||||
handler.addServletWithMapping(ProxyServlet.class, "/");
|
||||
handler.addServletWithMapping(ProxyServlet.class,"/");
|
||||
|
||||
server.start();
|
||||
server.join();
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -58,6 +58,7 @@ public class ResourceHandler extends AbstractHandler
|
|||
MimeTypes _mimeTypes = new MimeTypes();
|
||||
ByteArrayBuffer _cacheControl;
|
||||
boolean _aliases;
|
||||
boolean _directory;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public ResourceHandler()
|
||||
|
@ -98,6 +99,24 @@ public class ResourceHandler extends AbstractHandler
|
|||
_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()
|
||||
throws Exception
|
||||
|
@ -339,7 +358,14 @@ public class ResourceHandler extends AbstractHandler
|
|||
protected void doDirectory(HttpServletRequest request,HttpServletResponse response, Resource resource)
|
||||
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);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
|
Loading…
Reference in New Issue