Angel \”Java\” Lopez on Blog

November 27, 2010

A minimal Http Server in Java

Filed under: Java — ajlopez @ 6:53 pm

Recently, I wrote an spike, implementing a minimal Http Server in Java. It’s naive, and it don’t implement all the protocol: only the minimal GET.

The code:

package com.ajlopez;
public class HttpServer {
    public static void main(String[] args) {
	int port;
	ServerSocket serversocket;
	port = Integer.parseInt(args[0]);
	String rootpath = args[1];
	try {
		serversocket = new ServerSocket(port);
	} catch (IOException e) {
	while (true) {
	    try {
		Socket socket = serversocket.accept();
		BufferedReader reader = new BufferedReader(
                          new InputStreamReader(socket.getInputStream()));
		String line = reader.readLine();
		String [] words = line.split(" ");
		InputStream stream = new FileInputStream(rootpath + words[1]);
		OutputStream output = socket.getOutputStream();
		byte [] buffer = new byte[4096];
		int nbytes;
		while ((nbytes =!=-1)
	    } catch (IOException e) {

It receives two arguments: the port and the root directory. You can try

java com.ajlopez.HttpServer 10000 c:\apache-tomcat-6.0.18\webapps\docs

and enter


(don’t forget the index.html) and you get:

It’s toy code, but it works 😉

You can get it from pastie:

or download from my AjCodeKatas google code:

I could improve it: separate concerns, add multithreading, etc.

Keep tuned!

Angel “Java” Lopez


  1. […] A Minimal Http Server in Java […]

    Pingback by A Minimal Http Server in C# « Angel “Java” Lopez on Blog — February 1, 2011 @ 10:03 am

  2. […] A Minimal Http Server in Java […]

    Pingback by PythonSharp (1) A minimal Web Server « Angel ”Java” Lopez on Blog — December 7, 2012 @ 4:52 pm

  3. Hi! I just wanted to ask if you ever have any
    trouble with hackers? My last blog (wordpress) was hacked and I ended
    up losing a few months of hard work due to no backup.
    Do you have any methods to protect against hackers?

    Comment by gamblers — April 23, 2013 @ 11:07 am

  4. The all complaints against one click cash can be a good time to buy a fixer-upper AND the CASH to pay for additional things like updating the
    title and re-inspections. This can be easier to decide whether
    you will be able to get a payday loan, it is a great thing.

    Comment by payday fast cash loan — August 4, 2013 @ 7:17 am

  5. I got this site from my pal who shared with me regarding this web
    site and at the moment this time I am visiting this website and reading
    very informative content here.

    Comment by buy twitter followers mexico — March 1, 2014 @ 1:43 am

  6. When someone writes an article he/she retains the thought
    of a user in his/her brain that how a user can know it.
    So that’s why this post is great. Thanks!

    Comment by — March 3, 2014 @ 2:37 pm

  7. Thanks for any other fantastic post. Where else may anybody
    get that type of info in such an ideal way of writing?
    I’ve a presentation subsequent week, and I am at the
    search for such information.

    Comment by More Bonuses — April 3, 2014 @ 3:58 pm

  8. […] A Minimal Http Server in Java […]

    Pingback by PythonSharp (1) Un servidor web mínimo - Angel "Java" Lopez — June 18, 2014 @ 5:12 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: