Gee, thanks for the referral
--#--
There seems to be quite a bit of confusion, particularly
among marketing folks, about the environment variable called
HTTP_REFERER.
This can be a handy tool for logging how frequently
someone like Yahoo! is sending traffic your way. If
your server logs are set up to record the Referer URI,
you can actually count these things. (It's an easy switch in
most servers' configuration files or programs.)
If you have a log analysis tool handy, see who's the top
referrer to your site? Wow! It's your own site! But this
information is pretty useless, especially if you have
more than a few inlined images on your pages.
We're going to modify Bounce.java to actually serve
pages on request. Let's see what the browser does.
Bounce is going to become more of Web server. He's going to
actually serve pages. So, comment out the lines where Bounce
just echoes back the request lines from the client.
// out.println(line);
Because Bounce doesn't bounce back requests any more --
he serves what the client requested -- his name is no longer
apropos. Let's give him a new one: Kevin, as in "Hello,
my name is Kevin and I'm your server today."
Now, to add code that will recognize a GET request, just
look for a GET in the request header. If you find it, then
fetch the file. Use a BufferedReader object to read the file
line-by-line and, as you read it, send each line out to the
browser.
if (line.indexOf("GET") >= 0) {
protocol = "get";
filename = Kevin.getFileName(line);
}
...
if (protocol.compareTo("get") == 0) {
try {
BufferedReader getter = new BufferedReader(
new FileReader(filename));
while ((line = getter.readLine()) != null)
out.println(line);
getter.close();
}
catch(Exception any) {
System.err.println("Error reading get file: " + any.getMessage());
}
}
One little detail is left: parsing out the file path from the GET line.
public static String getFileName(String line) {
StringTokenizer st = new StringTokenizer(line);
String fn = "";
int count = 0;
while (st.hasMoreTokens()) {
count++;
if (count == 2)
return st.nextToken();
else st.nextToken();
}
return ""; // if it wasn't found, return an empty string
}