FileDocCategorySizeDatePackage
Httpd.javaAPI DocExample2478Sat Mar 13 20:22:30 GMT 2004None

Httpd

public class Httpd extends Object
A very very simple Web server.

NO SECURITY. ALMOST NO CONFIGURATION. NO CGI. NO SERVLETS.

This version is threaded. I/O is done in Handler.

version
$Id: Httpd.java,v 1.10 2004/03/14 02:22:29 ian Exp $

Fields Summary
public static final int
HTTP
The default port number
protected ServerSocket
sock
The server socket used to connect from clients
private Properties
wsp
A Properties, for loading configuration info
private Properties
mimeTypes
A Properties, for loading mime types into
private String
rootDir
The root directory
Constructors Summary
Httpd()
Construct a server object for a given port number

		super();
		wsp=new FileProperties("httpd.properties");
		rootDir = wsp.getProperty("rootDir", ".");
		mimeTypes = new FileProperties(wsp.getProperty("mimeProperties", "mime.properties"));
	
Methods Summary
public java.lang.StringgetMimeType(java.lang.String type)

		return mimeTypes.getProperty(type);
	
public java.lang.StringgetMimeType(java.lang.String type, java.lang.String dflt)

		return mimeTypes.getProperty(type, dflt);
	
public java.lang.StringgetRootDir()

		return rootDir;
	
public java.lang.StringgetServerProperty(java.lang.String name)

		return wsp.getProperty(name);
	
public static voidmain(java.lang.String[] argv)


	       
		System.out.println("DarwinSys JavaWeb Server 0.1 starting...");
		Httpd w = new Httpd();
		if (argv.length == 2 && argv[0].equals("-p")) {
			w.startServer(Integer.parseInt(argv[1]));
		} else {
			w.startServer(HTTP);
		}
		w.runServer();
		// NOTREACHED
	
voidrunServer()
Run the main loop of the Server. Each time a client connects, the ServerSocket accept() returns a new Socket for I/O, and we pass that to the Handler constructor, which creates a Thread, which we start.

		while (true) {
				final Socket clntSock = sock.accept();
				Thread t = new Thread(){
					public void run() {
						new Handler(Httpd.this).process(clntSock);
					}
				};
				t.start();
		}
	
public voidstartServer(int portNum)

		String portNumString = null;
		if (portNum == HTTP) {
			portNumString = wsp.getProperty("portNum");
			if (portNumString != null) {
				portNum = Integer.parseInt(portNumString);
			}
		}
		sock = new ServerSocket(portNum);
		System.out.println("Listening on port " + portNum);