import
import java.net.*;
import java.io.*;
import java.util.*;
The import statment makes various pre-written classes available for use in this program.
class declaration
public class httpServer
{
A document titled httpServer.java must have this declaration. Java enforces this relationship between document title and source code content.
Enforced structure can make it easier to read other peoples' code.
global variables
String DocumentRoot = "/web/edu/topics/java/server/web";
int port = 81;
These variables are needed in several methods within this class, so they are declared globally.
main method
public static void main(String args[])
{
httpServer h = new httpServer();
h.startIt();
}
The main method is where program execution starts when the class file is run from the command line.
In this example, the main method is used to instantiate an object of type httpServer.
h.startIt(); is calling a method in the server that begins listening to the port specified above.
methods
public void startIt()
{
try{
ServerSocket sso=new ServerSocket(port);
System.out.println("Java httpd awaiting socket connections on port "+port);
while(true){
Socket so=sso.accept();
new httpHandler(so,DocumentRoot).start();
}
}
catch(Exception e){
System.out.println("ServerSocket exception: "+e);
}
}
The try/catch mechanism is a way to handle "exceptions" that occur in the running of the program.
In this method, the ServerSocket class is used to listen to a port.
The accept() method of the ServerSocket class is where the code stalls until a connection is made.
The start() method causes the httpHandler (which extends the Thread class) to begin running.
threads
class httpHandler extends Thread {
This class is used each time a new connection arrives. It handles the socket connection then stops.
variables inside a httpHandler object
Socket so;
String PrependingPath = null;
httpHandler(Socket so, String DocumentRoot){
this.so=so;
this.PrependingPath = DocumentRoot;
}
These variables are given values when the httpHandler is constructed (above).
Note that these variables are global within the httpHandler class.
Arguments are passed to the httpHandler instance from the httpServer class through the constructor.
DataInputStream
The readLine() method of this class is useful for grabbing lines from the input stream.
The input stream in this case is coming from the browser.
StringTokenizer
The nextToken() method of this class is used to grab individual words from the lines read from the stream.
FileInputStream
This class helps to read the file from the hard drive so that its contents can be sent into the DataOutputStream back to the browser.
HTTP
if(s!=null && s.startsWith("POST ")){
post=true;
}
This server must first read the header to learn about the transaction.
It has to handle the transaction differently if the method is POST vs. GET
Response
dos.writeBytes(header);
dos.write(buf);
dos.flush();
A header is returned as well as the buffer containing the contents of the file.
The flush() method forces the data to be sent over the Internet to the browser without delay.