Kitteh - A lightweight and embeddable web server

SourceForge.net Logo

The site is work in progress, please be patient :)

Welcome to Kitteh. Kitteh is a small, simple to use, pure java web server intended to be embedded in applications.
You can use it to make a control panel for your daemon application, an user interface for your tools, etc.
Why learn AWT, Swing or SWT when you can reuse your html knowledge and have an interface that is truly portable? All without the burden of a full blown application server such as tomcat!

Kitteh is not intended to be used as a generic, public massively multi-user web server.

Check the following HelloWorld, minimal example. It's that easy!

	import net.sourceforge.kitteh.*;
	
	public class HelloWorld implements DocumentProducer
	{
		public void produceDocument(Request request, Response response)
		{
			response.setContent("<html><body><h1>Hello World!</h1></body></html>");
		}
	
		public static void main(String[] args) throws Exception
		{
			new WebServer( new HelloWorld(), 8080).run();
		}
	

Learn more and start having fun with Kitteh!




Download

Kitteh can be downloaded from here.



Documentation

Requisites

Kitteh is 100% java 1.4. It will run on almost any official and unofficial virtual machines.

Kitteh has no dependencies other than the standard java library.

Installation

Kitteh comes as a single jar, that is contained in the distribution package. Just copy kitteh.jar in your project and include it in the classpath.

Basic use

Kitteh has two main objects: WebServer and DocumentProducer. The WebServer does all the http protocol parsing and communication, while the DocumentProducer in an interface to classes that generate the actual content for each request.

Usually, you create a WebServer instance and pass your own DocumentProducer to it. DocumentProducer is a simple, one-method interface:

public interface DocumentProducer 
{
	void produceDocument(Request request, Response response) throws Exception, Redirection;
}

So it's easy to create your own DocumentProducer! Many generic DocumentProducers implementing different functionalities are included in Kitteh, and they can be chained together.

Request is an object that contains informations about the request, specifically:

Response is an object that will store your response and information about it. To set the content, you'll need to call one of the setContent() methods. You can pass along a String, an InputStream, etc. You can also set a content type with setContentType() (default is text/html).
You can control the headers that will be send in response by using the Map returned by getHeaders.
There is a method setCacheable where you can control whether the page you produced can be cached by the browser or not (it set some headers as needed).
Here's an example of a producer that will sum two numbers:

public class Sum implements DocumentProducer
{

	public void produceDocument(Request request, Response response) throws Exception, Redirection
	{
		try
		{
			int a = Integer.parseInt( request.getParameter("a") );
			int b = Integer.parseInt( request.getParameter("b") );
			int sum = a+b;
			response.setContent("<html><body>The sum is: "+sum+"</body></html>");
		}
		catch (NumberFormatException e)
		{
			String form = "<form><input name=\"a\"><input name=\"b\"><input type=\"submit\"></form>";
			response.setContent("<html><body>Please enter two number to sum:<br/>"+form+"</body></html>");
		}
	}

}
Avaiable DocumentProducers

Kitteh has some DocumentProducer that you can use along your own. They implements some useful functionality, such as dispatching, html authentication, static file serving etc. They can all be combined easily.