Thursday, October 15, 2009

Embedding an HTTP daemon in Java

Apache Tomcat, the well-known WebServer is about 84MB after installation. Other contenders are even bigger. Does it have to be so complicated ? The following blog is about the search for a 'right size' HTTP daemon embedable in Java.
Now, what's a Web Server anyway ?
To my mind, its just a piece of software that opens a socket (port 8080) where you send requests to (as defined by the HTTP protocol) and it returns data, in most cases something from the file system (HTML pages and such). This doesn't sound too complicated. In fact, there are examples on the net of HTTP daemons that only take 10 lines of source code (compare the C++ obfuscation contest).
But besides this little niche, there is also NanoHTTPD which already handles some of the ugliness of HTTP. So in order to serve up a system directory, which could contain fairly complicated AJAX/Javascript/JSP files, you don't need more than this single file of Java source code.
If serving a file directory is all you need, go back to the previous chapter.
Unfortunately, things are not that easy ... sometimes you also need to publish a WebService or a Servlet or an already packaged Web application (a war file). Then you need something slightly bigger. The best thing I could find in this category is Jetty. It gives up a nice stand-alone WebServer with all kinds of configuration choices and extension points, but most importantly, it is very easy to embed. By using three jar files (less then 1 MB in total) and writing very little code you again have a complete HTTP daemon. Oh, and also, Jetty is already used inside Eclipse and GWT.

No comments:

Post a Comment