title: multithreaded http server to take GET and POST from client browser
body: <p>I'm trying to run a simple multithreaded server that fetches a URL and also that allows browser to upload file to sever( GET and POST ) it fetches the webpage with GET I'm having trouble with POST, here is my WebServer I'm using for making upload work. Note: HttpRequest is another class that handles threads</p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  
  
  public class WebServer
  {
  
      public WebServer(int port)
      {
  
          System.out.println("starting web server on port " + port);
  
          ServerSocket serverSocket = null;
  
          try
          {
              //create the server
              serverSocket = new ServerSocket(port);
          }catch(IOException ex)
          {
              System.out.println("could not open port " + port);
              System.exit(1);
          }
  
          //loop indefinitely
          while(true)
          {
              try
              {
                  Socket connection = null;
                  connection = serverSocket.accept();
                  //accept the connection
  
                  //create a new thread, start it and return to the waiting state
                  HttpRequest request = new HttpRequest(connection);
                  Thread t = new Thread(request);
                  t.start();
  
              }catch(IOException ex)
              {
                  //fail if an error occurs
                  System.out.println("problem accepting connection");
                  System.exit(1);
              }
          }
  
      }
  
      public static void main(String[] args)
      {
  
          //simple validation for the port number
          if(args.length != 1)
          {
              System.out.println("please specify a port");
              System.exit(1);
          }
  
          int port = -1;
          try
          {
              port = Integer.parseInt(args[0]);
  
          }catch(NumberFormatException ex)
          {
              System.out.println("invalid port number");
              System.exit(1);
          }
  
          WebServer server = new WebServer (port);
  
      }
  
  }
  </code></pre>
  
  <p>here is the Http implements runnable</p>
  
  <pre><code>import java.net.*;
  import java.io.*;
  
  public class HttpRequest implements Runnable
  {
  
      private DataInputStream input = null;
      private Socket connection;
  
      private static DataOutputStream output = null;
  
      public HttpRequest(Socket connection)
      {
          this.connection = connection;
      }
  
      //required method so this can be used as a thread
      public void run()
      {
          try
          {
              //try and get the streams
              input = new DataInputStream(connection.getInputStream());
              output = new DataOutputStream(connection.getOutputStream());
          }
          catch(IOException ex)
          {
              System.out.println("could not get input/output streams from connection: " + connection.toString());
              return;
          }
  
          try
          {
              StringBuilder response = new StringBuilder("");
  
              String request = input.readLine();
  
              System.out.println("request: " + request);
  
              String[] requestArray = request.split(" ");
  
              //read off and ignore the rest of the input
              //added so this can be tested with real browsers
              while(input.available() != 0)
              {
                  input.read();
              }
  
              if (requestArray.length != 3)
              {
                  //request should be of the format GET /index.html HTTP/1.1, die if a bad request comes in
                  System.out.println("bad request: " + request);
                  return;
              }else
              {
                  //requested file should be the second entry, remove the leading '/'
                  File requestedFile = new File(requestArray[1].substring(1));
  
                  System.out.println("requested file: " + requestedFile);
  
                  //check the requested file exists
                  if(requestedFile.exists())
                  {
                      System.out.println("file found, sending response");
  
                      DataInputStream fileInput = new DataInputStream(new FileInputStream(requestedFile));
  
                      //output HTTP header, must be followed by two new lines
                      response.append("HTTP/1.1 200 OK\n\n");
  
                      String line = fileInput.readLine();
  
                      while(line != null)
                      {
                          response.append(line);
                          line = fileInput.readLine();
                      }
  
                      fileInput.close();
  
                      output.writeBytes(response.toString());
                      output.flush();
                      output.close();
  
                      Logger.writeToLog("Request: " + request + "\r\nResponse: " + response.toString());
  
                  }
                  else
                  {
                      System.out.println("file not found, sending 404");
  
                      response.append("HTTP/1.1 404 Not Found\n\n");
                      output.writeBytes(response.toString());
  
                      output.flush();
                      output.close();
                  }
  
              }
  
          }
          catch(IOException ex)
          {
              System.out.println("cannot read request from: " + connection.toString() + ex.toString());
              return;
          }
          catch(NullPointerException ex)
          {
              System.out.println("bad request: " + connection.toString());
              return;
          }
  
          try
          {
              input.close();
              output.close();
  
              connection.close();
          }
          catch(IOException ex)
          {
              System.out.println("Can't close connection: " + connection.toString());
              return;
          }
      }
  
  }
  </code></pre>