Deploy Web Application in Jetty Container in Embedded Mode

What is this tutorial all about ?

This tutorial is all about to deploy an web application in embedded Jetty web container. You do not need to deploy it manually into the web server.

Why do we need to deploy it in embedded mode ?

We sometimes need to deploy it in embedded mode, because during unit testing or integration testing we have to deploy the web application manually every time before we start the testing; so we want to deploy the application into embedded mode so that it starts up automatically and we perform unit testing or integration testing without taking care of deployment.

Eclipse Kepler
Maven 3.x
Spring 4
Step 1. Create a maven based web project from Eclipse IDE.

Select File -> New -> Other -> Maven -> Maven Project. Click on Next. Browse project location. Next select an Archetype from the list. Select Artifact Id – maven-archetype-webapp. Click on Next.

Now input Group Id: com.roytuts, Artifact Id: embedded-jetty-web

Click on Finish.
Step 2. Modify pom.xml to configure various dependencies

<project xmlns="" xmlns:xsi=""
    <name>embedded-jetty-web Maven Webapp</name>
        <!-- Jetty Server -->
        <!--jsp support for jetty -->
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">

Step 3. Create an XML file for Jetty web container configuration

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:context=""
    <!-- create a bean for Jetty Server -->
    <!-- start method will start the server -->
    <!-- stop method will shut down the server -->
    <bean id="jettyServer" class="org.mortbay.jetty.Server"
        init-method="start" destroy-method="stop">
        <!-- define thread pool -->
        <property name="threadPool">
            <bean id="ThreadPool" class="org.mortbay.thread.QueuedThreadPool">
                <constructor-arg value="5" />
        <property name="connectors">
                <bean id="Connector" class="org.mortbay.jetty.nio.SelectChannelConnector">
                    <!-- Jetty port to use -->
                    <property name="port" value="9090" />
                    <property name="maxIdleTime" value="30000" />
                    <property name="acceptors" value="2" />
                    <property name="confidentialPort" value="0" />
        <property name="handlers">
                <ref bean="webapp" />
    <bean id="webapp" class="org.mortbay.jetty.webapp.WebAppContext">
        <!-- resource webapp location -->
        <property name="resourceBase" value="src/main/webapp" />
        <!-- deployment descriptor location -->
        <property name="descriptor" value="WEB-INF/web.xml" />
        <!-- define context path -->
        <property name="contextPath" value="/EmbeddedJettyWeb" />

Step 4. Modify index.jsp file under src/main/webapp

    <h2>Web application has successfully been deployed in embedded
        Jetty web container</h2>

Step 5. Create a test class which will deploy the web application into Jetty web container.

package com.roytuts.embedded.jetty.container;
import javax.servlet.ServletContext;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.springframework.web.context.WebApplicationContext;
public class EmbeddedJettyTest {
    public static void main(String[] args) {
        // load the Jetty configuration XML file
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
        // register to cleanup everything before shutting down server
        // get the server bean instance defined in XML file
        Server server = (Server) ctx.getBean("jettyServer");
        // get the servlet context
        ServletContext servletContext = null;
        for (Handler handler : server.getHandlers()) {
            if (handler instanceof Context) {
                Context context = (Context) handler;
                servletContext = context.getServletContext();
        // set the context attributes for web application
        XmlWebApplicationContext wctx = new XmlWebApplicationContext();

Step 6. Change the display name in web.xml file.

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >
    <display-name>Embedded Jetty Web Container</display-name>

Step 7. Run the test class. You will see the below output in the console.

Apr 28, 2015 7:52:57 AM prepareRefresh
INFO: Refreshing startup date [Tue Apr 28 07:52:57 IST 2015]; root of context hierarchy
Apr 28, 2015 7:52:57 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [embedded-jetty-web-jetty-webserver.xml]
2015-04-28 07:52:57.811::INFO:  Logging to STDERR via org.mortbay.log.StdErrLog
2015-04-28 07:52:57.814::INFO:  jetty-6.1.14
2015-04-28 07:52:58.192::INFO:  Started SelectChannelConnector@
Apr 28, 2015 7:52:58 AM prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Tue Apr 28 07:52:58 IST 2015]; parent:


Step 8. Now open a browser and hit the URL http://localhost:9090/EmbeddedJettyWeb you will get the below output in the browser.

Jetty Spring Web Application in Embedded Mode
That’s all. Thank you for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *