My first experiences with embedded Jetty

In this post I’d like to share my first experiences with embedded Jetty. During the last weeks I had a few small projects in mind to work on. Because I’m constantly working with web applications at work I wanted to use this knowledge for my projects. In my current project at work we use a proprietary web server which offers a hugh number of features I would never use in my small projects. Usally I work with Tomcat on private projects with success but there could be other possibilities to host static web pages or web applications. At least I heard about that ūüôā

Jetty

Jetty is a open-source project which enables you to provide a HTTP Server, HTTP Client and handle javax.servlet Container. You can start the standalone Java application via CLI and configure it as you like (see¬†Quickstart). I won’t write more about Jetty (standalone). If you’d like to go into details please use the¬†Projectpage or¬†documentation (recent Version¬†9.4.6).

Jetty Embedded

Unfortunately, I can’t remember where I found this quote (roughly translated).

Don’t prepare your application for deployment on a application server. Integrate the application server INTO your application.

At first I was sceptical about this mindset, because I only know otherwise from my project at work. We define/create EARs and WARs to deploy on the proprietary web server. But it looks like there are alternatives to consider. And that’s what I did.

Examples

For this post I worked with two simple implementations. Both examples were build with this Maven POM. Here the current and used Jetty Dependency.

Static HTTP Server

In this example I created a working File/Web Server with a some lines of code. A few adjustments later I was able to export this Server as a executable JAR in my folder of choice. Depending on the ResourceBase configuration you can turn this JAR into a portable WebServer of your current or specific path (here  "./src/webapp" ).

This Static HTTP Server can be used for two purposes. You can host a simple¬†File Server to display the Files contained within the current folder. Or you’ll use it as Web Server¬†to host your¬†WebContent¬†in your browser of choice. Without any further configuration¬†Jetty will scan your¬†ResourceBase for a index.html file. This file can easily turn your File Server into a Web Server. Even if you don’t provide a¬†index.html file you are still able to enable or force the¬†Web Server¬†to start with the¬† resource_handler.setWelcomeFiles(String[] welcomeFiles);¬†¬†configuration.

Some impressions.

REST Service

In my next project I’d like to create a REST Service. I’d like to increase the value of my project with portablitiy achived by combining¬†Jetty embeded with¬†Jersey. This combination was working after some lines of code like the Static HTTP Server.

DummyClass Servlet

With this small Class I build a simple Test Tool to check if the Servlet is working. At first I placed the DummyClass within my RESTServer Class as integrated class to keep everything in one file. This wasn’t working at all. That’s why I split it into separate files (see¬†Commit).

DummyClass integrated into the file RESTServer.class

RESTServer

You can access the server via port 8090 and Servlet path /dummy/test¬†. I did not verify if you need to call the method .setInitOrder(int order).¬†I found this configuration in this post¬†and didn’t investigate further, but I worked. ūüôā

successful REST access with DummyClass

next steps

With this knowledge I’m able to simply setup a FileServer, HttpServer or RESTService with Jetty. My future projects will benefit from this prototype. I look forward to implement my project to automate the manual actions of my¬†remote access to my Media Center via Owncloud.¬† I thought about a small webUI to show all available files and make them selectable. A REST Service will be triggered with these selections and start their convertion in the background. Depending on the fun I have with this project I might add additional features.

sources

I used those two to create this post.:

You can find the source files for this post in my github repository.

This post is also available in deutsch.

Leave a Comment

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