FileDocCategorySizeDatePackage
JHTTP.javaAPI DocExample2196Sat Sep 09 20:50:20 BST 2000None

JHTTP

public class JHTTP extends Thread

Fields Summary
private File
documentRootDirectory
private String
indexFileName
private ServerSocket
server
private int
numThreads
Constructors Summary
public JHTTP(File documentRootDirectory, int port, String indexFileName)

    
       
       
    
    if (!documentRootDirectory.isDirectory()) {
      throw new IOException(documentRootDirectory 
       + " does not exist as a directory"); 
    }
    this.documentRootDirectory = documentRootDirectory;
    this.indexFileName = indexFileName;
    this.server = new ServerSocket(port);
  
public JHTTP(File documentRootDirectory, int port)

    this(documentRootDirectory, port, "index.html");
  
public JHTTP(File documentRootDirectory)

    this(documentRootDirectory, 80, "index.html");
  
Methods Summary
public static voidmain(java.lang.String[] args)


    // get the Document root
    File docroot;
    try {
      docroot = new File(args[0]);
    }
    catch (ArrayIndexOutOfBoundsException e) {
      System.out.println("Usage: java JHTTP docroot port indexfile");
      return;
    }
    
    // set the port to listen on
    int port;
    try {
      port = Integer.parseInt(args[1]);
      if (port < 0 || port > 65535) port = 80;
    }  
    catch (Exception e) {
      port = 80;
    }  
    
    try {            
      JHTTP webserver = new JHTTP(docroot, port);
      webserver.start();
    }
    catch (IOException e) {
      System.out.println("Server could not start because of an " 
       + e.getClass());
      System.out.println(e);
    }
  
  
public voidrun()

  
    for (int i = 0; i < numThreads; i++) {
      Thread t = new Thread(
       new RequestProcessor(documentRootDirectory, indexFileName));
      t.start();   
    }
    System.out.println("Accepting connections on port " 
     + server.getLocalPort());
    System.out.println("Document Root: " + documentRootDirectory);
    while (true) {
      try {
        Socket request = server.accept();
        RequestProcessor.processRequest(request);
      }
      catch (IOException e) { 
      }   
    }