embedded examples

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@40 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2009-03-30 00:47:19 +00:00
parent d6f2a290d3
commit f7b57d3fd8
15 changed files with 764 additions and 8 deletions

View File

@ -13,8 +13,8 @@ import javax.servlet.ServletResponse;
public class ContinuationFilter implements Filter
{
boolean _faux;
ServletContext _context;
private boolean _faux;
private ServletContext _context;
public void init(FilterConfig filterConfig) throws ServletException
{

View File

@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-project</artifactId>
<version>7.0.0.incubation0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>jetty-embedded</artifactId>
<name>Jetty :: Embedded Examples</name>
<description>Jetty Embedded Examples</description>
<build>
<plugins>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-deploy</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,61 @@
// ========================================================================
// 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 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.resource.Resource;
/* ------------------------------------------------------------ */
/** File server
* Usage - java org.eclipse.jetty.server.example.FileServer [ port [ docroot ]]
* @author gregw
*
*/
public class FileServer
{
public static void main(String[] args)
throws Exception
{
Server server = new Server(args.length==0?8080:Integer.parseInt(args[0]));
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.setResourceBase(args.length==2?args[1]:".");
HandlerList handlers = new HandlerList();
handlers.setHandlers(new Handler[]{resource_handler,new DefaultHandler()});
server.setHandler(handlers);
server.start();
server.join();
}
}

View File

@ -0,0 +1,86 @@
// ========================================================================
// 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.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;
import org.eclipse.jetty.server.handler.RequestLogHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
public class LikeJettyXml
{
public static void main(String[] args)
throws Exception
{
String jetty_home = System.getProperty("jetty.home",".");
Server server = new Server();
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setMaxThreads(100);
server.setThreadPool(threadPool);
Connector connector=new SelectChannelConnector();
connector.setPort(8080);
connector.setMaxIdleTime(30000);
server.setConnectors(new Connector[]{connector});
HandlerCollection handlers = new HandlerCollection();
ContextHandlerCollection contexts = new ContextHandlerCollection();
RequestLogHandler requestLogHandler = new 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.setScanInterval(1);
server.addBean(deployer0);
WebAppDeployer deployer1 = new WebAppDeployer();
deployer1.setContexts(contexts);
deployer1.setWebAppDir(jetty_home+"/webapps");
deployer1.setParentLoaderPriority(false);
deployer1.setExtract(true);
deployer1.setAllowDuplicates(false);
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");
server.addBean(login);
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();
}
}

View File

@ -0,0 +1,76 @@
// ========================================================================
// 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 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;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
public class ManyConnectors
{
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");
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});
server.setHandler(new HelloHandler());
server.start();
server.join();
}
public static class HelloHandler extends AbstractHandler
{
public void handle(String target, 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

@ -0,0 +1,81 @@
// ========================================================================
// 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 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.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;
public class ManyContexts
{
public static void main(String[] args)
throws Exception
{
Server server = new Server();
Connector connector=new SelectChannelConnector();
connector.setPort(8080);
server.setConnectors(new Connector[]{connector});
ContextHandler context0 = new ContextHandler();
context0.setContextPath("/zero");
Handler handler0=new HelloHandler();
context0.setHandler(handler0);
ContextHandler context1 = new ContextHandler();
context1.setContextPath("/one");
Handler handler1=new HelloHandler();
context1.setHandler(handler1);
ContextHandlerCollection contexts = new ContextHandlerCollection();
contexts.setHandlers(new Handler[]{context0,context1});
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[]{contexts,new DefaultHandler()});
server.setHandler(handlers);
server.start();
server.join();
}
public static class HelloHandler extends AbstractHandler
{
static int h=0;
int hello=h++;
public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest();
base_request.setHandled(true);
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/html");
response.getWriter().println("<h1>Hello OneContext "+hello+"</h1>");
}
}
}

View File

@ -0,0 +1,71 @@
// ========================================================================
// 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 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.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
public class ManyHandlers
{
public static void main(String[] args)
throws Exception
{
Server server = new Server();
Connector connector=new SelectChannelConnector();
connector.setPort(8080);
server.setConnectors(new Connector[]{connector});
Handler param=new ParamHandler();
Handler hello=new HelloHandler();
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[]{param,hello});
server.setHandler(handlers);
server.start();
server.join();
}
public static class ParamHandler extends AbstractHandler
{
public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
System.err.println(request.getParameterMap());
}
}
public static class HelloHandler extends AbstractHandler
{
public void handle(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getRequest();
base_request.setHandled(true);
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello ManyHandler</h1>");
}
}
}

View File

