Httpdpublic 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. |
Fields Summary |
---|
public static final int | HTTPThe default port number | protected ServerSocket | sockThe server socket used to connect from clients | private Properties | wspA Properties, for loading configuration info | private Properties | mimeTypesA Properties, for loading mime types into | private String | rootDirThe 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.String | getMimeType(java.lang.String type)
return mimeTypes.getProperty(type);
| public java.lang.String | getMimeType(java.lang.String type, java.lang.String dflt)
return mimeTypes.getProperty(type, dflt);
| public java.lang.String | getRootDir()
return rootDir;
| public java.lang.String | getServerProperty(java.lang.String name)
return wsp.getProperty(name);
| public static void | main(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
| void | runServer()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 void | startServer(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);
|
|