A simple Java HTTP server. Simple. This projects does not pretend to be anything like a full stack web application framework. It’s for simple, dynamic HTTP content.
This code has been extracted from the FitNesse project. One distinguishing aspect of FitNesse is it’s “stand-alone” ability. Back in the day, FitNesse was a web server in a jar file. It has since grown beyond that but the simplicity of a stand alone web server was grand.
MM HTTP offer that ability.
<repositories> <repository> <id>simple-http-server-mvn-repo</id> <url>https://raw.github.com/ancap-dev/simple-http-server/mvn-repo/</url> <snapshots> <enabled>true</enabled> <updatePolicy>always</updatePolicy> </snapshots> </repository> </repositories> <dependencies> <dependency> <groupId>ru.ancap</groupId> <artifactId>simple-http-server</artifactId> <version>1.0</version> </dependency> </dependencies>
Here is program that uses MM HTTP to start a server.
import mmhttp.protocol.*; import mmhttp.server.*; public class SampleMain { public static void main(String[] args) throws Exception { Server server = new Server(8000); server.register("hello.*", HelloResponder.class); server.start(); } public static class HelloResponder implements Responder { public Response makeResponse(Server server, Request request) throws Exception { return new SimpleResponse(200, "<h1>Hello World!</h1>"); } } }
And here’s how you compile and run it.
javac -classpath mmhttp-1.1.jar SampleMain.java java -classpath mmhttp-1.1.jar SampleMain
Then visit localhost:8000/hello to see it in action.
You will find the javadocs for the classes in this framework at:
slagyr.github.com/mmhttp/index.html
Command Pattern. This framework is built on the Command pattern where Responder is “Command” class. You can create your own implementations of Responder and register them, along with a resource pattern, on the server. When the Server receives a request, it will search through all the registered Responders to find a pattern that matches the requested resource. Once found, the Responder will be instantiated and invoked.