@ -0,0 +1,74 @@
// ========================================================================
// 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 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.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class ManyServletContexts
{
public static void main(String[] args)
throws Exception
{
Server server = new Server(8080);
ContextHandlerCollection contexts = new ContextHandlerCollection();
server.setHandler(contexts);
ServletContextHandler root = new ServletContextHandler(contexts,"/",ServletContextHandler.SESSIONS);
root.addServlet(new ServletHolder(new HelloServlet("Ciao")), "/*");
ServletContextHandler other = new ServletContextHandler(contexts,"/other",ServletContextHandler.SESSIONS);
other.addServlet("org.eclipse.jetty.server.example.ManyServletServletContextHandlers$HelloServlet", "/*");
StatisticsHandler stats = new StatisticsHandler();
contexts.addHandler(stats);
ServletContextHandler yetanother =new ServletContextHandler(stats,"/yo",ServletContextHandler.SESSIONS);
yetanother.addServlet(new ServletHolder(new HelloServlet("YO!")), "/*");
server.start();
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

@ -0,0 +1,56 @@
// ========================================================================
// 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 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.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.servlet.ServletHandler;
public class MinimalServlets
{
public static void main(String[] args)
throws Exception
{
Server server = new Server();
Connector connector=new SocketConnector();
connector.setPort(8080);
server.setConnectors(new Connector[]{connector});
ServletHandler handler=new ServletHandler();
server.setHandler(handler);
handler.addServletWithMapping("org.eclipse.jetty.server.example.MinimalServlets$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>");
}
}
}

View File

@ -0,0 +1,60 @@
// ========================================================================
// 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 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.handler.AbstractHandler;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
public class OneContext
{
public static void main(String[] args)
throws Exception
{
Server server = new Server(8080);
ContextHandler context = new ContextHandler();
context.setContextPath("/");
context.setResourceBase(".");
context.setClassLoader(Thread.currentThread().getContextClassLoader());
server.setHandler(context);
context.setHandler(new HelloHandler());
server.start();
server.join();
}
public static class HelloHandler extends AbstractHandler
{
public void handle(String target, 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

@ -0,0 +1,53 @@
// ========================================================================
// 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 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
{
Server server = new Server(8080);
server.setHandler(new HelloHandler());
server.start();
server.join();
}
public static class HelloHandler extends AbstractHandler
{
public void handle(String target, 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

@ -0,0 +1,54 @@
// ========================================================================
// 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 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.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class OneServletContext
{
public static void main(String[] args)
throws Exception
{
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
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());
}
}
}

View File

@ -0,0 +1,45 @@
// ========================================================================
// 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.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.webapp.WebAppContext;
public class OneWebApp
{
public static void main(String[] args)
throws Exception
{
String jetty_default=new java.io.File("./start.jar").exists()?".":"../..";;
String jetty_home = System.getProperty("jetty.home",jetty_default);
Server server = new Server();
Connector connector=new SelectChannelConnector();
connector.setPort(Integer.getInteger("jetty.port",8080).intValue());
server.setConnectors(new Connector[]{connector});
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(jetty_home+"/webapps/test");
webapp.setDefaultsDescriptor(jetty_home+"/etc/webdefault.xml");
server.setHandler(webapp);
server.start();
server.join();
}
}

View File

@ -24,6 +24,7 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeaders;
import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.io.ByteArrayBuffer;
@ -249,11 +250,14 @@ public class ResourceHandler extends AbstractHandler
response.sendRedirect(URIUtil.addPaths(request.getRequestURI(),URIUtil.SLASH));
return;
}
resource=getWelcome(resource);
if (resource==null || !resource.exists() || resource.isDirectory())
Resource welcome=getWelcome(resource);
if (welcome!=null && welcome.exists())
resource=welcome;
else
{
response.sendError(HttpServletResponse.SC_FORBIDDEN);
doDirectory(request,response,resource);
base_request.setHandled(true);
return;
}
}
@ -265,7 +269,7 @@ public class ResourceHandler extends AbstractHandler
long if_modified=request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE);
if (if_modified>0 && last_modified/1000<=if_modified/1000)
{
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
response.setStatus(HttpStatus.NOT_MODIFIED_304);
return;
}
}
@ -297,6 +301,13 @@ public class ResourceHandler extends AbstractHandler
}
}
/* ------------------------------------------------------------ */
protected void doDirectory(HttpServletRequest request,HttpServletResponse response, Resource resource)
throws IOException
{
response.sendError(HttpStatus.FORBIDDEN_403);
}
/* ------------------------------------------------------------ */
/** Set the response headers.
* This method is called to set the response headers such as content type and content length.

View File

@ -117,8 +117,9 @@
<module>jetty-rewrite</module>
<module>jetty-servlet-tester</module>
<module>jetty-start</module>
<module>jetty-distribution</module>
<module>jetty-embedded-examples</module>
<module>jetty-test-webapp</module>
<module>jetty-distribution</module>
</modules>
<repositories>
<repository>