FileDocCategorySizeDatePackage
WebServer.javaAPI DocExample4381Mon Oct 16 19:44:00 BST 2000None

WebServer

public class WebServer extends Object

Fields Summary
private ServerSocket
ss
Constructors Summary
public WebServer(String portNum)

       
     int port = new Integer( portNum ).intValue();
      try
      {  ss = new ServerSocket( port );
      }
      catch( IOException ioe )
      {  System.out.println( "Can't create server socket on port : " + port );
         System.exit( -1 );
      }
   
Methods Summary
public voidhandleRequest(java.net.Socket s)

  BufferedReader in = null;
      OutputStream os = null;
      try
      {  in = new BufferedReader( new InputStreamReader( s.getInputStream() ) );
         os = s.getOutputStream();
      }
      catch( IOException ioe )
      {  System.out.println( "Can't get streams" );
         System.exit( -1 );
      }

      String firstLine = "";
      String line = "";
      try
      {  firstLine = in.readLine();
         System.out.println( "*** " + firstLine );
         line = in.readLine();
         while( !line.equals( "" ) )
         {  System.out.println( line );
            line = in.readLine();
         }
      }
      catch( IOException ioe )
      {  System.out.println( "Error reading data from client" );
         System.exit( -1 );
      }

      StringTokenizer st = new StringTokenizer( firstLine );
      String command = st.nextToken();
      String filename = st.nextToken();

      if( command.equals( "GET" ) )
      {  sendFile( os, filename );
      }
      else
      {  sendError( os, 501, "NOT IMPLEMENTED" );
      }
   
public static voidmain(java.lang.String[] args)

  if( args.length == 1 )
      {  new WebServer( args[0] ).run();
      }
      else
      {  new WebServer( "44444" ).run();
      }
   
public voidrun()

  Socket sock = null;
      while( true )
      {  try
         {  sock = ss.accept();
         }
         catch( IOException ioe )
         {  System.out.println( "Accept failed" );
            System.exit( -1 );
         }
         handleRequest( sock );
         try
         {  sock.close();
         }
         catch( IOException ioe )
         {  System.out.println( "Error closing socket" );
         }
      }
   
public voidsendError(java.io.OutputStream os, int errorNo, java.lang.String errorText)

  PrintStream ps = new PrintStream( os );
      ps.println( "HTTP/1.0 " + errorNo + " " + errorText );
      ps.println( "Content-Type: text/html" );
      ps.println( "Connection: close" );
      ps.println();
      ps.println( "<HTML><HEAD><TITLE>Error " + errorNo + "</TITLE>" );
      ps.println( "<BODY><H2>HTTP Error " + errorNo + " " + errorText + "</H2>" );
      ps.println( "</BODY></HTML>" );
      ps.flush();
   
public voidsendFile(java.io.OutputStream os, java.lang.String filename)

  filename = filename.substring( 1, filename.length() );
      File f = new File( filename );
      if( !f.exists() )
      {  sendError( os, 404, "NOT FOUND" );
      }
      else
      {  String extension = filename.substring( filename.lastIndexOf( "." )+1,
                                                filename.length() );

         // Send header
         PrintStream ps = new PrintStream( os );
         ps.println( "HTTP/1.0 200 OK" );
         ps.print( "Content-Type: " );
         if( extension.equals( "html" ) )
         {  ps.println( "text/html" );
         }
         else if( extension.equals( "gif" ) )
         {  ps.println( "image/gif" );
         }
         else if( extension.equals( "jpeg" ) || extension.equals( "jpg" ) )
         {  ps.println( "image/jpeg" );
         }
         else
         {  ps.println( "text/plain" );
         }
         ps.println( "Connection: close" );
         ps.println();
         ps.flush();

         // send file body
         try
         {  InputStream is = new FileInputStream( filename );
            int i = is.read();
            while( i !=  -1 )
            {  os.write( i );
               i = is.read();
            }
            os.flush();
         }
         catch( IOException ioe )
         {  System.out.println( "Error sending file body " );
         }
      }