View Source Code
You can download and run any of these Java programs (making the assumption that you have some way of compiling them!)
If you have the SDK from Sun Microsystems you can compile them from the command prompt by typing:
javac prog_name.java
This will produce a class file.
You can run the program in a simular way from the command line by typing:
java prog_name
This program is a VERY simple web browser written in the Java language.
It uses the three classes that we have already looked at.
It works by prompting the user for a URL. This must be given in the full format or the java.net.URL class will throw an error.
It strips out three main elements; protocol, port, host, and filename.
The first check is on the protocol, our program will only deal with Http and not any other.
URL url = new URL(path);
String host = url.getHost();
String filename = url.getFile();
String protocol = url.getProtocol();
if (!protocol.equals("http")) {
throw new IllegalArgumentException("URL must use 'http:' protocol");
}
Next it checks for the port of the request, if one is not found it uses the default of port 80.
int port = url.getPort();
if (port == -1) {
port = 80;
// if no port, use the default HTTP port
}
Now we have checked the request is as we want it the next step is to open a connection with the server.
We do this by making use of the java.net.Socket class to create a new socket and pass it the parameters of the host and port number to be used.
Socket socket = new Socket(host, port);
Once a connection has been established, we send the command to get the data using the desired Http request method.
The first example uses the HEAD method.
// request data with method
to_server.println("HEAD" + filename + "HTTP/1.0\n");
// get data from server
Scanner serverOut = new Scanner(socket.getInputStream());
The program then iterates over the returned data outputting its contents to the screen (STDOUT).
while (serverOut.hasNextLine()) {
System.out.println(serverOut.nextLine());
}
When all the data has been delivered the program then closes the connection.
socket.close();
The source code has been fully anotated showing where in the program this all happens.
Firstly we will run it using the HEAD method.
A text file of the full output can be found here
Let's take a look at the responses we get.
- Http status code
- This variable is returned for all URLs requested. In this case 200 means "O.K.", page delivered with no problems.
- Date
- The time and date when the client machine requested the page.
- Server
- This gives information of the server that served the page. In our example it shows that is was from a UNIX machine using the Apache software (version 2.2.4)
It also lists all of the extra modules that have been compiled in with that server. For example PHP version 5.2.1
- Last-Modified
- This is the datestamp of the file when it was last modified on the server.
- Etag
- This stands for Enitity Tag. It is an identifier for the page. The primary use of this is in the caching mechnesism.
- Accept-Ranges
- This shows the value used for any of the Range requests. As of Http/1.1 the standard is byte.
- Content-Length
- This shows the size of the body element of the requested page. It is shown using octal notation.
- Content-Type
- As with Content-Length this gives information about the body element. It gives the media type of the requested page, and the character encoding set.
A common example of this is html/text
- Connection
- Defines a rule for the page. With our example the response of Close means that the connection must be terminated after the request, not held open.
Now we will run it again using the GET method, we will look at the differences in the output.
to_server.println("GET" + filename + "HTTP/1.0\n");
A text file of the full output can be found here
As you can see it returns ALL the data about the page, including the HTML tags!
A conventional browser (like FireFox) parses this output delivering a formatted page.
Any href tags are displayed as hyperlinks on the page.
Java isn't the only language you can do this in.
Here is the code of a simular program written in Perl.
Notice how it performs the same tasks as the Java program, but it only calls one external library to Java's three classes.
As you will notice it also does it in less lines of coding. Argubley more efficient!
Back to top