Charlotte is a really simple Web Sever developed in Java. Its feature set consists of serving static files from disk, and reporting errors like ”file not found”
But appearances can be deceiving, because Charlotte can also be described as a simple http application using a Java HTTP Framework called Lone. By that definition, Charlotte is reduced to a simple demonstration of what can be accomplished with Lone.
Lone was originally made as the final assignment in a programming class I took at my last year in High School. I was given the maximum Danish grade (13) for it.
Note: Charlotte and Lone are the names of some of my dear Danish co-workers.
Lone is a Framework for building HTTP applications, like search engines, image generators and even web serves (Charlotte).
To make an HTTP application with Charlotte, you create a class and implement the IHttpApplication interface. It consists of a single method handleRequest( httpContext context )
Lone is able run multiple HTTP applications simultaneously by using multihoming, which can be described as binding an HTTP application to one or more domains. You can also specify an default HTTP application for all domains which doesn’t have a associated HTTP application.
Lone is multithreaded in order to archive to maximize the performance. Each request is handled by it’s own thread. But anybody using Lone to create an HTTP application doesn’t have to worry about threads and synchronization, because it’s all abstracted at a lower layer; the SocketServer class.
SocketServer is an abstract base class for creating a server. It takes care of the multithreading and the socket connections. This way, inheriting classes doesn’t have to worry about low-level connection handling.
A HTTP transaction consists of two parts: A request and a response. Lone has classes for inspecting a request, and issuing a response.
The framework is simple, but is enough to build upon if additional features are required.
Note: ASP and ASP.NET inspired the notion of a Request, Response and Context class. Although writing context.Response.Write(“hello world”) feels a lot like ASP.NET, I think the concepts are broad and general enough for it not to be thought of as a rip-off. If anybody (Microsoft…) has any objections to this, feel free to contact me and we’ll work something out.
Each request from the client is parsed and represented as an instance of this class. Public properties like fullUri, httpVersion, host and the public method getHeader(string name) are useful when creating a response.
The response for a given request is constructed by using an instance of this class. It keeps track of the response step (in http you can’t send the header or body before you’ve sent an initial response line like “404 file not found”), keeps track of headers and buffers the body of the response until it’s ready to write it to the client. Methods like write(string what) and writeStream( InputStream stream ) are helpful in responding to requests.
Since a request is very tightly bound to it’s response, Lone has an HttpContext class, which only purpose is to bind an instance of a HttpReponse and HttpRequest class in a single object.
It makes code writing code that works with both the request and the response very easy.
if( context.Request.httpVersion == HttpVersion.v1_0 ){
Context.Response.write( “Unsupported”);
}
Another advantage is that you only need to pass a single object along as argument to various methods.
Charlotte is too simple to be used as a real web server. It quite frankly lacking a number of features we’ve come to expect from a modern web server.
All modern web servers implement caching in some form. It would be relatively easy to cache static pages by loading the files into memory on first request, and then serving subsequent requests from memory.
Almost every web server alive today can generate an individual response for each request. For example by generating a page from data in a database.
There aren’t enough options for configuring Charlotte. There should be a graphical user interface for setting op cache settings, multihoming, response timeout and so forth.
Charlotte would not be a wise choice for a high volume website, but it’s interesting because it’s a good tour of various aspects of Java development: inheritance, interfaces, multithreading, sockets, streams, Java type safe enums and more.
This is my first real project in java, so I’ve made a few observations during the development. Previously I’ve used a lot of C#, so the observations are mostly differences between the two languages.
In retrospect, I must confess that I’m a little disappointed by Java, when compared to C#, since I’ve heard a lot of good stuff about it.
Charlotte was made as my final project for a programming course. The idea was to create a project revolving as many java aspects as possible.
My experience in java is severely limited, so it’s very possible that I’ve missed some options, but I still think the final product is fully satisfactory.