Java Reference
In-Depth Information
[O'Reilly], recommended for administering Tomcat). The main program of my simple server
here constructs one instance of class Httpd . This creates a socket and waits for incoming cli-
ents in the accept( ) method. Each time there is a return from accept() , we have another
client, so we create a new thread to process that client. This happens in the main() and run-
server() methods, which are near the beginning of Example 22-17 .
Example 22-17. Httpd.java
/**
* A very very simple Web server.
* <p>
* NO SECURITY. ALMOST NO CONFIGURATION. NO CGI. NO SERVLETS.
*<p>
* This version is threaded. I/O is done in Handler.
*/
public
public class
Httpd {
/** The default port number */
public
class Httpd
public static
int HTTP = 80 ;
/** The server socket used to connect from clients */
protected
static final
final int
protected ServerSocket sock ;
/** A Properties, for loading configuration info */
private
private Properties wsp ;
/** A Properties, for loading mime types into */
private
private Properties mimeTypes ;
/** The root directory */
private
private String rootDir ;
public
public static
throws Exception {
System . out . println ( "DarwinSys JavaWeb Server 0.1 starting..." );
Httpd w = new
static void
void main ( String argv []) throws
new Httpd ();
iif ( argv . length == 2 && argv [ 0 ]. equals ( "-p" )) {
w . startServer ( Integer . parseInt ( argv [ 1 ]));
} else
else {
w . startServer ( HTTP );
}
w . runServer ();
// NOTREACHED
}
/** 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.
*/
void
void runServer () throws
throws Exception {
Search WWH ::




Custom Search