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.
Prerequsites

JDK 8
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="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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.roytuts</groupId>
    <artifactId>embedded-jetty-web</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>embedded-jetty-web Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jdk.version>1.8</jdk.version>
        <junit.version>4.11</junit.version>
        <jetty.version>6.1.14</jetty.version>
        <servlet.version>3.1.0</servlet.version>
        <spring.version>4.1.1.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Jetty Server -->
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jetty</artifactId>
            <version>${jetty.version}</version>
        </dependency>

        <!--jsp support for jetty -->
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jsp-2.1</artifactId>
            <version>${jetty.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>jsp-api-2.1</artifactId>
            <version>${jetty.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>embedded-jetty-web</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.10</version>
                <configuration>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>9090</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

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

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 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" />
            </bean>
        </property>
        <property name="connectors">
            <list>
                <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" />
                </bean>
            </list>
        </property>
        <property name="handlers">
            <list>
                <ref bean="webapp" />
            </list>
        </property>
    </bean>

    <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" />
    </bean>

</beans>

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

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

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.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

public class EmbeddedJettyTest {

    public static void main(String[] args) {

        // load the Jetty configuration XML file
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(
                "embedded-jetty-web-jetty-webserver.xml");
        // register to cleanup everything before shutting down server
        ctx.registerShutdownHook();

        // 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();
        wctx.setParent(ctx);
        wctx.setConfigLocation("");
        wctx.setServletContext(servletContext);
        wctx.refresh();

        servletContext.setAttribute(
                WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                wctx);

    }

}

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

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Embedded Jetty Web Container</display-name>
</web-app>

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

Apr 28, 2015 7:52:57 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@18e0d81: 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@0.0.0.0:9090
Apr 28, 2015 7:52:58 AM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Tue Apr 28 07:52:58 IST 2015]; parent: org.springframework.context.support.ClassPathXmlApplicationContext@18e0d81

 

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.

Soumitra Roy Sarkar

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials Twitter Facebook  Google Plus Linkedin Or Email Me

Leave a Reply

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