Web Server

                      Next      WebServe.java      Socket Class    ServerSocket Class      Explanation

import java.net.*;
import java.io.*;

public class WebServe {
   static String getPath(String msg) {
      if (msg.length() == 0 || !msg.substring(0,3).equals("GET")) return null;
      String path = msg.substring(msg.indexOf(' ')+1);
      path = path.substring(0, path.indexOf(' '));
      if (path.equals("")) return "index.html";
      if (path.charAt(path.length()-1) == '/') path += "index.html";
      return path;
   }

   static void fileFoundHeader(PrintWriter os, int filelength) {
      os.print("HTTP:/1.0 200 OK\n");
      os.print("MIME-Version: 1.0\n");
      os.print("Content-type: text/html\n");
      os.print("Content-length: "+filelength+"\n");
      os.print("\n");
   }

   static void sendReply(PrintWriter os, BufferedReader in, int flen) {
      try {
         char buffer[] = new char[flen];
         in.read(buffer, 0, flen);
         os.write(buffer, 0, flen);
         in.close();
      }
      catch (Exception e) { System.out.println(e); }
   }

   static void errorHeader(PrintWriter os, String err_message) {
      os.print("HTTP:/1.0 404 Not Found\n");
      os.print("Content-type: text/html\n");
      os.print("Content-length: "+err_message.length()+"\n");
      os.print("\n");
      os.print(err_message+"\n");
   }

   static File OpenFile(String filename) {
      File file = new File(filename);
      if (file.exists()) return file;
      if (filename.charAt(0) != '/') return file;
      return new File(filename.substring(1));
   }

   public static void main(String arg[]) {
      String path, a;  BufferedReader in, is;  PrintWriter os;
      try {
         ServerSocket server = new ServerSocket(8080);
         System.out.println("Webserver is fully operational and listening to port 8080...");
         while (true) {
            Socket client = server.accept();
            String destname = client.getInetAddress().getHostName();
            System.out.println("Connect:"+client.getInetAddress());  /***/
            
            os = new PrintWriter(client.getOutputStream());
            InputStream ins = client.getInputStream();
            is = new BufferedReader(new InputStreamReader(ins));
            String il = is.readLine();
            System.out.println("Got:"+il);                           /***/
            
            a = is.readLine();
            while (!a.equals("")) {
               System.out.println("Got:"+a);                         /***/
               a = is.readLine();
            }
            System.out.println("----------------------------");      /***/
            if ((path = getPath(il)) != null) {
               File file = OpenFile(path);
               if (file.exists()) {
                  try {
                     FileInputStream fis = new FileInputStream(file);
                     in = new BufferedReader(new InputStreamReader(fis));
                     fileFoundHeader(os, (int)file.length());
                     sendReply(os, in, (int)file.length());
                  }
                  catch (Exception e) {
                     errorHeader(os, "<h2>Can't Read "+path+"</h2>");
                  }
                  os.flush();
               }
               else
                  errorHeader(os, "<h2>Not Found "+path+"</h2>");
            }
            client.close();
         }
      }
      catch (IOException e) { System.out.println(e); }
   }
}