NanoHTTPD – a tiny web server in Java

NanoHTTPD is a light-weight HTTP server designed for embedding in other applications, released under a Modified BSD licence.

It is being developed at Github and uses Apache Maven for builds & unit testing:

Build status:

Coverage Status:

Current central released version:

Quickstart

We'll create a custom HTTP server project using Maven for build/dep system. This tutorial assumes you are using a Unix variant and a shell. First, install Maven and Java SDK if not already installed. Then run:

Status

We are currently in the process of stabilizing NanoHttpd from the many pull requests and feature requests that were integrated over the last few months. The next release will come soon, and there will not be any more “intended” major changes before the next release. If you want to use the bleeding edge version, you can clone it from Github, or get it from sonatype.org (see “Maven dependencies / Living on the edge” below).

Project structure

NanoHTTPD project currently consist of four parts:

/core – Fully functional HTTP(s) server consisting of one (1) Java file, ready to be customized/inherited for your own project

/samples – Simple examples on how to customize NanoHTTPD. See HelloServer.java for a killer app that greets you enthusiastically!

/websocket – Websocket implementation, also in a single Java file. Depends on core.

/webserver – Standalone file server. Run & enjoy. A popular use seems to be serving files out off an Android device.

/nanolets – Standalone nano app server, giving a servlet like system to the implementor.

/fileupload – integration of the apache common file upload library.

Features

Core

Only one Java file, providing HTTP 1.1 support.

No fixed config files, logging, authorization etc. (Implement by yourself if you need them. Errors are passed to java.util.logging, though.)

Support for HTTPS (SSL)

Basic support for cookies

Supports parameter parsing of GET and POST methods.

Some built-in support for HEAD, POST and DELETE requests. You can easily implement/customize any HTTP method, though.

Supports file upload. Uses memory for small uploads, temp files for large ones.

Never caches anything.

Does not limit bandwidth, request time or simultaneous connections by default.

All header names are converted to lower case so they don't vary between browsers/clients.

Persistent connections (Connection “keep-alive”) support allowing multiple requests to be served over a single socket connection.

Runtime extension support (extensions that serve particular MIME types) - example extension that serves Markdown formatted files. Simply including an extension JAR in the webserver classpath is enough for the extension to be loaded.

Here you write your own subclass of fi.iki.elonen.NanoWebSocketServer to configure and to serve the websocket requests. A small standard echo example is included as fi.iki.elonen.samples.echo.DebugWebSocketServer. You can use it as a starting point to implement your own services.

Develop a custom HTTP file server

For a more classic aproach, perhaps to just create a HTTP server serving mostly service files from your disk, you can use the module with artifactId nanohttpd-webserver.

The included class fi.iki.elonen.SimpleWebServer is intended to be used as a starting point for your own implementation but it also can be used as is. Staring the class as is will start a http server on port 8080 and publishing the current directory.