Deploy Web Application in Jetty Container in Embedded Mode

Introduction

This tutorial is all about to deploy a web application in embedded Jetty web container. Therefore, you do not need to deploy it manually into the external web server.

Sometimes, you need to deploy the application in embedded mode, because during unit testing or integration testing you need to deploy the web application manually every time before you start the testing. Therefore, if you deploy the application into an embedded server, it will start automatically and you can perform unit testing or integration testing without taking care of the deployment.

deploy web app in jetty server

Prerequisites

Maven 3.6.3 – 3.8.2

Combination 1 – Jetty 6.1.14, Java 1.8, Spring 4.1.1.RELEASE

Combination 2 – Jetty 11.0.7, Java 11 (or 9+), Spring 5.3.12

Project Setup

You can create a maven-based project in your favorite IDE or tool. The required dependencies in the pom.xml file are given below:

Jetty 11.0.7

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>web-app-embedded-jetty-6</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>11</maven.compiler.source>
		<maven.compiler.target>11</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>5.3.12</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>5.3.12</version>
		</dependency>

		<!-- Jetty Server -->
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-server</artifactId>
			<version>11.0.7</version>
		</dependency>

		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-servlet</artifactId>
			<version>11.0.7</version>
		</dependency>

		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-webapp</artifactId>
			<version>11.0.7</version>
		</dependency>

		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-annotations</artifactId>
			<version>11.0.7</version>
		</dependency>

		<!--jsp support for jetty -->
		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>apache-jsp</artifactId>
			<version>11.0.7</version>
		</dependency>

		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>apache-jstl</artifactId>
			<version>11.0.0</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>

			<plugin>
				<groupId>org.eclipse.jetty</groupId>
				<artifactId>jetty-maven-plugin</artifactId>
				<version>11.0.7</version>
			</plugin>
		</plugins>
	</build>
</project>

Jetty 6.1.14

<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>web-app-embedded-jetty-6</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>8</maven.compiler.source>
		<maven.compiler.target>8</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.1.1.RELEASE</version>
		</dependency>

		<!-- Jetty Server -->
		<dependency>
			<groupId>org.mortbay.jetty</groupId>
			<artifactId>jetty</artifactId>
			<version>6.1.14</version>
		</dependency>
		
		<!--jsp support for jetty -->
		<dependency>
			<groupId>org.mortbay.jetty</groupId>
			<artifactId>jsp-2.1</artifactId>
			<version>6.1.14</version>
		</dependency>

		<dependency>
			<groupId>org.mortbay.jetty</groupId>
			<artifactId>jsp-api-2.1</artifactId>
			<version>6.1.14</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</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>

Deployment Descriptor

The deployment descriptor file web.xml is put under src/main/webapp/WEB-INF folder. For this example, this web descriptor file does not have any important content.

<!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>

View File

The view file which will be displayed in the UI (User Interface) is simple JSP page with the following content and written into src/main/webapp/index.jsp file.

<html>
<body>
	<h2>Web Application has successfully been deployed in the embedded
		Jetty Web Container</h2>
</body>
</html>

Jetty Server Configuration

Jetty 11.0.7

For jetty version 11.0.7, the server configuration is done in Java class file.

public class EmbeddedJettyWebApp {

	public static void main(String[] args) throws Exception {
		Server server = new Server(createThreadPool());

		NetworkTrafficServerConnector connector = new NetworkTrafficServerConnector(server);
		connector.setPort(9090);
		connector.setHost("localhost");

		server.addConnector(connector);

		WebAppContext webAppContext = new WebAppContext();
		webAppContext.setContextPath("/EmbeddedJettyWebApp");
		webAppContext.setResourceBase("src/main/webapp");
		webAppContext.setDescriptor("src/main/webapp/WEB-INF/web.xml");
		webAppContext.setParentLoaderPriority(true);

		server.setHandler(webAppContext);

		server.setStopAtShutdown(true);

		server.start();
		server.join();
	}

	private static ThreadPool createThreadPool() {
		QueuedThreadPool threadPool = new QueuedThreadPool(5);
		return threadPool;
	}
}

In the above class file, I have configured context root, resource files, deployment descriptor, thread pool, connector with host and port.

Jetty 6.1.14

For Jetty version 6.1.14, I have configured server settings in the XML file embedded-jetty-web-app.xml and put it under class path folder src/main/resources.

<?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">
					<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="/EmbeddedJettyWebApp" />
	</bean>
</beans>

And I am configuring shutdown hook for the server in the Java class.

public class EmbeddedJettyWebApp {

	public static void main(String[] args) {
		// load the Jetty configuration XML file
		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("embedded-jetty-web-app.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);
	}

}

Testing the Application in Embedded Jetty Server

Run the main class, your application will be deployed into the embedded Jetty server at localhost on port 9090.

Now you can access the URL http://localhost:9090/EmbeddedJettyWebApp/ in the browser and you will see the following output in the browser:

web application embedded jetty

Hope you got an idea how to deploy web application in embedded Jetty web container.

Source Code

Download

Leave a Reply

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