A minimal Http Server in Java

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;
import java.io.*;
import java.net.*;
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) {
		e.printStackTrace();
		return;
	}
		
	while (true) {
	    try {
		Socket socket = serversocket.accept();
		BufferedReader reader = new BufferedReader(
                          new InputStreamReader(socket.getInputStream()));
		String line = reader.readLine();
		String [] words = line.split(" ");
		System.out.println(line);
		InputStream stream = new FileInputStream(rootpath + words[1]);
		OutputStream output = socket.getOutputStream();
		byte [] buffer = new byte[4096];
		int nbytes;
			
		while ((nbytes = stream.read(buffer))!=-1)
		    output.write(buffer,0,nbytes);
		
		output.close();
		stream.close();
		socket.close();
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	}
    }
}

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

http://localhost:10000/index.html

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

It’s toy code, but it works 😉

You can get it from pastie:

http://pastie.org/1305444

or download from my AjCodeKatas google code:

http://code.google.com/p/ajcodekatas/source/browse/#svn/trunk/Spikes/MinimalHttpServer

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

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

8 thoughts on “A minimal Http Server in Java

  1. Pingback: A Minimal Http Server in C# « Angel “Java” Lopez on Blog

  2. Pingback: PythonSharp (1) A minimal Web Server « Angel ”Java” Lopez on Blog

  3. gamblers

    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?

    Reply
  4. payday fast cash loan

    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.

    Reply
  5. More Bonuses

    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.

    Reply
  6. Pingback: PythonSharp (1) Un servidor web mínimo - Angel "Java" Lopez

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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