![]() |
Articles Feed |
Categories
Archives
- July 2010 (5)
- June 2010 (4)
- April 2010 (3)
- March 2010 (2)
- February 2010 (2)
- January 2010 (1)
- December 2009 (1)
- October 2009 (2)
- September 2009 (2)
- August 2009 (1)
- July 2009 (5)
- June 2009 (2)
- May 2009 (2)
- April 2009 (8)
- March 2009 (7)
- January 2009 (2)
- December 2008 (3)
- November 2008 (5)
- October 2008 (4)
- September 2008 (6)
- August 2008 (4)
- July 2008 (5)
- June 2008 (5)
- May 2008 (4)
- April 2008 (2)
- February 2008 (4)
- January 2008 (2)
- December 2007 (2)
- November 2007 (2)
- October 2007 (2)
- September 2007 (1)
- August 2007 (3)
- July 2007 (1)
- June 2007 (4)
- May 2007 (7)
- April 2007 (2)
- February 2007 (3)
- January 2007 (3)
- November 2006 (3)
- October 2006 (3)
- September 2006 (17)
- November 2004 (1)
MM-HTTP: Simple Java HTTP Server
by: micah | March 24th, 2009 |
Bloodroot seeds. They have nothing to do with this blog except that this was the first search result on flickr for ‘MM HTTP’. I think they’re quite interesting.
I was recently working on a Java project where I needed to serve up a couple pages via HTTP. These pages were nothing fancy but their content was purely dynamic. It’s also worth noting that the project was a shrink wrapped product.
The most immediate options that came to mind when I thought Java/Web were, Servlets, Struts, EJB, …. oh my! These options seemed like overkill. I didn’t want to complicated the deployment of this product with beasties like Tomcat and JBoss. That’s just silly. I wanted a simple web server that I could plug into my code and serve a couple dynamic pages. I took a quick peek at Jetty and thought “No thanks”.
Of course, all along I was thinking about FitNesse. FitNesse is a stand-alone web server. And it’s just a jar file. In fact, I have fond memories of writing the FitNesse HTTP server and it occurred to me how simple it would be to extract that module into a self contained library. So I did it. It was simple. And I call it MM-HTTP.
So how do you write a Hello World web app with MM-HTTP? Like this:
- import mmhttp.server.*;
- import mmhttp.protocol.*;
- public class HelloWorld
- {
- 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>");
- }
- }
- }
You’ll need to download the mmhttp jar file. Once you’ve pasted the above code in a HelloWorld.java file, you can compile and run the web server like so:
- javac -classpath mmhttp-1.1.jar HelloWorld.java
- java -classpath mmhttp-1.1.jar HelloWorld
Then plug the following URL into your browser. http://localhost:8000/hello. That’s it!
To find out more, visit the MMHTTP github page and the javadocs. Enjoy!
