Jetty is java servlet container developed by Eclipse Foundation and it has such small footprint that you can embedded it in you java app or can use as test server alternative to any web server like tomcat. All by writting small main class of just over of 10 line.

Jetty is used in a wide variety of projects and products, both in development and production. Jetty can be easily embedded in devices, tools, frameworks, application servers, and clusters.

JettyServer.java

This class have main method that can be direclty run from cmd line. You can run your web app without configuring any server or deploying your app to server.

package com.dgstack.eg;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class JettyServer {

	public static void main(String[] args) 
            throws Exception {
	    Server server = new Server(8080);
	    WebAppContext context = new WebAppContext();
	    context.setContextPath("/");
	    context.setDescriptor("src/main/webapp/web.xml");
	    context.setResourceBase("src/main/webapp/");
	    server.setHandler(context);
	    server.start();
	}
}

Inside main method we have instance of Server class which take port number as constructor arg. Then we have WebAppContext class were we have set the Context path, Descriptor (web.xml path) and Resource Base.

Dependency build.gradle

group 'com.dgstack.eg'
version '1.0'

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'application'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
  compile group: 'org.eclipse.jetty', name: 'jetty-server', version: '9.3.15.v20161220'
  compile group: 'org.eclipse.jetty', name: 'jetty-webapp', version: '9.3.15.v20161220'
}

task runApp(type: JavaExec) {
  classpath = sourceSets.main.runtimeClasspath
  main = 'com.dgstack.eg.JettyServer'
}

Get the code at this gist.

Share :

Subscribe & Social

Subscribe to this blog via RSS.

Recent Posts

Categories

Java 14

Python 2

Wordpress 1

Ops 3

Shared 3

Angular 1

Web 1

Draftblog 1

Popular Tags

Java (14) Python (2) Wordpress (1) Ops (3) Angular (1) Web (1) Draftblog (1)

About

DGStack is web blog by digvijay bhakuni. This blog is about computer programming tips and trick. And some short tutorial and gettings started post about the framework, libriay, api and much more. The blog target mostly open source stuff like java, j2ee, nodeJs, angularJs, spring, struts, mongodb, mean, etc

Disclamer

This website claims no credit for any images posted on this site unless otherwise noted. Images on this blog are copyright to its respectful owners. If there is an image appearing on this blog that belongs to you and do not wish for it appear on this site, please E-mail with a link to said image and it will be promptly removed. And the information is best of my knowledge one can always write comment or mail regarding information is this blog.