diff --git a/libraries/pom.xml b/libraries/pom.xml
index 09c8cb8335..3e802e76c8 100644
--- a/libraries/pom.xml
+++ b/libraries/pom.xml
@@ -194,6 +194,11 @@
jetty-servlet
${jetty.version}
+
+ org.eclipse.jetty
+ jetty-webapp
+ ${jetty.version}
+
rome
rome
@@ -741,12 +746,11 @@
3.6.2
1.5.0
3.1.0
- 9.4.3.v20170317
4.5.3
2.5
1.6
1.4.196
- 9.4.2.v20170220
+ 9.4.8.v20171121
4.5.3
2.5
1.2.0
diff --git a/libraries/src/main/java/com/baeldung/jetty/JettyServerFactory.java b/libraries/src/main/java/com/baeldung/jetty/JettyServerFactory.java
new file mode 100644
index 0000000000..00ba84368a
--- /dev/null
+++ b/libraries/src/main/java/com/baeldung/jetty/JettyServerFactory.java
@@ -0,0 +1,94 @@
+package com.baeldung.jetty;
+
+import org.eclipse.jetty.server.Handler;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.server.handler.HandlerCollection;
+import org.eclipse.jetty.webapp.WebAppContext;
+
+/**
+ * Simple factory for creating Jetty basic instances.
+ *
+ * @author Donato Rimenti
+ *
+ */
+public class JettyServerFactory {
+
+ /**
+ * Exposed context of the app.
+ */
+ public final static String APP_PATH = "/myApp";
+
+ /**
+ * The server port.
+ */
+ public final static int SERVER_PORT = 13133;
+
+ /**
+ * Private constructor to avoid instantiation.
+ */
+ private JettyServerFactory() {
+ }
+
+ /**
+ * Returns a simple server listening on port 80 with a timeout of 30 seconds
+ * for connections and no handlers.
+ *
+ * @return a server
+ */
+ public static Server createBaseServer() {
+ Server server = new Server();
+
+ // Adds a connector for port 80 with a timeout of 30 seconds.
+ ServerConnector connector = new ServerConnector(server);
+ connector.setPort(SERVER_PORT);
+ connector.setHost("127.0.0.1");
+ connector.setIdleTimeout(30000);
+ server.addConnector(connector);
+
+ return server;
+ }
+
+ /**
+ * Creates a server which delegates the request handling to a web
+ * application.
+ *
+ * @return a server
+ */
+ public static Server createWebAppServer() {
+ // Adds an handler to a server and returns it.
+ Server server = createBaseServer();
+ String webAppFolderPath = JettyServerFactory.class.getClassLoader().getResource("jetty-embedded-demo-app.war")
+ .getPath();
+ Handler webAppHandler = new WebAppContext(webAppFolderPath, APP_PATH);
+ server.setHandler(webAppHandler);
+
+ return server;
+ }
+
+ /**
+ * Creates a server which delegates the request handling to both a logging
+ * handler and to a web application, in this order.
+ *
+ * @return a server
+ */
+ public static Server createMultiHandlerServer() {
+ Server server = createBaseServer();
+
+ // Creates the handlers and adds them to the server.
+ HandlerCollection handlers = new HandlerCollection();
+
+ String webAppFolderPath = JettyServerFactory.class.getClassLoader().getResource("jetty-embedded-demo-app.war")
+ .getPath();
+ Handler customRequestHandler = new WebAppContext(webAppFolderPath, APP_PATH);
+ handlers.addHandler(customRequestHandler);
+
+ Handler loggingRequestHandler = new LoggingRequestHandler();
+ handlers.addHandler(loggingRequestHandler);
+
+ server.setHandler(handlers);
+
+ return server;
+ }
+
+}
\ No newline at end of file
diff --git a/libraries/src/main/java/com/baeldung/jetty/LoggingRequestHandler.java b/libraries/src/main/java/com/baeldung/jetty/LoggingRequestHandler.java
new file mode 100644
index 0000000000..a38759c903
--- /dev/null
+++ b/libraries/src/main/java/com/baeldung/jetty/LoggingRequestHandler.java
@@ -0,0 +1,168 @@
+package com.baeldung.jetty;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.eclipse.jetty.server.Handler;
+import org.eclipse.jetty.server.Request;
+import org.eclipse.jetty.server.Server;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Handler implementation which simply logs that a request has been received.
+ *
+ * @author Donato Rimenti
+ */
+public class LoggingRequestHandler implements Handler {
+
+ /**
+ * Logger.
+ */
+ private final static Logger LOG = LoggerFactory.getLogger(LoggingRequestHandler.class);
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.util.component.LifeCycle#addLifeCycleListener(org.
+ * eclipse.jetty.util.component.LifeCycle.Listener)
+ */
+ @Override
+ public void addLifeCycleListener(Listener arg0) {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.util.component.LifeCycle#isFailed()
+ */
+ @Override
+ public boolean isFailed() {
+ return false;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.util.component.LifeCycle#isRunning()
+ */
+ @Override
+ public boolean isRunning() {
+ return true;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.util.component.LifeCycle#isStarted()
+ */
+ @Override
+ public boolean isStarted() {
+ return true;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.util.component.LifeCycle#isStarting()
+ */
+ @Override
+ public boolean isStarting() {
+ return false;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.util.component.LifeCycle#isStopped()
+ */
+ @Override
+ public boolean isStopped() {
+ return false;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.util.component.LifeCycle#isStopping()
+ */
+ @Override
+ public boolean isStopping() {
+ return false;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.eclipse.jetty.util.component.LifeCycle#removeLifeCycleListener(org.
+ * eclipse.jetty.util.component.LifeCycle.Listener)
+ */
+ @Override
+ public void removeLifeCycleListener(Listener arg0) {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.util.component.LifeCycle#start()
+ */
+ @Override
+ public void start() throws Exception {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.util.component.LifeCycle#stop()
+ */
+ @Override
+ public void stop() throws Exception {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.server.Handler#destroy()
+ */
+ @Override
+ public void destroy() {
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.server.Handler#getServer()
+ */
+ @Override
+ public Server getServer() {
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.server.Handler#handle(java.lang.String,
+ * org.eclipse.jetty.server.Request, javax.servlet.http.HttpServletRequest,
+ * javax.servlet.http.HttpServletResponse)
+ */
+ @Override
+ public void handle(String arg0, Request arg1, HttpServletRequest arg2, HttpServletResponse arg3)
+ throws IOException, ServletException {
+ LOG.info("Received a new request");
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.jetty.server.Handler#setServer(org.eclipse.jetty.server.
+ * Server)
+ */
+ @Override
+ public void setServer(Server server) {
+ }
+
+}
diff --git a/libraries/src/test/java/com/baeldung/jetty/JettyServerFactoryUnitTest.java b/libraries/src/test/java/com/baeldung/jetty/JettyServerFactoryUnitTest.java
new file mode 100644
index 0000000000..849ba24f55
--- /dev/null
+++ b/libraries/src/test/java/com/baeldung/jetty/JettyServerFactoryUnitTest.java
@@ -0,0 +1,86 @@
+package com.baeldung.jetty;
+
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.eclipse.jetty.server.Server;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test for {@link JettyServerFactory}.
+ *
+ * @author Donato Rimenti
+ *
+ */
+public class JettyServerFactoryUnitTest {
+
+ /**
+ * Tests that when a base server is provided a request returns a status 404.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void givenBaseServer_whenHttpRequest_thenStatus404() throws Exception {
+ Server server = JettyServerFactory.createBaseServer();
+ server.start();
+
+ int statusCode = sendGetRequest();
+
+ Assert.assertEquals(404, statusCode);
+ server.stop();
+ }
+
+ /**
+ * Tests that when a web app server is provided a request returns a status
+ * 200.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void givenWebAppServer_whenHttpRequest_thenStatus200() throws Exception {
+ Server server = JettyServerFactory.createWebAppServer();
+ server.start();
+
+ int statusCode = sendGetRequest();
+
+ Assert.assertEquals(200, statusCode);
+ server.stop();
+ }
+
+ /**
+ * Tests that when a multi handler server is provided a request returns a
+ * status 200.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void givenMultiHandlerServerServer_whenHttpRequest_thenStatus200() throws Exception {
+ Server server = JettyServerFactory.createMultiHandlerServer();
+ server.start();
+
+ int statusCode = sendGetRequest();
+
+ Assert.assertEquals(200, statusCode);
+ server.stop();
+ }
+
+ /**
+ * Sends a default HTTP GET request to the server and returns the response
+ * status code.
+ *
+ * @return the status code of the response
+ * @throws Exception
+ */
+ private int sendGetRequest() throws Exception {
+ HttpHost target = new HttpHost("localhost", JettyServerFactory.SERVER_PORT);
+ HttpRequest request = new HttpGet(JettyServerFactory.APP_PATH);
+ HttpClient client = HttpClientBuilder.create().build();
+ HttpResponse response = client.execute(target, request);
+ return response.getStatusLine().getStatusCode();
+ }
+
+}
diff --git a/libraries/src/test/resources/jetty-embedded-demo-app.war b/libraries/src/test/resources/jetty-embedded-demo-app.war
new file mode 100644
index 0000000000..7f8bc37df0
Binary files /dev/null and b/libraries/src/test/resources/jetty-embedded-demo-app.war differ