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

public class UselessHTTPServer04 {
  public static void main(String args[]) throws Exception {
    int port = Integer.parseInt(args[0]);
    ServerSocket serverSock=new ServerSocket(port);

    while(true) {
      Socket conn = serverSock.accept();
      Scanner scanin = new Scanner(conn.getInputStream());
      String line=null;
      int nlines=0;

      while (true) {
        line = scanin.nextLine();
       if(line.length()==0) break;
        nlines = nlines + 1;
        System.out.println("line "+nlines+": "+line);
      }



      String reply="HTTP/1.0 404 Not Found\r\n" +
                   "Connection: close\r\n" +
                   "Content-Type: text/html\r\n" +
                   "\r\n" +
                   "<h1>Sorry, work in progress</h1>\r\n";
      OutputStream outs = conn.getOutputStream();
      outs.write(reply.getBytes());
      conn.close();
    }
  }
}

How do you add code after the for
loop that prints the lines but before the 404 message is sent. The
code should create an additional scanner just for the first request
line. Should use it to extract the first two words and store them
in the new string variables command and resource.

If you want to analyze the first line after printing all the lines then you need to store that first line somewhere. In fact, why not store all the input? Use an array or ArrayList of Strings to store all the data as you read it in, so it will all be available for processing later in the code.

I believe that you are in my CS class at Hertfordshire judging by this post. Your answer is clearly laid out in the practical 9 pdf on the hompages site. Look under heading "UselessHTTPServer05".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.