Java Webserver inspired from Thejesh

Posted in Free Software, Java at 3:15 pm by Pirate Praveen

Here is Thejesh’s page

The requirement is (from thejesh’s portal techmag.biz)

Let us write a simple web server in Java.The purpose is learning. A webserver listens at a given port and gives back the file asked (request). Here only http requests are considered.
Let us keep it simple and serve only HTML pages and not GIF’s and JPEG’s.

Points to consider:
(1) The server will to on a port. By default all the web servers listen to port 80. We are allowed to use other ports unless it is used by other process.
(2) Every webserver will have a root folder. Only the files under this root folder are accessible by the server.
(3) We will serve only .htm or .html files
(4) When we get the request. the first line of the request will contain the word “GET” followed by the file name.
(5) The server should be multithreaded to handle multiple requests.
(6) The first line of the response will be “HTTP/1.0 200 OK”
(7) The second line of the response contains “Content-type: text\html”
(8) The content of the requested fil follows next.

The Java code: Its in process. Now Version 0.2 (Released under GNU GPL)

import java.io.*;
import java.net.*;

public class WebServer {
public static void main(String[] args) throws IOException {

ServerSocket serverSocket = null;
System.out.println("WebServer started and is listening at port 80\n");
try {
serverSocket = new ServerSocket(80);
catch(IOException e) {
System.err.println("Cannot open port at 80");

Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
catch(IOException e) {
System.out.println("Accept Failed: 80");

PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

String inputLine="GET / HTTP/1.1",outputLine;
outputLine="<html><body><h2>Hello Ranganath</h2></body></html>";

do {
try {
}catch (IOException e) {
System.out.println("Read Error");


} while (inputLine != null);

static void putOutputMessage(PrintWriter out) throws IOException {
File index=new File("www/index.html");
FileReader in = new FileReader(index);
int c;

while((c=in.read()) !=-1) {


Change log

1. It can serve more than one requests (one after another – thread is not yet implemented). Used an infinite while loop 🙂
2. It reads index.html from www folder (file name extraction from request is yet to be implemented)


  1. Thejesh said,

    August 23, 2005 at 9:20 am

    Did you go for multi threading ?

  2. Administrator said,

    August 24, 2005 at 2:12 pm

    I’ll try it soon. I’ve made some more changes so that it can handle multiple requets (one after another) and it fetches any file and also it recognises two formats (text/html and text/plain from the filename extension) and then print the headder accordingly. I hope to put in some more changes. Shall we start something in savannah on this ?

    gcj 4.0 is out with Java 1.4 compatible and there is a mingW port of windows. You can download it at http://www.thisiscool.com/gcc_mingw.htm

    I’ve downloaded it it is cool.

Leave a Comment