Italian Version

PVD: Post Visitors' Data in your Mailbox
Installation and Tutorial


PVD 1.2

(4.27 Kb)

PVD is a simple applet that reads some info from the computer of the navigator visiting your site and sends the data to your email address (don't use it if you have hundreds of visits every day! Mr. Smiley). If you homestead at GeoCities, you can use it as it is. Otherwise you can read it to learn how to communicate with CGIs. I use this applet to collect data for my Site Statistics.
This is The Making of PVD: a tutorial that explains classes and methods used in the development of this applet. The complete source code is included in the zip file.

Version 1.1 of PVD courtesy of George Ruban (gruban@oocities.com)
This new version can be used on different pages. A new (Page) field was added using getDocumentBase() to get source page info. See the source code included in the ZIP file for details.

Version 1.2 of PVD Added background color to the applet rectangle.

Description and Installation

After downloading Post.zip you have two files: one is the source code (Post.java), the other the class file (Post.class). The Post class uses the CGI of GeoCities that sends Forms Data to the Email address of the site manager (www.oocities.org/cgi-bin/homestead/mail.pl?account).

You have to upload Post.class to your directory in GeoCities and add to the main page (or whatever page you want) the following HTML tags:

<APPLET CODE="Post.class" WIDTH=2 HEIGHT=2>
<PARAM NAME="account" VALUE="your_account">
<PARAM NAME="color" VALUE="background_color">
</APPLET>

for example, if your name in GeoCities is bgates and the background of your page is yellow (see below for more details about colors), you must write

<APPLET CODE="Post.class" WIDTH=2 HEIGHT=2>
<PARAM NAME="account" VALUE="bgates">
<PARAM NAME="color" VALUE="ffff00">
</APPLET>

Then every time someone accesses your main page, the applet sends the data of his/her computer in your mailbox. The applet has to show nothing on the screen so it's very small. WIDTH and HEIGHT aren't zero because Netscape Navigator 3.0 doesn't paint the frame with the background image if the size of the applet is less than 2x2.

REMEMBER The first letter of "Post.class" is uppercase, "your_account" is your name in GeoCities and it's the first thing you must change. Optionally you can add the color parameter using exadecimal notation: the first two letters regards red component, the next two green and the last two blue. In the example ffff00 means yellow because there is full (ff = 255) red, full green and no blue. Choose the color according to your background.

WARNING You can use this applet only for Statistical Purposes. Privacy is a right granted by law.

Examples of Output

These are two examples of mail messages generated by PVD:
  1. (Reloads)  0
    (LocalHost)  localhost/127.0.0.1
    (Date)  4/9/97, 13:13:35, -119
    (OpSys)  Windows 95, 4.0, x86
    (Browser)  ActiveX Scripting Host, ?, ?
    (Java)  1.0.2
    (Graphics)  1024x768, 96, DirectColorModel
    

    This guy uses Microsoft Internet Explorer 3.0. I know this because localhost is localhost/127.0.0.1 and the browser is ActiveX Scripting Host. Only this browser returns so poor information. Last versions of Explorer (4.0) returns a 1.1 Java version. Date is in the format day/month/year. -119 is the time zone in minutes (-2 hours) i.e. Italy or similar. 96 is the resolution of the monitor (in dots per inches) and DirectColorModel means that the graphic card uses more than 256 colors.

  2. (Reloads)  1
    (LocalHost)  ****.****.***.***/***.***.***.**
    (Date)  4/9/97, 8:50:43, 420
    (OpSys)  OSF1, V3.2, alpha
    (Browser)  Netscape Navigator, 3.0, Netscape Communications Corporation
    (Java)  1.02
    (Graphics)  1280x1024, 95, IndexColorModel
    

    Nescape Navigator 3.0 is more expressive. The localhost is hidden in this document to protect the innocents Mr. Smiley. 420 is the time zone (maybe Los Angeles), while IndexColorModel means that the graphic card is using 256 colors. The OpSys is an exotic OSF1 V3.2 running on an Alpha CPU. The visitor reloaded your page one time and you get a message for every reload.

    In the new version (1.1) there is also a Page field that is the page that hosts the applet. Thus you can use Post.class on more than one page.

Class and Methods

The only class used is java.applet.Applet and the only method overloaded is init(). We can separate the code in two parts:

  1. Collecting Data First of all, the applet collects all the interesting Data. Every read is wrapped in a try-catch block to avoid Security Exceptions. Every browser acts in a different manner so every read is wrapped.
  2. Sending Data This is the code responsible for a correct connection to the CGI. A connection is opened in output and then the CGI result is retrieved. The result is uninteresting for our purposes, so it's not displayed.

Selected Chuncks of Code

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

public class Post extends Applet {
reload variable is used to keep track of the visitor's reloads.
	static int reload;
This is the only overloaded method. It's called every time the page of the applet is reloaded. The method constructs a String called post containing all the data (reloads, local host, date, time, time zone, operative system, CPU, browser, java version, graphics). The format of the string is the usual one (identifier_1=value_1&identifier_2=value_2& etc) Chuncks Index
	public void init() {
		super.init();
		Date today = new Date();
		Runtime runtime = Runtime.getRuntime();
		String post = "Reloads=" + reload + "&LocalHost=";
		try {
			post += InetAddress.getLocalHost().toString();
		} catch(Exception e) {
			post += "?";
		}
		post +=
			"&Date=" +
			today.getDate()+"/"+(today.getMonth()+1)+"/"+today.getYear()+",+"+
			today.getHours()+":"+today.getMinutes()+":"+today.getSeconds()+",+"+
			today.getTimezoneOffset()+
			"&OpSys="
		;
		try {
			post +=
				System.getProperty("os.name") +
				",+" + System.getProperty("os.version") +
				",+" + System.getProperty("os.arch")
			;
		} catch(Exception e) {
			post += "?,+?,+?";
		}
		post += "&Browser=";
		try {
			post += System.getProperty("browser");
		} catch(Exception e) {
			post += "?";
		}
		try {
			post +=
				",+" + System.getProperty("browser.version") +
				",+" + System.getProperty("browser.vendor")
			;
		} catch(Exception e) {
			post += ",+?,+?";
		}
		post += "&Java=";
		try {
			post += System.getProperty("java.version");
		} catch(Exception e) {
			post += "?";
		}
		post += "&Graphics=";
		try {
			Toolkit kit = Toolkit.getDefaultToolkit();
			String colorModel = kit.getColorModel().getClass().toString();
			post +=
				kit.getScreenSize().width + "x" +
				kit.getScreenSize().height + ",+" +
				kit.getScreenResolution() + ",+" +
				colorModel.substring(colorModel.lastIndexOf('.')+1)
			;
		} catch(Exception e) {
			post += "?x?,+?,+?";
		}
		reload++;
Here the connection to the CGI is opened in output. The stream is printed and flushed. Then the results of the CGI are read (however they are not displayed and you can find them in the cache of the browser). Chuncks Index
		try {
		URL url = new URL(
			"http://www.oocities.org/cgi-bin/homestead/mail.pl?"+
			getParameter("account")
		);
		URLConnection uc = url.openConnection();
		uc.setDoOutput(true);
		PrintStream ps = new PrintStream(uc.getOutputStream());
		ps.print(post);
		ps.flush();
		ps.close();
		DataInputStream dis = new DataInputStream(uc.getInputStream());
		while(dis.readLine() != null);
		dis.close();
		} catch(Exception e) {
		}
	}
}

More Questions?

For any question or suggestion, please use the comments form or write directly to TETRACTYS Freeware.
TETRACTYS Freeware Main Page

In the next Issue Java Tutorials will be

JDBC: Quering a Database with Inquirer

Weekly Applets will be

Led Display: A Scrolling Text Display

while Java GeoUtilities will be

ParseMail: An application for UNIX Systems that filters out mail generated by PVD

GeoCities