Java Reference
In-Depth Information
body.toString().getBytes(), "text/html");
}
/**
* Read in the arguments and start the server.
*
* @param args Web server port and http root directory.
*/
public static void main(String args[])
{
try
{
if (args.length < 2)
{
System.out.println(
"Usage:\njava WebServer [port] [http root path]");
} else
{
int port;
try
{
port = Integer.parseInt(args[0]);
WebServer server = new WebServer(port, args[1]);
server.run();
} catch (NumberFormatException e)
{
System.out.println("Invalid port number");
}
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
The
main
function, the constructor, and the
run
method are all nearly the same as
those in Recipe 1.1. The only difference is the support of the additional command line argu-
ment for the “HTTP root” path. For more information on these three methods, review Recipe
1.1.
The
handleClientSession
method begins the same as Recipe 1.1; however,
once the connection is established, this recipe becomes more complex.
// Write the HTTP response.
StringTokenizer tok = new StringTokenizer(first);