这里有个JAVA和CGI程序结合的访问计数器的例子.
其实单纯做访问计数器,只有CGI就够了.
但希望统计用户访问该主页的时间时,只用CGI好象就不行了. (本例没有这个功能. :PPP)
count.pl
--------------------------------------------------------------
#!/usr/local/bin/perl
# Increments a visit count stored in the file named
# "counter" and send the count as a plain text document.
# Print a minimal MIME header
print "Content-type: text/plain\n\n";
$counterfile = "counter";
# Open the counter file and get current count
open(COUNTER, "$counterfile");
# Lock the file to guard against another process updating the
# file as this script uses it
$lock_exclusive = 2;
$unlock = 8;
flock(COUNTER, $lock_exclusive);
# Read and increment the count
$line = ;
close(COUNTER);
chop($line);
$count = $line;
$count++;
# Save the new count in the file
open(COUNTER, ">$counterfile");
print COUNTER ("$count\n");
close(COUNTER);
# Remember to unlock the file
flock(COUNTER, $unlock);
# Send count to caller
print "$count\n";
------------------------------------------------------------
VisitCount.java
----------------------------------------------------------
//---------------------------------------------------------------
// File: VisitCount.java
// Accesses a CGI program to update visit count and display
// the current count.
// Compile with: javac VisitCount.java
/*
* The VisitCount class is a Java applet that accesses a CGI
* program at the host URL and retrieves the visitor count (the
* CGI program also updates the count). Then it displays the
* current count. Use an tag to embed this applet
* at the location where you want to display the count.
*/
//---------------------------------------------------------------
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
public class VisitCount extends java.applet.Applet
{
String visitCount; // String to store visitor count
// Class initialization method
public void init()
{
try
{
// Open the URL that counts visitors to this site
// *** Edit URL before using at your site ***
URL url = new URL(
"http://www.lnbsoft.com/exec-bin/count.pl?update=1");
// Open a data stream and read a single line
try
{
DataInputStream in = new
DataInputStream(url.openStream());
visitCount = in.readLine();
}
catch(IOException e) {}
}
catch(MalformedURLException e) {}
}
// Method that paints the output
public void paint(java.awt.Graphics gc)
{
// Display the visitor count that was read from the URL
// Use whatever font style and color you want
// I used Helvetica for a message and a Courier font
// for the visitor count
Font helv = new Font("Helvetica", Font.BOLD, 16);
Font courier = new Font("Courier", Font.BOLD, 24);
gc.setFont(helv);
FontMetrics fm = gc.getFontMetrics();
String message = "Visitor number: ";
int xstart = 0;
int ystart = fm.getHeight();
gc.drawString(message, xstart, ystart);
// Advance horizontally by amount needed to display message
xstart += fm.stringWidth(message);
// Change font to Courier and display the count
gc.setFont(courier);
gc.setColor(Color.red);
gc.drawString(visitCount, xstart, ystart);
}
}
-----------------------------------------------------------------
HTML文件中调用方法.